You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/05/24 15:31:26 UTC

[incubator-trafficcontrol] 04/08: Implement Origin API integration tests

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit d914dc81561184b2136b356f698fdda8d541cce7
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Mon May 7 17:54:31 2018 -0600

    Implement Origin API integration tests
---
 traffic_ops/testing/api/v13/origins_test.go      | 184 +++++++++++++++++++++++
 traffic_ops/testing/api/v13/tc-fixtures.json     |  39 +++++
 traffic_ops/testing/api/v13/traffic_control.go   |   1 +
 traffic_ops/traffic_ops_golang/origin/origins.go |  32 +++-
 4 files changed, 250 insertions(+), 6 deletions(-)

diff --git a/traffic_ops/testing/api/v13/origins_test.go b/traffic_ops/testing/api/v13/origins_test.go
new file mode 100644
index 0000000..f80ea3f
--- /dev/null
+++ b/traffic_ops/testing/api/v13/origins_test.go
@@ -0,0 +1,184 @@
+package v13
+
+/*
+
+   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 (
+	"testing"
+
+	"github.com/apache/incubator-trafficcontrol/lib/go-log"
+)
+
+func TestOrigins(t *testing.T) {
+
+	CreateTestCDNs(t)
+	defer DeleteTestCDNs(t)
+	CreateTestTypes(t)
+	defer DeleteTestTypes(t)
+	CreateTestProfiles(t)
+	defer DeleteTestProfiles(t)
+	CreateTestCacheGroups(t)
+	defer DeleteTestCacheGroups(t)
+	CreateTestCoordinates(t)
+	defer DeleteTestCoordinates(t)
+	// TODO: add deliveryservices and tenants once their API integration tests are implemented
+
+	CreateTestOrigins(t)
+	defer DeleteTestOrigins(t)
+	UpdateTestOrigins(t)
+	GetTestOrigins(t)
+
+}
+
+func CreateTestOrigins(t *testing.T) {
+	failed := false
+
+	// GET ORIGIN1 profile
+	respProfiles, _, err := TOSession.GetProfileByName("ORIGIN1")
+	if err != nil {
+		t.Errorf("cannot GET Profiles - %v\n", err)
+		failed = true
+	}
+	respProfile := respProfiles[0]
+
+	// GET originCachegroup cachegroup
+	respCacheGroups, _, err := TOSession.GetCacheGroupByName("originCachegroup")
+	if err != nil {
+		t.Errorf("cannot GET CacheGroup by name: originCachegroup - %v\n", err)
+		failed = true
+	}
+	respCacheGroup := respCacheGroups[0]
+
+	// GET coordinate1 coordinate
+	respCoordinates, _, err := TOSession.GetCoordinateByName("coordinate1")
+	if err != nil {
+		t.Errorf("cannot GET Coordinate by name: coordinate1 - %v\n", err)
+		failed = true
+	}
+	respCoordinate := respCoordinates[0]
+
+	// loop through origins, assign FKs and create
+	for _, origin := range testData.Origins {
+		origin.CachegroupID = &respCacheGroup.ID
+		origin.CoordinateID = &respCoordinate.ID
+		origin.ProfileID = &respProfile.ID
+
+		resp, _, err := TOSession.CreateOrigin(origin)
+		log.Debugln("Response: ", origin.Name, " ", resp)
+		if err != nil {
+			t.Errorf("could not CREATE origins: %v\n", err)
+			failed = true
+		}
+	}
+
+	if !failed {
+		log.Debugln("CreateTestOrigins() PASSED")
+	}
+
+}
+
+func GetTestOrigins(t *testing.T) {
+	failed := false
+
+	for _, origin := range testData.Origins {
+		resp, _, err := TOSession.GetServerByHostName(*origin.Name)
+		if err != nil {
+			t.Errorf("cannot GET Origin by name: %v - %v\n", err, resp)
+			failed = true
+		}
+	}
+
+	if !failed {
+		log.Debugln("GetTestOrigins() PASSED")
+	}
+}
+
+func UpdateTestOrigins(t *testing.T) {
+	failed := false
+
+	firstOrigin := testData.Origins[0]
+	// Retrieve the origin by name so we can get the id for the Update
+	resp, _, err := TOSession.GetOriginByName(*firstOrigin.Name)
+	if err != nil {
+		t.Errorf("cannot GET origin by name: %v - %v\n", *firstOrigin.Name, err)
+		failed = true
+	}
+	remoteOrigin := resp[0]
+	updatedPort := 4321
+	updatedFQDN := "updated.example.com"
+
+	// update port and FQDN values on origin
+	remoteOrigin.Port = &updatedPort
+	remoteOrigin.FQDN = &updatedFQDN
+	updResp, _, err := TOSession.UpdateOriginByID(*remoteOrigin.ID, remoteOrigin)
+	if err != nil {
+		t.Errorf("cannot UPDATE Origin by name: %v - %v\n", err, updResp.Alerts)
+		failed = true
+	}
+
+	// Retrieve the origin to check port and FQDN values were updated
+	resp, _, err = TOSession.GetOriginByID(*remoteOrigin.ID)
+	if err != nil {
+		t.Errorf("cannot GET Origin by ID: %v - %v\n", *remoteOrigin.Name, err)
+		failed = true
+	}
+
+	respOrigin := resp[0]
+	if *respOrigin.Port != updatedPort || *respOrigin.FQDN != updatedFQDN {
+		t.Errorf("results do not match actual: %d, expected: %d\n", *respOrigin.Port, updatedPort)
+		t.Errorf("results do not match actual: %s, expected: %s\n", *respOrigin.FQDN, updatedFQDN)
+		failed = true
+	}
+
+	if !failed {
+		log.Debugln("UpdateTestOrigins() PASSED")
+	}
+}
+
+func DeleteTestOrigins(t *testing.T) {
+	failed := false
+
+	for _, origin := range testData.Origins {
+		resp, _, err := TOSession.GetOriginByName(*origin.Name)
+		if err != nil {
+			t.Errorf("cannot GET Origin by name: %v - %v\n", *origin.Name, err)
+			failed = true
+		}
+		if len(resp) > 0 {
+			respOrigin := resp[0]
+
+			delResp, _, err := TOSession.DeleteOriginByID(*respOrigin.ID)
+			if err != nil {
+				t.Errorf("cannot DELETE Origin by ID: %v - %v\n", err, delResp)
+				failed = true
+			}
+
+			// Retrieve the Origin to see if it got deleted
+			org, _, err := TOSession.GetOriginByName(*origin.Name)
+			if err != nil {
+				t.Errorf("error deleting Origin name: %s\n", err.Error())
+				failed = true
+			}
+			if len(org) > 0 {
+				t.Errorf("expected Origin name: %s to be deleted\n", *origin.Name)
+				failed = true
+			}
+		}
+	}
+
+	if !failed {
+		log.Debugln("DeleteTestOrigins() PASSED")
+	}
+}
diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json
index 57ce71b..c4d97ba 100644
--- a/traffic_ops/testing/api/v13/tc-fixtures.json
+++ b/traffic_ops/testing/api/v13/tc-fixtures.json
@@ -13,6 +13,13 @@
         {
             "latitude": 0,
             "longitude": 0,
+            "name": "originCachegroup",
+            "shortName": "og1",
+            "typeName": "ORG_LOC"
+        },
+        {
+            "latitude": 0,
+            "longitude": 0,
             "name": "parentCachegroup",
             "shortName": "pg1",
             "typeName": "MID_LOC"
@@ -195,6 +202,24 @@
             "name": "coordinate2"
         }
     ],
+    "origins": [
+        {
+            "name": "origin1",
+            "fqdn": "origin1.example.com",
+            "ipAddress": "1.2.3.4",
+            "ip6Address": "dead:beef:cafe::42",
+            "port": 1234,
+            "protocol": "http"
+        },
+        {
+            "name": "origin2",
+            "fqdn": "origin2.example.com",
+            "ipAddress": "5.6.7.8",
+            "ip6Address": "cafe::42",
+            "port": 5678,
+            "protocol": "https"
+        }
+    ],
     "parameters": [
         {
             "configFile": "rascal.properties",
@@ -415,6 +440,14 @@
         },
         {
             "cdnName": "cdn1",
+            "description": "origin description",
+            "lastUpdated": "2018-03-02T17:27:11.80173+00:00",
+            "name": "ORIGIN1",
+            "routing_disabled": false,
+            "type": "ORG_PROFILE"
+        },
+        {
+            "cdnName": "cdn1",
             "description": "cdn1 description",
             "lastUpdated": "2018-03-02T17:27:11.80452+00:00",
             "name": "CCR1",
@@ -997,6 +1030,12 @@
             "useInTable": "server"
         },
         {
+            "description": "Origin Cachegroup",
+            "lastUpdated": "2018-03-02T19:13:46.816199+00:00",
+            "name": "ORG_LOC",
+            "useInTable": "cachegroup"
+        },
+        {
             "description": "Mid Cachegroup",
             "lastUpdated": "2018-03-02T19:13:46.816199+00:00",
             "name": "MID_LOC",
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index 71c1124..c2dd5b9 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -30,6 +30,7 @@ type TrafficControl struct {
 	DeliveryServices               []v12.DeliveryService               `json:"deliveryservices"`
 	Divisions                      []v12.Division                      `json:"divisions"`
 	Coordinates                    []v13.Coordinate                    `json:"coordinates"`
+	Origins                        []v13.OriginNullable                `json:"origins"`
 	Profiles                       []v13.Profile                       `json:"profiles"`
 	Parameters                     []v12.Parameter                     `json:"parameters"`
 	ProfileParameters              []v13.ProfileParameter              `json:"profileParameters"`
diff --git a/traffic_ops/traffic_ops_golang/origin/origins.go b/traffic_ops/traffic_ops_golang/origin/origins.go
index e16b021..d0b4ce3 100644
--- a/traffic_ops/traffic_ops_golang/origin/origins.go
+++ b/traffic_ops/traffic_ops_golang/origin/origins.go
@@ -264,18 +264,38 @@ LEFT JOIN tenant t ON o.tenant = t.id`
 	return selectStmt
 }
 
-func checkTenancy(tenantID *int, db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
+func checkTenancy(originTenantID, deliveryserviceID *int, db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
 	if tenant.IsTenancyEnabled(db) {
-		if tenantID == nil {
+		if originTenantID == nil {
 			return tc.NilTenantError, tc.ForbiddenError
 		}
-		authorized, err := tenant.IsResourceAuthorizedToUser(*tenantID, user, db)
+		authorized, err := tenant.IsResourceAuthorizedToUser(*originTenantID, user, db)
 		if err != nil {
-			return tc.DBError, tc.SystemError
+			return err, tc.SystemError
 		}
 		if !authorized {
 			return tc.TenantUserNotAuthError, tc.ForbiddenError
 		}
+
+		if deliveryserviceID != nil {
+			var deliveryserviceTenantID *int
+			if err := db.QueryRow(`SELECT tenant_id FROM deliveryservice where id = $1`, *deliveryserviceID).Scan(&deliveryserviceTenantID); err != nil {
+				if err == sql.ErrNoRows {
+					return errors.New("checking tenancy: requested delivery service does not exist"), tc.DataConflictError
+				}
+				log.Errorf("could not get tenant_id from deliveryservice %d: %++v\n", *deliveryserviceID, err)
+				return err, tc.SystemError
+			}
+			if deliveryserviceTenantID != nil {
+				authorized, err := tenant.IsResourceAuthorizedToUser(*deliveryserviceTenantID, user, db)
+				if err != nil {
+					return err, tc.SystemError
+				}
+				if !authorized {
+					return tc.TenantDSUserNotAuthError, tc.ForbiddenError
+				}
+			}
+		}
 	}
 	return nil, tc.NoError
 }
@@ -287,7 +307,7 @@ func checkTenancy(tenantID *int, db *sqlx.DB, user auth.CurrentUser) (error, tc.
 //generic error message returned
 func (origin *TOOrigin) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
 	// TODO: enhance tenancy framework to handle this in isTenantAuthorized()
-	err, errType := checkTenancy(origin.TenantID, db, user)
+	err, errType := checkTenancy(origin.TenantID, origin.DeliveryServiceID, db, user)
 	if err != nil {
 		return err, errType
 	}
@@ -381,7 +401,7 @@ WHERE id=:id RETURNING last_updated`
 //to be added to the struct
 func (origin *TOOrigin) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
 	// TODO: enhance tenancy framework to handle this in isTenantAuthorized()
-	err, errType := checkTenancy(origin.TenantID, db, user)
+	err, errType := checkTenancy(origin.TenantID, origin.DeliveryServiceID, db, user)
 	if err != nil {
 		return err, errType
 	}

-- 
To stop receiving notification emails like this one, please contact
dewrich@apache.org.