You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2022/03/10 17:10:13 UTC

[GitHub] [trafficcontrol] rimashah25 commented on a change in pull request #6579: Refactor cdn locks tests

rimashah25 commented on a change in pull request #6579:
URL: https://github.com/apache/trafficcontrol/pull/6579#discussion_r823956751



##########
File path: traffic_ops/testing/api/v4/cdn_locks_test.go
##########
@@ -16,470 +16,296 @@ package v4
 */
 
 import (
+	"encoding/json"
 	"net/http"
 	"net/url"
-	"strconv"
 	"testing"
-	"time"
 
 	"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"
 	client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
 )
 
 func TestCDNLocks(t *testing.T) {
-	WithObjs(t, []TCObj{Types, CacheGroups, CDNs, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, Servers, ServerCapabilities, ServerServerCapabilitiesForTopologies, Topologies, Tenants, ServiceCategories, DeliveryServices, TopologyBasedDeliveryServiceRequiredCapabilities, Roles, Users}, func() {
-		CRDCdnLocks(t)
-		AdminCdnLocks(t)
-		SnapshotWithLock(t)
-		QueueUpdatesWithLock(t)
-		QueueUpdatesFromTopologiesWithLock(t)
-	})
-}
-
-func getCDNName(t *testing.T) string {
-	cdnResp, _, err := TOSession.GetCDNs(client.RequestOptions{})
-	if err != nil {
-		t.Fatalf("couldn't get CDNs: %v", err)
-	}
-	if len(cdnResp.Response) < 1 {
-		t.Fatalf("no valid CDNs in response")
-	}
-	return cdnResp.Response[0].Name
-}
-
-func getCDNNameAndServerID(t *testing.T) (string, int) {
-	serverID := -1
-	cdnResp, _, err := TOSession.GetCDNs(client.RequestOptions{})
-	if err != nil {
-		t.Fatalf("couldn't get CDNs: %v", err)
-	}
-	if len(cdnResp.Response) < 1 {
-		t.Fatalf("no valid CDNs in response")
-	}
-	for _, cdn := range cdnResp.Response {
-		opts := client.NewRequestOptions()
-		opts.QueryParameters.Set("cdn", strconv.Itoa(cdn.ID))
-		serversResp, _, err := TOSession.GetServers(opts)
-		if err != nil {
-			t.Errorf("could not get servers for cdn %s: %v", cdn.Name, err)
-		}
-		if len(serversResp.Response) != 0 && serversResp.Response[0].ID != nil {
-			serverID = *serversResp.Response[0].ID
-			return cdn.Name, serverID
+	WithObjs(t, []TCObj{Types, CacheGroups, CDNs, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, Servers, Topologies, Tenants, Roles, Users, CDNLocks}, func() {
+
+		opsUserSession := createSession(t, "opsuser", "pa$$word")
+		opsUserWithLockSession := createSession(t, "opslockuser", "pa$$word")
+
+		methodTests := map[string]map[string]struct {
+			endpointId    func() int
+			clientSession *client.Session
+			requestOpts   client.RequestOptions
+			requestBody   map[string]interface{}
+			expectations  []utils.CkReqFunc
+		}{
+			"GET": {
+				"OK when VALID request": {
+					clientSession: TOSession, expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
+						utils.ResponseLengthGreaterOrEqual(1)),
+				},
+			},
+			"POST": {
+				"CREATED when VALID request": {
+					clientSession: TOSession,
+					requestBody: map[string]interface{}{
+						"cdn":     "cdn3",
+						"message": "snapping cdn",
+						"soft":    true,
+					},
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusCreated),
+						validateResponseFields(map[string]interface{}{"username": "admin", "cdn": "cdn3", "message": "snapping cdn", "soft": true})),
+				},
+			},
+			"DELETE": {
+				"OK when VALID request": {
+					clientSession: TOSession, requestOpts: client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn1"}}},
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+				"FORBIDDEN when NON-ADMIN USER DOESNT OWN LOCK": {
+					clientSession: opsUserSession,
+					requestOpts:   client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn4"}}},
+					expectations:  utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
+				},
+				"OK when ADMIN USER DOESNT OWN LOCK": {
+					clientSession: TOSession, requestOpts: client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn4"}}},
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+			},
+			"SNAPSHOT": {
+				"OK when USER OWNS LOCK": {
+					clientSession: opsUserWithLockSession,
+					requestOpts:   client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn2"}}},
+					expectations:  utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+				"FORBIDDEN when ADMIN USER DOESNT OWN LOCK": {
+					clientSession: TOSession, requestOpts: client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn2"}}},
+					expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
+				},
+			},
+			"SERVERS QUEUE UPDATES": {
+				"OK when USER OWNS LOCK": {
+					endpointId: getServerID(t, "cdn2-test-edge"), clientSession: opsUserWithLockSession,
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+				"FORBIDDEN when ADMIN USER DOESNT OWN LOCK": {
+					endpointId: getServerID(t, "cdn2-test-edge"), clientSession: TOSession,
+					expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
+				},
+			},
+			"TOPOLOGY QUEUE UPDATES": {
+				"OK when USER OWNS LOCK": {
+					clientSession: opsUserWithLockSession,
+					requestOpts:   client.RequestOptions{QueryParameters: url.Values{"topology": {"top-for-ds-req"}}},
+					requestBody: map[string]interface{}{
+						"action": "queue",
+						"cdnId":  getCDNID(t, "cdn2"),
+					},
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+				"FORBIDDEN when ADMIN USER DOESNT OWN LOCK": {
+					clientSession: TOSession,
+					requestOpts:   client.RequestOptions{QueryParameters: url.Values{"topology": {"top-for-ds-req"}}},
+					requestBody: map[string]interface{}{
+						"action": "queue",
+						"cdnId":  getCDNID(t, "cdn2"),
+					},
+					expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
+				},
+			},
+			"CACHE GROUP UPDATE": {
+				"OK when USER OWNS LOCK": {
+					endpointId: GetCacheGroupId(t, "cachegroup1"), clientSession: opsUserWithLockSession,
+					requestBody: map[string]interface{}{
+						"name":      "cachegroup1",
+						"shortName": "newShortName",
+						"typeName":  "EDGE_LOC",
+						"typeId":    -1,
+					},
+					expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+				},
+				"FORBIDDEN when ADMIN USER DOESNT OWN LOCK": {
+					endpointId: GetCacheGroupId(t, "cachegroup1"), clientSession: TOSession,
+					requestBody: map[string]interface{}{
+						"name":      "cachegroup1",
+						"shortName": "newShortName",
+						"typeName":  "EDGE_LOC",
+						"typeId":    -1,
+					},
+					expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
+				},
+			},
 		}
-	}
-	return "", serverID
-}
 
-func getCDNDetailsAndTopologyName(t *testing.T) (int, string, string) {
-	opts := client.NewRequestOptions()
-	topologiesResp, _, err := TOSession.GetTopologies(client.RequestOptions{})
-	if err != nil {
-		t.Fatalf("couldn't get topologies, err: %v", err)
-	}
-	if len(topologiesResp.Response) == 0 {
-		t.Fatal("no topologies returned")
-	}
-	for _, top := range topologiesResp.Response {
-		for _, node := range top.Nodes {
-			opts.QueryParameters.Set("name", node.Cachegroup)
-			cacheGroupResp, _, err := TOSession.GetCacheGroups(opts)
-			if err != nil {
-				t.Errorf("error while GETting cachegroups: %v", err)
-			}
-			if len(cacheGroupResp.Response) != 0 && cacheGroupResp.Response[0].ID != nil {
-				cacheGroupID := *cacheGroupResp.Response[0].ID
-				opts.QueryParameters.Del("name")
-				opts.QueryParameters.Set("cachegroup", strconv.Itoa(cacheGroupID))
-				serversResp, _, err := TOSession.GetServers(opts)
-				if err != nil {
-					t.Errorf("couldn't get servers: %v", err)
+		for method, testCases := range methodTests {
+			t.Run(method, func(t *testing.T) {
+				for name, testCase := range testCases {
+
+					topology := ""
+					cdnLock := tc.CDNLock{}
+					cacheGroup := tc.CacheGroupNullable{}
+					topQueueUp := tc.TopologiesQueueUpdateRequest{}
+
+					if testCase.requestOpts.QueryParameters.Has("topology") {
+						topology = testCase.requestOpts.QueryParameters.Get("topology")
+					}
+
+					if testCase.requestBody != nil {
+						if getId, ok := testCase.requestBody["cdnId"]; ok {
+							testCase.requestBody["cdnId"] = getId.(func() int)()
+							dat, err := json.Marshal(testCase.requestBody)
+							assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
+							err = json.Unmarshal(dat, &topQueueUp)
+							assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
+						} else if typeName, ok := testCase.requestBody["typeName"]; ok {
+							testCase.requestBody["typeId"] = GetTypeId(t, typeName.(string))
+							dat, err := json.Marshal(testCase.requestBody)
+							assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
+							err = json.Unmarshal(dat, &cacheGroup)
+							assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
+						} else {
+							dat, err := json.Marshal(testCase.requestBody)
+							assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
+							err = json.Unmarshal(dat, &cdnLock)
+							assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
+						}
+					}
+
+					switch method {
+					case "GET":
+						t.Run(name, func(t *testing.T) {
+							resp, reqInf, err := testCase.clientSession.GetCDNLocks(testCase.requestOpts)
+							for _, check := range testCase.expectations {

Review comment:
       In the[ old code base](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/testing/api/v4/cdn_locks_test.go#L141), we were validating fields after we called CDNLocks. Should we follow the same and call validateResponseFields() just like we do for POST case?




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org