add tests for go-sockaddr template parsing

This commit is contained in:
Frank Schroeder 2017-07-28 15:40:22 +02:00
parent 5ee498cbc5
commit f27202b608
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
2 changed files with 76 additions and 18 deletions

View File

@ -2148,6 +2148,9 @@ func (c *Config) ResolveTmplAddrs() (err error) {
*addr = ip *addr = ip
} }
if c == nil {
return
}
parse(&c.Addresses.DNS, "DNS address") parse(&c.Addresses.DNS, "DNS address")
parse(&c.Addresses.HTTP, "HTTP address") parse(&c.Addresses.HTTP, "HTTP address")
parse(&c.Addresses.HTTPS, "HTTPS address") parse(&c.Addresses.HTTPS, "HTTPS address")

View File

@ -103,28 +103,56 @@ func TestDecodeConfig(t *testing.T) {
c: &Config{ACLTTL: 2 * time.Second, ACLTTLRaw: "2s"}, c: &Config{ACLTTL: 2 * time.Second, ACLTTLRaw: "2s"},
}, },
{ {
in: `{"addresses":{"dns":"a"}}`, in: `{"addresses":{"dns":"1.2.3.4"}}`,
c: &Config{Addresses: AddressConfig{DNS: "a"}}, c: &Config{Addresses: AddressConfig{DNS: "1.2.3.4"}},
}, },
{ {
in: `{"addresses":{"http":"a"}}`, in: `{"addresses":{"dns":"{{\"1.2.3.4\"}}"}}`,
c: &Config{Addresses: AddressConfig{HTTP: "a"}}, c: &Config{Addresses: AddressConfig{DNS: "1.2.3.4"}},
}, },
{ {
in: `{"addresses":{"https":"a"}}`, in: `{"addresses":{"http":"1.2.3.4"}}`,
c: &Config{Addresses: AddressConfig{HTTPS: "a"}}, c: &Config{Addresses: AddressConfig{HTTP: "1.2.3.4"}},
},
{
in: `{"addresses":{"http":"unix:///var/foo/bar"}}`,
c: &Config{Addresses: AddressConfig{HTTP: "unix:///var/foo/bar"}},
},
{
in: `{"addresses":{"http":"{{\"1.2.3.4\"}}"}}`,
c: &Config{Addresses: AddressConfig{HTTP: "1.2.3.4"}},
},
{
in: `{"addresses":{"https":"1.2.3.4"}}`,
c: &Config{Addresses: AddressConfig{HTTPS: "1.2.3.4"}},
},
{
in: `{"addresses":{"https":"unix:///var/foo/bar"}}`,
c: &Config{Addresses: AddressConfig{HTTPS: "unix:///var/foo/bar"}},
},
{
in: `{"addresses":{"https":"{{\"1.2.3.4\"}}"}}`,
c: &Config{Addresses: AddressConfig{HTTPS: "1.2.3.4"}},
}, },
{ {
in: `{"addresses":{"rpc":"a"}}`, in: `{"addresses":{"rpc":"a"}}`,
c: &Config{Addresses: AddressConfig{RPC: "a"}}, c: &Config{Addresses: AddressConfig{RPC: "a"}},
}, },
{ {
in: `{"advertise_addr":"a"}`, in: `{"advertise_addr":"1.2.3.4"}`,
c: &Config{AdvertiseAddr: "a"}, c: &Config{AdvertiseAddr: "1.2.3.4"},
}, },
{ {
in: `{"advertise_addr_wan":"a"}`, in: `{"advertise_addr":"{{\"1.2.3.4\"}}"}`,
c: &Config{AdvertiseAddrWan: "a"}, c: &Config{AdvertiseAddr: "1.2.3.4"},
},
{
in: `{"advertise_addr_wan":"1.2.3.4"}`,
c: &Config{AdvertiseAddrWan: "1.2.3.4"},
},
{
in: `{"advertise_addr_wan":"{{\"1.2.3.4\"}}"}`,
c: &Config{AdvertiseAddrWan: "1.2.3.4"},
}, },
{ {
in: `{"advertise_addrs":{"rpc":"1.2.3.4:5678"}}`, in: `{"advertise_addrs":{"rpc":"1.2.3.4:5678"}}`,
@ -202,8 +230,12 @@ func TestDecodeConfig(t *testing.T) {
c: &Config{Autopilot: Autopilot{CleanupDeadServers: Bool(true)}}, c: &Config{Autopilot: Autopilot{CleanupDeadServers: Bool(true)}},
}, },
{ {
in: `{"bind_addr":"a"}`, in: `{"bind_addr":"1.2.3.4"}`,
c: &Config{BindAddr: "a"}, c: &Config{BindAddr: "1.2.3.4"},
},
{
in: `{"bind_addr":"{{\"1.2.3.4\"}}"}`,
c: &Config{BindAddr: "1.2.3.4"},
}, },
{ {
in: `{"bootstrap":true}`, in: `{"bootstrap":true}`,
@ -230,8 +262,12 @@ func TestDecodeConfig(t *testing.T) {
c: &Config{CertFile: "a"}, c: &Config{CertFile: "a"},
}, },
{ {
in: `{"client_addr":"a"}`, in: `{"client_addr":"1.2.3.4"}`,
c: &Config{ClientAddr: "a"}, c: &Config{ClientAddr: "1.2.3.4"},
},
{
in: `{"client_addr":"{{\"1.2.3.4\"}}"}`,
c: &Config{ClientAddr: "1.2.3.4"},
}, },
{ {
in: `{"data_dir":"a"}`, in: `{"data_dir":"a"}`,
@ -531,12 +567,28 @@ func TestDecodeConfig(t *testing.T) {
c: &Config{RetryMaxAttemptsWan: 123}, c: &Config{RetryMaxAttemptsWan: 123},
}, },
{ {
in: `{"serf_lan_bind":"a"}`, in: `{"serf_lan_bind":"1.2.3.4"}`,
c: &Config{SerfLanBindAddr: "a"}, c: &Config{SerfLanBindAddr: "1.2.3.4"},
}, },
{ {
in: `{"serf_wan_bind":"a"}`, in: `{"serf_lan_bind":"unix:///var/foo/bar"}`,
c: &Config{SerfWanBindAddr: "a"}, c: &Config{SerfLanBindAddr: "unix:///var/foo/bar"},
},
{
in: `{"serf_lan_bind":"{{\"1.2.3.4\"}}"}`,
c: &Config{SerfLanBindAddr: "1.2.3.4"},
},
{
in: `{"serf_wan_bind":"1.2.3.4"}`,
c: &Config{SerfWanBindAddr: "1.2.3.4"},
},
{
in: `{"serf_wan_bind":"unix:///var/foo/bar"}`,
c: &Config{SerfWanBindAddr: "unix:///var/foo/bar"},
},
{
in: `{"serf_wan_bind":"{{\"1.2.3.4\"}}"}`,
c: &Config{SerfWanBindAddr: "1.2.3.4"},
}, },
{ {
in: `{"server":true}`, in: `{"server":true}`,
@ -1165,6 +1217,9 @@ func TestDecodeConfig(t *testing.T) {
if got, want := err, tt.err; !reflect.DeepEqual(got, want) { if got, want := err, tt.err; !reflect.DeepEqual(got, want) {
t.Fatalf("got error %v want %v", got, want) t.Fatalf("got error %v want %v", got, want)
} }
if err := c.ResolveTmplAddrs(); err != nil {
t.Fatalf("got error %v on ResolveTmplAddrs", err)
}
got, want := c, tt.c got, want := c, tt.c
verify.Values(t, "", got, want) verify.Values(t, "", got, want)
}) })