2024-11-11 14:32:15 +00:00
|
|
|
package daecommon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"isle/yamlutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2024-12-12 20:05:36 +00:00
|
|
|
func TestConfig_UnmarshalYAML(t *testing.T) {
|
2024-11-11 14:32:15 +00:00
|
|
|
tests := []struct {
|
2024-12-12 20:05:36 +00:00
|
|
|
label string
|
|
|
|
str string
|
|
|
|
wantConfig Config
|
|
|
|
wantErr string
|
2024-11-11 14:32:15 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-12-12 20:05:36 +00:00
|
|
|
label: "empty",
|
|
|
|
str: ``,
|
|
|
|
wantConfig: Config{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "DEPRECATED single global network",
|
|
|
|
str: `
|
2024-11-11 14:32:15 +00:00
|
|
|
{"dns":{"resolvers":["a"]}}
|
|
|
|
`,
|
2024-12-12 20:05:36 +00:00
|
|
|
wantConfig: Config{
|
2024-11-11 14:32:15 +00:00
|
|
|
Networks: map[string]NetworkConfig{
|
|
|
|
DeprecatedNetworkID: NewNetworkConfig(func(c *NetworkConfig) {
|
|
|
|
c.DNS.Resolvers = []string{"a"}
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-12-12 20:05:36 +00:00
|
|
|
label: "single network",
|
|
|
|
str: `
|
2024-11-11 14:32:15 +00:00
|
|
|
networks:
|
|
|
|
foo: {"dns":{"resolvers":["a"]}}
|
|
|
|
`,
|
2024-12-12 20:05:36 +00:00
|
|
|
wantConfig: Config{
|
2024-11-11 14:32:15 +00:00
|
|
|
Networks: map[string]NetworkConfig{
|
|
|
|
"foo": NewNetworkConfig(func(c *NetworkConfig) {
|
|
|
|
c.DNS.Resolvers = []string{"a"}
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-12-12 20:05:36 +00:00
|
|
|
label: "multiple networks",
|
|
|
|
str: `
|
2024-11-11 14:32:15 +00:00
|
|
|
networks:
|
|
|
|
foo: {"dns":{"resolvers":["a"]}}
|
|
|
|
bar: {}
|
|
|
|
`,
|
2024-12-12 20:05:36 +00:00
|
|
|
wantConfig: Config{
|
2024-11-11 14:32:15 +00:00
|
|
|
Networks: map[string]NetworkConfig{
|
|
|
|
"foo": NewNetworkConfig(func(c *NetworkConfig) {
|
|
|
|
c.DNS.Resolvers = []string{"a"}
|
|
|
|
}),
|
|
|
|
"bar": NewNetworkConfig(nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-12-12 20:05:36 +00:00
|
|
|
{
|
|
|
|
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"}}
|
|
|
|
`,
|
2024-12-14 14:57:07 +00:00
|
|
|
wantErr: `two networks with the same vpn.public_addr port: "bar" and "baz"`,
|
2024-12-12 20:05:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "err/invalid firewall",
|
|
|
|
str: `
|
|
|
|
networks:
|
|
|
|
foo: {"vpn":{"firewall":{"inbound":[{"host":"f","port":"no"}]}}}
|
|
|
|
`,
|
|
|
|
wantErr: "port was not a number",
|
|
|
|
},
|
2024-11-11 14:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-12-12 20:05:36 +00:00
|
|
|
if test.wantErr != "" {
|
|
|
|
assert.ErrorContains(t, err, test.wantErr)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.wantConfig, config)
|
|
|
|
}
|
2024-11-11 14:32:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|