Skip to content

Commit 210ac84

Browse files
giuseppeclaude
andcommitted
tests: use hide_stderr=True to avoid coverage output interference
When running tests with --enable-coverage, gcov writes diagnostic messages to stderr which get mixed with program output due to stderr=subprocess.STDOUT in run_and_get_output(). This causes test failures as the TAP parser encounters unexpected output. Add hide_stderr=True to most run_and_get_output() calls to discard coverage diagnostics while preserving the actual program output needed for test validation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 6d63b8b commit 210ac84

21 files changed

+134
-124
lines changed

tests/test_bpf_devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_bpf_devices_systemd():
8686
bpf_path = None
8787
try:
8888
# Run container with systemd cgroup manager.
89-
_, cid = run_and_get_output(conf, command='run', detach=True, cgroup_manager="systemd")
89+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True, cgroup_manager="systemd")
9090

9191
# Get systemd scope.
9292
state = run_crun_command(['state', cid])

tests/test_capabilities.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_no_caps():
2828
conf['process']['capabilities'] = {}
2929
for i in ['bounding', 'effective', 'inheritable', 'permitted', 'ambient']:
3030
conf['process']['capabilities'][i] = []
31-
out, _ = run_and_get_output(conf)
31+
out, _ = run_and_get_output(conf, hide_stderr=True)
3232
proc_status = parse_proc_status(out)
3333

3434
for i in ['CapInh', 'CapPrm', 'CapEff', 'CapBnd', 'CapAmb']:
@@ -45,7 +45,7 @@ def test_some_caps():
4545
conf['process']['capabilities'] = {}
4646
for i in ['bounding', 'effective', 'inheritable', 'permitted', 'ambient']:
4747
conf['process']['capabilities'][i] = []
48-
out, _ = run_and_get_output(conf)
48+
out, _ = run_and_get_output(conf, hide_stderr=True)
4949
proc_status = parse_proc_status(out)
5050

5151
for i in ['CapInh', 'CapPrm', 'CapEff', 'CapBnd', 'CapAmb']:
@@ -63,7 +63,7 @@ def test_unknown_caps():
6363
# unknown caps must be ignored
6464
for i in ['bounding', 'effective', 'inheritable', 'permitted', 'ambient']:
6565
conf['process']['capabilities'][i] = ['CAP_UNKNOWN', 'UNKNOWN_CAP']
66-
out, _ = run_and_get_output(conf)
66+
out, _ = run_and_get_output(conf, hide_stderr=True)
6767
proc_status = parse_proc_status(out)
6868

6969
for i in ['CapInh', 'CapPrm', 'CapEff', 'CapBnd', 'CapAmb']:
@@ -79,7 +79,7 @@ def test_new_privs():
7979
add_all_namespaces(conf)
8080

8181
conf['process']['noNewPrivileges'] = True
82-
out, _ = run_and_get_output(conf)
82+
out, _ = run_and_get_output(conf, hide_stderr=True)
8383
proc_status = parse_proc_status(out)
8484
no_new_privs = proc_status.get('NoNewPrivs', 'MISSING')
8585
if no_new_privs != "1":
@@ -95,7 +95,7 @@ def test_new_privs():
9595
return (77, "host already has NoNewPrivs=1")
9696

9797
conf['process']['noNewPrivileges'] = False
98-
out, _ = run_and_get_output(conf)
98+
out, _ = run_and_get_output(conf, hide_stderr=True)
9999
proc_status = parse_proc_status(out)
100100
no_new_privs = proc_status.get('NoNewPrivs', 'MISSING')
101101
if no_new_privs != "0":
@@ -112,7 +112,7 @@ def helper_test_some_caps(uid, captypes, proc_name):
112112
conf['process']['capabilities'] = {}
113113
for i in captypes + ['bounding']:
114114
conf['process']['capabilities'][i] = ["CAP_SYS_ADMIN"]
115-
out, _ = run_and_get_output(conf)
115+
out, _ = run_and_get_output(conf, hide_stderr=True)
116116
proc_status = parse_proc_status(out)
117117

