diff options
Diffstat (limited to 'tests/test_incus_runtime.py')
| -rw-r--r-- | tests/test_incus_runtime.py | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/tests/test_incus_runtime.py b/tests/test_incus_runtime.py index 6de4bd5c..ea5eb1fc 100644 --- a/tests/test_incus_runtime.py +++ b/tests/test_incus_runtime.py | |||
| @@ -5,15 +5,11 @@ | |||
| 5 | Incus runtime tests - boot container-image-host with incus and verify | 5 | Incus runtime tests - boot container-image-host with incus and verify |
| 6 | system container management. | 6 | system container management. |
| 7 | 7 | ||
| 8 | Build prerequisites (in local.conf): | 8 | The tests automatically build container-image-host with CONTAINER_PROFILE=incus |
| 9 | require conf/distro/include/meta-virt-host.conf | 9 | before booting. No local.conf changes needed. |
| 10 | require conf/distro/include/container-host-incus.conf | ||
| 11 | MACHINE = "qemux86-64" # or qemuarm64 | ||
| 12 | |||
| 13 | bitbake container-image-host | ||
| 14 | 10 | ||
| 15 | Run: | 11 | Run: |
| 16 | pytest tests/test_incus_runtime.py -v --machine qemux86-64 | 12 | pytest tests/test_incus_runtime.py -v --poky-dir /opt/bruce/poky |
| 17 | 13 | ||
| 18 | Options: | 14 | Options: |
| 19 | --boot-timeout QEMU boot timeout (default: 120s) | 15 | --boot-timeout QEMU boot timeout (default: 120s) |
| @@ -22,8 +18,11 @@ Options: | |||
| 22 | 18 | ||
| 23 | import os | 19 | import os |
| 24 | import re | 20 | import re |
| 21 | import subprocess | ||
| 22 | import tempfile | ||
| 25 | import time | 23 | import time |
| 26 | import pytest | 24 | import pytest |
| 25 | from pathlib import Path | ||
| 27 | 26 | ||
| 28 | try: | 27 | try: |
| 29 | import pexpect | 28 | import pexpect |
| @@ -38,19 +37,61 @@ pytestmark = [ | |||
| 38 | ] | 37 | ] |
| 39 | 38 | ||
| 40 | 39 | ||
| 40 | def _run_bitbake(build_dir, recipe, extra_vars=None, timeout=3600): | ||
| 41 | """Run bitbake with optional variable overrides via -R conf file.""" | ||
| 42 | bb_cmd = "bitbake" | ||
| 43 | conf_file = None | ||
| 44 | if extra_vars: | ||
| 45 | conf_file = tempfile.NamedTemporaryFile( | ||
| 46 | mode='w', suffix='.conf', prefix='pytest-incus-', | ||
| 47 | dir=str(build_dir / "conf"), delete=False) | ||
| 48 | for var, val in extra_vars.items(): | ||
| 49 | conf_file.write(f'{var} = "{val}"\n') | ||
| 50 | conf_file.close() | ||
| 51 | bb_cmd += f" -R {conf_file.name}" | ||
| 52 | bb_cmd += f" {recipe}" | ||
| 53 | poky_dir = build_dir.parent | ||
| 54 | full_cmd = f"bash -c 'cd {poky_dir} && source oe-init-build-env {build_dir} >/dev/null 2>&1 && {bb_cmd}'" | ||
| 55 | try: | ||
| 56 | return subprocess.run(full_cmd, shell=True, cwd=build_dir, | ||
| 57 | timeout=timeout, capture_output=True, text=True) | ||
| 58 | finally: | ||
| 59 | if conf_file: | ||
| 60 | os.unlink(conf_file.name) | ||
| 61 | |||
| 62 | |||
| 63 | @pytest.fixture(scope="module") | ||
| 64 | def incus_image(request): | ||
| 65 | """Build container-image-host with incus profile.""" | ||
| 66 | poky_dir = Path(request.config.getoption("--poky-dir")) | ||
| 67 | bd = request.config.getoption("--build-dir") | ||
| 68 | build_dir = Path(bd) if bd else poky_dir / "build" | ||
| 69 | result = _run_bitbake( | ||
| 70 | build_dir, "container-image-host", | ||
| 71 | extra_vars={ | ||
| 72 | "CONTAINER_PROFILE": "incus", | ||
| 73 | "DISTRO_FEATURES:append": " virtualization", | ||
| 74 | }, | ||
| 75 | ) | ||
| 76 | if result.returncode != 0: | ||
| 77 | pytest.fail(f"Incus image build failed: {result.stderr}") | ||
| 78 | |||
| 79 | |||
| 41 | @pytest.fixture(scope="module") | 80 | @pytest.fixture(scope="module") |
| 42 | def incus_qemu(request): | 81 | def incus_qemu(request, incus_image): |
| 43 | """Boot a QEMU VM with incus and return the pexpect session.""" | 82 | """Build incus image, boot a QEMU VM, and return the pexpect session.""" |
| 44 | machine = request.config.getoption("--machine", default="qemux86-64") | 83 | machine = request.config.getoption("--machine", default="qemux86-64") |
| 45 | boot_timeout = int(request.config.getoption("--boot-timeout", default="120")) | 84 | boot_timeout = int(request.config.getoption("--boot-timeout", default="120")) |
| 46 | no_kvm = request.config.getoption("--no-kvm", default=False) | 85 | no_kvm = request.config.getoption("--no-kvm", default=False) |
| 47 | 86 | ||
| 48 | builddir = os.environ.get("BUILDDIR", os.path.expanduser("~/poky/build")) | 87 | poky_dir = Path(request.config.getoption("--poky-dir")) |
| 88 | bd = request.config.getoption("--build-dir") | ||
| 89 | builddir = str(Path(bd) if bd else poky_dir / "build") | ||
| 49 | 90 | ||
| 50 | kvm_opt = "" if no_kvm else "kvm" | 91 | kvm_opt = "" if no_kvm else "kvm" |
| 51 | cmd = f"runqemu {machine} nographic slirp {kvm_opt} qemuparams=\"-m 4096\"" | 92 | cmd = f"runqemu {machine} container-image-host ext4 nographic slirp {kvm_opt} qemuparams=\"-m 4096\"" |
| 52 | 93 | ||
| 53 | child = pexpect.spawn(f"bash -c 'source {builddir}/oe-init-build-env {builddir} >/dev/null 2>&1 && {cmd}'", | 94 | child = pexpect.spawn(f"bash -c 'cd {poky_dir} && source oe-init-build-env {builddir} >/dev/null 2>&1 && {cmd}'", |
| 54 | timeout=boot_timeout, encoding="utf-8", logfile=None) | 95 | timeout=boot_timeout, encoding="utf-8", logfile=None) |
| 55 | 96 | ||
| 56 | # Wait for login prompt | 97 | # Wait for login prompt |
