2019-04-06 13:44:04 +00:00
|
|
|
package yaml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-04 12:23:33 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-14 19:16:03 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalNetwork(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
from string
|
|
|
|
want Network
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "{ name: foo, driver: bar }",
|
|
|
|
want: Network{
|
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "{ name: foo, driver: bar, driver_opts: { baz: qux } }",
|
|
|
|
want: Network{
|
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"baz": "qux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test.from)
|
|
|
|
got := Network{}
|
|
|
|
err := yaml.Unmarshal(in, &got)
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, test.want, got, "problem parsing network %q", test.from)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNetworks(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
from string
|
|
|
|
want []*Network
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "foo: { driver: bar }",
|
|
|
|
want: []*Network{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "foo: { name: baz }",
|
|
|
|
want: []*Network{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Driver: "bridge",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "foo: { name: baz, driver: bar }",
|
|
|
|
want: []*Network{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test.from)
|
|
|
|
got := Networks{}
|
|
|
|
err := yaml.Unmarshal(in, &got)
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, test.want, got.Networks, "problem parsing network %q", test.from)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNetworkErr(t *testing.T) {
|
|
|
|
testdata := []string{
|
|
|
|
"foo: { name: [ foo, bar] }",
|
|
|
|
"- foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test)
|
|
|
|
err := yaml.Unmarshal(in, new(Networks))
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.Error(t, err, "wanted error for networks %q", test)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|