Skip to content

Commit 0a5c722

Browse files
committed
test: refactor so that all vm related tests can be reused
This commit reshuffles the code a bit so that we a reusable ./test/vmtest directory that can be used by the images library. With that we can add a toplevel pyproject.toml file so that we can import vmtest via ```console $ pip install [email protected]/osbuild/bootc-image-builder ``` im other projects. Note that none of this is ideal, butt this is (hopefully) a temporary measure until we find a more permanent home for our vm runner or replace it with something like test.thing or the new osbuild QEMU code.
1 parent a8e8ad7 commit 0a5c722

File tree

14 files changed

+83
-73
lines changed

14 files changed

+83
-73
lines changed

pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Note that this is pyproject file is here only for the vmtest utils.
2+
# This should move out eventually to its own repo or a different place
3+
# like "images".
4+
5+
[build-system]
6+
requires = ["setuptools>=61.0"]
7+
build-backend = "setuptools.build_meta"
8+
9+
[project]
10+
name = "vmtest"
11+
version = "0.1.0"
12+
13+
[tool.setuptools.packages.find]
14+
include = ["vmtest"]

test/__init__.py

Whitespace-only changes.

test/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

3-
from testcases import TestCase
3+
from test.testcases import TestCase
4+
from vmtest.util import get_free_port
45

56

67
def pytest_addoption(parser):
@@ -20,3 +21,8 @@ def pytest_make_parametrize_id(config, val): # pylint: disable=W0613
2021
if isinstance(val, TestCase):
2122
return f"{val}"
2223
return None
24+
25+
26+
@pytest.fixture(name="free_port")
27+
def free_port_fixture():
28+
return get_free_port()

test/test_build_cross.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pytest
44

5-
from testcases import gen_testcases
5+
from test.testcases import gen_testcases
66

