1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package opts
import (
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestNetworkOptLegacySyntax(t *testing.T) {
testCases := []struct {
value string
expected []NetworkAttachmentOpts
}{
{
value: "docknet1",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
},
},
},
}
for _, tc := range testCases {
var network NetworkOpt
assert.NilError(t, network.Set(tc.value))
assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
}
}
func TestNetworkOptAdvancedSyntax(t *testing.T) {
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",
},
},
},
},
{
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",
},
},
},
{
value: "name=docknet1",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{},
},
},
},
{
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"},
},
},
},
{
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",
},
},
},
},
{
value: "name=docknet1,gw-priority=10",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{},
GwPriority: 10,
},
},
},
}
for _, tc := range testCases {
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()))
})
}
}
func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
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",
},
{
value: "gw-priority=invalid-integer",
expectedError: "invalid gw-priority (invalid-integer): invalid syntax",
},
}
for _, tc := range testCases {
t.Run(tc.value, func(t *testing.T) {
var network NetworkOpt
assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
})
}
}
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))
}
|