From af4be34a2af4ddd7b48bfb40c81cb155d881f856 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Fri, 13 Apr 2018 09:57:25 -0700 Subject: [PATCH] Update make static-assets goal and run format --- GNUmakefile | 2 +- agent/consul/prepared_query_endpoint.go | 8 +-- agent/dns.go | 26 +++---- agent/dns_test.go | 94 ++++++++++++------------- agent/http.go | 5 +- agent/prepared_query_endpoint_test.go | 10 +-- command/validate/validate.go | 6 +- command/validate/validate_test.go | 3 +- 8 files changed, 76 insertions(+), 78 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 81fc1a91c6..ef1a3bb0a4 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -122,7 +122,7 @@ ui: # changes to the UI assets that aren't checked in. static-assets: @go-bindata-assetfs -pkg agent -prefix pkg ./pkg/web_ui/... - @mv bindata_assetfs.go agent/ + @mv bindata.go agent/bindata_assetfs.go $(MAKE) format tools: diff --git a/agent/consul/prepared_query_endpoint.go b/agent/consul/prepared_query_endpoint.go index e4adca5076..ee6061a84e 100644 --- a/agent/consul/prepared_query_endpoint.go +++ b/agent/consul/prepared_query_endpoint.go @@ -407,12 +407,12 @@ func (p *PreparedQuery) Execute(args *structs.PreparedQueryExecuteRequest, } } } else { - p.srv.logger.Printf("[WARN] Prepared Query using near=_ip requires "+ - "the source IP to be set but none was provided. No distance "+ + p.srv.logger.Printf("[WARN] Prepared Query using near=_ip requires " + + "the source IP to be set but none was provided. No distance " + "sorting will be done.") - + } - + // Either a source IP was given but we couldnt find the associated node // or no source ip was given. In both cases we should wipe the Node value if qs.Node == "_ip" { diff --git a/agent/dns.go b/agent/dns.go index a8e8bf3e67..98cb5a9b5b 100644 --- a/agent/dns.go +++ b/agent/dns.go @@ -917,21 +917,21 @@ func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req, } } -func ednsSubnetForRequest(req *dns.Msg) (*dns.EDNS0_SUBNET) { +func ednsSubnetForRequest(req *dns.Msg) *dns.EDNS0_SUBNET { // IsEdns0 returns the EDNS RR if present or nil otherwise edns := req.IsEdns0() - + if edns == nil { return nil } - + for _, o := range edns.Option { if subnet, ok := o.(*dns.EDNS0_SUBNET); ok { return subnet } } - - return nil; + + return nil } // preparedQueryLookup is used to handle a prepared query. @@ -955,19 +955,19 @@ func (d *DNSServer) preparedQueryLookup(network, datacenter, query string, remot Node: d.agent.config.NodeName, }, } - + subnet := ednsSubnetForRequest(req) - + if subnet != nil { args.Source.Ip = subnet.Address.String() } else { switch v := remoteAddr.(type) { - case *net.UDPAddr: - args.Source.Ip = v.IP.String() - case *net.TCPAddr: - args.Source.Ip = v.IP.String() - case *net.IPAddr: - args.Source.Ip = v.IP.String() + case *net.UDPAddr: + args.Source.Ip = v.IP.String() + case *net.TCPAddr: + args.Source.Ip = v.IP.String() + case *net.IPAddr: + args.Source.Ip = v.IP.String() } } diff --git a/agent/dns_test.go b/agent/dns_test.go index 18c847c334..83bf2ca871 100644 --- a/agent/dns_test.go +++ b/agent/dns_test.go @@ -9,15 +9,15 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/testutil/retry" + "github.com/hashicorp/serf/coordinate" "github.com/miekg/dns" "github.com/pascaldekloe/goe/verify" - "github.com/hashicorp/serf/coordinate" + "github.com/stretchr/testify/require" ) const ( @@ -1839,22 +1839,22 @@ func TestDNS_ServiceLookup_TagPeriod(t *testing.T) { func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { ipCoord := lib.GenerateCoordinate(1 * time.Millisecond) - serviceNodes := []struct{ - name string - address string - coord *coordinate.Coordinate + serviceNodes := []struct { + name string + address string + coord *coordinate.Coordinate }{ - {"foo1", "198.18.0.1", lib.GenerateCoordinate(1 * time.Millisecond),}, - {"foo2", "198.18.0.2", lib.GenerateCoordinate(10 * time.Millisecond),}, - {"foo3", "198.18.0.3", lib.GenerateCoordinate(30 * time.Millisecond),}, + {"foo1", "198.18.0.1", lib.GenerateCoordinate(1 * time.Millisecond)}, + {"foo2", "198.18.0.2", lib.GenerateCoordinate(10 * time.Millisecond)}, + {"foo3", "198.18.0.3", lib.GenerateCoordinate(30 * time.Millisecond)}, } - + t.Parallel() a := NewTestAgent(t.Name(), "") defer a.Shutdown() - + added := 0 - + // Register nodes with a service for _, cfg := range serviceNodes { args := &structs.RegisterRequest{ @@ -1866,11 +1866,11 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { Port: 12345, }, } - + var out struct{} err := a.RPC("Catalog.Register", args, &out) require.NoError(t, err) - + // Send coordinate updates coordArgs := structs.CoordinateUpdateRequest{ Datacenter: "dc1", @@ -1879,12 +1879,12 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { } err = a.RPC("Coordinate.Update", &coordArgs, &out) require.NoError(t, err) - + added += 1 } - + fmt.Printf("Added %d service nodes\n", added) - + // Register a node without a service { args := &structs.RegisterRequest{ @@ -1892,11 +1892,11 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { Node: "bar", Address: "198.18.0.9", } - + var out struct{} err := a.RPC("Catalog.Register", args, &out) require.NoError(t, err) - + // Send coordinate updates for a few nodes. coordArgs := structs.CoordinateUpdateRequest{ Datacenter: "dc1", @@ -1906,7 +1906,7 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { err = a.RPC("Coordinate.Update", &coordArgs, &out) require.NoError(t, err) } - + // Register a prepared query Near = _ip { args := &structs.PreparedQueryRequest{ @@ -1916,7 +1916,7 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { Name: "some.query.we.like", Service: structs.ServiceQuery{ Service: "db", - Near: "_ip", + Near: "_ip", }, }, } @@ -1940,17 +1940,17 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { e.Address = net.ParseIP("198.18.0.9").To4() o.Option = append(o.Option, e) m.Extra = append(m.Extra, o) - + c := new(dns.Client) in, _, err := c.Exchange(m, a.DNSAddr()) if err != nil { r.Fatalf("Error with call to dns.Client.Exchange: %s", err) } - + if len(serviceNodes) != len(in.Answer) { r.Fatalf("Expecting %d A RRs in response, Actual found was %d", len(serviceNodes), len(in.Answer)) } - + for i, rr := range in.Answer { if aRec, ok := rr.(*dns.A); ok { if actual := aRec.A.String(); serviceNodes[i].address != actual { @@ -1965,22 +1965,22 @@ func TestDNS_PreparedQueryNearIPEDNS(t *testing.T) { func TestDNS_PreparedQueryNearIP(t *testing.T) { ipCoord := lib.GenerateCoordinate(1 * time.Millisecond) - serviceNodes := []struct{ - name string - address string - coord *coordinate.Coordinate + serviceNodes := []struct { + name string + address string + coord *coordinate.Coordinate }{ - {"foo1", "198.18.0.1", lib.GenerateCoordinate(1 * time.Millisecond),}, - {"foo2", "198.18.0.2", lib.GenerateCoordinate(10 * time.Millisecond),}, - {"foo3", "198.18.0.3", lib.GenerateCoordinate(30 * time.Millisecond),}, + {"foo1", "198.18.0.1", lib.GenerateCoordinate(1 * time.Millisecond)}, + {"foo2", "198.18.0.2", lib.GenerateCoordinate(10 * time.Millisecond)}, + {"foo3", "198.18.0.3", lib.GenerateCoordinate(30 * time.Millisecond)}, } - + t.Parallel() a := NewTestAgent(t.Name(), "") defer a.Shutdown() - + added := 0 - + // Register nodes with a service for _, cfg := range serviceNodes { args := &structs.RegisterRequest{ @@ -1992,11 +1992,11 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { Port: 12345, }, } - + var out struct{} err := a.RPC("Catalog.Register", args, &out) require.NoError(t, err) - + // Send coordinate updates coordArgs := structs.CoordinateUpdateRequest{ Datacenter: "dc1", @@ -2005,12 +2005,12 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { } err = a.RPC("Coordinate.Update", &coordArgs, &out) require.NoError(t, err) - + added += 1 } - + fmt.Printf("Added %d service nodes\n", added) - + // Register a node without a service { args := &structs.RegisterRequest{ @@ -2018,11 +2018,11 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { Node: "bar", Address: "198.18.0.9", } - + var out struct{} err := a.RPC("Catalog.Register", args, &out) require.NoError(t, err) - + // Send coordinate updates for a few nodes. coordArgs := structs.CoordinateUpdateRequest{ Datacenter: "dc1", @@ -2032,7 +2032,7 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { err = a.RPC("Coordinate.Update", &coordArgs, &out) require.NoError(t, err) } - + // Register a prepared query Near = _ip { args := &structs.PreparedQueryRequest{ @@ -2042,7 +2042,7 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { Name: "some.query.we.like", Service: structs.ServiceQuery{ Service: "db", - Near: "_ip", + Near: "_ip", }, }, } @@ -2051,21 +2051,21 @@ func TestDNS_PreparedQueryNearIP(t *testing.T) { err := a.RPC("PreparedQuery.Apply", args, &id) require.NoError(t, err) } - + retry.Run(t, func(r *retry.R) { m := new(dns.Msg) m.SetQuestion("some.query.we.like.query.consul.", dns.TypeA) - + c := new(dns.Client) in, _, err := c.Exchange(m, a.DNSAddr()) if err != nil { r.Fatalf("Error with call to dns.Client.Exchange: %s", err) } - + if len(serviceNodes) != len(in.Answer) { r.Fatalf("Expecting %d A RRs in response, Actual found was %d", len(serviceNodes), len(in.Answer)) } - + for i, rr := range in.Answer { if aRec, ok := rr.(*dns.A); ok { if actual := aRec.A.String(); serviceNodes[i].address != actual { diff --git a/agent/http.go b/agent/http.go index 1fcc329cad..9d331a034e 100644 --- a/agent/http.go +++ b/agent/http.go @@ -12,12 +12,12 @@ import ( "strings" "time" + "github.com/NYTimes/gziphandler" "github.com/armon/go-metrics" "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/go-cleanhttp" "github.com/mitchellh/mapstructure" - "github.com/NYTimes/gziphandler" ) // MethodNotAllowedError should be returned by a handler when the HTTP method is not allowed. @@ -507,7 +507,7 @@ func sourceAddrFromRequest(req *http.Request) string { return forwardIp.String() } } - + host, _, err := net.SplitHostPort(req.RemoteAddr) if err != nil { return "" @@ -521,7 +521,6 @@ func sourceAddrFromRequest(req *http.Request) string { } } - // parseSource is used to parse the ?near= query parameter, used for // sorting by RTT based on a source node. We set the source's DC to the target // DC in the request, if given, or else the agent's DC. diff --git a/agent/prepared_query_endpoint_test.go b/agent/prepared_query_endpoint_test.go index b32ab69f9a..e58e48fbbf 100644 --- a/agent/prepared_query_endpoint_test.go +++ b/agent/prepared_query_endpoint_test.go @@ -327,7 +327,7 @@ func TestPreparedQuery_Execute(t *testing.T) { t.Fatalf("bad: %v", r) } }) - + t.Run("", func(t *testing.T) { a := NewTestAgent(t.Name(), "") defer a.Shutdown() @@ -340,7 +340,7 @@ func TestPreparedQuery_Execute(t *testing.T) { Limit: 5, Source: structs.QuerySource{ Datacenter: "dc1", - Node: "_ip", + Node: "_ip", Ip: "127.0.0.1", }, Agent: structs.QuerySource{ @@ -384,7 +384,7 @@ func TestPreparedQuery_Execute(t *testing.T) { t.Fatalf("bad: %v", r) } }) - + t.Run("", func(t *testing.T) { a := NewTestAgent(t.Name(), "") defer a.Shutdown() @@ -397,7 +397,7 @@ func TestPreparedQuery_Execute(t *testing.T) { Limit: 5, Source: structs.QuerySource{ Datacenter: "dc1", - Node: "_ip", + Node: "_ip", Ip: "198.18.0.1", }, Agent: structs.QuerySource{ @@ -440,7 +440,7 @@ func TestPreparedQuery_Execute(t *testing.T) { if r.Failovers != 99 { t.Fatalf("bad: %v", r) } - + req, _ = http.NewRequest("GET", "/v1/query/my-id/execute?token=my-token&consistent=true&near=_ip&limit=5", body) req.Header.Add("X-Forwarded-For", "198.18.0.1, 198.19.0.1") resp = httptest.NewRecorder() diff --git a/command/validate/validate.go b/command/validate/validate.go index 96a84d6148..fe3e646533 100644 --- a/command/validate/validate.go +++ b/command/validate/validate.go @@ -45,12 +45,12 @@ func (c *cmd) Run(args []string) int { c.UI.Error("Must specify at least one config file or directory") return 1 } - + if c.configFormat != "" && c.configFormat != "json" && c.configFormat != "hcl" { c.UI.Error("-config-format must be either 'hcl' or 'json") - return 1 + return 1 } - + b, err := config.NewBuilder(config.Flags{ConfigFiles: configFiles, ConfigFormat: &c.configFormat}) if err != nil { c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error())) diff --git a/command/validate/validate_test.go b/command/validate/validate_test.go index c3f3dacc61..362a9bb818 100644 --- a/command/validate/validate_test.go +++ b/command/validate/validate_test.go @@ -7,9 +7,9 @@ import ( "strings" "testing" - require "github.com/stretchr/testify/require" "github.com/hashicorp/consul/testutil" "github.com/mitchellh/cli" + require "github.com/stretchr/testify/require" ) func TestValidateCommand_noTabs(t *testing.T) { @@ -72,7 +72,6 @@ func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) { err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644) require.Nilf(t, err, "err: %s", err) - cmd := New(cli.NewMockUi()) args := []string{"--config-format", "hcl", fp}