2017-05-15 20:02:04 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/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{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-07-13 05:50:01 -04:00
|
|
|
{
|
|
|
|
value: "name=docknet1,mac-address=52:0f:f3:dc:50:10",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{},
|
|
|
|
MacAddress: "52:0f:f3:dc:50:10",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "name=docknet1,link-local-ip=169.254.169.254,link-local-ip=169.254.10.10",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{},
|
|
|
|
LinkLocalIPs: []string{"169.254.169.254", "169.254.10.10"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-08 10:16:40 -04:00
|
|
|
{
|
|
|
|
value: "name=docknet1,\"driver-opt=com.docker.network.endpoint.sysctls=net.ipv6.conf.IFNAME.accept_ra=2,net.ipv6.conf.IFNAME.forwarding=1\"",
|
|
|
|
expected: []NetworkAttachmentOpts{
|
|
|
|
{
|
|
|
|
Target: "docknet1",
|
|
|
|
Aliases: []string{},
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
// The CLI converts IFNAME to ifname - it probably shouldn't, but the API
|
|
|
|
// allows ifname to cater for this.
|
|
|
|
"com.docker.network.endpoint.sysctls": "net.ipv6.conf.ifname.accept_ra=2,net.ipv6.conf.ifname.forwarding=1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-15 20:02:04 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-10-29 09:08:12 -04:00
|
|
|
tc := tc
|
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-10-29 09:08:12 -04:00
|
|
|
tc := tc
|
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
|
|
|
}
|
|
|
|
}
|
2024-02-20 04:46:05 -05:00
|
|
|
|
|
|
|
func TestNetworkOptStringNetOptString(t *testing.T) {
|
|
|
|
networkOpt := &NetworkOpt{}
|
|
|
|
result := networkOpt.String()
|
|
|
|
assert.Check(t, is.Equal("", result))
|
|
|
|
if result != "" {
|
|
|
|
t.Errorf("Expected an empty string, got %s", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkOptTypeNetOptType(t *testing.T) {
|
|
|
|
networkOpt := &NetworkOpt{}
|
|
|
|
result := networkOpt.Type()
|
|
|
|
assert.Check(t, is.Equal("network", result))
|
|
|
|
}
|