Skip to content

Commit

Permalink
Use Generics in TO Integration Tests (#7144)
Browse files Browse the repository at this point in the history
* Add generic pointer function

* Use generics and clean up

* undo accidental format

* use generic ptr func

* fix typo

* use generic ptr func

* updated tests to use generics

* created own test file for the new route

* Add license

* fix error

* Updated tests to use generics

* Updated v5 tests to use generics

* undo format
  • Loading branch information
ericholguin authored Oct 20, 2022
1 parent bd6f7ee commit 736b5d5
Show file tree
Hide file tree
Showing 73 changed files with 1,871 additions and 2,342 deletions.
8 changes: 5 additions & 3 deletions traffic_ops/testing/api/v4/deliveryservicesideligible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"testing"

"github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

func TestDeliveryServicesEligible(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, Topologies, ServiceCategories, DeliveryServices}, func() {

methodTests := utils.V4TestCase{
methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
EndpointId: GetDeliveryServiceId(t, "ds1"), ClientSession: TOSession,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1)),
EndpointId: GetDeliveryServiceId(t, "ds1"),
ClientSession: TOSession,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1)),
},
},
}
Expand Down
34 changes: 13 additions & 21 deletions traffic_ops/testing/api/v4/deliveryservicesregexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package v4

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand All @@ -32,25 +31,27 @@ import (
func TestDeliveryServicesRegexes(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Tenants, Users, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, Topologies, ServiceCategories, DeliveryServices, DeliveryServicesRegexes}, func() {

methodTests := utils.V4TestCase{
methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.DeliveryServiceRegexPost]{
"GET": {
"OK when VALID request": {
EndpointId: GetDeliveryServiceId(t, "ds1"), ClientSession: TOSession,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(3)),
EndpointId: GetDeliveryServiceId(t, "ds1"),
ClientSession: TOSession,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(3)),
},
"OK when VALID ID parameter": {
EndpointId: GetDeliveryServiceId(t, "ds1"), ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"id": {strconv.Itoa(getDSRegexID(t, "ds1"))}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1)),
EndpointId: GetDeliveryServiceId(t, "ds1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"id": {strconv.Itoa(getDSRegexID(t, "ds1"))}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1)),
},
},
"POST": {
"BAD REQUEST when MISSING REGEX PATTERN": {
EndpointId: GetDeliveryServiceId(t, "ds1"), ClientSession: TOSession,
RequestBody: map[string]interface{}{
"type": GetTypeId(t, "HOST_REGEXP"),
"setNumber": 3,
"pattern": "",
RequestBody: tc.DeliveryServiceRegexPost{
Type: GetTypeId(t, "HOST_REGEXP"),
SetNumber: 3,
Pattern: "",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
Expand All @@ -60,15 +61,6 @@ func TestDeliveryServicesRegexes(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {

dsRegex := tc.DeliveryServiceRegexPost{}
if testCase.RequestBody != nil {
dat, err := json.Marshal(testCase.RequestBody)
assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
err = json.Unmarshal(dat, &dsRegex)
assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
}

switch method {
case "GET":
t.Run(name, func(t *testing.T) {
Expand All @@ -79,7 +71,7 @@ func TestDeliveryServicesRegexes(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
alerts, reqInf, err := testCase.ClientSession.PostDeliveryServiceRegexesByDSID(testCase.EndpointId(), dsRegex, testCase.RequestOpts)
alerts, reqInf, err := testCase.ClientSession.PostDeliveryServiceRegexesByDSID(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
Expand Down
59 changes: 59 additions & 0 deletions traffic_ops/testing/api/v4/multiple_server_capabilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package v4

/*
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.
*/

import (
"net/http"
"testing"

"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

func TestMultipleServerCapabilities(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, ServerCapabilities, ServerServerCapabilities}, func() {

methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.MultipleServerCapabilities]{
"PUT": {
"OK when VALID REQUEST": {
ClientSession: TOSession,
RequestBody: tc.MultipleServerCapabilities{
ServerID: GetServerID(t, "dtrc-mid-04")(),
ServerCapabilities: []string{"disk", "blah"},
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
}

for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
switch method {
case "PUT":
t.Run(name, func(t *testing.T) {
alerts, reqInf, err := testCase.ClientSession.AssignMultipleServerCapability(testCase.RequestBody, testCase.RequestOpts, testCase.RequestBody.ServerID)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
}
}
})
}
})
}
82 changes: 28 additions & 54 deletions traffic_ops/testing/api/v4/server_server_capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package v4
*/

import (
"encoding/json"
"net/http"
"net/url"
"sort"
Expand All @@ -26,6 +25,7 @@ import (

"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-util"
"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"
Expand All @@ -37,9 +37,8 @@ func TestServerServerCapabilities(t *testing.T) {

currentTime := time.Now().UTC().Add(-15 * time.Second)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
var multipleSCs []string

methodTests := utils.V4TestCase{
methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ServerServerCapability]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
Expand Down Expand Up @@ -103,61 +102,51 @@ func TestServerServerCapabilities(t *testing.T) {
"POST": {
"BAD REQUEST when ALREADY EXISTS": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": GetServerID(t, "dtrc-mid-01")(),
"serverCapability": "disk",
RequestBody: tc.ServerServerCapability{
ServerID: util.Ptr(GetServerID(t, "dtrc-mid-01")()),
ServerCapability: util.Ptr("disk"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING SERVER ID": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverCapability": "disk",
RequestBody: tc.ServerServerCapability{
Server: util.Ptr("disk"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING SERVER CAPABILITY": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": GetServerID(t, "dtrc-mid-01")(),
RequestBody: tc.ServerServerCapability{
ServerID: util.Ptr(GetServerID(t, "dtrc-mid-01")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"NOT FOUND when SERVER CAPABILITY DOESNT EXIST": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": GetServerID(t, "dtrc-mid-01")(),
"serverCapability": "bogus",
RequestBody: tc.ServerServerCapability{
ServerID: util.Ptr(GetServerID(t, "dtrc-mid-01")()),
ServerCapability: util.Ptr("bogus"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when SERVER DOESNT EXIST": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": 99999999,
"serverCapability": "bogus",
RequestBody: tc.ServerServerCapability{
ServerID: util.Ptr(99999999),
ServerCapability: util.Ptr("bogus"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"BAD REQUEST when SERVER TYPE NOT EDGE or MID": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": GetServerID(t, "trafficvault")(),
"serverCapability": "bogus",
RequestBody: tc.ServerServerCapability{
ServerID: util.Ptr(GetServerID(t, "trafficvault")()),
ServerCapability: util.Ptr("bogus"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
},
"PUT": {
"OK When Assigned Multiple Server Capabilities": {
ClientSession: TOSession,
RequestBody: map[string]interface{}{
"serverId": GetServerID(t, "dtrc-mid-04")(),
"serverCapabilities": append(multipleSCs, "disk", "blah"),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
"DELETE": {
"OK when NOT the LAST SERVER of CACHE GROUP of TOPOLOGY DS which has REQUIRED CAPABILITIES": {
ClientSession: TOSession,
Expand Down Expand Up @@ -190,22 +179,6 @@ func TestServerServerCapabilities(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
ssc := tc.ServerServerCapability{}
msc := tc.MultipleServerCapabilities{}
var serverId int
var serverCapability string

if testCase.RequestBody != nil {
dat, err := json.Marshal(testCase.RequestBody)
assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
if method == "PUT" {
err = json.Unmarshal(dat, &msc)
} else {
err = json.Unmarshal(dat, &ssc)
}
assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
}

switch method {
case "GET":
t.Run(name, func(t *testing.T) {
Expand All @@ -216,25 +189,26 @@ func TestServerServerCapabilities(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
alerts, reqInf, err := testCase.ClientSession.CreateServerServerCapability(ssc, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
alerts, reqInf, err := testCase.ClientSession.AssignMultipleServerCapability(msc, testCase.RequestOpts, serverId)
alerts, reqInf, err := testCase.ClientSession.CreateServerServerCapability(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "DELETE":
t.Run(name, func(t *testing.T) {
var serverId int
var serverCapability string
var err error
if val, ok := testCase.RequestOpts.QueryParameters["serverId"]; ok {
serverId, _ = strconv.Atoi(val[0])
serverId, err = strconv.Atoi(val[0])
assert.RequireNoError(t, err, "Expected no error when converting string to int: %v", err)
} else {
t.Fatalf("Query Parameter: \"serverId\" is required for DELETE method tests.")
}
if val, ok := testCase.RequestOpts.QueryParameters["serverCapability"]; ok {
serverCapability = val[0]
} else {
t.Fatalf("Query Parameter: \"serverCapability\" is required for DELETE method tests.")
}
alerts, reqInf, err := testCase.ClientSession.DeleteServerServerCapability(serverId, serverCapability, testCase.RequestOpts)
for _, check := range testCase.Expectations {
Expand Down
Loading

0 comments on commit 736b5d5

Please sign in to comment.