118118
expected = "0000000000200000"

tests/test_cwd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_cwd():
2222
conf['process']['args'] = ['/init', 'cwd']
2323
conf['process']['cwd'] = "/var"
2424
add_all_namespaces(conf)
25-
out, _ = run_and_get_output(conf)
25+
out, _ = run_and_get_output(conf, hide_stderr=True)
2626
if "/var" not in out:
2727
return -1
2828
return 0
@@ -33,7 +33,7 @@ def test_cwd_relative():
3333
conf['process']['cwd'] = "/sbin"
3434
add_all_namespaces(conf)
3535
try:
36-
out, _ = run_and_get_output(conf)
36+
out, _ = run_and_get_output(conf, hide_stderr=True)
3737
if "hello" not in str(out):
3838
return -1
3939
except Exception as e:
@@ -46,7 +46,7 @@ def test_cwd_relative_subdir():
4646
conf['process']['cwd'] = "/"
4747
add_all_namespaces(conf)
4848
try:
49-
out, _ = run_and_get_output(conf)
49+
out, _ = run_and_get_output(conf, hide_stderr=True)
5050
if "hello" not in str(out):
5151
return -1
5252
except:
@@ -59,7 +59,7 @@ def test_cwd_not_exist():
5959
conf['process']['cwd'] = "/doesnotexist"
6060
add_all_namespaces(conf)
6161
try:
62-
run_and_get_output(conf)
62+
run_and_get_output(conf, hide_stderr=True)
6363
except:
6464
return -1
6565
return 0
@@ -70,7 +70,7 @@ def test_cwd_absolute():
7070
conf['process']['cwd'] = "/sbin"
7171
add_all_namespaces(conf)
7272
try:
73-
out, _ = run_and_get_output(conf)
73+
out, _ = run_and_get_output(conf, hide_stderr=True)
7474
if "hello" not in str(out):
7575
return -1
7676
except:

tests/test_devices.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_mode_device():
4545
conf['linux']['devices'] = [{"path": "/dev/foo", "type": "b", "major": 1, "minor": 5, "uid": 10, "gid": 11, "fileMode": 0o157},]
4646
try:
4747
expected = "157"
48-
out = run_and_get_output(conf)
48+
out = run_and_get_output(conf, hide_stderr=True)
4949
if expected not in out[0]:
5050
logger.info("device mode test failed with userns=%s: expected '%s' in output", have_userns, expected)
5151
logger.info("actual output: %s", out[0])
@@ -78,7 +78,7 @@ def test_owner_device():
7878
conf['linux']['devices'] = [{"path": "/dev/foo", "type": "b", "major": 1, "minor": 5, "uid": 10, "gid": 11},]
7979
try:
8080
expected = "10:11"
81-
out = run_and_get_output(conf)
81+
out = run_and_get_output(conf, hide_stderr=True)
8282
if expected not in out[0]:
8383
logger.info("device owner test failed with userns=%s: expected '%s' in output", have_userns, expected)
8484
logger.info("actual output: %s", out)
@@ -137,7 +137,7 @@ def test_create_or_bind_mount_device():
137137
"gid": 0
138138
}]
139139
try:
140-
run_and_get_output(conf)
140+
run_and_get_output(conf, hide_stderr=True)
141141
except Exception as e:
142142
logger.info("# %s", str(e))
143143
return -1
@@ -169,7 +169,7 @@ def test_allow_device():
169169
}
170170
conf['mounts'].append(dev)
171171
try:
172-
run_and_get_output(conf)
172+
run_and_get_output(conf, hide_stderr=True)
173173
except Exception as e:
174174
return -1
175175
return 0
@@ -199,7 +199,7 @@ def test_allow_access():
199199
}
200200
conf['mounts'].append(dev)
201201
try:
202-
run_and_get_output(conf)
202+
run_and_get_output(conf, hide_stderr=True)
203203
except Exception as e:
204204
return -1
205205
return 0
@@ -214,7 +214,7 @@ def test_mknod_device():
214214
conf['linux']['devices'] = [{"path": "/foo-dev", "type": "b", "major": 10, "minor": 229},
215215
{"path": "/subdir/foo-dev", "type": "b", "major": 10, "minor": 229},]
216216
try:
217-
run_and_get_output(conf)
217+
run_and_get_output(conf, hide_stderr=True)
218218
except Exception as e:
219219
return -1
220220
return 0
@@ -228,7 +228,7 @@ def test_trailing_slash_mknod_device():
228228
conf['process']['args'] = ['/init', 'true']
229229
conf['linux']['devices'] = [{"path": "/mnt/", "type": "b", "major": 10, "minor": 229}]
230230
try:
231-
run_and_get_output(conf)
231+
run_and_get_output(conf, hide_stderr=True)
232232
except Exception as e:
233233
return -1
234234
return 0
@@ -305,7 +305,7 @@ def test_net_devices():
305305
}
306306

