Skip to content

Commit 235af84

Browse files
committed
chore(tests): configure kernel/rootfs in microvm_factory fixture
- Make simple the common case of "run this kernel/rootfs" - Cleanup VMs automatically during test teardown - Some artifacts fixes to make it work Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent 196a756 commit 235af84

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

tests/conftest.py

+40-2
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,32 @@ class MicroVMFactory:
451451
def __init__(self, tmp_path, bin_cloner):
452452
self.tmp_path = tmp_path
453453
self.bin_cloner_path = bin_cloner
454+
self.vms = []
454455

455-
def build(self):
456+
def build(self, kernel=None, rootfs=None):
456457
"""Build a fresh microvm."""
457458
vm = init_microvm(self.tmp_path, self.bin_cloner_path)
459+
self.vms.append(vm)
460+
if kernel is not None:
461+
kernel_path = Path(kernel.local_path())
462+
vm.kernel_file = kernel_path
463+
if rootfs is not None:
464+
rootfs_path = Path(rootfs.local_path())
465+
rootfs_path2 = Path(vm.path) / rootfs_path.name
466+
# TBD only iff ext4 / rw
467+
shutil.copyfile(rootfs_path, rootfs_path2)
468+
vm.rootfs_file = rootfs_path2
469+
vm.ssh_config["ssh_key_path"] = rootfs.ssh_key().local_path()
458470
return vm
459471

460-
yield MicroVMFactory(tmp_path, bin_cloner_path)
472+
def kill(self):
473+
"""Clean up all built VMs"""
474+
for vm in self.vms:
475+
vm.kill()
476+
477+
uvm_factory = MicroVMFactory(tmp_path, bin_cloner_path)
478+
yield uvm_factory
479+
uvm_factory.kill()
461480

462481

463482
@pytest.fixture(params=MICROVM_S3_FETCHER.list_microvm_images(capability_filter=["*"]))
@@ -602,6 +621,25 @@ def pytest_generate_tests(metafunc):
602621
)
603622

604623

624+
@pytest.fixture(params=ARTIFACTS_COLLECTION.kernels(), ids=lambda kernel: kernel.name())
625+
def guest_kernel(request):
626+
"""Return all supported guest kernels."""
627+
kernel = request.param
628+
kernel.download()
629+
return kernel
630+
631+
632+
@pytest.fixture(
633+
params=ARTIFACTS_COLLECTION.disks("ubuntu"), ids=lambda rootfs: rootfs.name()
634+
)
635+
def rootfs(request):
636+
"""Return all supported rootfs."""
637+
rootfs = request.param
638+
rootfs.download()
639+
rootfs.ssh_key().download()
640+
return rootfs
641+
642+
605643
TEST_MICROVM_CAP_FIXTURE_TEMPLATE = (
606644
"@pytest.fixture("
607645
" params=MICROVM_S3_FETCHER.list_microvm_images(\n"

tests/framework/artifacts.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ def download(self, target_folder=ARTIFACTS_LOCAL_ROOT, force=False):
9393
"""Save the artifact in the folder specified target_path."""
9494
assert self.bucket is not None
9595
self._local_folder = target_folder
96-
Path(self.local_dir()).mkdir(parents=True, exist_ok=True)
97-
if force or not os.path.exists(self.local_path()):
98-
self._bucket.download_file(self._key, self.local_path())
96+
local_path = Path(self.local_path())
97+
local_path.parent.mkdir(parents=True, exist_ok=True)
98+
if force or not local_path.exists():
99+
self._bucket.download_file(self._key, local_path)
99100
# Artifacts are read-only by design.
100-
os.chmod(self.local_path(), S_IREAD)
101+
local_path.chmod(0o400)
101102

102103
def local_path(self):
103104
"""Return the local path where the file was downloaded."""
@@ -273,7 +274,12 @@ def ssh_key(self):
273274
"""Return a ssh key artifact."""
274275
# Replace extension.
275276
key_file_path = str(Path(self.key).with_suffix(".id_rsa"))
276-
return Artifact(self.bucket, key_file_path, artifact_type=ArtifactType.SSH_KEY)
277+
return Artifact(
278+
self.bucket,
279+
key_file_path,
280+
artifact_type=ArtifactType.SSH_KEY,
281+
local_folder=self._local_folder,
282+
)
277283

278284

279285
class FirecrackerArtifact(Artifact):

0 commit comments

Comments
 (0)