Test daemon config validation, fix a bug which came out of it

This commit is contained in:
Brian Picciano 2024-12-12 21:05:36 +01:00
parent b4a58d1508
commit c21b3e0c33
2 changed files with 57 additions and 32 deletions

View File

@ -203,10 +203,7 @@ func (c Config) Validate() error {
nebulaPorts := map[string]string{} nebulaPorts := map[string]string{}
for id, network := range c.Networks { for id, network := range c.Networks {
if network.VPN.PublicAddr == "" { if network.VPN.PublicAddr != "" {
continue
}
_, port, err := net.SplitHostPort(network.VPN.PublicAddr) _, port, err := net.SplitHostPort(network.VPN.PublicAddr)
if err != nil { if err != nil {
return fmt.Errorf( return fmt.Errorf(
@ -214,13 +211,14 @@ func (c Config) Validate() error {
) )
} else if otherID, ok := nebulaPorts[port]; ok { } else if otherID, ok := nebulaPorts[port]; ok {
return fmt.Errorf( return fmt.Errorf(
"two networks with the same vpn.public_addr: %q and %q", "two networks with the same vpn.public_addr port: %q and %q",
id, id,
otherID, otherID,
) )
} }
nebulaPorts[port] = id nebulaPorts[port] = id
}
if err := network.Validate(); err != nil { if err := network.Validate(); err != nil {
return fmt.Errorf("invalid config for network %q: %w", id, err) return fmt.Errorf("invalid config for network %q: %w", id, err)

View File

@ -8,19 +8,24 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func TestConfig_UnmarshalYAML(t *testing.T) { // TODO test validation func TestConfig_UnmarshalYAML(t *testing.T) {
tests := []struct { tests := []struct {
label string label string
str string str string
config Config wantConfig Config
wantErr string
}{ }{
{"empty", ``, Config{}},
{ {
"DEPRECATED single global network", label: "empty",
` str: ``,
wantConfig: Config{},
},
{
label: "DEPRECATED single global network",
str: `
{"dns":{"resolvers":["a"]}} {"dns":{"resolvers":["a"]}}
`, `,
Config{ wantConfig: Config{
Networks: map[string]NetworkConfig{ Networks: map[string]NetworkConfig{
DeprecatedNetworkID: NewNetworkConfig(func(c *NetworkConfig) { DeprecatedNetworkID: NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"} c.DNS.Resolvers = []string{"a"}
@ -29,12 +34,12 @@ func TestConfig_UnmarshalYAML(t *testing.T) { // TODO test validation
}, },
}, },
{ {
"single network", label: "single network",
` str: `
networks: networks:
foo: {"dns":{"resolvers":["a"]}} foo: {"dns":{"resolvers":["a"]}}
`, `,
Config{ wantConfig: Config{
Networks: map[string]NetworkConfig{ Networks: map[string]NetworkConfig{
"foo": NewNetworkConfig(func(c *NetworkConfig) { "foo": NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"} c.DNS.Resolvers = []string{"a"}
@ -43,13 +48,13 @@ func TestConfig_UnmarshalYAML(t *testing.T) { // TODO test validation
}, },
}, },
{ {
"multiple networks", label: "multiple networks",
` str: `
networks: networks:
foo: {"dns":{"resolvers":["a"]}} foo: {"dns":{"resolvers":["a"]}}
bar: {} bar: {}
`, `,
Config{ wantConfig: Config{
Networks: map[string]NetworkConfig{ Networks: map[string]NetworkConfig{
"foo": NewNetworkConfig(func(c *NetworkConfig) { "foo": NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"} c.DNS.Resolvers = []string{"a"}
@ -58,6 +63,24 @@ func TestConfig_UnmarshalYAML(t *testing.T) { // TODO test validation
}, },
}, },
}, },
{
label: "err/shared vpn.public_addr port",
str: `
networks:
foo: {"vpn":{"public_addr":"1.1.1.1:4001"}}
bar: {"vpn":{"public_addr":"1.1.1.1:4000"}}
baz: {"vpn":{"public_addr":"2.2.2.2:4000"}}
`,
wantErr: `two networks with the same vpn.public_addr port: "baz" and "bar"`,
},
{
label: "err/invalid firewall",
str: `
networks:
foo: {"vpn":{"firewall":{"inbound":[{"host":"f","port":"no"}]}}}
`,
wantErr: "port was not a number",
},
} }
for _, test := range tests { for _, test := range tests {
@ -65,8 +88,12 @@ func TestConfig_UnmarshalYAML(t *testing.T) { // TODO test validation
test.str = yamlutil.ReplacePrefixTabs(test.str) test.str = yamlutil.ReplacePrefixTabs(test.str)
var config Config var config Config
err := yaml.Unmarshal([]byte(test.str), &config) err := yaml.Unmarshal([]byte(test.str), &config)
if test.wantErr != "" {
assert.ErrorContains(t, err, test.wantErr)
} else {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, test.config, config) assert.Equal(t, test.wantConfig, config)
}
}) })
} }
} }