307307
try:
308-
out = run_and_get_output(conf)
308+
out = run_and_get_output(conf, hide_stderr=True)
309309
logger.info("test_net_devices: specify_broadcast=%s, specify_name=%s", specify_broadcast, specify_name)
310310
logger.info("test_net_devices: output: %s", out[0])
311311
if "address: 10.1.2.3" not in out[0]:
@@ -345,7 +345,7 @@ def test_mknod_fifo_device():
345345
{"path": "/dev/testfifo", "type": "p", "fileMode": 0o0660, "uid": 1, "gid": 2}
346346
]
347347
try:
348-
run_and_get_output(conf)
348+
run_and_get_output(conf, hide_stderr=True)
349349
except Exception as e:
350350
logger.info("test_mknod_fifo_device failed: %s", e)
351351
return -1
@@ -362,7 +362,7 @@ def test_mknod_char_device():
362362
{"path": "/dev/testchar", "type": "c", "major": 251, "minor": 1, "fileMode": 0o0640, "uid": 3, "gid": 4}
363363
]
364364
try:
365-
run_and_get_output(conf)
365+
run_and_get_output(conf, hide_stderr=True)
366366
except Exception as e:
367367
logger.info("test_mknod_char_device failed: {e}")
368368
return -1

tests/test_domainname.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_domainname():
2222
conf['process']['args'] = ['/init', 'getdomainname']
2323
conf['domainname'] = "foomachine"
2424
add_all_namespaces(conf)
25-
out, _ = run_and_get_output(conf)
25+
out, _ = run_and_get_output(conf, hide_stderr=True)
2626
if "foomachine" not in out:
2727
return -1
2828
conf = base_config()
@@ -32,7 +32,7 @@ def test_domainname():
3232
# in both of the above situation the test should pass. Anything other than this
3333
# must be considered as failure.
3434
try:
35-
out, cid = run_and_get_output(conf)
35+
out, cid = run_and_get_output(conf, hide_stderr=True)
3636
if out == "(none)\n":
3737
return 0
3838
logger.info("unexpected success")
@@ -53,7 +53,7 @@ def test_domainname_conflict_sysctl():
5353
conf['linux']['sysctl'] = {'kernel.domainname' : 'foo'}
5454
cid = None
5555
try:
56-
out, cid = run_and_get_output(conf)
56+
out, cid = run_and_get_output(conf, hide_stderr=True)
5757
if out == "(none)\n":
5858
return 0
5959
logger.info("unexpected success")
@@ -75,7 +75,7 @@ def test_domainname_with_sysctl():
7575
conf['linux']['sysctl'] = {'kernel.domainname' : 'foo'}
7676
cid = None
7777
try:
78-
out, cid = run_and_get_output(conf)
78+
out, cid = run_and_get_output(conf, hide_stderr=True)
7979
if out == "(none)\n":
8080
return 0
8181
return 0

