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 2018/11/02 15:06:18 UTC

[trafficcontrol] 09/09: fix profiles/parameters test and client

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

commit cae9c512bbf4bcbde57b2e9e97108fb4a6dac3f2
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Wed Oct 31 08:02:42 2018 -0600

    fix profiles/parameters test and client
---
 traffic_ops/client/parameter.go                    |  16 +++
 traffic_ops/client/profile_parameter.go            |   2 +-
 .../testing/api/v14/profile_parameters_test.go     | 132 +++++++++++++++++++++
 traffic_ops/testing/api/v14/profiles_test.go       |  96 ++++++++++++---
 traffic_ops/testing/api/v14/tc-fixtures.json       |  16 +--
 traffic_ops/testing/api/v14/traffic_control.go     |   1 +
 6 files changed, 236 insertions(+), 27 deletions(-)

diff --git a/traffic_ops/client/parameter.go b/traffic_ops/client/parameter.go
index f5706cd..e8a5eca 100644
--- a/traffic_ops/client/parameter.go
+++ b/traffic_ops/client/parameter.go
@@ -178,6 +178,22 @@ func (to *Session) GetParameterByNameAndConfigFile(name string, configFile strin
 	return data.Response, reqInf, nil
 }
 
+// GET a Parameter by the Parameter Name and ConfigFile and Value
+// TODO: API should support all 3,  but does not support filter by value
+// currently.  Until then, loop thru hits until you find one with that value
+func (to *Session) GetParameterByNameAndConfigFileAndValue(name, configFile, value string) ([]tc.Parameter, ReqInf, error) {
+	params, reqInf, err := to.GetParameterByNameAndConfigFile(name, configFile)
+	if err != nil {
+		return params, reqInf, err
+	}
+	for _, p := range params {
+		if p.Value == value {
+			return []tc.Parameter{p}, reqInf, err
+		}
+	}
+	return nil, reqInf, err
+}
+
 // DELETE a Parameter by ID
 func (to *Session) DeleteParameterByID(id int) (tc.Alerts, ReqInf, error) {
 	URI := fmt.Sprintf("%s/%d", API_v13_Parameters, id)
diff --git a/traffic_ops/client/profile_parameter.go b/traffic_ops/client/profile_parameter.go
index 69cda78..667cfd3 100644
--- a/traffic_ops/client/profile_parameter.go
+++ b/traffic_ops/client/profile_parameter.go
@@ -83,7 +83,7 @@ func (to *Session) GetProfileParameterByQueryParams(queryParams string) ([]tc.Pr
 
 // DELETE a Parameter by Parameter
 func (to *Session) DeleteParameterByProfileParameter(profile int, parameter int) (tc.Alerts, ReqInf, error) {
-	URI := fmt.Sprintf("%s/profile/%d/parameter/%d", API_v13_Profile_Parameters, profile, parameter)
+	URI := fmt.Sprintf("%s/%d/%d", API_v13_Profile_Parameters, profile, parameter)
 	resp, remoteAddr, err := to.request(http.MethodDelete, URI, nil)
 	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
 	if err != nil {
diff --git a/traffic_ops/testing/api/v14/profile_parameters_test.go b/traffic_ops/testing/api/v14/profile_parameters_test.go
new file mode 100644
index 0000000..2a96777
--- /dev/null
+++ b/traffic_ops/testing/api/v14/profile_parameters_test.go
@@ -0,0 +1,132 @@
+/*
+
+   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 v14
+
+import (
+	"fmt"
+	"sync"
+	"testing"
+
+	"github.com/apache/trafficcontrol/lib/go-log"
+	"github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+const queryParamFormat = "?profileId=%d&parameterId=%d"
+
+func TestProfileParameters(t *testing.T) {
+
+	CreateTestCDNs(t)
+	CreateTestTypes(t)
+	CreateTestParameters(t)
+	CreateTestProfiles(t)
+	CreateTestProfileParameters(t)
+	GetTestProfileParameters(t)
+	DeleteTestProfileParameters(t)
+	DeleteTestParameters(t)
+	DeleteTestProfiles(t)
+	DeleteTestTypes(t)
+	DeleteTestCDNs(t)
+
+}
+
+func CreateTestProfileParameters(t *testing.T) {
+
+	firstProfile := testData.Profiles[0]
+	profileResp, _, err := TOSession.GetProfileByName(firstProfile.Name)
+	if err != nil {
+		t.Errorf("cannot GET Profile by name: %v - %v\n", firstProfile.Name, err)
+	}
+
+	firstParameter := testData.Parameters[0]
+	paramResp, _, err := TOSession.GetParameterByName(firstParameter.Name)
+	if err != nil {
+		t.Errorf("cannot GET Parameter by name: %v - %v\n", firstParameter.Name, err)
+	}
+
+	profileID := profileResp[0].ID
+	parameterID := paramResp[0].ID
+
+	pp := tc.ProfileParameter{
+		ProfileID:   profileID,
+		ParameterID: parameterID,
+	}
+	resp, _, err := TOSession.CreateProfileParameter(pp)
+	log.Debugln("Response: ", resp)
+	if err != nil {
+		t.Errorf("could not CREATE profile parameters: %v\n", err)
+	}
+
+}
+
+func GetTestProfileParameters(t *testing.T) {
+
+	for _, pp := range testData.ProfileParameters {
+		queryParams := fmt.Sprintf(queryParamFormat, pp.ProfileID, pp.ParameterID)
+		resp, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
+		if err != nil {
+			t.Errorf("cannot GET Parameter by name: %v - %v\n", err, resp)
+		}
+	}
+}
+
+func DeleteTestProfileParametersParallel(t *testing.T) {
+
+	var wg sync.WaitGroup
+	for _, pp := range testData.ProfileParameters {
+
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			DeleteTestProfileParameter(t, pp)
+		}()
+
+	}
+	wg.Wait()
+}
+
+func DeleteTestProfileParameters(t *testing.T) {
+
+	for _, pp := range testData.ProfileParameters {
+		DeleteTestProfileParameter(t, pp)
+	}
+}
+
+func DeleteTestProfileParameter(t *testing.T, pp tc.ProfileParameter) {
+
+	queryParams := fmt.Sprintf(queryParamFormat, pp.ProfileID, pp.ParameterID)
+	// Retrieve the PtofileParameter by profile so we can get the id for the Update
+	resp, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
+	if err != nil {
+		t.Errorf("cannot GET Parameter by profile: %v - %v\n", pp.Profile, err)
+	}
+	if len(resp) > 0 {
+		respPP := resp[0]
+
+		delResp, _, err := TOSession.DeleteParameterByProfileParameter(respPP.ProfileID, respPP.ParameterID)
+		if err != nil {
+			t.Errorf("cannot DELETE Parameter by profile: %v - %v\n", err, delResp)
+		}
+
+		// Retrieve the Parameter to see if it got deleted
+		pps, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
+		if err != nil {
+			t.Errorf("error deleting Parameter name: %s\n", err.Error())
+		}
+		if len(pps) > 0 {
+			t.Errorf("expected Parameter Name: %s and ConfigFile: %s to be deleted\n", pp.Profile, pp.Parameter)
+		}
+	}
+}
diff --git a/traffic_ops/testing/api/v14/profiles_test.go b/traffic_ops/testing/api/v14/profiles_test.go
index f8e25fa..82ab0e2 100644
--- a/traffic_ops/testing/api/v14/profiles_test.go
+++ b/traffic_ops/testing/api/v14/profiles_test.go
@@ -16,6 +16,7 @@
 package v14
 
 import (
+	"strings"
 	"testing"
 
 	"github.com/apache/trafficcontrol/lib/go-log"
@@ -30,9 +31,11 @@ func TestProfiles(t *testing.T) {
 	// attempt to create profiles with missing info
 	CreateBadProfiles(t)
 	CreateTestProfiles(t)
+	CreateTestParameters(t)
 	UpdateTestProfiles(t)
 	GetTestProfiles(t)
 	GetTestProfilesWithParameters(t)
+	DeleteTestParameters(t)
 	DeleteTestProfiles(t)
 	DeleteTestTypes(t)
 	DeleteTestCDNs(t)
@@ -70,6 +73,41 @@ func CreateTestProfiles(t *testing.T) {
 		if err != nil {
 			t.Errorf("could not CREATE profiles with name: %s %v\n", pr.Name, err)
 		}
+		profiles, _, err := TOSession.GetProfileByName(pr.Name)
+		if err != nil {
+			t.Errorf("could not GET profile with name: %s %v\n", pr.Name, err)
+		}
+		if len(profiles) == 0 {
+			t.Errorf("could not GET profile %++v: not found\n", pr)
+		}
+		profileID := profiles[0].ID
+
+		for _, param := range pr.Parameters {
+			if param.Name == nil || param.Value == nil || param.ConfigFile == nil {
+				t.Errorf("invalid parameter specification: %++v", param)
+				continue
+			}
+			_, _, err := TOSession.CreateParameter(tc.Parameter{Name: *param.Name, Value: *param.Value, ConfigFile: *param.ConfigFile})
+			if err != nil {
+				// ok if already exists
+				if !strings.Contains(err.Error(), "already exists") {
+					t.Errorf("could not CREATE parameter %++v: %s\n", param, err.Error())
+					continue
+				}
+			}
+			p, _, err := TOSession.GetParameterByNameAndConfigFileAndValue(*param.Name, *param.ConfigFile, *param.Value)
+			if err != nil {
+				t.Errorf("could not GET parameter %++v: %s\n", param, err.Error())
+			}
+			if len(p) == 0 {
+				t.Errorf("could not GET parameter %++v: not found\n", param)
+			}
+			_, _, err = TOSession.CreateProfileParameter(tc.ProfileParameter{ProfileID: profileID, ParameterID: p[0].ID})
+			if err != nil {
+				t.Errorf("could not CREATE profile_parameter %++v: %s\n", param, err.Error())
+			}
+		}
+
 	}
 }
 
@@ -127,12 +165,23 @@ func GetTestProfilesWithParameters(t *testing.T) {
 	resp, _, err := TOSession.GetProfileByName(firstProfile.Name)
 	if err != nil {
 		t.Errorf("cannot GET Profile by name: %v - %v\n", err, resp)
+		return
+	}
+	if len(resp) == 0 {
+		t.Errorf("cannot GET Profile by name: not found - %v\n", resp)
+		return
+	}
+	respProfile := resp[0]
+	// query by name does not retrieve associated parameters.  But query by id does.
+	resp, _, err = TOSession.GetProfileByID(respProfile.ID)
+	if err != nil {
+		t.Errorf("cannot GET Profile by name: %v - %v\n", err, resp)
 	}
 	if len(resp) > 0 {
-		respProfile := resp[0]
+		respProfile = resp[0]
 		respParameters := respProfile.Parameters
 		if len(respParameters) == 0 {
-			t.Errorf("expected a profile with parameters to be retrieved: %v - %v\n", err, respProfile)
+			t.Errorf("expected a profile with parameters to be retrieved: %v - %v\n", err, respParameters)
 		}
 	}
 }
@@ -143,25 +192,40 @@ func DeleteTestProfiles(t *testing.T) {
 		// Retrieve the Profile by name so we can get the id for the Update
 		resp, _, err := TOSession.GetProfileByName(pr.Name)
 		if err != nil {
-			t.Errorf("cannot GET Profile by name: %v - %v\n", pr.Name, err)
+			t.Errorf("cannot GET Profile by name: %s - %v\n", pr.Name, err)
+			continue
+		}
+		if len(resp) == 0 {
+			t.Errorf("cannot GET Profile by name: not found - %s\n", pr.Name)
+			continue
 		}
-		if len(resp) > 0 {
-			respProfile := resp[0]
 
-			delResp, _, err := TOSession.DeleteProfileByID(respProfile.ID)
+		profileID := resp[0].ID
+		// query by name does not retrieve associated parameters.  But query by id does.
+		resp, _, err = TOSession.GetProfileByID(profileID)
+		if err != nil {
+			t.Errorf("cannot GET Profile by id: %v - %v\n", err, resp)
+		}
+		// delete any profile_parameter associations first
+		for _, param := range resp[0].Parameters {
+			_, _, err := TOSession.DeleteParameterByProfileParameter(profileID, *param.ID)
 			if err != nil {
-				t.Errorf("cannot DELETE Profile by name: %v - %v\n", err, delResp)
+				t.Errorf("cannot DELETE profile_parameter with profileID %d, parameterID %d: %s\n", profileID, *param.ID, err.Error())
 			}
-			//time.Sleep(1 * time.Second)
+		}
+		delResp, _, err := TOSession.DeleteProfileByID(profileID)
+		if err != nil {
+			t.Errorf("cannot DELETE Profile by name: %v - %v\n", err, delResp)
+		}
+		//time.Sleep(1 * time.Second)
 
-			// Retrieve the Profile to see if it got deleted
-			prs, _, err := TOSession.GetProfileByName(pr.Name)
-			if err != nil {
-				t.Errorf("error deleting Profile name: %s\n", err.Error())
-			}
-			if len(prs) > 0 {
-				t.Errorf("expected Profile Name: %s to be deleted\n", pr.Name)
-			}
+		// Retrieve the Profile to see if it got deleted
+		prs, _, err := TOSession.GetProfileByName(pr.Name)
+		if err != nil {
+			t.Errorf("error deleting Profile name: %s\n", err.Error())
+		}
+		if len(prs) > 0 {
+			t.Errorf("expected Profile Name: %s to be deleted\n", pr.Name)
 		}
 	}
 }
diff --git a/traffic_ops/testing/api/v14/tc-fixtures.json b/traffic_ops/testing/api/v14/tc-fixtures.json
index 8a1389f..211b651 100644
--- a/traffic_ops/testing/api/v14/tc-fixtures.json
+++ b/traffic_ops/testing/api/v14/tc-fixtures.json
@@ -561,18 +561,14 @@
             "type": "ATS_PROFILE",
             "params": [
                 {
-                    "configFile": "url_sig.config",
-                    "lastUpdated": "2018-01-19T19:01:21.431062+00:00",
-                    "name": "error_url",
-                    "secure": true,
-                    "value": "403"
+                    "name": "param1",
+                    "configFile": "param1.txt",
+                    "value": "value1"
                 },
                 {
-                    "configFile": "records.config",
-                    "lastUpdated": "2018-01-19T19:01:21.432692+00:00",
-                    "name": "CONFIG proxy.config.allocator.debug_filter",
-                    "secure": false,
-                    "value": "INT 0"
+                    "name": "param2",
+                    "configFile": "param2.txt",
+                    "value": "value2"
                 }
             ]
         },
diff --git a/traffic_ops/testing/api/v14/traffic_control.go b/traffic_ops/testing/api/v14/traffic_control.go
index 5fd6ce5..200bfdc 100644
--- a/traffic_ops/testing/api/v14/traffic_control.go
+++ b/traffic_ops/testing/api/v14/traffic_control.go
@@ -33,6 +33,7 @@ type TrafficControl struct {
 	Origins                        []tc.Origin                        `json:"origins"`
 	Profiles                       []tc.Profile                       `json:"profiles"`
 	Parameters                     []tc.Parameter                     `json:"parameters"`
+	ProfileParameters              []tc.ProfileParameter              `json:"profileParameters"`
 	PhysLocations                  []tc.PhysLocation                  `json:"physLocations"`
 	Regions                        []tc.Region                        `json:"regions"`
 	Roles                          []tc.Role                          `json:"roles"`