Skip to content

Commit 38c5795

Browse files
committed
tests: add capabilities to net test
Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent b403b8a commit 38c5795

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

tests/test_devices.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,48 @@ def test_net_devices():
248248

249249
for specify_broadcast in [True, False]:
250250
for specify_name in [True, False]:
251-
subprocess.run(["ip", "link", "add", "testdevice", "type", "dummy"])
251+
sys.stderr.write("# test_net_devices: creating testdevice with specify_broadcast=%s, specify_name=%s\n" % (specify_broadcast, specify_name))
252+
result = subprocess.run(["ip", "link", "add", "testdevice", "type", "dummy"], capture_output=True, text=True)
253+
if result.returncode != 0:
254+
sys.stderr.write("# ip link add failed: %s\n" % result.stderr)
255+
return -1
252256
if specify_broadcast:
253-
subprocess.run(["ip", "addr", "add", "10.1.2.3/24", "brd", "10.1.2.254", "dev", "testdevice"])
257+
result = subprocess.run(["ip", "addr", "add", "10.1.2.3/24", "brd", "10.1.2.254", "dev", "testdevice"], capture_output=True, text=True)
258+
if result.returncode != 0:
259+
sys.stderr.write("# ip addr add with broadcast failed: %s\n" % result.stderr)
260+
return -1
254261
else:
255-
subprocess.run(["ip", "addr", "add", "10.1.2.3/24", "dev", "testdevice"])
262+
result = subprocess.run(["ip", "addr", "add", "10.1.2.3/24", "dev", "testdevice"], capture_output=True, text=True)
263+
if result.returncode != 0:
264+
sys.stderr.write("# ip addr add without broadcast failed: %s\n" % result.stderr)
265+
return -1
256266

257267
conf = base_config()
258268
add_all_namespaces(conf)
269+
270+
# Add network capabilities needed for network device operations
271+
conf['process']['capabilities'] = {
272+
"bounding": [
273+
"CAP_NET_ADMIN",
274+
"CAP_NET_RAW",
275+
"CAP_SYS_ADMIN"
276+
],
277+
"effective": [
278+
"CAP_NET_ADMIN",
279+
"CAP_NET_RAW",
280+
"CAP_SYS_ADMIN"
281+
],
282+
"inheritable": [
283+
"CAP_NET_ADMIN",
284+
"CAP_NET_RAW",
285+
"CAP_SYS_ADMIN"
286+
],
287+
"permitted": [
288+
"CAP_NET_ADMIN",
289+
"CAP_NET_RAW",
290+
"CAP_SYS_ADMIN"
291+
]
292+
}
259293
if specify_name:
260294
conf['process']['args'] = ['/init', 'ip', 'newtestdevice']
261295
conf['linux']['netDevices'] = {
@@ -272,19 +306,28 @@ def test_net_devices():
272306

273307
try:
274308
out = run_and_get_output(conf)
309+
sys.stderr.write("# test_net_devices: specify_broadcast=%s, specify_name=%s\n" % (specify_broadcast, specify_name))
310+
sys.stderr.write("# test_net_devices: output: %s\n" % repr(out[0]))
275311
if "address: 10.1.2.3" not in out[0]:
276312
sys.stderr.write("# address not found in output\n")
313+
sys.stderr.write("# full output: %s\n" % repr(out[0]))
277314
return 1
278315
if specify_broadcast:
279316
if "broadcast: 10.1.2.254" not in out[0]:
280317
sys.stderr.write("# broadcast address not found in output\n")
318+
sys.stderr.write("# full output: %s\n" % repr(out[0]))
281319
return 1
282320
else:
283321
if "broadcast" in out[0]:
284-
sys.stderr.write("# broadcast address found in output\n")
322+
sys.stderr.write("# broadcast address found in output when it shouldn't be\n")
323+
sys.stderr.write("# full output: %s\n" % repr(out[0]))
285324
return 1
286325
except Exception as e:
326+
sys.stderr.write("# test_net_devices exception: %s\n" % str(e))
287327
return -1
328+
finally:
329+
# Clean up the test device
330+
subprocess.run(["ip", "link", "del", "testdevice"], capture_output=True)
288331
finally:
289332
os.setns(current_netns, os.CLONE_NEWNET)
290333
os.close(current_netns)

0 commit comments

Comments
 (0)