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/03 20:06:09 UTC
[incubator-trafficcontrol] 02/04: Implement Location support in the
client, 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 a613a3564c465ab6899e1682419f481a624e09a2
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Mon Apr 9 17:21:56 2018 -0600
Implement Location support in the client, integration tests
---
traffic_ops/client/v13/location.go | 133 +++++++++++++++++++++++++
traffic_ops/testing/api/v13/locations_test.go | 129 ++++++++++++++++++++++++
traffic_ops/testing/api/v13/tc-fixtures.json | 12 +++
traffic_ops/testing/api/v13/traffic_control.go | 1 +
4 files changed, 275 insertions(+)
diff --git a/traffic_ops/client/v13/location.go b/traffic_ops/client/v13/location.go
new file mode 100644
index 0000000..b54bade
--- /dev/null
+++ b/traffic_ops/client/v13/location.go
@@ -0,0 +1,133 @@
+/*
+
+ 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 v13
+
+import (
+ "encoding/json"
+ "fmt"
+ "net"
+ "net/http"
+
+ "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+ "github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
+)
+
+const (
+ API_v13_Locations = "/api/1.3/locations"
+)
+
+// Create a Location
+func (to *Session) CreateLocation(location v13.Location) (tc.Alerts, ReqInf, error) {
+
+ var remoteAddr net.Addr
+ reqBody, err := json.Marshal(location)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return tc.Alerts{}, reqInf, err
+ }
+ resp, remoteAddr, err := to.request(http.MethodPost, API_v13_Locations, reqBody)
+ if err != nil {
+ return tc.Alerts{}, reqInf, err
+ }
+ defer resp.Body.Close()
+ var alerts tc.Alerts
+ err = json.NewDecoder(resp.Body).Decode(&alerts)
+ return alerts, reqInf, nil
+}
+
+// Update a Location by ID
+func (to *Session) UpdateLocationByID(id int, location v13.Location) (tc.Alerts, ReqInf, error) {
+
+ var remoteAddr net.Addr
+ reqBody, err := json.Marshal(location)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return tc.Alerts{}, reqInf, err
+ }
+ route := fmt.Sprintf("%s?id=%d", API_v13_Locations, id)
+ resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody)
+ if err != nil {
+ return tc.Alerts{}, reqInf, err
+ }
+ defer resp.Body.Close()
+ var alerts tc.Alerts
+ err = json.NewDecoder(resp.Body).Decode(&alerts)
+ return alerts, reqInf, nil
+}
+
+// Returns a list of Locations
+func (to *Session) GetLocations() ([]v13.Location, ReqInf, error) {
+ resp, remoteAddr, err := to.request(http.MethodGet, API_v13_Locations, nil)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return nil, reqInf, err
+ }
+ defer resp.Body.Close()
+
+ var data v13.LocationsResponse
+ err = json.NewDecoder(resp.Body).Decode(&data)
+ return data.Response, reqInf, nil
+}
+
+// GET a Location by the Location id
+func (to *Session) GetLocationByID(id int) ([]v13.Location, ReqInf, error) {
+ route := fmt.Sprintf("%s?id=%d", API_v13_Locations, id)
+ resp, remoteAddr, err := to.request(http.MethodGet, route, nil)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return nil, reqInf, err
+ }
+ defer resp.Body.Close()
+
+ var data v13.LocationsResponse
+ if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+ return nil, reqInf, err
+ }
+
+ return data.Response, reqInf, nil
+}
+
+// GET a Location by the Location name
+func (to *Session) GetLocationByName(name string) ([]v13.Location, ReqInf, error) {
+ url := fmt.Sprintf("%s?name=%s", API_v13_Locations, name)
+ resp, remoteAddr, err := to.request(http.MethodGet, url, nil)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return nil, reqInf, err
+ }
+ defer resp.Body.Close()
+
+ var data v13.LocationsResponse
+ if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+ return nil, reqInf, err
+ }
+
+ return data.Response, reqInf, nil
+}
+
+// DELETE a Location by ID
+func (to *Session) DeleteLocationByID(id int) (tc.Alerts, ReqInf, error) {
+ route := fmt.Sprintf("%s?id=%d", API_v13_Locations, id)
+ resp, remoteAddr, err := to.request(http.MethodDelete, route, nil)
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if err != nil {
+ return tc.Alerts{}, reqInf, err
+ }
+ defer resp.Body.Close()
+ var alerts tc.Alerts
+ err = json.NewDecoder(resp.Body).Decode(&alerts)
+ return alerts, reqInf, nil
+}
diff --git a/traffic_ops/testing/api/v13/locations_test.go b/traffic_ops/testing/api/v13/locations_test.go
new file mode 100644
index 0000000..669e6ce
--- /dev/null
+++ b/traffic_ops/testing/api/v13/locations_test.go
@@ -0,0 +1,129 @@
+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"
+ tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+)
+
+func TestLocations(t *testing.T) {
+ CreateTestLocations(t)
+ GetTestLocations(t)
+ UpdateTestLocations(t)
+ DeleteTestLocations(t)
+}
+
+func CreateTestLocations(t *testing.T) {
+ failed := false
+
+ for _, loc := range testData.Locations {
+
+ _, _, err := TOSession.CreateLocation(loc)
+ if err != nil {
+ t.Errorf("could not CREATE locations: %v\n", err)
+ failed = true
+ }
+ }
+ if !failed {
+ log.Debugln("CreateTestLocations() PASSED: ")
+ }
+}
+
+func GetTestLocations(t *testing.T) {
+ failed := false
+ for _, loc := range testData.Locations {
+ resp, _, err := TOSession.GetLocationByName(loc.Name)
+ if err != nil {
+ t.Errorf("cannot GET Location: %v - %v\n", err, resp)
+ failed = true
+ }
+ }
+ if !failed {
+ log.Debugln("GetTestLocations() PASSED: ")
+ }
+}
+
+func UpdateTestLocations(t *testing.T) {
+ failed := false
+ firstLoc := testData.Locations[0]
+ resp, _, err := TOSession.GetLocationByName(firstLoc.Name)
+ if err != nil {
+ t.Errorf("cannot GET Location by name: %v - %v\n", firstLoc.Name, err)
+ failed = true
+ }
+ loc := resp[0]
+ expectedName := "blah"
+ loc.Name = expectedName
+
+ var alert tc.Alerts
+ alert, _, err = TOSession.UpdateLocationByID(loc.ID, loc)
+ if err != nil {
+ t.Errorf("cannot UPDATE Location by id: %v - %v\n", err, alert)
+ failed = true
+ }
+
+ // Retrieve the Location to check Location name got updated
+ resp, _, err = TOSession.GetLocationByID(loc.ID)
+ if err != nil {
+ t.Errorf("cannot GET Location by name: '$%s', %v\n", firstLoc.Name, err)
+ failed = true
+ }
+ loc = resp[0]
+ if loc.Name != expectedName {
+ t.Errorf("results do not match actual: %s, expected: %s\n", loc.Name, expectedName)
+ }
+ if !failed {
+ log.Debugln("UpdateTestLocations() PASSED: ")
+ }
+}
+
+func DeleteTestLocations(t *testing.T) {
+ failed := false
+
+ for _, loc := range testData.Locations {
+ // Retrieve the Location by name so we can get the id for the Update
+ resp, _, err := TOSession.GetLocationByName(loc.Name)
+ if err != nil {
+ t.Errorf("cannot GET Location by name: %v - %v\n", loc.Name, err)
+ failed = true
+ }
+ if len(resp) > 0 {
+ respLoc := resp[0]
+ _, _, err := TOSession.DeleteLocationByID(respLoc.ID)
+ if err != nil {
+ t.Errorf("cannot DELETE Location by name: '%s' %v\n", respLoc.Name, err)
+ failed = true
+ }
+ // Retrieve the Location to see if it got deleted
+ locs, _, err := TOSession.GetLocationByName(loc.Name)
+ if err != nil {
+ t.Errorf("error deleting Location name: %s\n", err.Error())
+ failed = true
+ }
+ if len(locs) > 0 {
+ t.Errorf("expected Location name: %s to be deleted\n", loc.Name)
+ failed = true
+ }
+ }
+ }
+
+ if !failed {
+ log.Debugln("DeleteTestLocations() PASSED: ")
+ }
+}
diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json
index 290dcc1..c4fbbd7 100644
--- a/traffic_ops/testing/api/v13/tc-fixtures.json
+++ b/traffic_ops/testing/api/v13/tc-fixtures.json
@@ -183,6 +183,18 @@
"name": "division2"
}
],
+ "locations": [
+ {
+ "latitude": 1.1,
+ "longitude": 2.2,
+ "name": "location1"
+ },
+ {
+ "latitude": 3.3,
+ "longitude": 4.4,
+ "name": "location2"
+ }
+ ],
"parameters": [
{
"configFile": "rascal.properties",
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index 015089e..0c50a1c 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -29,6 +29,7 @@ type TrafficControl struct {
DeliveryServiceRequestComments []v12.DeliveryServiceRequestComment `json:"deliveryServiceRequestComments"`
DeliveryServices []v12.DeliveryService `json:"deliveryservices"`
Divisions []v12.Division `json:"divisions"`
+ Locations []v13.Location `json:"locations"`
Profiles []v13.Profile `json:"profiles"`
Parameters []v12.Parameter `json:"parameters"`
ProfileParameters []v13.ProfileParameter `json:"profileParameters"`
--
To stop receiving notification emails like this one, please contact
dewrich@apache.org.