You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2018/07/11 01:19:51 UTC

[trafficcontrol] 04/13: successfully created the staticdnsentry API tests

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

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

commit 9aba036b33277346c69bac09c633227f6a82049d
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Wed May 30 12:40:27 2018 -0600

    successfully created the staticdnsentry API tests
    
    hooked in the lookup by XMLID
---
 .../v13/{staticdnsentry.go => staticdnsentries.go} |   5 +
 traffic_ops/client/v13/deliveryservice.go          |  10 ++
 .../client/v13/deliveryservice_endpoints.go        |   4 +
 traffic_ops/client/v13/staticdnsentry.go           | 133 +++++++++++++++++++++
 .../testing/api/v13/staticdnsentries_test.go       | 118 ++++++++++++++++++
 traffic_ops/testing/api/v13/tc-fixtures.json       |  11 +-
 traffic_ops/testing/api/v13/traffic_control.go     |   1 +
 7 files changed, 278 insertions(+), 4 deletions(-)

diff --git a/lib/go-tc/v13/staticdnsentry.go b/lib/go-tc/v13/staticdnsentries.go
similarity index 96%
rename from lib/go-tc/v13/staticdnsentry.go
rename to lib/go-tc/v13/staticdnsentries.go
index 0baa9bd..14941f4 100644
--- a/lib/go-tc/v13/staticdnsentry.go
+++ b/lib/go-tc/v13/staticdnsentries.go
@@ -21,6 +21,11 @@ import tc "github.com/apache/trafficcontrol/lib/go-tc"
  * under the License.
  */
 