tests/test_exec.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_exec():
3030
add_all_namespaces(conf)
3131
cid = None
3232
try:
33-
_, cid = run_and_get_output(conf, command='run', detach=True)
33+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
3434
out = run_crun_command(["exec", cid, "/init", "echo", "foo"])
3535
if "foo" not in out:
3636
logger.info("exec test failed: expected 'foo' in output")
@@ -67,7 +67,7 @@ def test_uid_tty():
6767
last_error = None
6868
try:
6969
cid = "container-%s" % os.getpid()
70-
proc = run_and_get_output(conf, command='run', id_container=cid, use_popen=True)
70+
proc = run_and_get_output(conf, hide_stderr=True, command='run', id_container=cid, use_popen=True)
7171
for i in range(0, 500):
7272
try:
7373
out = run_crun_command(["exec", "-t", "--user", "1", cid, "/init", "owner", "/proc/self/fd/0"])
@@ -102,7 +102,7 @@ def test_exec_root_netns_with_userns():
102102
conf['linux']['namespaces'].append({"type" : "network", "path" : "/proc/1/ns/net"})
103103
cid = None
104104
try:
105-
_, cid = run_and_get_output(conf, command='run', detach=True)
105+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
106106

107107
with open("/proc/net/route") as f:
108108
payload = f.read()
@@ -148,7 +148,7 @@ def test_exec_not_exists_helper(detach):
148148
add_all_namespaces(conf)
149149
cid = None
150150
try:
151-
_, cid = run_and_get_output(conf, command='run', detach=True)
151+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
152152
try:
153153
if detach:
154154
out = run_crun_command(["exec", "-d", cid, "/not.here"])
@@ -176,7 +176,7 @@ def test_exec_additional_gids():
176176
cid = None
177177
tempdir = tempfile.mkdtemp()
178178
try:
179-
_, cid = run_and_get_output(conf, command='run', detach=True)
179+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
180180

181181
process_file = os.path.join(tempdir, "process.json")
182182
with open(process_file, "w") as f:
@@ -216,7 +216,7 @@ def test_exec_populate_home_env_from_process_uid():
216216
cid = None
217217
tempdir = tempfile.mkdtemp()
218218
try:
219-
_, cid = run_and_get_output(conf, command='run', detach=True)
219+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
220220

221221
process_file = os.path.join(tempdir, "process.json")
222222
with open(process_file, "w") as f:
@@ -274,7 +274,7 @@ def test_exec_add_capability():
274274
"CAP_KILL": cap_kill_dict, \
275275
"CAP_SYS_ADMIN": cap_sys_admin_dict}
276276
try:
277-
_, cid = run_and_get_output(conf, command='run', detach=True)
277+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
278278
for cap, value in cap_dict.items():
279279
out = run_crun_command(["exec", "--cap", cap, cid, "/init", "cat", "/proc/self/status"])
280280
for i in ['bounding', 'effective', 'inheritable', 'permitted', 'ambient']:
@@ -300,7 +300,7 @@ def test_exec_add_env():
300300
env_dict_orig = {"HOME":"/", "PATH":"/bin"}
301301
env_dict_new = {"HOME":"/tmp", "PATH":"/usr/bin","FOO":"BAR"}
302302
try:
303-
_, cid = run_and_get_output(conf, command='run', detach=True)
303+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
304304
# check original environment variable
305305
for env, value in env_dict_orig.items():
306306
out = run_crun_command(["exec", cid, "/init", "printenv", env])
@@ -338,7 +338,7 @@ def test_exec_set_user():
338338
uid_gid_list = ["1000:1000", "0:0", "65535:65535"]
339339

