forked from containers/netavark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path400-ipvlan.bats
325 lines (277 loc) · 10.9 KB
/
400-ipvlan.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
#!/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 ipvlan to
run_in_host_netns ip link add dummy0 type dummy
}
@test "simple ipvlan setup" {
run_netavark --file ${TESTSDIR}/testfiles/ipvlan.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" "==" "ipvlan" "Container interface is a ipvlan 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/ipvlan.json teardown $(get_container_netns_path)
assert "" "no errors"
}
@test "ipvlan 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/ipvlan-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/ipvlan-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 "ipvlan setup no default route" {
run_netavark --file ${TESTSDIR}/testfiles/ipvlan-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/ipvlan-nodefaultroute.json teardown $(get_container_netns_path)
assert "" "no errors"
}
@test "ipvlan setup internal" {
run_netavark --file ${TESTSDIR}/testfiles/ipvlan-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" "==" "ipvlan" "Container interface is a ipvlan 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 ipvlan must not contain
run_in_container_netns ip r
assert "$output" !~ 'default' "ipvlan must not contain default gateway in route at all"
}
@test "ipvlan setup with mtu" {
run_netavark --file ${TESTSDIR}/testfiles/ipvlan-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" "==" "ipvlan" "Container interface is a ipvlan 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 "ipvlan modes" {
for mode in l2 l3 l3s; 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": "ipvlan",
"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" "==" "ipvlan" "Container interface is a ipvlan device"
assert_json "$link_info" ".[].linkinfo.info_data.mode" "==" "$mode" "Container interface has correct ipvlan mode"
run_netavark teardown $(get_container_netns_path) <<<"$config"
done
}
@test "ipvlan 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": "ipvlan",
"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 "ipvlan 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": "ipvlan",
"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 "ipvlan 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": "ipvlan",
"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 ipvlan 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 ipvlan interface should be gone"
}