From 36c12482c6ff4349307c93c67e9cd914ed0c6d1c Mon Sep 17 00:00:00 2001 From: Lukasz Mrugala Date: Wed, 6 Mar 2024 13:06:19 +0000 Subject: [PATCH] scripts: tests: --package-artifacts fix, blackbox tests Requirements added for bz2. Blackbox test for --package-artifacts added. package.py no longer includes twister-out no matter the outdir. Signed-off-by: Lukasz Mrugala --- scripts/pylib/twister/twisterlib/package.py | 2 +- scripts/requirements-run-test.txt | 3 ++ .../tests/twister_blackbox/test_outfile.py | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/scripts/pylib/twister/twisterlib/package.py b/scripts/pylib/twister/twisterlib/package.py index 5ae17e7601..c86c99c153 100644 --- a/scripts/pylib/twister/twisterlib/package.py +++ b/scripts/pylib/twister/twisterlib/package.py @@ -15,7 +15,7 @@ class Artifacts: def make_tarfile(self, output_filename, source_dirs): root = os.path.basename(self.options.outdir) with tarfile.open(output_filename, "w:bz2") as tar: - tar.add("twister-out", recursive=False) + tar.add(self.options.outdir, recursive=False) for d in source_dirs: f = os.path.relpath(d, self.options.outdir) tar.add(d, arcname=os.path.join(root, f)) diff --git a/scripts/requirements-run-test.txt b/scripts/requirements-run-test.txt index 8369212811..a710eb962a 100644 --- a/scripts/requirements-run-test.txt +++ b/scripts/requirements-run-test.txt @@ -14,3 +14,6 @@ cbor>=1.0.0 # use for twister psutil + +# Artifacts package creation +bz diff --git a/scripts/tests/twister_blackbox/test_outfile.py b/scripts/tests/twister_blackbox/test_outfile.py index 5e019d672f..46c006323c 100644 --- a/scripts/tests/twister_blackbox/test_outfile.py +++ b/scripts/tests/twister_blackbox/test_outfile.py @@ -13,6 +13,7 @@ import os import shutil import pytest import sys +import tarfile from conftest import ZEPHYR_BASE, TEST_DATA, sample_filename_mock, testsuite_filename_mock from twisterlib.testplan import TestPlan @@ -199,3 +200,55 @@ class TestOutfile: # However, the cost of testing that this leaves less seems to outweigh the benefits. # So we'll only check for the most important artifact. assert 'zephyr.elf' in zephyr_artifact_list + + def test_package_artifacts(self, out_path): + test_platforms = ['qemu_x86'] + path = os.path.join(TEST_DATA, 'samples', 'hello_world') + package_name = 'PACKAGE' + package_path = os.path.join(out_path, package_name) + args = ['-i', '--outdir', out_path, '-T', path] + \ + ['--package-artifacts', package_path] + \ + [val for pair in zip( + ['-p'] * len(test_platforms), test_platforms + ) for val in pair] + + with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \ + pytest.raises(SystemExit) as sys_exit: + self.loader.exec_module(self.twister_module) + + assert str(sys_exit.value) == '0' + + # Check whether we have something as basic as zephyr.elf file + with tarfile.open(package_path, "r") as tar: + assert any([path.endswith('zephyr.elf') for path in tar.getnames()]) + + # Delete everything but for the package + for clean_up in os.listdir(os.path.join(out_path)): + if not clean_up.endswith(package_name): + clean_up_path = os.path.join(out_path, clean_up) + if os.path.isfile(clean_up_path): + os.remove(clean_up_path) + else: + shutil.rmtree(os.path.join(out_path, clean_up)) + + # Unpack the package + with tarfile.open(package_path, "r") as tar: + tar.extractall(path=out_path) + + # Why does package.py put files inside the out_path folder? + # It forces us to move files up one directory after extraction. + file_names = os.listdir(os.path.join(out_path, os.path.basename(out_path))) + for file_name in file_names: + shutil.move(os.path.join(out_path, os.path.basename(out_path), file_name), out_path) + + args = ['-i', '--outdir', out_path, '-T', path] + \ + ['--test-only'] + \ + [val for pair in zip( + ['-p'] * len(test_platforms), test_platforms + ) for val in pair] + + with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \ + pytest.raises(SystemExit) as sys_exit: + self.loader.exec_module(self.twister_module) + + assert str(sys_exit.value) == '0'