Skip to content

Commit f25ab54

Browse files
committed
refactor(test): Store binary_dir inside microvm
Just store the binary dir inside the microvm class instead of separate paths to firecracker and jailer binaries. This centralizes the existance assertions, but means we cant theoretically use jailer and firecracker binaries from different locations anymore (however, nothing in the test framework was doing this, as MicroVMFactory already did not support this and hardcoded the binary names relative to the --binary-dir option). Signed-off-by: Patrick Roy <[email protected]>
1 parent bbdae10 commit f25ab54

File tree

6 files changed

+33
-31
lines changed

6 files changed

+33
-31
lines changed

tests/conftest.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import host_tools.cargo_build as build_tools
3434
from framework import defs, utils
3535
from framework.artifacts import disks, kernel_params
36+
from framework.defs import DEFAULT_BINARY_DIR
3637
from framework.microvm import MicroVMFactory
3738
from framework.properties import global_props
3839
from framework.utils_cpu_templates import (
@@ -293,14 +294,9 @@ def get(self, _netns_id):
293294
def microvm_factory(request, record_property, results_dir, netns_factory):
294295
"""Fixture to create microvms simply."""
295296

296-
if binary_dir := request.config.getoption("--binary-dir"):
297-
fc_binary_path = Path(binary_dir) / "firecracker"
298-
jailer_binary_path = Path(binary_dir) / "jailer"
299-
if not fc_binary_path.exists():
300-
raise RuntimeError("Firecracker binary does not exist")
301-
else:
302-
fc_binary_path, jailer_binary_path = build_tools.get_firecracker_binaries()
303-
record_property("firecracker_bin", str(fc_binary_path))
297+
binary_dir = request.config.getoption("--binary-dir") or DEFAULT_BINARY_DIR
298+
299+
record_property("firecracker_bin", str(binary_dir / "firecracker"))
304300

305301
# If `--custom-cpu-template` option is provided, the given CPU template will
306302
# be applied afterwards unless overwritten.
@@ -316,8 +312,7 @@ def microvm_factory(request, record_property, results_dir, netns_factory):
316312
# We could override the chroot base like so
317313
# jailer_kwargs={"chroot_base": "/srv/jailo"}
318314
uvm_factory = MicroVMFactory(
319-
fc_binary_path,
320-
jailer_binary_path,
315+
binary_dir,
321316
netns_factory=netns_factory,
322317
custom_cpu_template=custom_cpu_template,
323318
)

tests/framework/defs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
# Absolute path to the test results folder
2727
TEST_RESULTS_DIR = FC_WORKSPACE_DIR / "test_results"
2828

29+
DEFAULT_BINARY_DIR = (
30+
LOCAL_BUILD_PATH
31+
/ "cargo_target"
32+
/ f"{platform.machine()}-unknown-linux-musl"
33+
/ "release"
34+
)
35+
2936
# The minimum required host kernel version for which io_uring is supported in
3037
# Firecracker.
3138
MIN_KERNEL_VERSION_FOR_IO_URING = "5.10.51"

tests/framework/microvm.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,13 +1073,25 @@ def wait_for_ssh_up(self):
10731073
class MicroVMFactory:
10741074
"""MicroVM factory"""
10751075

1076-
def __init__(self, fc_binary_path: Path, jailer_binary_path: Path, **kwargs):
1076+
def __init__(self, binary_path: Path, **kwargs):
10771077
self.vms = []
1078-
self.fc_binary_path = Path(fc_binary_path)
1079-
self.jailer_binary_path = Path(jailer_binary_path)
1078+
self.binary_path = binary_path
10801079
self.netns_factory = kwargs.pop("netns_factory", net_tools.NetNs)
10811080
self.kwargs = kwargs
10821081

1082+
assert self.fc_binary_path.exists(), "missing firecracker binary"
1083+
assert self.jailer_binary_path.exists(), "missing jailer binary"
1084+
1085+
@property
1086+
def fc_binary_path(self):
1087+
"""The path to the firecracker binary from which this factory will build VMs"""
1088+
return self.binary_path / "firecracker"
1089+
1090+
@property
1091+
def jailer_binary_path(self):
1092+
"""The path to the jailer binary using which this factory will build VMs"""
1093+
return self.binary_path / "jailer"
1094+
10831095
def build(self, kernel=None, rootfs=None, **kwargs):
10841096
"""Build a microvm"""
10851097
kwargs = self.kwargs | kwargs

tests/host_tools/cargo_build.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,6 @@ def get_binary(name, *, workspace_dir=FC_WORKSPACE_DIR, example=None):
6565
return bin_path
6666

6767

68-
def get_firecracker_binaries(*, workspace_dir=FC_WORKSPACE_DIR):
69-
"""Build the Firecracker and Jailer binaries if they don't exist.
70-
71-
Returns the location of the firecracker related binaries eventually after
72-
building them in case they do not exist at the specified root_path.
73-
"""
74-
return get_binary("firecracker", workspace_dir=workspace_dir), get_binary(
75-
"jailer", workspace_dir=workspace_dir
76-
)
77-
78-
7968
def get_example(name, *args, package="firecracker", **kwargs):
8069
"""Build an example binary"""
8170
return get_binary(package, *args, **kwargs, example=name)

tools/sandbox.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from pathlib import Path
1515

1616
from framework.artifacts import disks, kernels
17+
from framework.defs import DEFAULT_BINARY_DIR
1718
from framework.microvm import MicroVMFactory
18-
from host_tools.cargo_build import get_firecracker_binaries
1919

2020
kernels = list(kernels("vmlinux-*"))
2121
rootfs = list(disks("ubuntu*ext4"))
@@ -61,17 +61,16 @@ def parse_byte_size(param):
6161
args = parser.parse_args()
6262
print(args)
6363

64-
bins = None
64+
binary_dir = None
6565
if args.binary_dir:
6666
binary_dir = Path(args.binary_dir).resolve()
67-
bins = binary_dir / "firecracker", binary_dir / "jailer"
6867
else:
69-
bins = get_firecracker_binaries()
68+
binary_dir = DEFAULT_BINARY_DIR
7069

7170
cpu_template = None
7271
if args.cpu_template_path is not None:
7372
cpu_template = json.loads(args.cpu_template_path.read_text())
74-
vmfcty = MicroVMFactory(*bins)
73+
vmfcty = MicroVMFactory(binary_dir)
7574

7675
print(f"uvm with kernel {args.kernel} ...")
7776
uvm = vmfcty.build(args.kernel, args.rootfs)

tools/test-popular-containers/test-docker-rootfs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

1818
# pylint: disable=wrong-import-position
1919
from framework.artifacts import kernels
20+
from framework.defs import DEFAULT_BINARY_DIR
2021
from framework.microvm import MicroVMFactory
21-
from host_tools.cargo_build import get_firecracker_binaries
2222

2323
# pylint: enable=wrong-import-position
2424

2525
kernels = list(kernels("vmlinux-*"))
2626
# Use the latest guest kernel
2727
kernel = kernels[-1]
2828

29-
vmfcty = MicroVMFactory(*get_firecracker_binaries())
29+
vmfcty = MicroVMFactory(DEFAULT_BINARY_DIR)
3030
# (may take a while to compile Firecracker...)
3131

3232
for rootfs in Path(".").glob("*.ext4"):

0 commit comments

Comments
 (0)