Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the management of workdir cleanup for debugging purposes #221

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to avoid cleanup when a test fails? Don't we want the next text to start cleanly? I understand if we running individually and we can set "debug", but not generally

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each qemu has its own working folder (/tmp/tdxtest-xxxxx/) that is not shared with other tests qemu instances