This is an automated email from the ASF dual-hosted git repository.
ocket8888 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 c34b120497 Refactor cdn queue updates test (#6894)
c34b120497 is described below
commit c34b120497d10e62bb8ca3c0f43da146a2292f05
Author: Eric Holguin <[email protected]>
AuthorDate: Tue Jun 14 19:21:08 2022 -0600
Refactor cdn queue updates test (#6894)
* Refactor cdn queue updates test
* Fix order of struct field
---
.../v4/cdn_queue_updates_by_type_profile_test.go | 215 ---------------------
.../testing/api/v4/cdn_queue_updates_test.go | 130 +++++++++++++
2 files changed, 130 insertions(+), 215 deletions(-)
diff --git
a/traffic_ops/testing/api/v4/cdn_queue_updates_by_type_profile_test.go
b/traffic_ops/testing/api/v4/cdn_queue_updates_by_type_profile_test.go
deleted file mode 100644
index 1a1bccbdd9..0000000000
--- a/traffic_ops/testing/api/v4/cdn_queue_updates_by_type_profile_test.go
+++ /dev/null
@@ -1,215 +0,0 @@
-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 (
- "strconv"
- "strings"
- "testing"
-
- client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
-)
-
-func TestCDNQueueUpdateByProfileAndType(t *testing.T) {
- WithObjs(t, []TCObj{Types, CDNs, Profiles, Statuses, Divisions,
Regions, PhysLocations, CacheGroups, Servers}, func() {
- QueueUpdatesByType(t)
- QueueUpdatesByProfile(t)
- })
-}
-
-func QueueUpdatesByType(t *testing.T) {
- allServersResp, _, err :=
TOSession.GetServers(client.NewRequestOptions())
- if err != nil {
- t.Fatalf("couldn't get all servers: %v", err)
- }
-
- // Clear updates on all servers to begin with
- for _, s := range allServersResp.Response {
- if s.ID != nil {
- _, _, err = TOSession.SetServerQueueUpdate(*s.ID,
false, client.NewRequestOptions())
- if err != nil {
- t.Errorf("couldn't clear updates on server with
ID: %d, err: %v", *s.ID, err.Error())
- }
- }
- }
-
- allServersResp, _, err =
TOSession.GetServers(client.NewRequestOptions())
- if err != nil {
- t.Fatalf("couldn't get all servers: %v", err)
- }
- queryOpts := client.NewRequestOptions()
- if len(testData.Servers) < 1 {
- t.Fatalf("no servers to run the tests on...quitting.")
- }
- server := testData.Servers[0]
- opts := client.NewRequestOptions()
- if server.CDNName == nil {
- t.Fatalf("server doesn't have a CDN name...quitting")
- }
- opts.QueryParameters.Set("name", *server.CDNName)
-
- // Get the first server's CDN ID
- cdns, _, err := TOSession.GetCDNs(opts)
- if err != nil {
- t.Fatalf("error while getting CDNs: %v", err)
- }
- if len(cdns.Response) < 1 {
- t.Fatalf("expected 1 CDN in response, got %d",
len(cdns.Response))
- }
- opts.QueryParameters.Del("name")
- queryOpts.QueryParameters.Set("type", server.Type)
- // Queue updates by type (and CDN)
- _, _, err = TOSession.QueueUpdatesForCDN(cdns.Response[0].ID, true,
queryOpts)
- if err != nil {
- t.Errorf("couldn't queue updates by type (and CDN): %v", err)
- }
-
- // Get all the servers for the same CDN and type as that of the first
server
- opts.QueryParameters.Set("cdn", strconv.Itoa(cdns.Response[0].ID))
- opts.QueryParameters.Set("type", server.Type)
- serverIDMap := make(map[int]bool, 0)
- resp, _, err := TOSession.GetServers(opts)
- if err != nil {
- t.Fatalf("couldn't get servers by cdn and type: %v", err)
- }
- if len(resp.Response) < 1 {
- t.Fatalf("expected atleast one server in response, got %d",
len(resp.Response))
- }
- for _, s := range resp.Response {
- if s.UpdPending == nil || !*s.UpdPending {
- t.Errorf("expected updates to be queued on all the
servers filtered by type and CDN, but %s didn't queue updates", *s.HostName)
- }
- if s.ID != nil {
- serverIDMap[*s.ID] = true
- }
- }
-
- // Make sure that the servers that are not filtered by the above
criteria do not have updates queued
- allServersResp, _, err =
TOSession.GetServers(client.NewRequestOptions())
- if err != nil {
- t.Fatalf("couldn't get all servers: %v", err)
- }
- for _, s := range allServersResp.Response {
- if s.ID != nil {
- if _, ok := serverIDMap[*s.ID]; !ok {
- if s.UpdPending != nil && *s.UpdPending {
- t.Errorf("did not expect server with
ID: %d to have queued updates", *s.ID)
- }
- }
-
- }
- }
- _, _, err = TOSession.QueueUpdatesForCDN(cdns.Response[0].ID, false,
queryOpts)
- if err != nil {
- t.Errorf("couldn't queue updates by type (and CDN): %v", err)
- }
-}
-
-func QueueUpdatesByProfile(t *testing.T) {
- allServersResp, _, err :=
TOSession.GetServers(client.NewRequestOptions())
- if err != nil {
- t.Fatalf("couldn't get all servers: %v", err)
- }
-
- // Clear updates on all servers to begin with
- for _, s := range allServersResp.Response {
- if s.ID != nil {
- _, _, err = TOSession.SetServerQueueUpdate(*s.ID,
false, client.NewRequestOptions())
- if err != nil {
- t.Errorf("couldn't clear updates on server with
ID: %d, err: %v", *s.ID, err.Error())
- }
- }
- }
-
- queryOpts := client.NewRequestOptions()
- if len(testData.Servers) < 1 {
- t.Fatalf("no servers to run the tests on...quitting.")
- }
- server := testData.Servers[0]
- opts := client.NewRequestOptions()
- if server.CDNName == nil || len(server.ProfileNames) == 0 {
- t.Fatalf("server doesn't have a CDN name or a profile
name...quitting")
- }
-
- //Get the first server's CDN ID
- opts.QueryParameters.Set("name", strings.TrimSpace(*server.CDNName))
-
- cdns, _, err := TOSession.GetCDNs(opts)
- if err != nil {
- t.Fatalf("error while getting CDNs: %v", err)
- }
- if len(cdns.Response) < 1 {
- t.Fatalf("expected 1 CDN in response, got %d",
len(cdns.Response))
- }
- opts.QueryParameters.Del("name")
-
- // Get the first server's Profile ID
- opts.QueryParameters.Set("name", server.ProfileNames[0])
- profiles, _, err := TOSession.GetProfiles(opts)
- if err != nil {
- t.Fatalf("error while getting profiles: %v", err)
- }
- if len(profiles.Response) < 1 {
- t.Fatalf("expected 1 profile in response, got %d",
len(profiles.Response))
- }
- opts.QueryParameters.Del("name")
- queryOpts.QueryParameters.Set("profile", profiles.Response[0].Name)
- // Queue updates by profile (and CDN)
- _, _, err = TOSession.QueueUpdatesForCDN(cdns.Response[0].ID, true,
queryOpts)
- if err != nil {
- t.Errorf("couldn't queue updates by profile (and CDN): %v", err)
- }
-
- // Get all the servers for the same CDN and profile as that of the
first server
- opts.QueryParameters.Set("cdn", strconv.Itoa(cdns.Response[0].ID))
- opts.QueryParameters.Set("profileName", profiles.Response[0].Name)
- serverIDMap := make(map[int]bool, 0)
- resp, _, err := TOSession.GetServers(opts)
- if err != nil {
- t.Fatalf("couldn't get servers by cdn and profile: %v", err)
- }
- if len(resp.Response) < 1 {
- t.Fatalf("expected atleast one server in response, got %d",
len(resp.Response))
- }
- for _, s := range resp.Response {
- if s.UpdPending == nil || !*s.UpdPending {
- t.Errorf("expected updates to be queued on all the
servers filtered by profile and CDN, but %s didn't queue updates", *s.HostName)
- }
- if s.ID != nil {
- serverIDMap[*s.ID] = true
- }
- }
-
- // Make sure that the servers that are not filtered by the above
criteria do not have updates queued
- allServersResp, _, err =
TOSession.GetServers(client.NewRequestOptions())
- if err != nil {
- t.Fatalf("couldn't get all servers: %v", err)
- }
- for _, s := range allServersResp.Response {
- if s.ID != nil {
- if _, ok := serverIDMap[*s.ID]; !ok {
- if s.UpdPending != nil && *s.UpdPending {
- t.Errorf("did not expect server with
ID: %d to have queued updates", *s.ID)
- }
- }
-
- }
- }
- _, _, err = TOSession.QueueUpdatesForCDN(cdns.Response[0].ID, false,
queryOpts)
- if err != nil {
- t.Errorf("couldn't queue updates by type (and CDN): %v", err)
- }
-}
diff --git a/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
b/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
new file mode 100644
index 0000000000..a5cef60d2d
--- /dev/null
+++ b/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
@@ -0,0 +1,130 @@
+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"
+ "net/url"
+ "strconv"
+ "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"
+ client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
+)
+
+func TestCDNQueueUpdates(t *testing.T) {
+ WithObjs(t, []TCObj{Types, CDNs, Profiles, Statuses, Divisions,
Regions, PhysLocations, CacheGroups, Servers}, func() {
+
+ methodTests := utils.V4TestCase{
+ "POST": {
+ "OK when VALID TYPE parameter": {
+ EndpointId: GetCDNID(t, "cdn1"),
+ ClientSession: TOSession,
+ RequestOpts:
client.RequestOptions{QueryParameters: url.Values{"type": {"EDGE"}}},
+ RequestBody: map[string]interface{}{
+ "action": "queue",
+ },
+ Expectations:
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
+
validateServersUpdatePending(GetCDNID(t, "cdn1")(), map[string]string{"type":
"EDGE"})),
+ },
+ "OK when VALID PROFILE parameter": {
+ EndpointId: GetCDNID(t, "cdn1"),
+ ClientSession: TOSession,
+ RequestOpts:
client.RequestOptions{QueryParameters: url.Values{"profile": {"EDGE1"}}},
+ RequestBody: map[string]interface{}{
+ "action": "queue",
+ },
+ Expectations:
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
+
validateServersUpdatePending(GetCDNID(t, "cdn1")(),
map[string]string{"profileName": "EDGE1"})),
+ },
+ },
+ }
+ for method, testCases := range methodTests {
+ t.Run(method, func(t *testing.T) {
+ for name, testCase := range testCases {
+
+ var queueUpdate bool
+ if testCase.RequestBody != nil {
+ if action, ok :=
testCase.RequestBody["action"]; ok {
+ if action == "queue" {
+ queueUpdate =
true
+ } else if action ==
"dequeue" {
+ queueUpdate =
false
+ } else {
+ t.Errorf("Not a
valid action: %v", action)
+ }
+ }
+ }
+
+ switch method {
+ case "POST":
+ t.Run(name, func(t *testing.T) {
+ // Clear updates on all
associated cdn servers to begin with
+ _, _, err :=
TOSession.QueueUpdatesForCDN(testCase.EndpointId(), false,
client.RequestOptions{})
+
assert.RequireNoError(t, err, "Failed to clear updates for cdn %d",
testCase.EndpointId())
+ resp, reqInf, err :=
testCase.ClientSession.QueueUpdatesForCDN(testCase.EndpointId(), queueUpdate,
testCase.RequestOpts)
+ for _, check := range
testCase.Expectations {
+ check(t,
reqInf, resp, tc.Alerts{}, err)
+ }
+ })
+ }
+ }
+ })
+ }
+ })
+}
+
+func validateServersUpdatePending(cdnID int, params map[string]string)
utils.CkReqFunc {
+ return func(t *testing.T, _ toclientlib.ReqInf, _ interface{}, _
tc.Alerts, _ error) {
+ // Get all the servers for the same CDN and type as that of the
first server
+ serverIDMap := make(map[int]bool, 0)
+ opts := client.NewRequestOptions()
+ opts.QueryParameters.Set("cdn", strconv.Itoa(cdnID))
+ for k, v := range params {
+ opts.QueryParameters.Set(k, v)
+ }
+
+ servers, _, err := TOSession.GetServers(opts)
+ assert.RequireNoError(t, err, "Couldn't get servers by cdn and
parameters: %v", err)
+ assert.RequireGreaterOrEqual(t, len(servers.Response), 1,
"expected atleast one server in response, got %d", len(servers.Response))
+
+ for _, server := range servers.Response {
+ assert.RequireNotNil(t, server.HostName, "Expected
server hostname to not be nil.")
+ assert.RequireNotNil(t, server.UpdPending, "Expected
Update Pending field for server %s to not be nil.", *server.HostName)
+ assert.Equal(t, true, *server.UpdPending, "Expected
updates to be queued on all the servers filtered by CDN and parameter, but %s
didn't queue updates", *server.HostName)
+ if server.ID != nil {
+ serverIDMap[*server.ID] = true
+ }
+ }
+
+ // Make sure that the servers that are not filtered by the
above criteria do not have updates queued
+ allServersResp, _, err :=
TOSession.GetServers(client.NewRequestOptions())
+ assert.RequireNoError(t, err, "Couldn't get all servers: %v",
err)
+
+ for _, server := range allServersResp.Response {
+ if server.ID != nil {
+ if _, ok := serverIDMap[*server.ID]; !ok {
+ assert.RequireNotNil(t,
server.HostName, "Expected server hostname to not be nil.")
+ assert.RequireNotNil(t,
server.UpdPending, "Expected Update Pending field for server %s to not be
nil.", *server.HostName)
+ assert.Equal(t, false,
*server.UpdPending, "Did not expect server %s to have queued updates",
*server.HostName)
+ }
+ }
+ }
+ }
+}