Skip to content

Commit

Permalink
CI: Install drakvuf-sandbox for tests using virtualenv (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 authored Aug 22, 2024
1 parent 7adc3a7 commit 061e7dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
32 changes: 16 additions & 16 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -31,7 +31,7 @@
"redis-server.service",
]

DRAKVUF_SANDBOX_DEBS = [
DRAKVUF_SANDBOX_WHLS = [
"drakvuf_sandbox-*.whl",
]

Expand All @@ -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}")
Expand Down Expand Up @@ -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.")
Expand All @@ -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())

Expand Down Expand Up @@ -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")
Expand Down
19 changes: 7 additions & 12 deletions test/test_drakrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,35 @@
@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")
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")
6 changes: 0 additions & 6 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 061e7dc

Please sign in to comment.