From bdc036420bfc6df8613a95bcd7d3c29b82c3065a Mon Sep 17 00:00:00 2001 From: Aaron Hurt Date: Fri, 19 Apr 2019 13:00:18 -0500 Subject: [PATCH] misc fixups for #629 --- main.go | 2 +- proxy/http_integration_test.go | 14 +++++++------- proxy/listen_test.go | 3 ++- proxy/tcp_integration_test.go | 4 ++-- proxy/ws_integration_test.go | 4 ++-- route/issue57_test.go | 3 ++- route/parse_new.go | 8 +++++--- route/parse_test.go | 5 +++-- route/route_bench_test.go | 3 ++- route/table.go | 6 +++--- route/table_test.go | 7 ++++--- route/target_test.go | 3 ++- 12 files changed, 35 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index a751626a7..406f1106e 100644 --- a/main.go +++ b/main.go @@ -432,7 +432,7 @@ func watchBackend(cfg *config.Config, first chan bool) { svccfg string mancfg string customBE string - next bytes.Buffer + next *bytes.Buffer once sync.Once ) diff --git a/proxy/http_integration_test.go b/proxy/http_integration_test.go index 2132d3dd9..99aaeed52 100644 --- a/proxy/http_integration_test.go +++ b/proxy/http_integration_test.go @@ -193,7 +193,7 @@ func TestProxyStripsPath(t *testing.T) { proxy := httptest.NewServer(&HTTPProxy{ Transport: http.DefaultTransport, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable("route add mock /foo/bar " + server.URL + ` opts "strip=/foo"`) + tbl, _ := route.NewTable(bytes.NewBufferString("route add mock /foo/bar " + server.URL + ` opts "strip=/foo"`)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globEnabled) }, }) @@ -220,7 +220,7 @@ func TestProxyHost(t *testing.T) { routes += "route add mock /hostcustom http://a.com/ opts \"host=foo.com\"\n" routes += "route add mock /hostcustom http://b.com/ opts \"host=bar.com\"\n" routes += "route add mock / http://a.com/" - tbl, _ := route.NewTable(routes) + tbl, _ := route.NewTable(bytes.NewBufferString(routes)) proxy := httptest.NewServer(&HTTPProxy{ Transport: &http.Transport{ @@ -271,7 +271,7 @@ func TestProxyHost(t *testing.T) { func TestHostRedirect(t *testing.T) { routes := "route add https-redir *:80 https://$host$path opts \"redirect=301\"\n" - tbl, _ := route.NewTable(routes) + tbl, _ := route.NewTable(bytes.NewBufferString(routes)) proxy := httptest.NewServer(&HTTPProxy{ Transport: http.DefaultTransport, @@ -311,7 +311,7 @@ func TestPathRedirect(t *testing.T) { routes := "route add mock / http://a.com/$path opts \"redirect=301\"\n" routes += "route add mock /foo http://a.com/abc opts \"redirect=301\"\n" routes += "route add mock /bar http://b.com/$path opts \"redirect=302 strip=/bar\"\n" - tbl, _ := route.NewTable(routes) + tbl, _ := route.NewTable(bytes.NewBufferString(routes)) proxy := httptest.NewServer(&HTTPProxy{ Transport: http.DefaultTransport, @@ -483,7 +483,7 @@ func TestProxyHTTPSUpstream(t *testing.T) { Config: config.Proxy{}, Transport: &http.Transport{TLSClientConfig: tlsClientConfig()}, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable("route add srv / " + server.URL + ` opts "proto=https"`) + tbl, _ := route.NewTable(bytes.NewBufferString("route add srv / " + server.URL + ` opts "proto=https"`)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globEnabled) }, }) @@ -510,7 +510,7 @@ func TestProxyHTTPSUpstreamSkipVerify(t *testing.T) { TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable("route add srv / " + server.URL + ` opts "proto=https tlsskipverify=true"`) + tbl, _ := route.NewTable(bytes.NewBufferString("route add srv / " + server.URL + ` opts "proto=https tlsskipverify=true"`)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globEnabled) }, }) @@ -711,7 +711,7 @@ func BenchmarkProxyLogger(b *testing.B) { }, Transport: http.DefaultTransport, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable("route add mock / " + server.URL) + tbl, _ := route.NewTable(bytes.NewBufferString("route add mock / " + server.URL)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globEnabled) }, Logger: l, diff --git a/proxy/listen_test.go b/proxy/listen_test.go index c795e51b2..1e80e6f93 100644 --- a/proxy/listen_test.go +++ b/proxy/listen_test.go @@ -1,6 +1,7 @@ package proxy import ( + "bytes" "net/http" "net/http/httptest" "strings" @@ -33,7 +34,7 @@ func TestGracefulShutdown(t *testing.T) { h := &HTTPProxy{ Transport: http.DefaultTransport, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable("route add svc / " + srv.URL) + tbl, _ := route.NewTable(bytes.NewBufferString("route add svc / " + srv.URL)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globDisabled) }, } diff --git a/proxy/tcp_integration_test.go b/proxy/tcp_integration_test.go index 425e43758..069de7818 100644 --- a/proxy/tcp_integration_test.go +++ b/proxy/tcp_integration_test.go @@ -43,7 +43,7 @@ func TestTCPProxy(t *testing.T) { go func() { h := &tcp.Proxy{ Lookup: func(h string) *route.Target { - tbl, _ := route.NewTable("route add srv :57778 tcp://" + srv.Addr) + tbl, _ := route.NewTable(bytes.NewBufferString("route add srv :57778 tcp://" + srv.Addr)) return tbl.LookupHost(h, route.Picker["rr"]) }, } @@ -224,7 +224,7 @@ func TestTCPProxyWithProxyProto(t *testing.T) { go func() { h := &tcp.Proxy{ Lookup: func(h string) *route.Target { - tbl, _ := route.NewTable("route add srv :57778 tcp://" + srv.Addr + " opts \"pxyproto=true\"") + tbl, _ := route.NewTable(bytes.NewBufferString("route add srv :57778 tcp://" + srv.Addr + " opts \"pxyproto=true\"")) tgt := tbl.LookupHost(h, route.Picker["rr"]) return tgt }, diff --git a/proxy/ws_integration_test.go b/proxy/ws_integration_test.go index 853371ca2..3f83a5a51 100644 --- a/proxy/ws_integration_test.go +++ b/proxy/ws_integration_test.go @@ -45,7 +45,7 @@ func TestProxyWSUpstream(t *testing.T) { Transport: &http.Transport{TLSClientConfig: tlsClientConfig()}, InsecureTransport: &http.Transport{TLSClientConfig: tlsInsecureConfig()}, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable(routes) + tbl, _ := route.NewTable(bytes.NewBufferString(routes)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globDisabled) }, }) @@ -57,7 +57,7 @@ func TestProxyWSUpstream(t *testing.T) { Transport: &http.Transport{TLSClientConfig: tlsClientConfig()}, InsecureTransport: &http.Transport{TLSClientConfig: tlsInsecureConfig()}, Lookup: func(r *http.Request) *route.Target { - tbl, _ := route.NewTable(routes) + tbl, _ := route.NewTable(bytes.NewBufferString(routes)) return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"], globDisabled) }, }) diff --git a/route/issue57_test.go b/route/issue57_test.go index c87dd8b00..364a95f4e 100644 --- a/route/issue57_test.go +++ b/route/issue57_test.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "net/http" "testing" ) @@ -28,7 +29,7 @@ func TestIssue57(t *testing.T) { want := "http://foo.com:800" for i, tt := range tests { - tbl, err := NewTable(tt) + tbl, err := NewTable(bytes.NewBufferString(tt)) if err != nil { t.Fatalf("%d: got %v want nil", i, err) } diff --git a/route/parse_new.go b/route/parse_new.go index 095343968..f023c3baf 100644 --- a/route/parse_new.go +++ b/route/parse_new.go @@ -67,12 +67,14 @@ route weight service host/path weight w tags "tag1,tag2" // The commands are parsed in order and order matters. // Deleting a route that has not been created yet yields // a different result than the other way around. -func Parse(in bytes.Buffer) (defs []*RouteDef, err error) { +func Parse(in *bytes.Buffer) (defs []*RouteDef, err error) { var def *RouteDef - scanner := bufio.NewScanner(&in) + var i int + scanner := bufio.NewScanner(in) for scanner.Scan() { def, err = nil, nil result := strings.TrimSpace(scanner.Text()) + i++ switch { case reComment.MatchString(result) || reBlankLine.MatchString(result): continue @@ -86,7 +88,7 @@ func Parse(in bytes.Buffer) (defs []*RouteDef, err error) { err = errors.New("syntax error: 'route' expected") } if err != nil { - return nil, fmt.Errorf("line %d: %s", err) + return nil, fmt.Errorf("line %d: %s", i, err) } defs = append(defs, def) } diff --git a/route/parse_test.go b/route/parse_test.go index 5c3159226..13b14c739 100644 --- a/route/parse_test.go +++ b/route/parse_test.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "reflect" "regexp" "testing" @@ -162,8 +163,8 @@ func TestParse(t *testing.T) { return } - run := func(in string, def []*RouteDef, fail bool, parseFn func(string) ([]*RouteDef, error)) { - out, err := parseFn(in) + run := func(in string, def []*RouteDef, fail bool, parseFn func(*bytes.Buffer) ([]*RouteDef, error)) { + out, err := parseFn(bytes.NewBufferString(in)) switch { case err == nil && fail: t.Errorf("got error nil want fail") diff --git a/route/route_bench_test.go b/route/route_bench_test.go index e5693c572..343246bee 100644 --- a/route/route_bench_test.go +++ b/route/route_bench_test.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "fmt" "net/http" "sync" @@ -98,7 +99,7 @@ func makeRoutes(domains, paths, depth, urls int) Table { } } - t, err := NewTable(s) + t, err := NewTable(bytes.NewBufferString(s)) if err != nil { panic(err) } diff --git a/route/table.go b/route/table.go index ee0ce9d9c..f1d50eec3 100644 --- a/route/table.go +++ b/route/table.go @@ -108,8 +108,8 @@ func hostpath(prefix string) (host string, path string) { return p[0], "/" + p[1] } -func NewTable(s bytes.Buffer) (t Table, err error) { - defs, err := Parse(s) +func NewTable(b *bytes.Buffer) (t Table, err error) { + defs, err := Parse(b) if err != nil { return nil, err } @@ -490,7 +490,7 @@ func (t Table) String() string { // Dump returns the routing table as a detailed func (t Table) Dump() string { - w := new(bytes.Buffer) + var w *bytes.Buffer hosts := []string{} for k := range t { diff --git a/route/table_test.go b/route/table_test.go index cf6fd182b..fb621c8c0 100644 --- a/route/table_test.go +++ b/route/table_test.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "crypto/tls" "fmt" "math" @@ -380,7 +381,7 @@ func TestTableParse(t *testing.T) { // actual lookup which it probably should. t.Run(tt.desc, func(t *testing.T) { // parse the routes - tbl, err := NewTable(strings.Join(tt.in, "\n")) + tbl, err := NewTable(bytes.NewBufferString(strings.Join(tt.in, "\n"))) if err != nil { t.Fatalf("got %v want nil", err) } @@ -499,7 +500,7 @@ func TestTableLookupIssue448(t *testing.T) { route add mock / http://foo.com/ ` - tbl, err := NewTable(s) + tbl, err := NewTable(bytes.NewBufferString(s)) if err != nil { t.Fatal(err) } @@ -581,7 +582,7 @@ func TestTableLookup(t *testing.T) { route add svc xyz.com:80/ https://xyz.com ` - tbl, err := NewTable(s) + tbl, err := NewTable(bytes.NewBufferString(s)) if err != nil { t.Fatal(err) } diff --git a/route/target_test.go b/route/target_test.go index 0eed8a0bb..81989f815 100644 --- a/route/target_test.go +++ b/route/target_test.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "net/url" "testing" ) @@ -140,7 +141,7 @@ func TestTarget_BuildRedirectURL(t *testing.T) { return nil } for _, tt := range tests { - tbl, _ := NewTable(tt.route) + tbl, _ := NewTable(bytes.NewBufferString(tt.route)) route := firstRoute(tbl) target := route.Targets[0] for _, rt := range tt.tests {