+// StaticDNSEntries ...
+type StaticDNSEntriesResponse struct {
+	Response []StaticDNSEntry `json:"response"`
+}
+
 // StatisDNSEntry ...
 type StaticDNSEntry struct {
 
diff --git a/traffic_ops/client/v13/deliveryservice.go b/traffic_ops/client/v13/deliveryservice.go
index 0ad4599..c5a5448 100644
--- a/traffic_ops/client/v13/deliveryservice.go
+++ b/traffic_ops/client/v13/deliveryservice.go
@@ -56,6 +56,16 @@ func (to *Session) GetDeliveryServicesByServer(id int) ([]tc.DeliveryServiceV13,
 	return data.Response, reqInf, nil
 }
 
+func (to *Session) GetDeliveryServiceByXMLID(XMLID string) ([]tc.DeliveryService, ReqInf, error) {
+	var data tc.GetDeliveryServiceResponse
+	reqInf, err := get(to, deliveryServicesByXMLID(XMLID), &data)
+	if err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
 // DeliveryService gets the DeliveryService for the ID it's passed
 // Deprecated: use GetDeliveryService
 func (to *Session) DeliveryService(id string) (*tc.DeliveryServiceV13, error) {
diff --git a/traffic_ops/client/v13/deliveryservice_endpoints.go b/traffic_ops/client/v13/deliveryservice_endpoints.go
index c0251cd..ada7517 100644
--- a/traffic_ops/client/v13/deliveryservice_endpoints.go
+++ b/traffic_ops/client/v13/deliveryservice_endpoints.go
@@ -64,3 +64,7 @@ func deliveryServiceSSLKeysByIDEp(id string) string {
 func deliveryServiceSSLKeysByHostnameEp(hostname string) string {
 	return apiBase + dsPath + "/hostname/" + hostname + "/sslkeys.json"
 }
+
+func deliveryServicesByXMLID(XMLID string) string {
+	return apiBase + dsPath + "?xmlId=" + XMLID
+}
diff --git a/traffic_ops/client/v13/staticdnsentry.go b/traffic_ops/client/v13/staticdnsentry.go
new file mode 100644
index 0000000..45eca07
--- /dev/null
+++ b/traffic_ops/client/v13/staticdnsentry.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_StaticDNSEntries = "/api/1.3/staticdnsentries"
+)
+
+// Create a StaticDNSEntry
+func (to *Session) CreateStaticDNSEntry(cdn v13.StaticDNSEntry) (tc.Alerts, ReqInf, error) {
+
+	var remoteAddr net.Addr
+	reqBody, err := json.Marshal(cdn)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return tc.Alerts{}, reqInf, err
+	}
+	resp, remoteAddr, err := to.request(http.MethodPost, API_v13_StaticDNSEntries, 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 StaticDNSEntry by ID
+func (to *Session) UpdateStaticDNSEntryByID(id int, cdn v13.StaticDNSEntry) (tc.Alerts, ReqInf, error) {
+
+	var remoteAddr net.Addr
+	reqBody, err := json.Marshal(cdn)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return tc.Alerts{}, reqInf, err
+	}
+	route := fmt.Sprintf("%s/%d", API_v13_StaticDNSEntries, 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 StaticDNSEntrys
+func (to *Session) GetStaticDNSEntries() ([]v13.StaticDNSEntry, ReqInf, error) {
+	resp, remoteAddr, err := to.request(http.MethodGet, API_v13_StaticDNSEntries, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return nil, reqInf, err
+	}
+	defer resp.Body.Close()
+
+	var data v13.StaticDNSEntriesResponse
+	err = json.NewDecoder(resp.Body).Decode(&data)
+	return data.Response, reqInf, nil
+}
+
+// GET a StaticDNSEntry by the StaticDNSEntry ID
+func (to *Session) GetStaticDNSEntryByID(id int) ([]v13.StaticDNSEntry, ReqInf, error) {
+	route := fmt.Sprintf("%s/%d", API_v13_StaticDNSEntries, 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.StaticDNSEntriesResponse
+	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
+// GET a StaticDNSEntry by the StaticDNSEntry hsot
+func (to *Session) GetStaticDNSEntriesByHost(host string) ([]v13.StaticDNSEntry, ReqInf, error) {
+	url := fmt.Sprintf("%s?host=%s", API_v13_StaticDNSEntries, host)
+	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.StaticDNSEntriesResponse
+	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
+// DELETE a StaticDNSEntry by ID
+func (to *Session) DeleteStaticDNSEntryByID(id int) (tc.Alerts, ReqInf, error) {
+	route := fmt.Sprintf("%s/%d", API_v13_StaticDNSEntries, 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/staticdnsentries_test.go b/traffic_ops/testing/api/v13/staticdnsentries_test.go
new file mode 100644
index 0000000..cd669b0
--- /dev/null
+++ b/traffic_ops/testing/api/v13/staticdnsentries_test.go
@@ -0,0 +1,118 @@
+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 TestStaticDNSEntries(t *testing.T) {
+
+	CreateTestStaticDNSEntries(t)
+	UpdateTestStaticDNSEntries(t)
+	GetTestStaticDNSEntries(t)
+	DeleteTestStaticDNSEntries(t)
+
+}
+
+func CreateTestStaticDNSEntries(t *testing.T) {
+
+	for _, staticDNSEntry := range testData.StaticDNSEntries {
+
+		// GET DeliveryService by Name
+		//respStatuses, _, err := TOSession.GetStatusByName("ONLINE")
+		//if err != nil {
+		//t.Errorf("cannot GET Status by name: ONLINE - %v\n", err)
+		//}
+		//respStatus := respStatuses[0]
+		resp, _, err := TOSession.CreateStaticDNSEntry(staticDNSEntry)
+		log.Debugln("Response: ", resp)
+		if err != nil {
+			t.Errorf("could not CREATE staticDNSEntry: %v\n", err)
+		}
+	}
+
+}
+
+func UpdateTestStaticDNSEntries(t *testing.T) {
+
+	firstStaticDNSEntrie := testData.StaticDNSEntries[0]
+	// Retrieve the StaticDNSEntrie by name so we can get the id for the Update
+	resp, _, err := TOSession.GetStaticDNSEntriesByHost(firstStaticDNSEntrie.Host)
+	if err != nil {
+		t.Errorf("cannot GET StaticDNSEntries by name: '%s', %v\n", firstStaticDNSEntrie.Host, err)
+	}
+	remoteStaticDNSEntry := resp[0]
+	expectedAddress := "address99"
+	remoteStaticDNSEntry.Address = expectedAddress
+	var alert tc.Alerts
+	alert, _, err = TOSession.UpdateStaticDNSEntryByID(remoteStaticDNSEntry.ID, remoteStaticDNSEntry)
+	if err != nil {
+		t.Errorf("cannot UPDATE StaticDNSEntrie by id: %v - %v\n", err, alert)
+	}
+
+	// Retrieve the StaticDNSEntrie to check StaticDNSEntrie name got updated
+	resp, _, err = TOSession.GetStaticDNSEntryByID(remoteStaticDNSEntry.ID)
+	if err != nil {
+		t.Errorf("cannot GET StaticDNSEntries by name: '$%s', %v\n", firstStaticDNSEntrie.Host, err)
+	}
+	respStaticDNSEntry := resp[0]
+	if respStaticDNSEntry.Address != expectedAddress {
+		t.Errorf("results do not match actual: %s, expected: %s\n", respStaticDNSEntry.Address, expectedAddress)
+	}
+
+}
+
+func GetTestStaticDNSEntries(t *testing.T) {
+
+	for _, staticDNSEntry := range testData.StaticDNSEntries {
+		resp, _, err := TOSession.GetStaticDNSEntriesByHost(staticDNSEntry.Host)
+		if err != nil {
+			t.Errorf("cannot GET StaticDNSEntries by name: %v - %v\n", err, resp)
+		}
+	}
+}
+
+func DeleteTestStaticDNSEntries(t *testing.T) {
+
+	for _, staticDNSEntry := range testData.StaticDNSEntries {
+		// Retrieve the StaticDNSEntrie by name so we can get the id for the Update
+		resp, _, err := TOSession.GetStaticDNSEntriesByHost(staticDNSEntry.Host)
+		if err != nil {
+			t.Errorf("cannot GET StaticDNSEntries by name: %v - %v\n", staticDNSEntry.Host, err)
+		}
+		if len(resp) > 0 {
+			respStaticDNSEntrie := resp[0]
+
+			_, _, err := TOSession.DeleteStaticDNSEntryByID(respStaticDNSEntrie.ID)
+			if err != nil {
+				t.Errorf("cannot DELETE StaticDNSEntrie by name: '%s' %v\n", respStaticDNSEntrie.Host, err)
+			}
+
+			// Retrieve the StaticDNSEntrie to see if it got deleted
+			staticDNSEntries, _, err := TOSession.GetStaticDNSEntriesByHost(staticDNSEntry.Host)
+			if err != nil {
+				t.Errorf("error deleting StaticDNSEntrie name: %s\n", err.Error())
+			}
+			if len(staticDNSEntries) > 0 {
+				t.Errorf("expected StaticDNSEntry name: %s to be deleted\n", staticDNSEntry.Host)
+			}
+		}
+	}
+}
diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json
index 4948293..d0799d0 100644
--- a/traffic_ops/testing/api/v13/tc-fixtures.json
+++ b/traffic_ops/testing/api/v13/tc-fixtures.json
@@ -1018,19 +1018,22 @@
         {
             "host": "host1",
             "address": "address1",
-            "typeName": "AAAA_RECORD",
+            "dsname": "ds1",
+            "typename": "AAAA_RECORD",
             "ttl": 10 
         },
         {
             "host": "host2",
             "address": "address2",
-            "typeName": "A_RECORD",
+            "dsname": "ds2",
+            "typename": "A_RECORD",
             "ttl": 10 
         },
         {
             "host": "host3",
-            "name": "127.0.0.1",
-            "typeName": "CNAME_RECORD",
+            "dsName": "ds3",
+            "address": "127.0.0.1",
+            "typename": "CNAME_RECORD",
             "ttl": 10 
         }
     ],
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index 98fcdfc..80e40ab 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -39,6 +39,7 @@ type TrafficControl struct {
 	Roles                          []v13.Role                          `json:"roles"`
 	Servers                        []v13.Server                        `json:"servers"`
 	Statuses                       []v12.Status                        `json:"statuses"`
+	StaticDNSEntries               []v13.StaticDNSEntry                `json:"staticdnsentries"`
 	Tenants                        []v12.Tenant                        `json:"tenants"`
 	Types                          []v12.Type                          `json:"types"`
 }