From 7511490c12fcf87799b7d820601a53413a49d8dd Mon Sep 17 00:00:00 2001 From: Evan Gilman Date: Sun, 11 Sep 2016 11:02:11 -0700 Subject: [PATCH] Add more address selector tests --- command/agent/agent.go | 2 +- command/agent/agent_test.go | 48 ++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index f09cacb32..5169ceffe 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -492,7 +492,7 @@ func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, erro return "", err } - if preferBind && strings.HasPrefix(addr.IP.String(), "0.0.0.0:") { + if preferBind && addr.IP.String() == "0.0.0.0" { addr, err = selector(false) if err != nil { return "", err diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 5f24403cd..e2b236b2a 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -98,7 +98,7 @@ func TestAgent_ServerConfig(t *testing.T) { // Returns error on bad serf addr conf.AdvertiseAddrs.Serf = "nope" _, err := a.serverConfig() - if err == nil || !strings.Contains(err.Error(), "serf advertise") { + if err == nil || !strings.Contains(err.Error(), "error resolving Serf addr") { t.Fatalf("expected serf address error, got: %#v", err) } conf.AdvertiseAddrs.Serf = "127.0.0.1:4000" @@ -106,7 +106,7 @@ func TestAgent_ServerConfig(t *testing.T) { // Returns error on bad rpc addr conf.AdvertiseAddrs.RPC = "nope" _, err = a.serverConfig() - if err == nil || !strings.Contains(err.Error(), "rpc advertise") { + if err == nil || !strings.Contains(err.Error(), "error resolving RPC addr") { t.Fatalf("expected rpc address error, got: %#v", err) } conf.AdvertiseAddrs.RPC = "127.0.0.1:4001" @@ -155,9 +155,9 @@ func TestAgent_ServerConfig(t *testing.T) { conf.Addresses.RPC = "127.0.0.2" conf.Addresses.Serf = "127.0.0.2" conf.Addresses.HTTP = "127.0.0.2" - conf.AdvertiseAddrs.HTTP = "10.0.0.1" + conf.AdvertiseAddrs.HTTP = "10.0.0.10:4646" conf.AdvertiseAddrs.RPC = "" - conf.AdvertiseAddrs.Serf = "10.0.0.2" + conf.AdvertiseAddrs.Serf = "10.0.0.12:4004" out, err = a.serverConfig() if err != nil { @@ -169,22 +169,48 @@ func TestAgent_ServerConfig(t *testing.T) { if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.2" { t.Fatalf("expect 127.0.0.2, got: %s", addr) } - if addr := a.serverHTTPAddr; addr != "10.0.0.1:4646" { - t.Fatalf("expect 10.0.0.1:4646, got: %s", addr) + if addr := a.serverHTTPAddr; addr != "10.0.0.10:4646" { + t.Fatalf("expect 10.0.0.10:4646, got: %s", addr) } // NOTE: AdvertiseAddr > Addresses > BindAddr > Defaults - if addr := a.serverRPCAddr; addr != "127.0.0.1:4001" { - t.Fatalf("expect 127.0.0.1:4001, got: %s", addr) + if addr := a.serverRPCAddr; addr != "127.0.0.2:4003" { + t.Fatalf("expect 127.0.0.2:4003, got: %s", addr) } - if addr := a.serverSerfAddr; addr != "10.0.0.2:4000" { - t.Fatalf("expect 10.0.0.2:4000, got: %s", addr) + if addr := a.serverSerfAddr; addr != "10.0.0.12:4004" { + t.Fatalf("expect 10.0.0.12:4004, got: %s", addr) } - // It correctly identifies the bind address when requested + // It correctly identifies the bind and advertise address when requested if addr, err := a.selectAddr(a.getHTTPAddr, true); addr != "127.0.0.2:4646" || err != nil { t.Fatalf("expect 127.0.0.2:4646, got: %s", addr) } + if addr, err := a.selectAddr(a.getHTTPAddr, false); addr != "10.0.0.10:4646" || err != nil { + t.Fatalf("expect 10.0.0.10:4646, got: %s", addr) + } + + if addr, err := a.selectAddr(a.getRPCAddr, true); addr != "127.0.0.2:4003" || err != nil { + t.Fatalf("expect 127.0.0.2:4003, got: %s", addr) + } + + if addr, err := a.selectAddr(a.getRPCAddr, false); addr != "127.0.0.2:4003" || err != nil { + t.Fatalf("expect 127.0.0.2:4003, got: %s", addr) + } + + if addr, err := a.selectAddr(a.getSerfAddr, true); addr != "127.0.0.2:4004" || err != nil { + t.Fatalf("expect 127.0.0.2:4004, got: %s", addr) + } + + if addr, err := a.selectAddr(a.getSerfAddr, false); addr != "10.0.0.12:4004" || err != nil { + t.Fatalf("expect 10.0.0.12:4004, got: %s", addr) + } + + // selectAddr does not return 0.0.0.0 with preferBind + conf.Addresses.HTTP = "0.0.0.0" + if addr, err := a.selectAddr(a.getHTTPAddr, true); addr != "10.0.0.10:4646" || err != nil { + t.Fatalf("expect 10.0.0.10:4646, got: %s", addr) + } + conf.Server.NodeGCThreshold = "42g" out, err = a.serverConfig() if err == nil || !strings.Contains(err.Error(), "unknown unit") {