diff --git a/qemu/tests/test_qmp_drive.py b/qemu/tests/test_qmp_drive.py index 3a57f05..9fb410f 100755 --- a/qemu/tests/test_qmp_drive.py +++ b/qemu/tests/test_qmp_drive.py @@ -13,9 +13,11 @@ was never compared with the one the reference was captured under. python3 test_qmp_drive.py """ +import atexit import json import os import re +import shutil import socket import subprocess import sys @@ -29,6 +31,15 @@ HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import qmp # noqa: E402 +# Every temp dir this module makes (a drive's out dir per run_script, the +# console and QMP socket dirs) lands under one root per run, removed at exit, +# so a run leaves $TMPDIR as it found it (bfe-core1106-sdk#30: about 40 dirs +# per run used to stay behind). The prefix is short: an AF_UNIX socket path +# under it must stay inside about 108 bytes. +_TMP_ROOT = tempfile.mkdtemp(prefix="qdt.") +tempfile.tempdir = _TMP_ROOT +atexit.register(shutil.rmtree, _TMP_ROOT, True) + SIZE = 64 @@ -1574,5 +1585,29 @@ class StallProbe(unittest.TestCase): self.assertTrue(qmp.is_stall("control channel: bridge: no reply from warden-ui within 30 s: hit")) self.assertFalse(qmp.is_stall("obj moved: box=1,2,3x4")) +class TempDirs(unittest.TestCase): + # One test per kind of temp dir this module makes: a drive's out dir + # (run_script), a console dir and a QMP socket dir. + CHILD_TESTS = ( + "DriveVerbs.test_shot_and_region_names_cannot_escape_outdir", + "ConsoleWatchTests.test_finds_a_marker_split_across_two_reads_and_caches_it", + "QmpSocketTimeout.test_main_bounds_a_wedged_qmp_peer_instead_of_hanging_forever", + ) + + @unittest.skipIf(os.environ.get("QMP_DRIVE_TEMP_CHILD"), "the child run itself") + def test_a_run_leaves_tmpdir_as_it_found_it(self): + fresh = tempfile.mkdtemp(prefix="fresh.", dir=os.path.dirname(_TMP_ROOT)) + try: + env = dict(os.environ, TMPDIR=fresh, QMP_DRIVE_TEMP_CHILD="1") + child = subprocess.run( + [sys.executable, os.path.abspath(__file__), *self.CHILD_TESTS], + env=env, capture_output=True, text=True, timeout=120) + self.assertEqual(child.returncode, 0, child.stderr[-2000:]) + self.assertIn(f"Ran {len(self.CHILD_TESTS)} tests", child.stderr) + self.assertEqual(os.listdir(fresh), []) + finally: + shutil.rmtree(fresh, ignore_errors=True) + + if __name__ == "__main__": unittest.main(verbosity=1)