isle/go/daemon/daecommon/config_test.go

100 lines
2.1 KiB
Go
Raw Normal View History

package daecommon
import (
"isle/yamlutil"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestConfig_UnmarshalYAML(t *testing.T) {
tests := []struct {
label string
str string
wantConfig Config
wantErr string
}{
{
label: "empty",
str: ``,
wantConfig: Config{},
},
{
label: "DEPRECATED single global network",
str: `
{"dns":{"resolvers":["a"]}}
`,
wantConfig: Config{
Networks: map[string]NetworkConfig{
DeprecatedNetworkID: NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"}
}),
},
},
},
{
label: "single network",
str: `
networks:
foo: {"dns":{"resolvers":["a"]}}
`,
wantConfig: Config{
Networks: map[string]NetworkConfig{
"foo": NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"}
}),
},
},
},
{
label: "multiple networks",
str: `
networks:
foo: {"dns":{"resolvers":["a"]}}
bar: {}
`,
wantConfig: Config{
Networks: map[string]NetworkConfig{
"foo": NewNetworkConfig(func(c *NetworkConfig) {
c.DNS.Resolvers = []string{"a"}
}),
"bar": NewNetworkConfig(nil),
},
},
},
{
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 {
t.Run(test.label, func(t *testing.T) {
test.str = yamlutil.ReplacePrefixTabs(test.str)
var config Config
err := yaml.Unmarshal([]byte(test.str), &config)
if test.wantErr != "" {
assert.ErrorContains(t, err, test.wantErr)
} else {
assert.NoError(t, err)
assert.Equal(t, test.wantConfig, config)
}
})
}
}