forked from containers/netavark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path300-macvlan.bats
391 lines (334 loc) · 12.9 KB
/
300-macvlan.bats
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env bats -*- bats -*-
#
# macvlan driver test
#
load helpers
function setup() {
basic_setup
# create a extra interface which we can use to connect the macvlan to
run_in_host_netns ip link add dummy0 type dummy
}
@test "simple macvlan setup" {
run_netavark --file ${TESTSDIR}/testfiles/macvlan.json setup $(get_container_netns_path)
result="$output"
mac=$(jq -r '.podman.interfaces.eth0.mac_address' <<< "$result" )
# check that interface exists
run_in_container_netns ip -j --details link show eth0
link_info="$output"
assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device"
ipaddr="10.88.0.2/16"
run_in_container_netns ip addr show eth0
assert "$output" "=~" "$ipaddr" "IP address matches container address"
assert_json "$result" ".podman.interfaces.eth0.subnets[0].ipnet" "==" "$ipaddr" "Result contains correct IP address"
# check gateway assignment
run_in_container_netns ip r
assert "$output" "=~" "default via 10.88.0.1" "gateway must be there in default route"
assert_json "$result" ".podman.interfaces.eth0.subnets[0].gateway" == "10.88.0.1" "Result contains gateway address"
run_in_container_netns cat /proc/sys/net/ipv6/conf/eth0/autoconf
assert "0" "autoconf is disabled"
run_netavark --file ${TESTSDIR}/testfiles/macvlan.json teardown $(get_container_netns_path)
assert "" "no errors"
}
@test "macvlan setup with static routes" {
# add second interface and routes through that interface to test proper teardown
run_in_container_netns ip link add type dummy
run_in_container_netns ip a add 10.91.0.10/24 dev dummy0
run_in_container_netns ip link set dummy0 up
run_netavark --file ${TESTSDIR}/testfiles/macvlan-staticroutes.json setup $(get_container_netns_path)
# check static routes
run_in_container_netns ip r
assert "$output" "=~" "10.89.0.0/24 via 10.88.0.2" "static route not set"
assert "$output" "=~" "10.90.0.0/24 via 10.88.0.3" "static route not set"
assert "$output" "=~" "10.92.0.0/24 via 10.91.0.1" "static route not set"
run_in_container_netns ip -6 r
assert "$output" "=~" "fd:2f2f::/64 via fd:1f1f::20" "static route not set"
run_netavark --file ${TESTSDIR}/testfiles/macvlan-staticroutes.json teardown $(get_container_netns_path)
assert "" "no errors"
# check static routes get removed
run_in_container_netns ip r
assert "$output" "!~" "10.89.0.0/24 via 10.88.0.2" "static route not removed"
assert "$output" "!~" "10.90.0.0/24 via 10.88.0.3" "static route not removed"
assert "$output" "!~" "10.92.0.0/24 via 10.91.0.1" "static route not removed"
run_in_container_netns ip -6 r
assert "$output" "!~" "fd:2f2f::/64 via fd:1f1f::20" "static route not removed"
run_in_container_netns ip link delete dummy0
}
@test "macvlan setup no default route" {
run_netavark --file ${TESTSDIR}/testfiles/macvlan-nodefaultroute.json setup $(get_container_netns_path)
run_in_container_netns ip r
assert "$output" "!~" "default" "default route exists"
run_in_container_netns ip -6 r
assert "$output" "!~" "default" "default route exists"
run_netavark --file ${TESTSDIR}/testfiles/macvlan-nodefaultroute.json teardown $(get_container_netns_path)
assert "" "no errors"
}
@test "macvlan setup internal" {
run_netavark --file ${TESTSDIR}/testfiles/macvlan-internal.json setup $(get_container_netns_path)
result="$output"
mac=$(jq -r '.podman.interfaces.eth0.mac_address' <<< "$result" )
# check that interface exists
run_in_container_netns ip -j --details link show eth0
link_info="$output"
assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device"
ipaddr="10.88.0.2/16"
run_in_container_netns ip addr show eth0
assert "$output" "=~" "$ipaddr" "IP address matches container address"
assert_json "$result" ".podman.interfaces.eth0.subnets[0].ipnet" "==" "$ipaddr" "Result contains correct IP address"
# internal macvlan must not contain
run_in_container_netns ip r
assert "$output" !~ 'default' "macvlan must not contain default gateway in route at all"
}
@test "macvlan setup with mtu" {
run_netavark --file ${TESTSDIR}/testfiles/macvlan-mtu.json setup $(get_container_netns_path)
result="$output"
mac=$(jq -r '.podman.interfaces.eth0.mac_address' <<< "$result" )
# check that interface exists
run_in_container_netns ip -j --details link show eth0
link_info="$output"
assert_json "$link_info" ".[].mtu" "==" "1400" "MTU matches configured MTU"
assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device"
ipaddr="10.88.0.2"
run_in_container_netns ip -j addr show eth0
link_info="$output"
assert_json "$link_info" ".[].addr_info[0].local" "==" "$ipaddr" "IP address matches container address"
assert_json "$link_info" ".[].addr_info[0].prefixlen" "==" "16" "IP prefix matches container subnet"
assert_json "$result" ".podman.interfaces.eth0.subnets[0].ipnet" "==" "$ipaddr/16" "Result contains correct IP address"
}
@test "macvlan modes" {
for mode in bridge private vepa passthru source; do
# echo here so we know which test failed
echo "mode $mode"
read -r -d '\0' config <<EOF
{
"container_id": "someID",
"container_name": "someName",
"networks": {
"podman": {
"static_ips": [
"10.88.0.2"
],
"interface_name": "eth0"
}
},
"network_info": {
"podman": {
"name": "podman",
"id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9",
"driver": "macvlan",
"network_interface": "dummy0",
"subnets": [
{
"subnet": "10.88.0.0/16",
"gateway": "10.88.0.1"
}
],
"ipv6_enabled": false,
"internal": false,
"dns_enabled": false,
"ipam_options": {
"driver": "host-local"
},
"options": {
"mode": "$mode"
}
}
}
}\0
EOF
run_netavark setup $(get_container_netns_path) <<<"$config"
run_in_container_netns ip -j --details link show eth0
link_info="$output"
assert_json "$link_info" ".[].mtu" "==" "1500" "MTU matches expected MTU"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
assert_json "$link_info" ".[].linkinfo.info_kind" "==" "macvlan" "Container interface is a macvlan device"
assert_json "$link_info" ".[].linkinfo.info_data.mode" "==" "$mode" "Container interface has correct macvlan mode"
run_netavark teardown $(get_container_netns_path) <<<"$config"
done
}
@test "macvlan ipam none" {
read -r -d '\0' config <<EOF
{
"container_id": "someID",
"container_name": "someName",
"networks": {
"podman": {
"interface_name": "eth0"
}
},
"network_info": {
"podman": {
"name": "podman",
"id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9",
"driver": "macvlan",
"network_interface": "dummy0",
"subnets": [],
"ipv6_enabled": false,
"internal": false,
"dns_enabled": false,
"ipam_options": {
"driver": "none"
}
}
}
}\0
EOF
run_netavark setup $(get_container_netns_path) <<<"$config"
result="$output"
mac=$(jq -r '.podman.interfaces.eth0.mac_address' <<< "$result" )
# check that interface exists
run_in_container_netns ip -j link show eth0
link_info="$output"
assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
run_in_container_netns ip -j --details addr show eth0
assert_json "$link_info" ".[].addr_info" "==" "null" "No ip addresses configured"
# check gateway assignment
run_in_container_netns ip r
assert "$output" "==" "" "No routes configured"
}
@test "macvlan static mac" {
mac="aa:bb:cc:dd:ee:ff"
read -r -d '\0' config <<EOF
{
"container_id": "someID",
"container_name": "someName",
"networks": {
"podman": {
"static_ips": [
"10.88.0.2"
],
"static_mac": "$mac",
"interface_name": "eth0"
}
},
"network_info": {
"podman": {
"name": "podman",
"id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9",
"driver": "macvlan",
"network_interface": "dummy0",
"subnets": [
{
"subnet": "10.88.0.0/16",
"gateway": "10.88.0.1"
}
],
"ipv6_enabled": false,
"internal": false,
"dns_enabled": false,
"ipam_options": {
"driver": "host-local"
}
}
}
}\0
EOF
run_netavark setup $(get_container_netns_path) <<<"$config"
result="$output"
assert_json "$result" ".podman.interfaces.eth0.mac_address" == "$mac" "MAC matches input mac"
# check that interface exists
run_in_container_netns ip -j link show eth0
link_info="$output"
assert_json "$link_info" ".[].address" "==" "$mac" "MAC matches container mac"
assert_json "$link_info" '.[].flags[] | select(.=="UP")' "==" "UP" "Container interface is up"
}
@test "macvlan same interface name on host" {
read -r -d '\0' config <<EOF
{
"container_id": "someID",
"container_name": "someName",
"networks": {
"podman": {
"static_ips": [
"10.88.0.2"
],
"interface_name": "eth0"
}
},
"network_info": {
"podman": {
"name": "podman",
"id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9",
"driver": "macvlan",
"network_interface": "eth0",
"subnets": [
{
"subnet": "10.88.0.0/16",
"gateway": "10.88.0.1"
}
],
"ipv6_enabled": false,
"internal": false,
"dns_enabled": false,
"ipam_options": {
"driver": "host-local"
}
}
}
}\0
EOF
run_in_host_netns ip link add eth0 type dummy
run_netavark setup $(get_container_netns_path) <<<"$config"
run_in_container_netns ip link show eth0
run_netavark teardown $(get_container_netns_path) <<<"$config"
}
@test "macvlan same interface name on container" {
read -r -d '\0' config <<EOF
{
"container_id": "someID",
"container_name": "someName",
"networks": {
"podman": {
"static_ips": [
"10.88.0.2"
],
"interface_name": "eth0"
}
},
"network_info": {
"podman": {
"name": "podman",
"id": "2f259bab93aaaaa2542ba43ef33eb990d0999ee1b9924b557b7be53c0b7a1bb9",
"driver": "macvlan",
"network_interface": "dummy0",
"subnets": [
{
"subnet": "10.88.0.0/16",
"gateway": "10.88.0.1"
}
],
"ipv6_enabled": false,
"internal": false,
"dns_enabled": false,
"ipam_options": {
"driver": "host-local"
}
}
}
}\0
EOF
run_in_container_netns ip link add eth0 type dummy
expected_rc=1 run_netavark setup $(get_container_netns_path) <<<"$config"
# make sure the tmp interface is not leaked on the host or netns
run_in_host_netns ip -o link show
assert "${#lines[@]}" == 2 "only two interfaces (lo, dummy0) on the host, the tmp macvlan interface should be gone"
run_in_container_netns ip -o link show
assert "${#lines[@]}" == 2 "only two interfaces (lo, eth0) in the netns, the tmp macvlan interface should be gone"
}
@test "macvlan route metric from config" {
run_netavark --file ${TESTSDIR}/testfiles/metric-macvlan.json setup $(get_container_netns_path)
run_in_container_netns ip -j route list match 0.0.0.0
default_route="$output"
assert_json "$default_route" '.[0].dst' == "default" "Default route was selected"
assert_json "$default_route" '.[0].metric' == "200" "Route metric set from config"
run_in_container_netns ip -j -6 route list match ::0
default_route_v6="$output"
assert_json "$default_route_v6" '.[0].dst' == "default" "Default route was selected"
assert_json "$default_route_v6" '.[0].metric' == "200" "v6 route metric matches v4"
}