From 061e7dc62b41c99e1372236d6e072311100905b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Srokosz?= Date: Thu, 22 Aug 2024 18:58:47 +0200 Subject: [PATCH] CI: Install drakvuf-sandbox for tests using virtualenv (#950) --- test/conftest.py | 32 ++++++++++++++++---------------- test/test_drakrun.py | 19 +++++++------------ test/utils.py | 6 ------ 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 71ce2c952..940325753 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -8,7 +8,7 @@ from invoke.exceptions import UnexpectedExit from vm_runner_client import DrakvufVM -from utils import apt_install, pip_install +from utils import apt_install logging.basicConfig(level=logging.INFO) @@ -31,7 +31,7 @@ "redis-server.service", ] -DRAKVUF_SANDBOX_DEBS = [ +DRAKVUF_SANDBOX_WHLS = [ "drakvuf_sandbox-*.whl", ] @@ -40,7 +40,7 @@ ] -def resolve_debs(debs): +def resolve_package_paths(debs): debs_path = Path(DRAKVUF_DEBS_PATH) if not debs_path.is_dir(): raise RuntimeError(f"Incorrect DRAKVUF_DEBS_PATH: {DRAKVUF_DEBS_PATH}") @@ -74,8 +74,8 @@ def drakmon_ssh(drakmon_setup: DrakvufVM): def drakmon_setup(): logging.info("Running end to end test: creating VM") - drakvuf_sandbox_debs = list(resolve_debs(DRAKVUF_SANDBOX_DEBS)) - drakvuf_debs = list(resolve_debs(DRAKVUF_DEBS)) + drakvuf_sandbox_whls = list(resolve_package_paths(DRAKVUF_SANDBOX_WHLS)) + drakvuf_debs = list(resolve_package_paths(DRAKVUF_DEBS)) drakvuf_vm = DrakvufVM.create(BASE_IMAGE) logging.info(f"VM {drakvuf_vm.identity} created.") @@ -84,7 +84,7 @@ def drakmon_setup(): drakvuf_vm.wait_for_state(alive=True) with drakvuf_vm.connect_ssh() as ssh: - for deb in (drakvuf_sandbox_debs + drakvuf_debs): + for deb in (drakvuf_sandbox_whls + drakvuf_debs): logging.info("Uploading %s", deb.name) ssh.put(deb.as_posix()) @@ -114,23 +114,23 @@ def drakmon_setup(): with drakvuf_vm.connect_ssh() as ssh: ssh.run("apt-get --allow-releaseinfo-change update", in_stream=False) - apt_install(ssh, ["redis-server", "python3", "python3-pip", "git", "dnsmasq", "bridge-utils"]) - pip_install(ssh, ["pip"], "--upgrade") - for d in drakvuf_sandbox_debs: - if str(d).endswith(".deb"): - apt_install(ssh, ["./" + d.name]) - else: - pip_install(ssh, ["./" + d.name]) + apt_install(ssh, ["redis-server", "python3", "python3-pip", "python3-venv", "git", "dnsmasq", "bridge-utils"]) + logging.info("Setting up pip and virtualenv") + ssh.run(f"DEBIAN_FRONTEND=noninteractive pip3 install --upgrade pip", in_stream=False) + ssh.run(f"DEBIAN_FRONTEND=noninteractive python3 -m venv /root/venv", in_stream=False) + + for d in drakvuf_sandbox_whls: + ssh.run(f"DEBIAN_FRONTEND=noninteractive /root/venv/bin/pip install ./{d.name}", in_stream=False) # Import snapshot assert SNAPSHOT_VERSION is not None - ssh.run(f"draksetup install-minio") - ssh.run(f"draksetup init --unattended") + ssh.run(f"/root/venv/bin/draksetup install-minio") + ssh.run(f"/root/venv/bin/draksetup init --unattended") ssh.run(f'DRAKRUN_MINIO_ADDRESS="{MINIO_HOST}" ' f'DRAKRUN_MINIO_SECURE=0 ' f'DRAKRUN_MINIO_ACCESS_KEY="{MINIO_ACCESS_KEY}" ' f'DRAKRUN_MINIO_SECRET_KEY="{MINIO_SECRET_KEY}" ' - f'draksetup snapshot import --bucket snapshots --name {SNAPSHOT_VERSION} --full') + f'/root/venv/bin/draksetup snapshot import --bucket snapshots --name {SNAPSHOT_VERSION} --full') # Shut up QEMU ssh.run("ln -s /dev/null /root/SW_DVD5_Win_Pro_7w_SP1_64BIT_Polish_-2_MLF_X17-59386.ISO") diff --git a/test/test_drakrun.py b/test/test_drakrun.py index c99dc55cd..ed2d135e5 100644 --- a/test/test_drakrun.py +++ b/test/test_drakrun.py @@ -4,12 +4,7 @@ @pytest.fixture(scope="session") def pytest_installed(drakmon_ssh): """ Ensure that pytest is installed """ - # pip is sometimes broken on virtualenv generated by dh-virtualenv - # https://github.com/spotify/dh-virtualenv/issues/338 - drakmon_ssh.run("wget https://bootstrap.pypa.io/get-pip.py &&" - "python3 get-pip.py") - # Then we can actually try to install something - drakmon_ssh.run("pip3 install pytest==6.2.2 pytest-steps==1.7.3") + drakmon_ssh.run("/root/venv/bin/pip3 install pytest==6.2.2 pytest-steps==1.7.3") @pytest.fixture(scope="session") @@ -17,27 +12,27 @@ def drakrun_test_dir(pytest_installed, drakmon_ssh): """ Find location of tests """ res = drakmon_ssh.run( """ - python3 -c "import os; import drakrun.test.conftest; print(os.path.dirname(drakrun.test.conftest.__file__))" + /root/venv/bin/python3 -c "import os; import drakrun.test.conftest; print(os.path.dirname(drakrun.test.conftest.__file__))" """) return res.stdout.strip() def test_lvm(drakmon_ssh, drakrun_test_dir): - drakmon_ssh.run(f"pytest {drakrun_test_dir}/test_lvm.py") + drakmon_ssh.run(f"/root/venv/bin/pytest {drakrun_test_dir}/test_lvm.py") def test_util(drakmon_ssh, drakrun_test_dir): - drakmon_ssh.run(f"pytest {drakrun_test_dir}/test_util.py") + drakmon_ssh.run(f"/root/venv/bin/pytest {drakrun_test_dir}/test_util.py") def test_network(drakmon_ssh, drakrun_test_dir): - drakmon_ssh.run(f"pytest {drakrun_test_dir}/test_network.py") + drakmon_ssh.run(f"/root/venv/bin/pytest {drakrun_test_dir}/test_network.py") def test_vm(drakmon_ssh, drakrun_test_dir): - drakmon_ssh.run(f"pytest {drakrun_test_dir}/test_vm.py") + drakmon_ssh.run(f"/root/venv/bin/pytest {drakrun_test_dir}/test_vm.py") def test_draksetup_test(drakmon_ssh): - drakmon_ssh.run(f"draksetup test") + drakmon_ssh.run(f"/root/venv/bin/draksetup test") diff --git a/test/utils.py b/test/utils.py index ea03f0c25..435fad0ed 100644 --- a/test/utils.py +++ b/test/utils.py @@ -8,12 +8,6 @@ def apt_install(c, packages): c.run(f"DEBIAN_FRONTEND=noninteractive apt-get install -y {deps}", in_stream=False) -def pip_install(c, packages, flags=""): - deps = " ".join(packages) - logging.info(f"Installing {packages} with pip3") - c.run(f"DEBIAN_FRONTEND=noninteractive pip3 install {flags} {deps}", in_stream=False) - - def dpkg_install(c, deb_file): logging.info(f"Installing {deb_file} with dpkg") c.run(f"DEBIAN_FRONTEND=noninteractive dpkg -i {deb_file}", in_stream=False)