Test/manifest_tests: use temporary dir if workdir is not specified

In case the workdir is not provided to the script explicitly as an
argument, the script will use a temporary directory under /var/tmp as
its workdir. In such case, the workdir will be deleted on exit. This
should mitigate potentially confusing behavior when executing the script
multiple times with different arguments, while never specifying the
workdir.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2025-01-30 10:35:21 +01:00 committed by Tomáš Hozza
parent a6c09fd441
commit 0b158c3fd3

View file

@ -15,6 +15,7 @@ import json
import os
import subprocess
import sys
import tempfile
from typing import Dict, List, Optional, Tuple
OSBUILD_IMAGES_REPO_URL = os.environ.get("OSBUILD_IMAGES_REPO_URL", "https://github.com/osbuild/images.git")
@ -339,8 +340,8 @@ def get_argparser():
"--workdir",
metavar="PATH",
type=os.path.abspath,
default="./osbuild-manifest-tests-workdir",
help="Working directory where the images repository is checked out and the image build cache is downloaded."
help="Working directory where the images repository is checked out and the image build cache is downloaded. " +
"If not provided, a temporary directory will be used and deleted on exit."
)
parser.add_argument(
"--results-dir",
@ -384,7 +385,9 @@ def main():
if args.image_type and args.skip_image_type:
parser.error("Options --image-type and --skip-image-type are mutually exclusive.")
workdir = args.workdir
try:
tmpdir = tempfile.TemporaryDirectory(dir='/var/tmp', prefix='osbuild-manifest-tests-workdir')
workdir = args.workdir or tmpdir
os.makedirs(workdir, exist_ok=True)
print(f"👷 Using working directory: {workdir}")
os.chdir(workdir)
@ -452,6 +455,9 @@ def main():
else:
print(f"✅ {test_case} PASSED")
test_cases_failed[test_case] = False
finally:
# We can't use the context manager here, because some files in it may be owned by root
subprocess.run(["sudo", "rm", "-rf", tmpdir.name], check=False)
print("Test results:")
for test_case, failed in test_cases_failed.items():