340340
try:
341-
_, cid = run_and_get_output(conf, command='run', detach=True)
341+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
342342
# check current user id
343343
out = run_crun_command(["exec", cid, "/init", "id"])
344344
if uid_gid_list[1] not in out:
@@ -361,7 +361,7 @@ def test_exec_no_new_privs():
361361
conf['process']['capabilities'] = {}
362362
cid = None
363363
try:
364-
_, cid = run_and_get_output(conf, command='run', detach=True)
364+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
365365
# check original value of NoNewPrivs
366366
out = run_crun_command(["exec", cid, "/init", "cat", "/proc/self/status"])
367367
proc_status = parse_proc_status(out)
@@ -386,7 +386,7 @@ def test_exec_write_pid_file():
386386
cid = None
387387
tempdir = tempfile.mkdtemp()
388388
try:
389-
_, cid = run_and_get_output(conf, command='run', detach=True)
389+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
390390
pid_file = os.path.join(tempdir, cid)
391391
out = run_crun_command(["exec", "--pid-file", pid_file, cid, "/init", "echo", "hello"])
392392
if "hello" not in out:
@@ -449,7 +449,7 @@ def exec_and_get_affinity_mask(cid, exec_cpu_affinity=None):
449449
try:
450450
with open("/proc/self/status") as f:
451451
current_cpu_mask = cpu_mask_from_proc_status(f.read())
452-
_, cid = run_and_get_output(conf, command='run', detach=True)
452+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
453453

454454
mask = exec_and_get_affinity_mask(cid)
455455
if mask != current_cpu_mask:
@@ -483,7 +483,7 @@ def test_exec_getpgrp():
483483
conf['process']['args'] = ['/init', 'pause']
484484
cid = None
485485
try:
486-
_, cid = run_and_get_output(conf, command='run', detach=True)
486+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
487487
for terminal in [True, False]:
488488
if terminal and os.isatty(1) == False:
489489
continue
@@ -512,7 +512,7 @@ def test_exec_error_propagation():
512512
add_all_namespaces(conf)
513513
cid = None
514514
try:
515-
_, cid = run_and_get_output(conf, command='run', detach=True)
515+
_, cid = run_and_get_output(conf, hide_stderr=True, command='run', detach=True)
516516
try:
517517
out = run_crun_command_raw(["exec", "--cwd", "/invalid/nonexistent/path", cid, "/init", "echo", "test"])
518518
return -1

tests/test_hooks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_fail_prestart():
2323
conf['hooks'] = {"prestart" : [{"path" : "/bin/false"}]}
2424
add_all_namespaces(conf)
2525
try:
26-
out, _ = run_and_get_output(conf)
26+
out, _ = run_and_get_output(conf, hide_stderr=True)
2727
except:
2828
return 0
2929
return -1
@@ -33,7 +33,7 @@ def test_success_prestart():
3333
conf['hooks'] = {"prestart" : [{"path" : "/bin/true"}]}
3434
add_all_namespaces(conf)
3535
try:
36-
out, _ = run_and_get_output(conf)
36+
out, _ = run_and_get_output(conf, hide_stderr=True)
3737
except:
3838
return -1
3939
return 0
@@ -48,7 +48,7 @@ def test_hook_env_inherit():
4848
add_all_namespaces(conf)
4949
print(conf['hooks'])
5050
try:
51-
out, _ = run_and_get_output(conf)
51+
out, _ = run_and_get_output(conf, hide_stderr=True)
5252
except:
5353
return -1
5454
return 0
@@ -62,7 +62,7 @@ def test_hook_env_no_inherit():
6262
add_all_namespaces(conf)
6363
print(conf['hooks'])
6464
try:
65-
out, _ = run_and_get_output(conf)
65+
out, _ = run_and_get_output(conf, hide_stderr=True)
6666
except:
6767
return -1
6868
return 0

0 commit comments

Comments
 (0)