7-
from test_build_disk import ( # pylint: disable=unused-import
7+
from test.test_build_disk import ( # pylint: disable=unused-import
88
assert_disk_image_boots,
99
build_container_fixture,
1010
gpg_conf_fixture,

test/test_build_disk.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
import pytest
1717
# local test utils
18-
import testutil
19-
from containerbuild import build_container_fixture # pylint: disable=unused-import
20-
from testcases import CLOUD_BOOT_IMAGE_TYPES, DISK_IMAGE_TYPES, gen_testcases
21-
from vm import AWS, QEMU
18+
from test import testutil
19+
from .containerbuild import build_container_fixture # pylint: disable=unused-import
20+
from .testcases import CLOUD_BOOT_IMAGE_TYPES, DISK_IMAGE_TYPES, gen_testcases
21+
import vmtest.util
22+
from vmtest.vm import AWS_REGION, AWS, QEMU
2223

2324
if not testutil.has_executable("podman"):
2425
pytest.skip("no podman, skipping integration tests that required podman", allow_module_level=True)
@@ -113,7 +114,7 @@ def registry_conf_fixture(shared_tmpdir, request):
113114
{local_registry}:
114115
lookaside: file:///{sigstore_dir}
115116
"""
116-
registry_port = testutil.get_free_port()
117+
registry_port = vmtest.util.get_free_port()
117118
# We cannot use localhost as we need to access the registry from both
118119
# the host system and the bootc-image-builder container.
119120
default_ip = testutil.get_ip_from_default_route()
@@ -410,7 +411,7 @@ def build_images(shared_tmpdir, build_container, request, force_aws_upload, gpg_
410411

411412
upload_args = [
412413
f"--aws-ami-name=bootc-image-builder-test-{str(uuid.uuid4())}",
413-
f"--aws-region={testutil.AWS_REGION}",
414+
f"--aws-region={AWS_REGION}",
414415
"--aws-bucket=bootc-image-builder-ci",
415416
]
416417
elif force_aws_upload:
@@ -492,7 +493,7 @@ def build_images(shared_tmpdir, build_container, request, force_aws_upload, gpg_
492493
metadata["ami_id"] = parse_ami_id_from_log(journal_output)
493494

494495
def del_ami():
495-
testutil.deregister_ami(metadata["ami_id"])
496+
testutil.deregister_ami(metadata["ami_id"], AWS_REGION)
496497
request.addfinalizer(del_ami)
497498

498499
journal_log_path.write_text(journal_output, encoding="utf8")

test/test_build_iso.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
import pytest
77
# local test utils
8-
import testutil
9-
from containerbuild import build_container_fixture # pylint: disable=unused-import
10-
from testcases import gen_testcases
11-
from vm import QEMU
8+
from test import testutil
9+
from .containerbuild import build_container_fixture # pylint: disable=unused-import
10+
from test.testcases import gen_testcases
11+
from vmtest.vm import QEMU
1212

13-
from test_build_disk import (
13+
from test.test_build_disk import (
1414
assert_kernel_args,
1515
ImageBuildResult,
1616
)
17-
from test_build_disk import ( # pylint: disable=unused-import
17+
from test.test_build_disk import ( # pylint: disable=unused-import
1818
gpg_conf_fixture,
1919
image_type_fixture,
2020
registry_conf_fixture,

test/test_manifest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import pytest
1212

13-
import testutil
14-
from containerbuild import build_container_fixture as _
15-
from containerbuild import make_container
16-
from testcases import gen_testcases
13+
from test import testutil
14+
from test.containerbuild import build_container_fixture as _
15+
from test.containerbuild import make_container
16+
from test.testcases import gen_testcases
1717

1818
if not testutil.has_executable("podman"):
1919
pytest.skip("no podman, skipping integration tests that required podman", allow_module_level=True)

test/test_opts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import subprocess
44

55
import pytest
6-
import testutil
6+
from test import testutil
77
# pylint: disable=unused-import
8-
from containerbuild import build_container_fixture, build_fake_container_fixture
8+
from test.containerbuild import build_container_fixture, build_fake_container_fixture
99

1010

1111
@pytest.fixture(name="container_storage", scope="session")

test/test_progress.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import pytest
44

5-
import testutil
5+
from test import testutil
66
# pylint: disable=unused-import,duplicate-code
7-
from test_opts import container_storage_fixture
8-
from containerbuild import (
7+
from test.test_opts import container_storage_fixture
8+
from test.containerbuild import (
99
build_container_fixture,
1010
build_erroring_container_fixture,
1111
build_fake_container_fixture,

test/testutil.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
import pathlib
33
import platform
44
import shutil
5-
import socket
65
import subprocess
7-
import time
86

97
import boto3
108
from botocore.exceptions import ClientError
119

12-
AWS_REGION = "us-east-1"
13-
1410

1511
def run_journalctl(*args):
1612
pre = []
@@ -35,28 +31,6 @@ def has_executable(name):
3531
return shutil.which(name) is not None
3632

3733

38-
def get_free_port() -> int:
39-
# this is racy but there is no race-free way to do better with the qemu CLI
40-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
41-
s.bind(("localhost", 0))
42-
return s.getsockname()[1]
43-
44-
45-
def wait_ssh_ready(address, port, sleep, max_wait_sec):
46-
for _ in range(int(max_wait_sec / sleep)):
47-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
48-
s.settimeout(sleep)
49-
try:
50-
s.connect((address, port))
51-
data = s.recv(256)
52-
if b"OpenSSH" in data:
53-
return
54-
except (ConnectionRefusedError, ConnectionResetError, TimeoutError):
55-
pass
56-
time.sleep(sleep)
57-
raise ConnectionRefusedError(f"cannot connect to port {port} after {max_wait_sec}s")
58-
59-
6034
def has_x86_64_v3_cpu():
6135
# x86_64-v3 has multiple features, see
6236
# https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels
@@ -95,8 +69,8 @@ def write_aws_creds(path):
9569
return True
9670

9771

98-
def deregister_ami(ami_id):
99-
ec2 = boto3.resource("ec2", region_name=AWS_REGION)
72+
def deregister_ami(ami_id, aws_region):
73+
ec2 = boto3.resource("ec2", region_name=aws_region)
10074
try:
10175
print(f"Deregistering image {ami_id}")
10276
ami = ec2.Image(ami_id)

0 commit comments

Comments
 (0)