Modify ResolveTmplAddrs to parse advertise IPs, added test cases that fail to parse correctly

This commit is contained in:
Preetha Appan 2017-07-28 15:01:32 -05:00
parent 4b82d09df0
commit ac068de336
2 changed files with 37 additions and 27 deletions

View File

@ -2127,7 +2127,7 @@ func ReadConfigPaths(paths []string) (*Config, error) {
// and performs go-sockaddr/template Parse on each known address in case the
// user specified a template config for any of their values.
func (c *Config) ResolveTmplAddrs() (err error) {
parse := func(addr *string, name string) {
parse := func(addr *string, validateIP bool, name string) {
if *addr == "" || err != nil {
return
}
@ -2137,11 +2137,8 @@ func (c *Config) ResolveTmplAddrs() (err error) {
err = fmt.Errorf("Resolution of %s failed: %v", name, err)
return
}
if strings.HasPrefix(ip, "unix://") {
*addr = ip
return
}
if net.ParseIP(ip) == nil {
if validateIP && net.ParseIP(ip) == nil {
err = fmt.Errorf("Failed to parse %s: %v", name, ip)
return
}
@ -2151,15 +2148,15 @@ func (c *Config) ResolveTmplAddrs() (err error) {
if c == nil {
return
}
parse(&c.Addresses.DNS, "DNS address")
parse(&c.Addresses.HTTP, "HTTP address")
parse(&c.Addresses.HTTPS, "HTTPS address")
parse(&c.AdvertiseAddr, "Advertise address")
parse(&c.AdvertiseAddrWan, "Advertise WAN address")
parse(&c.BindAddr, "Bind address")
parse(&c.ClientAddr, "Client address")
parse(&c.SerfLanBindAddr, "Serf LAN address")
parse(&c.SerfWanBindAddr, "Serf WAN address")
parse(&c.Addresses.DNS, false, "DNS address")
parse(&c.Addresses.HTTP, false, "HTTP address")
parse(&c.Addresses.HTTPS, false, "HTTPS address")
parse(&c.AdvertiseAddr, true, "Advertise address")
parse(&c.AdvertiseAddrWan, true, "Advertise WAN address")
parse(&c.BindAddr, false, "Bind address")
parse(&c.ClientAddr, false, "Client address")
parse(&c.SerfLanBindAddr, true, "Serf LAN address")
parse(&c.SerfWanBindAddr, true, "Serf WAN address")
return
}

View File

@ -50,18 +50,28 @@ func TestConfigEncryptBytes(t *testing.T) {
func TestDecodeConfig(t *testing.T) {
tests := []struct {
desc string
in string
c *Config
err error
desc string
in string
c *Config
err error
parseTemplateErr error
}{
// special flows
{
in: `{"bad": "no way jose"}`,
err: errors.New("Config has invalid keys: bad"),
},
// happy flows in alphabeical order
{
in: `{"advertise_addr":"unix:///path/to/file"}`,
parseTemplateErr: errors.New("Failed to parse Advertise address: unix:///path/to/file"),
c: &Config{AdvertiseAddr: "unix:///path/to/file"},
},
{
in: `{"advertise_addr_wan":"unix:///path/to/file"}`,
parseTemplateErr: errors.New("Failed to parse Advertise WAN address: unix:///path/to/file"),
c: &Config{AdvertiseAddrWan: "unix:///path/to/file"},
},
// happy flows in alphabetical order
{
in: `{"acl_agent_master_token":"a"}`,
c: &Config{ACLAgentMasterToken: "a"},
@ -571,8 +581,9 @@ func TestDecodeConfig(t *testing.T) {
c: &Config{SerfLanBindAddr: "1.2.3.4"},
},
{
in: `{"serf_lan_bind":"unix:///var/foo/bar"}`,
c: &Config{SerfLanBindAddr: "unix:///var/foo/bar"},
in: `{"serf_lan_bind":"unix:///var/foo/bar"}`,
c: &Config{SerfLanBindAddr: "unix:///var/foo/bar"},
parseTemplateErr: errors.New("Failed to parse Serf LAN address: unix:///var/foo/bar"),
},
{
in: `{"serf_lan_bind":"{{\"1.2.3.4\"}}"}`,
@ -583,8 +594,9 @@ func TestDecodeConfig(t *testing.T) {
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":"unix:///var/foo/bar"}`,
c: &Config{SerfWanBindAddr: "unix:///var/foo/bar"},
parseTemplateErr: errors.New("Failed to parse Serf WAN address: unix:///var/foo/bar"),
},
{
in: `{"serf_wan_bind":"{{\"1.2.3.4\"}}"}`,
@ -1217,8 +1229,9 @@ func TestDecodeConfig(t *testing.T) {
if got, want := err, tt.err; !reflect.DeepEqual(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)
err = c.ResolveTmplAddrs()
if got, want := err, tt.parseTemplateErr; !reflect.DeepEqual(got, want) {
t.Fatalf("got error %v on ResolveTmplAddrs, expected %v", err, want)
}
got, want := c, tt.c
verify.Values(t, "", got, want)