Skip to content

Commit

Permalink
Improve the management of workdir cleanup for debugging purposes (#221)
Browse files Browse the repository at this point in the history
- In case of test failure, we would like that the workdir of qemu
  is not cleaned up so that we can do some debugging.
- Improve the management of environment variable TDXTEST_DEBUG
  that forbids the clean up of these workdirs at any case
  • Loading branch information
hector-cao authored Sep 18, 2024
1 parent 8fd5854 commit 80b75fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
19 changes: 15 additions & 4 deletions tests/lib/Qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,21 @@ class QemuMachineService:
QEMU_MACHINE_QMP = enum.auto()

class QemuMachine:
debug_enabled = False

def __init__(self,
name='default',
machine=QemuEfiMachine.OVMF_Q35_TDX,
memory='2G',
service_blacklist=[]):
self.name = name
self.debug = os.environ.get('TDXTEST_DEBUG', False)
self.image_dir = '/var/tmp/tdxtest/'
self.guest_initial_img = os.environ.get('TDXTEST_GUEST_IMG', f'{self.image_dir}/tdx-guest.qcow2')
self._setup_workdir()
self._create_image()

# TODO : WA for log, to be removed
print(f'\n\nQemuMachine created (debug={self.debug}).')
print(f'\n\nQemuMachine created.')

self.qcmd = QemuCommand(
self.workdir_name,
Expand All @@ -463,6 +464,14 @@ def __init__(self,
self.out = None
self.err = None

@staticmethod
def is_debug_enabled():
return QemuMachine.debug_enabled

@staticmethod
def set_debug(debug : bool):
QemuMachine.debug_enabled = debug

def _create_image(self):
# create an overlay image backed by the original image
# See https://wiki.qemu.org/Documentation/CreateSnapshot
Expand All @@ -475,9 +484,10 @@ def _setup_workdir(self):
run_path = pathlib.Path('/run/user/%d/' % (os.getuid()))
if run_path.exists():
tempfile.tempdir = str(run_path)
# delete=False : we want to manage cleanup ourself for debugging purposes
# delete parameter is only available from 3.12
if (sys.version_info[0]==3) and (sys.version_info[1]>11):
self.workdir = tempfile.TemporaryDirectory(prefix=f'tdxtest-{self.name}-', delete=not self.debug)
self.workdir = tempfile.TemporaryDirectory(prefix=f'tdxtest-{self.name}-', delete=False)
else:
self.workdir = tempfile.TemporaryDirectory(prefix=f'tdxtest-{self.name}-')
self.workdir_name = self.workdir.name
Expand Down Expand Up @@ -554,5 +564,6 @@ def __del__(self):
Make sure we stop the qemu process and clean up the working dir
"""
self.stop()
if not self.debug:
needs_cleanup = (not QemuMachine.is_debug_enabled())
if needs_cleanup:
self.workdir.cleanup()
28 changes: 28 additions & 0 deletions tests/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import pytest

import Qemu

@pytest.fixture(autouse=True)
def run_before_and_after_tests(tmpdir):
"""
Fixture to execute before and after a test is run
Even in case of test failure
"""
# Setup: fill with any logic you want
# enable the debug flag if TDXTEST_DEBUG is set
debug = os.environ.get('TDXTEST_DEBUG', False)
if debug:
Qemu.QemuMachine.set_debug(debug)

yield # this is where the testing happens

# Teardown : fill with any logic you want

def pytest_exception_interact(node, call, report):
"""
Called at test failure
"""
if report.failed:
# enable debug flag to avoid cleanup to happen
Qemu.QemuMachine.set_debug(True)

0 comments on commit 80b75fd

Please sign in to comment.