You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ra...@apache.org on 2020/05/21 23:25:20 UTC
[trafficcontrol] branch master updated: More Topologies validation
(#4694)
This is an automated email from the ASF dual-hosted git repository.
rawlin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
The following commit(s) were added to refs/heads/master by this push:
new 53a75fd More Topologies validation (#4694)
53a75fd is described below
commit 53a75fdf586a5ed2c71f6c99ddfb0f36854a7258
Author: Zach Hoffman <za...@zrhoffman.net>
AuthorDate: Thu May 21 17:25:08 2020 -0600
More Topologies validation (#4694)
* - Update Topology TO client functions to return HTTP response status
codes
- Verify that the responses we receive for POSTing invalid topologies
are 400-level
* Only check for cycles if all cachegroups named in the topology exist
* Perform no further validation if the topology has an out-of-bounds
parent index
* Do not return status code, ReqInf will hold status code in the future
* Remove status code assertion
* Revert "Remove status code assertion"
* Return the response even if the status code is not 200
* Add StatusCode field to ReqInf
* Set ReqInf status code in Topology client lib functions
* Topology tests: Get status code from reqInf
* is -> are
* - Mention the remote address in the go doc
- Fix typo
* Assign to cachegroupIndex directly from range clause
---
traffic_ops/client/job.go | 2 +-
traffic_ops/client/session.go | 13 ++++++-----
traffic_ops/client/topology.go | 26 +++++++++++++++++-----
traffic_ops/testing/api/v3/topologies_test.go | 14 +++++++++++-
.../traffic_ops_golang/topology/topologies.go | 22 ++++++++++--------
.../traffic_ops_golang/topology/validation.go | 10 ++++++---
6 files changed, 63 insertions(+), 24 deletions(-)
diff --git a/traffic_ops/client/job.go b/traffic_ops/client/job.go
index 34fb238..a84b0f6 100644
--- a/traffic_ops/client/job.go
+++ b/traffic_ops/client/job.go
@@ -160,7 +160,7 @@ func (to *Session) GetInvalidationJobs(ds *interface{}, user *interface{}) ([]tc
}
resp, remoteAddr, err := to.request(http.MethodGet, path, nil)
- reqInf := ReqInf{CacheHitStatusMiss, remoteAddr}
+ reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
if err != nil {
return nil, reqInf, err
}
diff --git a/traffic_ops/client/session.go b/traffic_ops/client/session.go
index 9915a08..db5b4b2 100644
--- a/traffic_ops/client/session.go
+++ b/traffic_ops/client/session.go
@@ -307,7 +307,9 @@ func NewNoAuthSession(toURL string, insecure bool, userAgent string, useCache bo
}, useCache)
}
-// ErrUnlessOk returns nil and an error if the given Response's status code is anything but 200 OK. This includes reading the Response.Body and Closing it. Otherwise, the given response and error are returned unchanged.
+// ErrUnlessOk returns the response, the remote address, and an error if the given Response's status code is anything
+// but 200 OK. This includes reading the Response.Body and Closing it. Otherwise, the given response, the remote
+// address, and a nil error are returned.
func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err error, path string) (*http.Response, net.Addr, error) {
if err != nil {
return resp, remoteAddr, err
@@ -319,14 +321,14 @@ func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err err
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotImplemented {
- return nil, remoteAddr, errors.New("Traffic Ops Server returned 'Not Implemented', this client is probably newer than Traffic Ops, and you probably need to either upgrade Traffic Ops, or use a client whose version matches your Traffic Ops version.")
+ return resp, remoteAddr, errors.New("Traffic Ops Server returned 'Not Implemented', this client is probably newer than Traffic Ops, and you probably need to either upgrade Traffic Ops, or use a client whose version matches your Traffic Ops version.")
}
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
- return nil, remoteAddr, readErr
+ return resp, remoteAddr, readErr
}
- return nil, remoteAddr, errors.New(resp.Status + "[" + strconv.Itoa(resp.StatusCode) + "] - Error requesting Traffic Ops " + to.getURL(path) + " " + string(body))
+ return resp, remoteAddr, errors.New(resp.Status + "[" + strconv.Itoa(resp.StatusCode) + "] - Error requesting Traffic Ops " + to.getURL(path) + " " + string(body))
}
func (to *Session) getURL(path string) string { return to.URL + path }
@@ -385,7 +387,7 @@ func (to *Session) RawRequest(method, path string, body []byte) (*http.Response,
resp, err := to.Client.Do(req)
if err != nil {
- return nil, remoteAddr, err
+ return resp, remoteAddr, err
}
return resp, remoteAddr, nil
@@ -394,6 +396,7 @@ func (to *Session) RawRequest(method, path string, body []byte) (*http.Response,
type ReqInf struct {
CacheHitStatus CacheHitStatus
RemoteAddr net.Addr
+ StatusCode int
}
type CacheHitStatus string
diff --git a/traffic_ops/client/topology.go b/traffic_ops/client/topology.go
index bc95dff..bfddda2 100644
--- a/traffic_ops/client/topology.go
+++ b/traffic_ops/client/topology.go
@@ -18,17 +18,16 @@ package client
import (
"encoding/json"
"fmt"
- "github.com/apache/trafficcontrol/lib/go-log"
- "github.com/apache/trafficcontrol/lib/go-tc"
"net"
"net/http"
"net/url"
-)
-const (
- ApiTopologies = apiBase + "/topologies"
+ "github.com/apache/trafficcontrol/lib/go-log"
+ "github.com/apache/trafficcontrol/lib/go-tc"
)
+const ApiTopologies = apiBase + "/topologies"
+
// CreateTopology creates a topology and returns the response.
func (to *Session) CreateTopology(top tc.Topology) (*tc.TopologyResponse, ReqInf, error) {
var remoteAddr net.Addr
@@ -38,6 +37,9 @@ func (to *Session) CreateTopology(top tc.Topology) (*tc.TopologyResponse, ReqInf
return nil, reqInf, err
}
resp, remoteAddr, err := to.request(http.MethodPost, ApiTopologies, reqBody)
+ if resp != nil {
+ reqInf.StatusCode = resp.StatusCode
+ }
if err != nil {
return nil, reqInf, err
}
@@ -53,6 +55,9 @@ func (to *Session) CreateTopology(top tc.Topology) (*tc.TopologyResponse, ReqInf
func (to *Session) GetTopologies() ([]tc.Topology, ReqInf, error) {
resp, remoteAddr, err := to.request(http.MethodGet, ApiTopologies, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if resp != nil {
+ reqInf.StatusCode = resp.StatusCode
+ }
if err != nil {
return nil, reqInf, err
}
@@ -71,6 +76,9 @@ func (to *Session) GetTopology(name string) (*tc.Topology, ReqInf, error) {
reqUrl := fmt.Sprintf("%s?name=%s", ApiTopologies, url.QueryEscape(name))
resp, remoteAddr, err := to.request(http.MethodGet, reqUrl, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if resp != nil {
+ reqInf.StatusCode = resp.StatusCode
+ }
if err != nil {
return nil, reqInf, err
}
@@ -97,10 +105,14 @@ func (to *Session) UpdateTopology(name string, t tc.Topology) (*tc.TopologyRespo
}
route := fmt.Sprintf("%s?name=%s", ApiTopologies, name)
resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody)
+ if resp != nil {
+ reqInf.StatusCode = resp.StatusCode
+ }
if err != nil {
return nil, reqInf, err
}
defer log.Close(resp.Body, "unable to close response")
+
var response = new(tc.TopologyResponse)
err = json.NewDecoder(resp.Body).Decode(response)
return response, reqInf, err
@@ -111,10 +123,14 @@ func (to *Session) DeleteTopology(name string) (tc.Alerts, ReqInf, error) {
reqUrl := fmt.Sprintf("%s?name=%s", ApiTopologies, url.QueryEscape(name))
resp, remoteAddr, err := to.request(http.MethodDelete, reqUrl, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+ if resp != nil {
+ reqInf.StatusCode = resp.StatusCode
+ }
if err != nil {
return tc.Alerts{}, reqInf, err
}
defer log.Close(resp.Body, "unable to close response")
+
var alerts tc.Alerts
if err = json.NewDecoder(resp.Body).Decode(&alerts); err != nil {
return tc.Alerts{}, reqInf, err
diff --git a/traffic_ops/testing/api/v3/topologies_test.go b/traffic_ops/testing/api/v3/topologies_test.go
index c8e69bc..c81f2a9 100644
--- a/traffic_ops/testing/api/v3/topologies_test.go
+++ b/traffic_ops/testing/api/v3/topologies_test.go
@@ -88,11 +88,23 @@ func ValidationTestTopologies(t *testing.T) {
{Cachegroup: "parentCachegroup", Parents: []int{2}},
{Cachegroup: "secondaryCachegroup", Parents: []int{1}},
}}},
+ {reasonToFail: "a nonexistent cache group", Topology: tc.Topology{Name: "nonexistent-cg", Description: "Invalid because it references a cache group that does not exist", Nodes: []tc.TopologyNode{
+ {Cachegroup: "legitcachegroup", Parents: []int{0}},
+ }}},
+ {reasonToFail: "an out-of-bounds parent index", Topology: tc.Topology{Name: "oob-parent", Description: "Invalid because it contains a parent", Nodes: []tc.TopologyNode{
+ {Cachegroup: "cachegroup1", Parents: []int{7}},
+ }}},
}
+ var statusCode int
for _, testCase := range invalidTopologyTestCases {
- if _, _, err := TOSession.CreateTopology(testCase.Topology); err == nil {
+ _, reqInf, err := TOSession.CreateTopology(testCase.Topology)
+ if err == nil {
t.Fatalf("expected POST with %v to return an error, actual: nil", testCase.reasonToFail)
}
+ statusCode = reqInf.StatusCode
+ if statusCode < 400 || statusCode >= 500 {
+ t.Fatalf("Expected a 400-level status code for topology %s but got %d", testCase.Topology.Name, statusCode)
+ }
}
}
diff --git a/traffic_ops/traffic_ops_golang/topology/topologies.go b/traffic_ops/traffic_ops_golang/topology/topologies.go
index 595d5da..ddcc02c 100644
--- a/traffic_ops/traffic_ops_golang/topology/topologies.go
+++ b/traffic_ops/traffic_ops_golang/topology/topologies.go
@@ -116,20 +116,24 @@ func (topology *TOTopology) Validate() error {
}
}
rules["duplicate cachegroup name"] = checkUniqueCacheGroupNames(topology.Nodes)
+ if !cacheGroupsExist {
+ return util.JoinErrs(tovalidate.ToErrors(rules))
+ }
- if cacheGroupsExist {
- for index, node := range topology.Nodes {
- rules[fmt.Sprintf("parent '%v' edge type", node.Cachegroup)] = checkForEdgeParents(topology.Nodes, cacheGroups, index)
- }
+ for index, node := range topology.Nodes {
+ rules[fmt.Sprintf("parent '%v' edge type", node.Cachegroup)] = checkForEdgeParents(topology.Nodes, cacheGroups, index)
+ }
+ /* Only perform further checks if everything so far is valid */
+ if err = util.JoinErrs(tovalidate.ToErrors(rules)); err != nil {
+ return err
+ }
- for _, leafMid := range checkForLeafMids(topology.Nodes, cacheGroups) {
- rules[fmt.Sprintf("node %v leaf mid", leafMid.Cachegroup)] = fmt.Errorf("cachegroup %v's type is %v; it cannot be a leaf (it must have at least 1 child)", leafMid.Cachegroup, tc.CacheGroupMidTypeName)
- }
+ for _, leafMid := range checkForLeafMids(topology.Nodes, cacheGroups) {
+ rules[fmt.Sprintf("node %v leaf mid", leafMid.Cachegroup)] = fmt.Errorf("cachegroup %v's type is %v; it cannot be a leaf (it must have at least 1 child)", leafMid.Cachegroup, tc.CacheGroupMidTypeName)
}
rules["topology cycles"] = checkForCycles(topology.Nodes)
- errs := tovalidate.ToErrors(rules)
- return util.JoinErrs(errs)
+ return util.JoinErrs(tovalidate.ToErrors(rules))
}
// Implementation of the Identifier, Validator interface functions
diff --git a/traffic_ops/traffic_ops_golang/topology/validation.go b/traffic_ops/traffic_ops_golang/topology/validation.go
index c4270ac..6af9c98 100644
--- a/traffic_ops/traffic_ops_golang/topology/validation.go
+++ b/traffic_ops/traffic_ops_golang/topology/validation.go
@@ -56,10 +56,14 @@ func checkForSelfParents(nodes []tc.TopologyNode, index int) error {
func checkForEdgeParents(nodes []tc.TopologyNode, cachegroups []tc.CacheGroupNullable, nodeIndex int) error {
node := nodes[nodeIndex]
errs := make([]error, len(node.Parents))
- for parentIndex := range node.Parents {
- cacheGroupType := cachegroups[node.Parents[parentIndex]].Type
+ for parentIndex, cachegroupIndex := range node.Parents {
+ if cachegroupIndex < 0 || cachegroupIndex >= len(cachegroups) {
+ errs = append(errs, fmt.Errorf("parent %d of cachegroup %s refers to a cachegroup at index %d, but no such cachegroup exists", parentIndex, node.Cachegroup, cachegroupIndex))
+ break
+ }
+ cacheGroupType := cachegroups[parentIndex].Type
if *cacheGroupType == tc.CacheGroupEdgeTypeName {
- errs[parentIndex] = fmt.Errorf("cachegroup %v's type is %v; it cannot be a parent of %v", nodes[parentIndex].Cachegroup, tc.CacheGroupEdgeTypeName, node.Cachegroup)
+ errs = append(errs, fmt.Errorf("cachegroup %v's type is %v; it cannot be a parent of %v", nodes[parentIndex].Cachegroup, tc.CacheGroupEdgeTypeName, node.Cachegroup))
}
}
return util.JoinErrs(errs)