Fix int pointer formatting and server config test

This commit is contained in:
Michael Schurter 2016-11-08 16:02:20 -08:00
parent 4235d65011
commit 6d83a192cf
2 changed files with 58 additions and 51 deletions

View File

@ -94,6 +94,10 @@ func TestAgent_RPCPing(t *testing.T) {
func TestAgent_ServerConfig(t *testing.T) { func TestAgent_ServerConfig(t *testing.T) {
conf := DefaultConfig() conf := DefaultConfig()
a := &Agent{config: conf} a := &Agent{config: conf}
testlogger := func(s string) {
t.Log(s)
}
dev := false
// Returns error on bad serf addr // Returns error on bad serf addr
conf.AdvertiseAddrs.Serf = "nope" conf.AdvertiseAddrs.Serf = "nope"
@ -113,6 +117,9 @@ func TestAgent_ServerConfig(t *testing.T) {
conf.AdvertiseAddrs.HTTP = "10.10.11.1:4005" conf.AdvertiseAddrs.HTTP = "10.10.11.1:4005"
// Parses the advertise addrs correctly // Parses the advertise addrs correctly
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err := a.serverConfig() out, err := a.serverConfig()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -125,20 +132,27 @@ func TestAgent_ServerConfig(t *testing.T) {
if serfPort != 4000 { if serfPort != 4000 {
t.Fatalf("expected 4000, got: %d", serfPort) t.Fatalf("expected 4000, got: %d", serfPort)
} }
if addr := out.RPCAdvertise; addr.IP.String() != "127.0.0.1" || addr.Port != 4001 {
// Assert addresses weren't changed
if addr := conf.AdvertiseAddrs.RPC; addr != "127.0.0.1:4001" {
t.Fatalf("bad rpc advertise addr: %#v", addr) t.Fatalf("bad rpc advertise addr: %#v", addr)
} }
if addr := a.serverHTTPAddr; addr != "10.10.11.1:4005" { if addr := conf.AdvertiseAddrs.HTTP; addr != "10.10.11.1:4005" {
t.Fatalf("expect 10.11.11.1:4005, got: %v", addr) t.Fatalf("expect 10.11.11.1:4005, got: %v", addr)
} }
if addr := a.serverRPCAddr; addr != "127.0.0.1:4001" { if addr := conf.Addresses.RPC; addr != "0.0.0.0:4647" {
t.Fatalf("expect 127.0.0.1:4001, got: %v", addr) t.Fatalf("expect 0.0.0.0:4001, got: %v", addr)
} }
// Sets up the ports properly // Sets up the ports properly
conf.Addresses.RPC = ""
conf.Addresses.Serf = ""
conf.Ports.RPC = 4003 conf.Ports.RPC = 4003
conf.Ports.Serf = 4004 conf.Ports.Serf = 4004
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -152,93 +166,83 @@ func TestAgent_ServerConfig(t *testing.T) {
// Prefers advertise over bind addr // Prefers advertise over bind addr
conf.BindAddr = "127.0.0.3" conf.BindAddr = "127.0.0.3"
conf.Addresses.HTTP = "127.0.0.2"
conf.Addresses.RPC = "127.0.0.2" conf.Addresses.RPC = "127.0.0.2"
conf.Addresses.Serf = "127.0.0.2" conf.Addresses.Serf = "127.0.0.2"
conf.Addresses.HTTP = "127.0.0.2"
conf.AdvertiseAddrs.HTTP = "10.0.0.10" conf.AdvertiseAddrs.HTTP = "10.0.0.10"
conf.AdvertiseAddrs.RPC = "" conf.AdvertiseAddrs.RPC = ""
conf.AdvertiseAddrs.Serf = "10.0.0.12:4004" conf.AdvertiseAddrs.Serf = "10.0.0.12:4004"
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
fmt.Println(conf.Addresses.RPC)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if addr := out.RPCAddr.IP.String(); addr != "127.0.0.2" { if addr := out.RPCAddr.IP.String(); addr != "127.0.0.2" {
t.Fatalf("expect 127.0.0.2, got: %s", addr) t.Fatalf("expect 127.0.0.2, got: %s", addr)
} }
if port := out.RPCAddr.Port; port != 4003 {
t.Fatalf("expect 4647, got: %d", port)
}
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.2" { if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.2" {
t.Fatalf("expect 127.0.0.2, got: %s", addr) t.Fatalf("expect 127.0.0.2, got: %s", addr)
} }
if addr := a.serverHTTPAddr; addr != "10.0.0.10:4646" { if port := out.SerfConfig.MemberlistConfig.BindPort; port != 4004 {
t.Fatalf("expect 10.0.0.10:4646, got: %s", addr) t.Fatalf("expect 4648, got: ^d", port)
} }
// NOTE: AdvertiseAddr > Addresses > BindAddr > Defaults if addr := conf.Addresses.HTTP; addr != "127.0.0.2:4646" {
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.12:4004" {
t.Fatalf("expect 10.0.0.12:4004, got: %s", addr)
}
// 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) t.Fatalf("expect 127.0.0.2:4646, got: %s", addr)
} }
if addr := conf.Addresses.RPC; addr != "127.0.0.2:4003" {
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) t.Fatalf("expect 127.0.0.2:4003, got: %s", addr)
} }
if addr := conf.Addresses.Serf; addr != "127.0.0.2:4004" {
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) t.Fatalf("expect 10.0.0.12:4004, got: %s", addr)
} }
if addr := conf.AdvertiseAddrs.HTTP; addr != "10.0.0.10:4646" {
// We don't resolve 0.0.0.0 unless we're asking for bind t.Fatalf("expect 10.0.0.10:4646, got: %s", addr)
conf.Addresses.HTTP = "0.0.0.0"
conf.AdvertiseAddrs.HTTP = ""
if addr, err := a.getHTTPAddr(false); addr.IP.String() != "127.0.0.3" || err != nil {
t.Fatalf("expect 127.0.0.3, got: %s", addr.IP.String())
} }
if addr := conf.AdvertiseAddrs.RPC; addr != "127.0.0.3:4003" {
// We still get 0.0.0.0 when explicitly asking for bind t.Fatalf("expect 127.0.0.3:4003, got: %s", addr)
if addr, err := a.getHTTPAddr(true); addr.IP.String() != "0.0.0.0" || err != nil {
t.Fatalf("expect 0.0.0.0, got: %s", addr.IP.String())
} }
if addr := conf.AdvertiseAddrs.Serf; addr != "10.0.0.12:4004" {
// selectAddr does not return 0.0.0.0 with preferBind t.Fatalf("expect 10.0.0.12:4004, got: %s", addr)
if addr, err := a.selectAddr(a.getHTTPAddr, true); addr != "127.0.0.3:4646" || err != nil {
t.Fatalf("expect 127.0.0.3:4646, got: %s", addr)
} }
conf.Server.NodeGCThreshold = "42g" conf.Server.NodeGCThreshold = "42g"
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if err == nil || !strings.Contains(err.Error(), "unknown unit") { if err == nil || !strings.Contains(err.Error(), "unknown unit") {
t.Fatalf("expected unknown unit error, got: %#v", err) t.Fatalf("expected unknown unit error, got: %#v", err)
} }
conf.Server.NodeGCThreshold = "10s" conf.Server.NodeGCThreshold = "10s"
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if threshold := out.NodeGCThreshold; threshold != time.Second*10 { if threshold := out.NodeGCThreshold; threshold != time.Second*10 {
t.Fatalf("expect 10s, got: %s", threshold) t.Fatalf("expect 10s, got: %s", threshold)
} }
conf.Server.HeartbeatGrace = "42g" conf.Server.HeartbeatGrace = "42g"
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if err == nil || !strings.Contains(err.Error(), "unknown unit") { if err == nil || !strings.Contains(err.Error(), "unknown unit") {
t.Fatalf("expected unknown unit error, got: %#v", err) t.Fatalf("expected unknown unit error, got: %#v", err)
} }
conf.Server.HeartbeatGrace = "37s" conf.Server.HeartbeatGrace = "37s"
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if threshold := out.HeartbeatGrace; threshold != time.Second*37 { if threshold := out.HeartbeatGrace; threshold != time.Second*37 {
t.Fatalf("expect 37s, got: %s", threshold) t.Fatalf("expect 37s, got: %s", threshold)
@ -254,6 +258,9 @@ func TestAgent_ServerConfig(t *testing.T) {
conf.Ports.HTTP = 4646 conf.Ports.HTTP = 4646
conf.Ports.RPC = 4647 conf.Ports.RPC = 4647
conf.Ports.Serf = 4648 conf.Ports.Serf = 4648
if ok := conf.normalize(testlogger, dev); !ok {
t.Fatalf("failed to normalize config")
}
out, err = a.serverConfig() out, err = a.serverConfig()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -264,13 +271,13 @@ func TestAgent_ServerConfig(t *testing.T) {
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.3" { if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.3" {
t.Fatalf("expect 127.0.0.3, got: %s", addr) t.Fatalf("expect 127.0.0.3, got: %s", addr)
} }
if addr := a.serverHTTPAddr; addr != "127.0.0.3:4646" { if addr := conf.Addresses.HTTP; addr != "127.0.0.3:4646" {
t.Fatalf("expect 127.0.0.3:4646, got: %s", addr) t.Fatalf("expect 127.0.0.3:4646, got: %s", addr)
} }
if addr := a.serverRPCAddr; addr != "127.0.0.3:4647" { if addr := conf.Addresses.RPC; addr != "127.0.0.3:4647" {
t.Fatalf("expect 127.0.0.3:4647, got: %s", addr) t.Fatalf("expect 127.0.0.3:4647, got: %s", addr)
} }
if addr := a.serverSerfAddr; addr != "127.0.0.3:4648" { if addr := conf.Addresses.Serf; addr != "127.0.0.3:4648" {
t.Fatalf("expect 127.0.0.3:4648, got: %s", addr) t.Fatalf("expect 127.0.0.3:4648, got: %s", addr)
} }

View File

@ -772,7 +772,7 @@ func normalizeBind(addr *string, bind string, defport *int) error {
} }
// missing port, add default // missing port, add default
newaddr := fmt.Sprintf("%s:%d", *addr, defport) newaddr := fmt.Sprintf("%s:%d", *addr, *defport)
*addr = newaddr *addr = newaddr
return nil return nil
} }