zrhoffman commented on code in PR #7009:
URL: https://github.com/apache/trafficcontrol/pull/7009#discussion_r949466185
##########
traffic_ops/testing/api/v4/cdn_locks_test.go:
##########
@@ -417,6 +522,16 @@ func TestCDNLocks(t *testing.T) {
assert.NoError(t, err,
"Error occurred when marshalling request body: %v", err)
err =
json.Unmarshal(dat, &staticDNSEntry)
assert.NoError(t, err,
"Error occurred when unmarshalling request body: %v", err)
+ } else if _, ok :=
testCase.RequestBody["routing_disabled"]; ok {
+ dat, err :=
json.Marshal(testCase.RequestBody)
+ assert.NoError(t, err,
"Error occurred when marshalling request body: %v", err)
+ err =
json.Unmarshal(dat, &profile)
+ assert.NoError(t, err,
"Error occurred when unmarshalling request body: %v", err)
+ } else if _, ok :=
testCase.RequestBody["parameterId"]; ok {
+ dat, err :=
json.Marshal(testCase.RequestBody)
+ assert.NoError(t, err,
"Error occurred when marshalling request body: %v", err)
+ err =
json.Unmarshal(dat, &profileParameter)
+ assert.NoError(t, err,
"Error occurred when unmarshalling request body: %v", err)
Review Comment:
Same thing here, use a `map[string]func(*testing.T)` instead of a very large
if/else if/else statement
##########
traffic_ops/testing/api/v4/cdn_locks_test.go:
##########
@@ -528,6 +643,52 @@ func TestCDNLocks(t *testing.T) {
}
})
}
+ case "PROFILE POST":
+ {
+ t.Run(name, func(t
*testing.T) {
+ alerts, reqInf,
err := testCase.ClientSession.CreateProfile(profile, testCase.RequestOpts)
+ for _, check :=
range testCase.Expectations {
+
check(t, reqInf, nil, alerts, err)
+ }
+ })
+ }
Review Comment:
This is a big switch statement, and each case added is a performance
penalty. Instead of having cases, how about a map of test functions?
```go
cases := map[string]func(*testing.T){
"GET": func(t *testing.T) {
/* ... */
},
"POST": func(t *testing.T) {
/* ... */
},
/* and so on */
}
```
called by
```go
cases[method](t)
```
##########
traffic_ops/testing/api/v3/profiles_export_test.go:
##########
@@ -0,0 +1,90 @@
+/*
+
+ 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 v3
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/traffic_ops/testing/api/assert"
+ "github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+ "github.com/apache/trafficcontrol/traffic_ops/toclientlib"
+)
+
+func TestProfilesExport(t *testing.T) {
+ WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles,
ProfileParameters}, func() {
+
+ methodTests := utils.V3TestCase{
+ "GET": {
+ "OK when VALID request": {
+ EndpointId: GetProfileID(t, "EDGE1"),
+ ClientSession: TOSession,
+ Expectations:
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
+
validateProfilesExportFields(map[string]interface{}{"ProfileCDNName": "cdn1",
"ProfileName": "EDGE1",
+ "ProfileDescription":
"edge1 description", "ProfileType": "ATS_PROFILE"})),
+ },
+ "NOT FOUND when PROFILE DOESNT EXIST": {
+ EndpointId: func() int { return
1111111111 },
+ ClientSession: TOSession,
+ Expectations:
utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
+ },
+ },
+ }
+
+ for method, testCases := range methodTests {
+ t.Run(method, func(t *testing.T) {
+ for name, testCase := range testCases {
+ switch method {
+ case "GET":
+ t.Run(name, func(t *testing.T) {
+ resp, reqInf, err :=
testCase.ClientSession.ExportProfile(testCase.EndpointId())
+ for _, check := range
testCase.Expectations {
+ check(t,
reqInf, resp, resp.Alerts, err)
+ }
+ })
+ }
+ }
+ })
+ }
+
+ })
+}
+
+func validateProfilesExportFields(expectedResp map[string]interface{})
utils.CkReqFunc {
+ return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _
tc.Alerts, _ error) {
+ assert.RequireNotNil(t, resp, "Expected Profiles Export
response to not be nil.")
+ profileExport := resp.(*tc.ProfileExportResponse)
+ for field, expected := range expectedResp {
+ switch field {
+ case "ProfileCDNName":
+ assert.RequireNotNil(t,
profileExport.Profile.CDNName, "Expected Profile CDNName to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.CDNName, "Expected Profile.CDNName to be %v, but got
%d", expected, *profileExport.Profile.CDNName)
+ case "ProfileDescription":
+ assert.RequireNotNil(t,
profileExport.Profile.Description, "Expected Profile Description to not be
nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Description, "Expected Profile.Description to be %v, but
got %d", expected, *profileExport.Profile.Description)
+ case "ProfileName":
+ assert.RequireNotNil(t,
profileExport.Profile.Name, "Expected Profile Name to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Name, "Expected Profile.Name to be %v, but got %d",
expected, *profileExport.Profile.Name)
+ case "ProfileType":
+ assert.RequireNotNil(t,
profileExport.Profile.Type, "Expected Profile Type to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Type, "Expected Profile.Type to be %v, but got %d",
expected, *profileExport.Profile.Type)
+ default:
+ t.Errorf("Expected field: %v, does not exist in
response", field)
Review Comment:
IMO this should be fatal so that an error this significant is not difficult
to find
##########
traffic_ops/testing/api/v4/profiles_export_test.go:
##########
@@ -0,0 +1,90 @@
+/*
+
+ 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 v4
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/traffic_ops/testing/api/assert"
+ "github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+ "github.com/apache/trafficcontrol/traffic_ops/toclientlib"
+)
+
+func TestProfilesExport(t *testing.T) {
+ WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles,
ProfileParameters}, func() {
+
+ methodTests := utils.V4TestCase{
+ "GET": {
+ "OK when VALID request": {
+ EndpointId: GetProfileID(t, "EDGE1"),
+ ClientSession: TOSession,
+ Expectations:
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
+
validateProfilesExportFields(map[string]interface{}{"ProfileCDNName": "cdn1",
"ProfileName": "EDGE1",
+ "ProfileDescription":
"edge1 description", "ProfileType": "ATS_PROFILE"})),
+ },
+ "NOT FOUND when PROFILE DOESNT EXIST": {
+ EndpointId: func() int { return
1111111111 },
+ ClientSession: TOSession,
+ Expectations:
utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
+ },
+ },
+ }
+
+ for method, testCases := range methodTests {
+ t.Run(method, func(t *testing.T) {
+ for name, testCase := range testCases {
+ switch method {
+ case "GET":
+ t.Run(name, func(t *testing.T) {
+ resp, reqInf, err :=
testCase.ClientSession.ExportProfile(testCase.EndpointId(),
testCase.RequestOpts)
+ for _, check := range
testCase.Expectations {
+ check(t,
reqInf, resp, resp.Alerts, err)
+ }
+ })
+ }
+ }
+ })
+ }
+
+ })
+}
+
+func validateProfilesExportFields(expectedResp map[string]interface{})
utils.CkReqFunc {
+ return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _
tc.Alerts, _ error) {
+ assert.RequireNotNil(t, resp, "Expected Profiles Export
response to not be nil.")
+ profileExport := resp.(tc.ProfileExportResponse)
+ for field, expected := range expectedResp {
+ switch field {
+ case "ProfileCDNName":
+ assert.RequireNotNil(t,
profileExport.Profile.CDNName, "Expected Profile CDNName to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.CDNName, "Expected Profile.CDNName to be %v, but got
%d", expected, *profileExport.Profile.CDNName)
+ case "ProfileDescription":
+ assert.RequireNotNil(t,
profileExport.Profile.Description, "Expected Profile Description to not be
nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Description, "Expected Profile.Description to be %v, but
got %d", expected, *profileExport.Profile.Description)
+ case "ProfileName":
+ assert.RequireNotNil(t,
profileExport.Profile.Name, "Expected Profile Name to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Name, "Expected Profile.Name to be %v, but got %d",
expected, *profileExport.Profile.Name)
+ case "ProfileType":
+ assert.RequireNotNil(t,
profileExport.Profile.Type, "Expected Profile Type to not be nil.")
+ assert.Equal(t, expected,
*profileExport.Profile.Type, "Expected Profile.Type to be %v, but got %d",
expected, *profileExport.Profile.Type)
+ default:
+ t.Errorf("Expected field: %v, does not exist in
response", field)
+ }
Review Comment:
These fields are all strings, and you already have the field name. So
instead of a switch statement, you could use
```go
fieldValue :=
reflect.Indirect(reflect.ValueOf(profileExport.Profile).FieldByName(field)).String()
```
to get the field value and only write `assert.RequireNotNil() +
assert.Equal()` once.
Note that the field name strings would need to be changed for this to work
(`"ProfileCDNName"` -> `"CDNName"`, `"ProfileDescription"` -> `"Description"`,
etc.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]