1 package opts
2
3 import (
4 "testing"
5
6 "gotest.tools/v3/assert"
7 is "gotest.tools/v3/assert/cmp"
8 )
9
10 func TestNetworkOptLegacySyntax(t *testing.T) {
11 testCases := []struct {
12 value string
13 expected []NetworkAttachmentOpts
14 }{
15 {
16 value: "docknet1",
17 expected: []NetworkAttachmentOpts{
18 {
19 Target: "docknet1",
20 },
21 },
22 },
23 }
24 for _, tc := range testCases {
25 var network NetworkOpt
26 assert.NilError(t, network.Set(tc.value))
27 assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
28 }
29 }
30
31 func TestNetworkOptAdvancedSyntax(t *testing.T) {
32 testCases := []struct {
33 value string
34 expected []NetworkAttachmentOpts
35 }{
36 {
37 value: "name=docknet1,alias=web,driver-opt=field1=value1",
38 expected: []NetworkAttachmentOpts{
39 {
40 Target: "docknet1",
41 Aliases: []string{"web"},
42 DriverOpts: map[string]string{
43 "field1": "value1",
44 },
45 },
46 },
47 },
48 {
49 value: "name=docknet1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2",
50 expected: []NetworkAttachmentOpts{
51 {
52 Target: "docknet1",
53 Aliases: []string{"web1", "web2"},
54 DriverOpts: map[string]string{
55 "field1": "value1",
56 "field2": "value2",
57 },
58 },
59 },
60 },
61 {
62 value: "name=docknet1,ip=172.20.88.22,ip6=2001:db8::8822",
63 expected: []NetworkAttachmentOpts{
64 {
65 Target: "docknet1",
66 Aliases: []string{},
67 IPv4Address: "172.20.88.22",
68 IPv6Address: "2001:db8::8822",
69 },
70 },
71 },
72 {
73 value: "name=docknet1",
74 expected: []NetworkAttachmentOpts{
75 {
76 Target: "docknet1",
77 Aliases: []string{},
78 },
79 },
80 },
81 {
82 value: "name=docknet1,mac-address=52:0f:f3:dc:50:10",
83 expected: []NetworkAttachmentOpts{
84 {
85 Target: "docknet1",
86 Aliases: []string{},
87 MacAddress: "52:0f:f3:dc:50:10",
88 },
89 },
90 },
91 {
92 value: "name=docknet1,link-local-ip=169.254.169.254,link-local-ip=169.254.10.10",
93 expected: []NetworkAttachmentOpts{
94 {
95 Target: "docknet1",
96 Aliases: []string{},
97 LinkLocalIPs: []string{"169.254.169.254", "169.254.10.10"},
98 },
99 },
100 },
101 }
102 for _, tc := range testCases {
103 tc := tc
104 t.Run(tc.value, func(t *testing.T) {
105 var network NetworkOpt
106 assert.NilError(t, network.Set(tc.value))
107 assert.Check(t, is.DeepEqual(tc.expected, network.Value()))
108 })
109 }
110 }
111
112 func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
113 testCases := []struct {
114 value string
115 expectedError string
116 }{
117 {
118 value: "invalidField=docknet1",
119 expectedError: "invalid field",
120 },
121 {
122 value: "network=docknet1,invalid=web",
123 expectedError: "invalid field",
124 },
125 {
126 value: "driver-opt=field1=value1,driver-opt=field2=value2",
127 expectedError: "network name/id is not specified",
128 },
129 }
130 for _, tc := range testCases {
131 tc := tc
132 t.Run(tc.value, func(t *testing.T) {
133 var network NetworkOpt
134 assert.ErrorContains(t, network.Set(tc.value), tc.expectedError)
135 })
136 }
137 }
138
View as plain text