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/05/02 15:23:59 UTC

[incubator-trafficcontrol] branch master updated: Fix TO Go CRConfig endpoints to wrap response obj

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/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new d750f12  Fix TO Go CRConfig endpoints to wrap response obj
d750f12 is described below

commit d750f126ab62e37c4d6d29f7d7c919cafa6796b8
Author: Robert Butts <ro...@apache.org>
AuthorDate: Tue May 1 16:51:42 2018 -0600

    Fix TO Go CRConfig endpoints to wrap response obj
    
    Fixes #2210
---
 .../crconfig/deliveryservice_test.go               | 73 +++++++++++-----------
 traffic_ops/traffic_ops_golang/crconfig/handler.go | 10 +--
 .../traffic_ops_golang/crconfig/snapshot_test.go   |  7 ++-
 .../traffic_ops_golang/crconfig/stats_test.go      | 14 ++---
 4 files changed, 53 insertions(+), 51 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
index c7d7057..fe066b4 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
@@ -20,12 +20,12 @@ package crconfig
  */
 
 import (
-	"strconv"
-	"strings"
+	"encoding/json"
 	"math/rand"
 	"reflect"
+	"strconv"
+	"strings"
 	"testing"
-	"encoding/json"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc"
 
@@ -46,7 +46,7 @@ func randMatchlistArr() []tc.MatchList {
 	arr := []tc.MatchList{}
 	for i := 0; i < num; i++ {
 		arr = append(arr, tc.MatchList{
-			Regex: *randStr(),
+			Regex:     *randStr(),
 			MatchType: *randStr(),
 		})
 	}
@@ -59,7 +59,7 @@ func randMatchsetArr() []*tc.MatchSet {
 	arr := []*tc.MatchSet{}
 	for i := 0; i < num; i++ {
 		arr = append(arr, &tc.MatchSet{
-			Protocol: httpStr,
+			Protocol:  httpStr,
 			MatchList: randMatchlistArr(),
 		})
 	}
@@ -82,44 +82,44 @@ func randDS() tc.CRConfigDeliveryService {
 	geoProviderStr := GeoProviderMaxmindStr
 	return tc.CRConfigDeliveryService{
 		CoverageZoneOnly: false,
-		Dispersion:           &tc.CRConfigDispersion{
-			Limit: 42,
+		Dispersion: &tc.CRConfigDispersion{
+			Limit:    42,
 			Shuffled: true,
 		},
 		// Domains: []string{"foo"},
 		GeoLocationProvider: &geoProviderStr,
 		// MatchSets:            randMatchsetArr(),
-		MissLocation:         &tc.CRConfigLatitudeLongitudeShort{
+		MissLocation: &tc.CRConfigLatitudeLongitudeShort{
 			Lat: *randFloat64(),
 			Lon: *randFloat64(),
 		},
-		Protocol:             &tc.CRConfigDeliveryServiceProtocol{
+		Protocol: &tc.CRConfigDeliveryServiceProtocol{
 			// AcceptHTTP: &truePtr,
-			AcceptHTTPS: false,
+			AcceptHTTPS:     false,
 			RedirectOnHTTPS: false,
 		},
 		RegionalGeoBlocking: &falseStrPtr,
-		ResponseHeaders: nil,
-		RequestHeaders: nil,
+		ResponseHeaders:     nil,
+		RequestHeaders:      nil,
 		Soa: &tc.SOA{
-			Admin:     &ttlAdmin,
-			ExpireSeconds:     &ttlExpire,
-			MinimumSeconds:     &ttlMinimum,
+			Admin:          &ttlAdmin,
+			ExpireSeconds:  &ttlExpire,
+			MinimumSeconds: &ttlMinimum,
 			RefreshSeconds: &ttlRefresh,
-			RetrySeconds: &ttlRetry,
+			RetrySeconds:   &ttlRetry,
 		},
 		SSLEnabled: false,
-		TTL: ttl,
-		TTLs: &tc.CRConfigTTL {
-			ASeconds: &ttlStr,
-			AAAASeconds : &ttlStr,
-			NSSeconds: &ttlNS,
-			SOASeconds: &ttlSOA,
+		TTL:        ttl,
+		TTLs: &tc.CRConfigTTL{
+			ASeconds:    &ttlStr,
+			AAAASeconds: &ttlStr,
+			NSSeconds:   &ttlNS,
+			SOASeconds:  &ttlSOA,
 		},
 		// MaxDNSIPsForLocation: randInt(),
 		IP6RoutingEnabled: randBool(),
-		RoutingName: randStr(),
-		BypassDestination:    map[string]*tc.CRConfigBypassDestination {
+		RoutingName:       randStr(),
+		BypassDestination: map[string]*tc.CRConfigBypassDestination{
 			"HTTP": &tc.CRConfigBypassDestination{
 				// IP: randStr(),
 				// IP6: randStr(),
@@ -130,13 +130,13 @@ func randDS() tc.CRConfigDeliveryService {
 			},
 		},
 		DeepCachingType: nil,
-		GeoEnabled: nil,
+		GeoEnabled:      nil,
 		// GeoLimitRedirectURL: randStr(),
-		StaticDNSEntries:     []tc.StaticDNSEntry{
+		StaticDNSEntries: []tc.StaticDNSEntry{
 			tc.StaticDNSEntry{
-				Name: *randStr(),
-				TTL: *randInt(),
-				Type: *randStr(),
+				Name:  *randStr(),
+				TTL:   *randInt(),
+				Type:  *randStr(),
 				Value: *randStr(),
 			},
 		},
@@ -212,7 +212,7 @@ func TestMakeDSes(t *testing.T) {
 func ExpectedGetServerProfileParams(expectedMakeDSes map[string]tc.CRConfigDeliveryService) map[string]map[string]string {
 	expected := map[string]map[string]string{}
 	for dsName, _ := range expectedMakeDSes {
-		expected[dsName] = map[string]string {
+		expected[dsName] = map[string]string{
 			"param0": "val0",
 			"param1": "val1",
 		}
@@ -278,7 +278,7 @@ func ExpectedGetDSRegexesDomains(expectedDSParams map[string]string) (map[string
 	}
 
 	for dsName, _ := range expectedDSParams {
-		pattern := `.*\.`+dsName+`\..*`
+		pattern := `.*\.` + dsName + `\..*`
 
 		matchsets[dsName][setnum] = &tc.MatchSet{}
 		matchset := matchsets[dsName][setnum]
@@ -311,7 +311,6 @@ func TestGetDSRegexesDomains(t *testing.T) {
 
 	cdn := "mycdn"
 
-
 	expectedMakeDSes := ExpectedMakeDSes()
 	expectedServerProfileParams := ExpectedGetServerProfileParams(expectedMakeDSes)
 	expectedDSParams, err := getDSParams(expectedServerProfileParams)
@@ -334,14 +333,14 @@ func TestGetDSRegexesDomains(t *testing.T) {
 	}
 
 	if !reflect.DeepEqual(expectedMatchsets, actualMatchsets) {
-			t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expectedMatchsets, actualMatchsets)
+		t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expectedMatchsets, actualMatchsets)
 	}
 	if !reflect.DeepEqual(expectedDomains, actualDomains) {
-			t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expectedDomains, actualDomains)
+		t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expectedDomains, actualDomains)
 	}
 }
 
-func ExpectedGetStaticDNSEntries(expectedMakeDSes map[string]tc.CRConfigDeliveryService) (map[tc.DeliveryServiceName][]tc.StaticDNSEntry) {
+func ExpectedGetStaticDNSEntries(expectedMakeDSes map[string]tc.CRConfigDeliveryService) map[tc.DeliveryServiceName][]tc.StaticDNSEntry {
 	expected := map[tc.DeliveryServiceName][]tc.StaticDNSEntry{}
 	for dsName, ds := range expectedMakeDSes {
 		for _, entry := range ds.StaticDNSEntries {
@@ -355,7 +354,7 @@ func MockGetStaticDNSEntries(mock sqlmock.Sqlmock, expected map[tc.DeliveryServi
 	rows := sqlmock.NewRows([]string{"ds", "name", "ttl", "value", "type"})
 	for dsName, entries := range expected {
 		for _, entry := range entries {
-			rows = rows.AddRow(dsName, entry.Name, entry.TTL, entry.Value, entry.Type + "_RECORD")
+			rows = rows.AddRow(dsName, entry.Name, entry.TTL, entry.Value, entry.Type+"_RECORD")
 		}
 	}
 	mock.ExpectQuery("select").WithArgs(cdn).WillReturnRows(rows)
@@ -383,6 +382,6 @@ func TestGetStaticDNSEntries(t *testing.T) {
 		t.Fatalf("getStaticDNSEntries len expected: %v, actual: %v", len(expected), len(actual))
 	}
 	if !reflect.DeepEqual(expected, actual) {
-			t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expected, actual)
+		t.Errorf("getDSRegexesDomains expected: %+v, actual: %+v", expected, actual)
 	}
 }
diff --git a/traffic_ops/traffic_ops_golang/crconfig/handler.go b/traffic_ops/traffic_ops_golang/crconfig/handler.go
index 78b1fd1..3c11d5e 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/handler.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/handler.go
@@ -22,7 +22,6 @@ package crconfig
 import (
 	"encoding/json"
 	"errors"
-	"fmt"
 	"net/http"
 	"net/url"
 	"strconv"
@@ -71,14 +70,17 @@ func Handler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
 			return
 		}
 
-		respBts, err := json.Marshal(crConfig)
+		resp := struct {
+			Response *tc.CRConfig `json:"response"`
+		}{crConfig}
+		respBts, err := json.Marshal(resp)
 		if err != nil {
 			handleErrs(http.StatusInternalServerError, err)
 			return
 		}
 		log.Infof("CRConfig time to generate: %+v\n", time.Since(start))
 		w.Header().Set("Content-Type", "application/json")
-		fmt.Fprintf(w, "%s", respBts)
+		w.Write(respBts)
 	}
 }
 
@@ -107,7 +109,7 @@ func SnapshotGetHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
 			return
 		}
 		w.Header().Set("Content-Type", "application/json")
-		fmt.Fprintf(w, "%s", []byte(snapshot))
+		w.Write([]byte(`{"response":` + snapshot + `}`))
 	}
 }
 
diff --git a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
index dc8cd22..e5732a1 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/snapshot_test.go
@@ -20,10 +20,10 @@ package crconfig
  */
 
 import (
-	"reflect"
+	"database/sql/driver"
 	"encoding/json"
+	"reflect"
 	"testing"
-	"database/sql/driver"
 	"time"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc"
@@ -72,6 +72,7 @@ func TestGetSnapshot(t *testing.T) {
 }
 
 type AnyTime struct{}
+
 // Match satisfies sqlmock.Argument interface
 func (a AnyTime) Match(v driver.Value) bool {
 	_, ok := v.(time.Time)
@@ -79,12 +80,12 @@ func (a AnyTime) Match(v driver.Value) bool {
 }
 
 type Any struct{}
+
 // Match satisfies sqlmock.Argument interface
 func (a Any) Match(v driver.Value) bool {
 	return true
 }
 
-
 func MockSnapshot(mock sqlmock.Sqlmock, expected []byte, cdn string) {
 	mock.ExpectExec("insert").WithArgs(cdn, expected, AnyTime{}).WillReturnResult(sqlmock.NewResult(1, 1))
 }
diff --git a/traffic_ops/traffic_ops_golang/crconfig/stats_test.go b/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
index 166df4e..77b9f1f 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/stats_test.go
@@ -21,19 +21,19 @@ package crconfig
 
 import (
 	"reflect"
-	"time"
 	"testing"
+	"time"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc"
 )
 
 func ExpectedMakeStats() tc.CRConfigStats {
 	return tc.CRConfigStats{
-		CDNName:         randStr(),
-		TMHost:          randStr(),
-		TMPath:          randStr(),
-		TMUser:          randStr(),
-		TMVersion:       randStr(),
+		CDNName:   randStr(),
+		TMHost:    randStr(),
+		TMPath:    randStr(),
+		TMUser:    randStr(),
+		TMVersion: randStr(),
 	}
 }
 
@@ -46,7 +46,7 @@ func TestMakeStats(t *testing.T) {
 	if !reflect.DeepEqual(expected, actual) {
 		t.Errorf("makeStats expected: %+v, actual: %+v", expected, actual)
 	}
-	if actual.DateUnixSeconds == nil ||  *actual.DateUnixSeconds < start.Unix() || *actual.DateUnixSeconds > end.Unix() {
+	if actual.DateUnixSeconds == nil || *actual.DateUnixSeconds < start.Unix() || *actual.DateUnixSeconds > end.Unix() {
 		t.Errorf("makeStats DateUniSeconds expected: < %+v > %+v, actual: %+v", start.Unix(), end.Unix(), actual.DateUnixSeconds)
 	}
 }

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