Skip to content

Commit 5b706ed

Browse files
zulinx86pb8o
authored andcommitted
test: Add pytest option to apply custom CPU template to any microVM
Sometimes we would like to test a custom CPU template for debugging / testing purposes. The new pytest option `--custom-cpu-template` allows us to apply the given CPU template to any microVM in any test unless it is overwritten by a test. Since `cpu_template_any` yields not only CPU templates but also None, the given CPU template will be used in the None case. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 80b4cb4 commit 5b706ed

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

tests/conftest.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424

2525
import inspect
26+
import json
2627
import os
2728
import re
2829
import shutil
@@ -66,6 +67,14 @@ def pytest_addoption(parser):
6667
help="use firecracker/jailer binaries from this directory instead of compiling from source",
6768
)
6869

70+
parser.addoption(
71+
"--custom-cpu-template",
72+
action="store",
73+
help="Path to custom CPU template to be applied unless overwritten by a test",
74+
default=None,
75+
type=Path,
76+
)
77+
6978

7079
@pytest.hookimpl(wrapper=True, tryfirst=True)
7180
def pytest_runtest_makereport(item, call): # pylint:disable=unused-argument
@@ -270,9 +279,22 @@ def microvm_factory(request, record_property, results_dir):
270279
fc_binary_path, jailer_binary_path = build_tools.get_firecracker_binaries()
271280
record_property("firecracker_bin", str(fc_binary_path))
272281

282+
# If `--custom-cpu-template` option is provided, the given CPU template will
283+
# be applied afterwards unless overwritten.
284+
custom_cpu_template_path = request.config.getoption("--custom-cpu-template")
285+
custom_cpu_template = (
286+
{
287+
"name": custom_cpu_template_path.stem,
288+
"template": json.loads(custom_cpu_template_path.read_text("utf-8")),
289+
}
290+
if custom_cpu_template_path
291+
else None
292+
)
273293
# We could override the chroot base like so
274294
# jailer_kwargs={"chroot_base": "/srv/jailo"}
275-
uvm_factory = MicroVMFactory(fc_binary_path, jailer_binary_path)
295+
uvm_factory = MicroVMFactory(
296+
fc_binary_path, jailer_binary_path, custom_cpu_template=custom_cpu_template
297+
)
276298
yield uvm_factory
277299

278300
# if the test failed, save important files from the root of the uVM into `test_results` for troubleshooting

tests/framework/microvm.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def __init__(
188188
monitor_memory: bool = True,
189189
jailer_kwargs: Optional[dict] = None,
190190
numa_node=None,
191+
custom_cpu_template: Path = None,
191192
):
192193
"""Set up microVM attributes, paths, and data structures."""
193194
# pylint: disable=too-many-statements
@@ -246,6 +247,9 @@ def __init__(
246247
self.vcpus_count = None
247248
self.mem_size_bytes = None
248249
self.cpu_template_name = None
250+
# The given custom CPU template will be set in basic_config() but could
251+
# be overwritten via set_cpu_template().
252+
self.custom_cpu_template = custom_cpu_template
249253

250254
self._connections = []
251255

@@ -748,6 +752,9 @@ def basic_config(
748752
self.vcpus_count = vcpu_count
749753
self.mem_size_bytes = mem_size_mib * 2**20
750754

755+
if self.custom_cpu_template is not None:
756+
self.set_cpu_template(self.custom_cpu_template)
757+
751758
if cpu_template is not None:
752759
self.set_cpu_template(cpu_template)
753760

0 commit comments

Comments
 (0)