2017-05-15 20:02:04 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-05-15 20:02:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkOptLegacySyntax(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected []NetworkAttachmentOpts
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "docknet1",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var network NetworkOpt
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, network.Set(tc.value))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
|
2017-05-15 20:02:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:33:55 -04:00
|
|
|
func TestNetworkOptAdvancedSyntax(t *testing.T) {
|
2017-05-15 20:02:04 -04:00
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected []NetworkAttachmentOpts
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "name=docknet1,alias=web,driver-opt=field1=value1",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{"web"},
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"field1": "value1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "name=docknet1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{"web1", "web2"},
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"field1": "value1",
|
|
|
|
"field2": "value2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-03 08:37:26 -04:00
|
|
|
{
|
|
|
|
value: "name=docknet1,ip=172.20.88.22,ip6=2001:db8::8822",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{},
|
|
|
|
IPv4Address: "172.20.88.22",
|
|
|
|
IPv6Address: "2001:db8::8822",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-15 20:02:04 -04:00
|
|
|
{
|
|
|
|
value: "name=docknet1",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-04-03 06:33:55 -04:00
|
|
|
t.Run(tc.value, func(t *testing.T) {
|
|
|
|
var network NetworkOpt
|
|
|
|
assert.NilError(t, network.Set(tc.value))
|
|
|
|
assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
|
|
|
|
})
|
2017-05-15 20:02:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:33:55 -04:00
|
|
|
func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
|
2017-05-15 20:02:04 -04:00
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "invalidField=docknet1",
|
|
|
|
expectedError: "invalid field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "network=docknet1,invalid=web",
|
|
|
|
expectedError: "invalid field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "driver-opt=field1=value1,driver-opt=field2=value2",
|
|
|
|
expectedError: "network name/id is not specified",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-04-03 06:33:55 -04:00
|
|
|
t.Run(tc.value, func(t *testing.T) {
|
|
|
|
var network NetworkOpt
|
|
|
|
assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
|
|
|
|
})
|
2017-05-15 20:02:04 -04:00
|
|
|
}
|
|
|
|
}
|