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 2019/05/14 16:39:42 UTC

[trafficcontrol] branch master updated: TO: Hdr rw config test (#3559)

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


The following commit(s) were added to refs/heads/master by this push:
     new b5022ef  TO: Hdr rw config test (#3559)
b5022ef is described below

commit b5022efd2584e1126d995b9abbed0e0d5fc16118
Author: Matthew Allen Moltzau <ma...@moltzau.net>
AuthorDate: Tue May 14 10:39:36 2019 -0600

    TO: Hdr rw config test (#3559)
    
    * HdrRwConfigTest for mids and edges
    
    -[x] Verifies the correct number of lines is output
    -[ ] Verifies proxy.config.http.origin_max_connections is output
        - Should not output when maxOriginConnections == 0
        - Should not output when no servers exist for ds
    -[ ] Verifies proxy.config.http.origin_max_connections has correct number
        - Probably not going to verify the number exactly
    
    * Fixed divide by zero
    
    * Added test case for \n in addition to __RETURN__
---
 .../testing/api/v14/hdr_rw_dot_config_test.go      | 131 +++++++++++++++++++++
 .../traffic_ops_golang/ats/headerrewrite.go        |  38 +++---
 2 files changed, 153 insertions(+), 16 deletions(-)

diff --git a/traffic_ops/testing/api/v14/hdr_rw_dot_config_test.go b/traffic_ops/testing/api/v14/hdr_rw_dot_config_test.go
new file mode 100644
index 0000000..0f4ee4d
--- /dev/null
+++ b/traffic_ops/testing/api/v14/hdr_rw_dot_config_test.go
@@ -0,0 +1,131 @@
+package v14
+
+/*
+   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 (
+	"fmt"
+	"strconv"
+	"strings"
+	"testing"
+
+	"github.com/apache/trafficcontrol/lib/go-log"
+	"github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+const EdgeHdrRwPrefix = "hdr_rw"
+const MidHdrRwPrefix = "hdr_rw_mid"
+
+func TestHdrRwDotConfig(t *testing.T) {
+	WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, DeliveryServices}, func() {
+		GetTestHdrRwDotConfig(t)
+		GetTestHdrRwMidDotConfig(t)
+		GetTestHdrRwDotConfigWithNewline(t)
+	})
+}
+
+func getFirstDnsOrHttpDeliveryService(t *testing.T) *tc.DeliveryServiceNullable {
+	dses, _, err := TOSession.GetDeliveryServicesNullable()
+	if err != nil {
+		t.Errorf("Cannot test hdr_rw_dot_config with no http or dns deliveryservices: %s", err)
+		return nil
+	}
+
+	for _, ds := range dses {
+		switch *ds.Type {
+		case tc.DSTypeDNS:
+		case tc.DSTypeDNSLive:
+		case tc.DSTypeDNSLiveNational:
+		case tc.DSTypeHTTP:
+		case tc.DSTypeHTTPLive:
+		case tc.DSTypeHTTPLiveNational:
+		case tc.DSTypeHTTPNoCache:
+		default:
+			continue
+		}
+
+		return &ds
+	}
+
+	t.Errorf("Cannot test hdr_rw_dot_config with no http or dns deliveryservices: %s", err)
+	return nil
+
+}
+
+func getExpectedLines(rwRules string) int {
+	if rwRules == "" {
+		return 1 // for the header comment
+	}
+	return 2 + strings.Count(rwRules, "__RETURN__") + strings.Count(rwRules, "\n")
+}
+
+func GetTestHdrRwDotConfigWithNewline(t *testing.T) {
+	ds := getFirstDnsOrHttpDeliveryService(t)
+	*ds.EdgeHeaderRewrite = "rw1\nrw2\nedge\nheader\nre-rewrite [L]"
+	_, err := TOSession.UpdateDeliveryServiceNullable(strconv.Itoa(*ds.ID), ds)
+	if err != nil {
+		t.Errorf("couldn't update delivery servie: %v\n", err)
+	}
+
+	filename := fmt.Sprintf("%s_%s.config", EdgeHdrRwPrefix, *ds.XMLID)
+	config, _, _ := TOSession.GetATSCDNConfig(*ds.CDNID, filename)
+
+	expectedLines := getExpectedLines(*ds.EdgeHeaderRewrite)
+	count := strings.Count(config, "\n")
+	if expectedLines != count {
+		t.Errorf("expected %d lines in the config (actual = %d)\n", expectedLines, count)
+	} else {
+		log.Debugf("Tested %s sucessfully\n", filename)
+	}
+}
+
+func GetTestHdrRwDotConfig(t *testing.T) {
+	ds := getFirstDnsOrHttpDeliveryService(t)
+	*ds.EdgeHeaderRewrite = "rw1__RETURN__rw2__RETURN__edge__RETURN__header__RETURN__re-rewrite [L]"
+	_, err := TOSession.UpdateDeliveryServiceNullable(strconv.Itoa(*ds.ID), ds)
+	if err != nil {
+		t.Errorf("couldn't update delivery servie: %v\n", err)
+	}
+
+	filename := fmt.Sprintf("%s_%s.config", EdgeHdrRwPrefix, *ds.XMLID)
+	config, _, _ := TOSession.GetATSCDNConfig(*ds.CDNID, filename)
+
+	expectedLines := getExpectedLines(*ds.EdgeHeaderRewrite)
+	count := strings.Count(config, "\n")
+	if expectedLines != count {
+		t.Errorf("expected %d lines in the config (actual = %d)\n", expectedLines, count)
+	} else {
+		log.Debugf("Tested %s sucessfully\n", filename)
+	}
+}
+
+func GetTestHdrRwMidDotConfig(t *testing.T) {
+	ds := getFirstDnsOrHttpDeliveryService(t)
+	*ds.MidHeaderRewrite = "rw1__RETURN__mid__RETURN__header__RETURN__re-rewrite [L]"
+	_, err := TOSession.UpdateDeliveryServiceNullable(strconv.Itoa(*ds.ID), ds)
+	if err != nil {
+		t.Errorf("couldn't update delivery servie: %v\n", err)
+	}
+
+	filename := fmt.Sprintf("%s_%s.config", MidHdrRwPrefix, *ds.XMLID)
+	config, _, _ := TOSession.GetATSCDNConfig(*ds.CDNID, filename)
+
+	expectedLines := getExpectedLines(*ds.MidHeaderRewrite)
+	count := strings.Count(config, "\n")
+	if expectedLines != count {
+		t.Errorf("expected %d lines in the config (actual = %d)\n", expectedLines, count)
+	} else {
+		log.Debugf("Tested %s sucessfully\n", filename)
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/ats/headerrewrite.go b/traffic_ops/traffic_ops_golang/ats/headerrewrite.go
index 9c60437..221c9ef 100644
--- a/traffic_ops/traffic_ops_golang/ats/headerrewrite.go
+++ b/traffic_ops/traffic_ops_golang/ats/headerrewrite.go
@@ -21,14 +21,15 @@ package ats
 
 import (
 	"errors"
-	"github.com/apache/trafficcontrol/lib/go-tc"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
-	"github.com/jmoiron/sqlx"
 	"math"
 	"net/http"
 	"regexp"
 	"strconv"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
+	"github.com/jmoiron/sqlx"
 )
 
 func GetEdgeHeaderRewriteDotConfig(w http.ResponseWriter, r *http.Request) {
@@ -73,12 +74,14 @@ func GetEdgeHeaderRewriteDotConfig(w http.ResponseWriter, r *http.Request) {
 			api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting ds server count: "+err.Error()))
 			return
 		}
-		maxOriginConnectionsPerEdge := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineEdgeCount)))
-		text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerEdge)
-		if ds.EdgeHeaderRewrite == nil {
-			text += " [L]"
-		} else {
-			text += "\n"
+		if dsOnlineEdgeCount > 0 {
+			maxOriginConnectionsPerEdge := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineEdgeCount)))
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerEdge)
+			if ds.EdgeHeaderRewrite == nil {
+				text += " [L]"
+			} else {
+				text += "\n"
+			}
 		}
 	}
 
@@ -136,12 +139,14 @@ func GetMidHeaderRewriteDotConfig(w http.ResponseWriter, r *http.Request) {
 			api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting ds server count: "+err.Error()))
 			return
 		}
-		maxOriginConnectionsPerMid := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineMidCount)))
-		text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerMid)
-		if ds.MidHeaderRewrite == nil {
-			text += " [L]"
-		} else {
-			text += "\n"
+		if dsOnlineMidCount > 0 {
+			maxOriginConnectionsPerMid := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineMidCount)))
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerMid)
+			if ds.MidHeaderRewrite == nil {
+				text += " [L]"
+			} else {
+				text += "\n"
+			}
 		}
 	}
 
@@ -165,6 +170,7 @@ func getDeliveryService(tx *sqlx.Tx, xmlId string) (tc.DeliveryServiceNullable,
 	}
 	return ds, nil
 }
+
 // getOnlineDSEdgeCount gets the count of online or reported edges assigned to a delivery service
 func getOnlineDSEdgeCount(tx *sqlx.Tx, dsID int) (int, error) {
 	count := 0