You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ra...@apache.org on 2020/04/17 22:15:53 UTC

[trafficcontrol] branch master updated: Fix retrieval of Delivery Service Regexes (#4636)

This is an automated email from the ASF dual-hosted git repository.

rawlin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 5668b37  Fix retrieval of Delivery Service Regexes (#4636)
5668b37 is described below

commit 5668b37f3e525f7209ea113725a3a44cb5a8bacc
Author: Steve Hamrick <sh...@users.noreply.github.com>
AuthorDate: Fri Apr 17 16:15:43 2020 -0600

    Fix retrieval of Delivery Service Regexes (#4636)
    
    * Add test create/delete for DS_REGEX
    
    * Add license
    
    * Fix bug and add tests
    
    * Fix naming issue and check pointers
---
 lib/go-tc/deliveryservice_regexes.go               |   7 +
 traffic_ops/client/deliveryservice_regexes.go      |  41 ++++++
 .../testing/api/v2/deliveryservicesregexes_test.go | 148 +++++++++++++++++++++
 traffic_ops/testing/api/v2/tc-fixtures.json        |  20 +++
 traffic_ops/testing/api/v2/traffic_control_test.go |   1 +
 traffic_ops/testing/api/v2/withobjs_test.go        |   2 +
 .../deliveryservicesregexes.go                     |   3 +-
 7 files changed, 221 insertions(+), 1 deletion(-)

diff --git a/lib/go-tc/deliveryservice_regexes.go b/lib/go-tc/deliveryservice_regexes.go
index e6b4c5f..f5f63b7 100644
--- a/lib/go-tc/deliveryservice_regexes.go
+++ b/lib/go-tc/deliveryservice_regexes.go
@@ -51,6 +51,13 @@ type DeliveryServiceIDRegex struct {
 	Pattern   string `json:"pattern"`
 }
 
+// Used to represent the entire deliveryservice_regex for testing
+type DeliveryServiceRegexesTest struct {
+	DSName string `json:"dsName"`
+	DSID   int
+	DeliveryServiceIDRegex
+}
+
 // DeliveryServiceRegexPost holds all of the information necessary to create a
 // new routing regular expression for a delivery service.
 type DeliveryServiceRegexPost struct {
diff --git a/traffic_ops/client/deliveryservice_regexes.go b/traffic_ops/client/deliveryservice_regexes.go
new file mode 100644
index 0000000..e2bbd70
--- /dev/null
+++ b/traffic_ops/client/deliveryservice_regexes.go
@@ -0,0 +1,41 @@
+/*
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package client
+
+import (
+	"fmt"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+const (
+	// See: https://traffic-control-cdn.readthedocs.io/en/latest/api/v2/deliveryservices_id_regexes.html
+	API_DS_REGEXES = apiBase + "/deliveryservices/%v/regexes"
+)
+
+// GetDeliveryServiceRegexesByDSID gets DeliveryServiceRegexes by a DS id
+// also accepts an optional map of query parameters
+func (to *Session) GetDeliveryServiceRegexesByDSID(dsID int, params map[string]string) ([]tc.DeliveryServiceIDRegex, ReqInf, error) {
+	response := struct {
+		Response []tc.DeliveryServiceIDRegex `json:"response"`
+	}{}
+
+	reqInf, err := get(to, fmt.Sprintf(API_DS_REGEXES, dsID)+mapToQueryParameters(params), &response)
+	if err != nil {
+		return []tc.DeliveryServiceIDRegex{}, reqInf, err
+	}
+	return response.Response, reqInf, nil
+}
diff --git a/traffic_ops/testing/api/v2/deliveryservicesregexes_test.go b/traffic_ops/testing/api/v2/deliveryservicesregexes_test.go
new file mode 100644
index 0000000..581e952
--- /dev/null
+++ b/traffic_ops/testing/api/v2/deliveryservicesregexes_test.go
@@ -0,0 +1,148 @@
+/*
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package v2
+
+import (
+	"fmt"
+	"strconv"
+	"testing"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+func TestDeliveryServicesRegexes(t *testing.T) {
+	WithObjs(t, []TCObj{CDNs, Types, Tenants, Users, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, DeliveryServices, DeliveryServicesRegexes}, func() {
+		QueryDSRegexTest(t)
+	})
+}
+
+func CreateTestDeliveryServicesRegexes(t *testing.T) {
+	db, err := OpenConnection()
+	if err != nil {
+		t.Fatal("cannot open db")
+	}
+	defer func() {
+		err := db.Close()
+		if err != nil {
+			t.Errorf("unable to close connection to db, error: %v", err)
+		}
+	}()
+
+	dbRegexInsertTemplate := "INSERT INTO regex (pattern, type) VALUES ('%v', '%v');"
+	dbRegexQueryTemplate := "SELECT id FROM regex order by id desc limit 1;"
+	dbDSRegexInsertTemplate := "INSERT INTO deliveryservice_regex (deliveryservice, regex, set_number) VALUES ('%v', '%v', '%v');"
+
+	for i, regex := range testData.DeliveryServicesRegexes {
+		loadDSRegexIDs(t, &regex)
+
+		err = execSQL(db, fmt.Sprintf(dbRegexInsertTemplate, regex.Pattern, regex.Type), "regex")
+		if err != nil {
+			t.Fatalf("unable to create regex: %v", err)
+		}
+
+		row := db.QueryRow(dbRegexQueryTemplate)
+		err = row.Scan(&regex.ID)
+		if err != nil {
+			t.Fatalf("unable to query regex: %v", err)
+		}
+
+		err = execSQL(db, fmt.Sprintf(dbDSRegexInsertTemplate, regex.DSID, regex.ID, regex.SetNumber), "deliveryservice_regex")
+		if err != nil {
+			t.Fatalf("unable to create ds regex %v", err)
+		}
+
+		testData.DeliveryServicesRegexes[i] = regex
+	}
+}
+
+func DeleteTestDeliveryServicesRegexes(t *testing.T) {
+	db, err := OpenConnection()
+	if err != nil {
+		t.Fatal("cannot open db")
+	}
+	defer func() {
+		err := db.Close()
+		if err != nil {
+			t.Errorf("unable to close connection to db, error: %v", err)
+		}
+	}()
+
+	for _, regex := range testData.DeliveryServicesRegexes {
+		err = execSQL(db, fmt.Sprintf("DELETE FROM deliveryservice_regex WHERE deliveryservice = '%v' and regex ='%v';", regex.DSID, regex.ID), "regex")
+		if err != nil {
+			t.Fatalf("unable to delete deliveryservice_regex by regex %v and ds %v: %v", regex.ID, regex.DSID, err)
+		}
+
+		err := execSQL(db, fmt.Sprintf("DELETE FROM regex WHERE Id = '%v';", regex.ID), "regex")
+		if err != nil {
+			t.Fatalf("unable to delete regex %v: %v", regex.ID, err)
+		}
+	}
+}
+
+func loadDSRegexIDs(t *testing.T, test *tc.DeliveryServiceRegexesTest) {
+	dsTypes, _, err := TOSession.GetTypeByName(test.TypeName)
+	if err != nil {
+		t.Fatalf("unable to get type by name %v: %v", test.TypeName, err)
+	}
+	if len(dsTypes) < 1 {
+		t.Fatalf("could not find any types by name %v", test.TypeName)
+	}
+	test.Type = dsTypes[0].ID
+
+	dses, _, err := TOSession.GetDeliveryServiceByXMLIDNullable(test.DSName)
+	if err != nil {
+		t.Fatalf("unable to ds by xmlid %v: %v", test.DSName, err)
+	}
+	if len(dses) != 1 {
+		t.Fatalf("unable to find ds by xmlid %v", test.DSName)
+	}
+	test.DSID = *dses[0].ID
+}
+
+func QueryDSRegexTest(t *testing.T) {
+	ds, _, err := TOSession.GetDeliveryServiceByXMLIDNullable("ds1")
+	if err != nil {
+		t.Fatalf("unable to get ds ds1: %v", err)
+	}
+	if len(ds) == 0 {
+		t.Fatal("unable to get ds ds1")
+	}
+	var dsID int
+	if ds[0].ID == nil {
+		t.Fatal("ds has a nil id")
+	} else {
+		dsID = *ds[0].ID
+	}
+
+	dsRegexes, _, err := TOSession.GetDeliveryServiceRegexesByDSID(dsID, nil)
+	if err != nil {
+		t.Fatal("unable to get ds_regex by id " + strconv.Itoa(dsID))
+	}
+	if len(dsRegexes) != 3 {
+		t.Fatal("expected to get 3 ds_regex, got " + strconv.Itoa(len(dsRegexes)))
+	}
+
+	params := make(map[string]string)
+	params["id"] = strconv.Itoa(dsRegexes[0].ID)
+	dsRegexes, _, err = TOSession.GetDeliveryServiceRegexesByDSID(dsID, params)
+	if err != nil {
+		t.Fatalf("unable to get ds_regex by id %v with query param %v", dsID, params["id"])
+	}
+	if len(dsRegexes) != 1 {
+		t.Fatal("expected to get 1 ds_regex, got " + strconv.Itoa(len(dsRegexes)))
+	}
+}
diff --git a/traffic_ops/testing/api/v2/tc-fixtures.json b/traffic_ops/testing/api/v2/tc-fixtures.json
index 5b1346f..6f4262e 100644
--- a/traffic_ops/testing/api/v2/tc-fixtures.json
+++ b/traffic_ops/testing/api/v2/tc-fixtures.json
@@ -699,6 +699,26 @@
             "anonymousBlockingEnabled": true
         }
     ],
+    "deliveryServicesRegexes": [
+        {
+            "dsName": "ds1",
+            "typeName": "HOST_REGEXP",
+            "setNumber": 1,
+            "pattern" : ".*"
+        },
+        {
+            "dsName": "ds1",
+            "typeName": "HOST_REGEXP",
+            "setNumber": 2,
+            "pattern" : "\\d+"
+        },
+        {
+            "dsName": "ds2",
+            "typeName": "HOST_REGEXP",
+            "setNumber": 1,
+            "pattern" : ".*"
+        }
+    ],
     "deliveryservicesRequiredCapabilities": [
         {
             "xmlID": "ds1",
diff --git a/traffic_ops/testing/api/v2/traffic_control_test.go b/traffic_ops/testing/api/v2/traffic_control_test.go
index 7013684..7ec60c4 100644
--- a/traffic_ops/testing/api/v2/traffic_control_test.go
+++ b/traffic_ops/testing/api/v2/traffic_control_test.go
@@ -27,6 +27,7 @@ type TrafficControl struct {
 	CacheGroupParameterRequests          []tc.CacheGroupParameterRequest         `json:"cachegroupParameters"`
 	Capabilities                         []tc.Capability                         `json:"capability"`
 	Coordinates                          []tc.Coordinate                         `json:"coordinates"`
+	DeliveryServicesRegexes              []tc.DeliveryServiceRegexesTest         `json:"deliveryServicesRegexes"`
 	DeliveryServiceRequests              []tc.DeliveryServiceRequest             `json:"deliveryServiceRequests"`
 	DeliveryServiceRequestComments       []tc.DeliveryServiceRequestComment      `json:"deliveryServiceRequestComments"`
 	DeliveryServices                     []tc.DeliveryService                    `json:"deliveryservices"`
diff --git a/traffic_ops/testing/api/v2/withobjs_test.go b/traffic_ops/testing/api/v2/withobjs_test.go
index cbc0c79..1b1fc0e 100644
--- a/traffic_ops/testing/api/v2/withobjs_test.go
+++ b/traffic_ops/testing/api/v2/withobjs_test.go
@@ -43,6 +43,7 @@ const (
 	CDNFederations
 	Coordinates
 	DeliveryServices
+	DeliveryServicesRegexes
 	DeliveryServiceRequests
 	DeliveryServiceRequestComments
 	DeliveryServicesRequiredCapabilities
@@ -82,6 +83,7 @@ var withFuncs = map[TCObj]TCObjFuncs{
 	CDNFederations:                       {CreateTestCDNFederations, DeleteTestCDNFederations},
 	Coordinates:                          {CreateTestCoordinates, DeleteTestCoordinates},
 	DeliveryServices:                     {CreateTestDeliveryServices, DeleteTestDeliveryServices},
+	DeliveryServicesRegexes:              {CreateTestDeliveryServicesRegexes, DeleteTestDeliveryServicesRegexes},
 	DeliveryServiceRequests:              {CreateTestDeliveryServiceRequests, DeleteTestDeliveryServiceRequests},
 	DeliveryServiceRequestComments:       {CreateTestDeliveryServiceRequestComments, DeleteTestDeliveryServiceRequestComments},
 	DeliveryServicesRequiredCapabilities: {CreateTestDeliveryServicesRequiredCapabilities, DeleteTestDeliveryServicesRequiredCapabilities},
diff --git a/traffic_ops/traffic_ops_golang/deliveryservicesregexes/deliveryservicesregexes.go b/traffic_ops/traffic_ops_golang/deliveryservicesregexes/deliveryservicesregexes.go
index a5fff6b..0f4952c 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservicesregexes/deliveryservicesregexes.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservicesregexes/deliveryservicesregexes.go
@@ -112,7 +112,7 @@ JOIN type as rt ON r.type = rt.id
 	if err != nil {
 		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, fmt.Errorf("getting accessible tenants for user - %v", err))
 	}
-	if len(where) == 0 {
+	if len(where) > 0 {
 		where += " AND ds.tenant_id = ANY(:tenants) "
 	} else {
 		where = dbhelpers.BaseWhere + " ds.tenant_id = ANY(:tenants) "
@@ -120,6 +120,7 @@ JOIN type as rt ON r.type = rt.id
 	queryValues["tenants"] = pq.Array(accessibleTenants)
 
 	query := q + where + " ORDER BY dsr.set_number ASC" + pagination
+
 	rows, err := inf.Tx.NamedQuery(query, queryValues)
 	if err != nil {
 		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("querying deliveryserviceregexes get: "+err.Error()))