Skip to content

Commit 87f49b4

Browse files
committed
refactor(tests): drop test_context decorator
The same thing can be done with a fixture, without having to resort to pytest hooks. Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent 235af84 commit 87f49b4

File tree

4 files changed

+8
-150
lines changed

4 files changed

+8
-150
lines changed

tests/conftest.py

-72
Original file line numberDiff line numberDiff line change
@@ -497,39 +497,6 @@ def test_microvm_any(request, microvm):
497497
yield microvm
498498

499499

500-
@pytest.fixture
501-
def test_multiple_microvms(test_fc_session_root_path, context, bin_cloner_path):
502-
"""Yield one or more microvms based on the context provided.
503-
504-
`context` is a dynamically parameterized fixture created inside the special
505-
function `pytest_generate_tests` and it holds a tuple containing the name
506-
of the guest image used to spawn a microvm and the number of microvms
507-
to spawn.
508-
"""
509-
microvms = []
510-
(microvm_resources, how_many) = context
511-
512-
# When the context specifies multiple microvms, we use the first vm to
513-
# populate the other ones by hardlinking its resources.
514-
first_vm = init_microvm(test_fc_session_root_path, bin_cloner_path)
515-
MICROVM_S3_FETCHER.init_vm_resources(microvm_resources, first_vm)
516-
microvms.append(first_vm)
517-
518-
# It is safe to do this as the dynamically generated fixture `context`
519-
# asserts that the `how_many` parameter is always positive
520-
# (i.e strictly greater than 0).
521-
for _ in range(how_many - 1):
522-
vm = init_microvm(test_fc_session_root_path, bin_cloner_path)
523-
MICROVM_S3_FETCHER.hardlink_vm_resources(microvm_resources, first_vm, vm)
524-
microvms.append(vm)
525-
526-
yield microvms
527-
528-
for i in range(how_many):
529-
microvms[i].kill()
530-
shutil.rmtree(os.path.join(test_fc_session_root_path, microvms[i].id))
531-
532-
533500
@pytest.fixture(autouse=True, scope="session")
534501
def test_spectre_mitigations():
535502
"""Check the kernel is compiled with SPECTREv2 mitigations."""
@@ -582,45 +549,6 @@ def firecracker_release(request):
582549
return firecracker
583550

584551

585-
def pytest_generate_tests(metafunc):
586-
"""Implement customized parametrization scheme.
587-
588-
This is a special hook which is called by the pytest infrastructure when
589-
collecting a test function. The `metafunc` contains the requesting test
590-
context. Amongst other things, the `metafunc` provides the list of fixture
591-
names that the calling test function is using. If we find a fixture that
592-
is called `context`, we check the calling function through the
593-
`metafunc.function` field for the `_pool_size` attribute which we
594-
previously set with a decorator. Then we create the list of parameters
595-
for this fixture.
596-
The parameter will be a list of tuples of the form (cap, pool_size).
597-
For each parameter from the list (i.e. tuple) a different test case
598-
scenario will be created.
599-
"""
600-
if "context" in metafunc.fixturenames:
601-
# In order to create the params for the current fixture, we need the
602-
# capability and the number of vms we want to spawn.
603-
604-
# 1. Look if the test function set the pool size through the decorator.
605-
# If it did not, we set it to 1.
606-
how_many = int(getattr(metafunc.function, "_pool_size", None))
607-
assert how_many > 0
608-
609-
# 2. Check if the test function set the capability field through
610-
# the decorator. If it did not, we set it to any.
611-
cap = getattr(metafunc.function, "_capability", "*")
612-
613-
# 3. Before parametrization, get the list of images that have the
614-
# desired capability. By parametrize-ing the fixture with it, we
615-
# trigger tests cases for each of them.
616-
image_list = MICROVM_S3_FETCHER.list_microvm_images(capability_filter=[cap])
617-
metafunc.parametrize(
618-
"context",
619-
[(item, how_many) for item in image_list],
620-
ids=["{}, {} instance(s)".format(item, how_many) for item in image_list],
621-
)
622-
623-
624552
@pytest.fixture(params=ARTIFACTS_COLLECTION.kernels(), ids=lambda kernel: kernel.name())
625553
def guest_kernel(request):
626554
"""Return all supported guest kernels."""

tests/framework/decorators.py

-11
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,3 @@ def timed(*args, **kwargs):
4646
return result
4747

4848
return timed
49-
50-
51-
def test_context(cap, count=1):
52-
"""Set the image capability and vm count attribute for individual tests."""
53-
54-
def wrap(func):
55-
setattr(func, "_capability", cap)
56-
setattr(func, "_pool_size", count)
57-
return func
58-
59-
return wrap

tests/framework/s3fetcher.py

-42
Original file line numberDiff line numberDiff line change
@@ -143,48 +143,6 @@ def init_vm_resources(self, microvm_image_name, microvm):
143143
microvm.ssh_config["ssh_key_path"] = microvm_dest_path
144144
os.chmod(microvm_dest_path, 0o400)
145145

146-
def hardlink_vm_resources(self, microvm_image_name, from_microvm, to_microvm):
147-
"""Hardlink resources from one microvm to another.
148-
149-
Assumes the correct microvm image structure for the source vm specified
150-
by the `from_microvm` parameter and copies all necessary resources into
151-
the destination microvm specified through the `to_microvm` parameter.
152-
"""
153-
for resource_key in self._microvm_images[microvm_image_name]:
154-
if resource_key in [
155-
self.MICROVM_IMAGE_KERNEL_RELPATH,
156-
self.MICROVM_IMAGE_BLOCKDEV_RELPATH,
157-
]:
158-
# Kernel and blockdev dirs already exist in the microvm's
159-
# allocated resources.
160-
continue
161-
162-
microvm_dest_path = os.path.join(to_microvm.path, resource_key)
163-
microvm_source_path = os.path.join(from_microvm.path, resource_key)
164-
165-
if resource_key.endswith("/"):
166-
# Create a new microvm_directory if one is encountered.
167-
os.mkdir(microvm_dest_path)
168-
continue
169-
170-
if not os.path.exists(microvm_dest_path):
171-
os.link(microvm_source_path, microvm_dest_path)
172-
173-
if resource_key.endswith(self.MICROVM_IMAGE_KERNEL_FILE_SUFFIX):
174-
to_microvm.kernel_file = microvm_dest_path
175-
176-
if resource_key.endswith(self.MICROVM_IMAGE_ROOTFS_FILE_SUFFIX):
177-
to_microvm.rootfs_file = microvm_dest_path
178-
179-
if resource_key.endswith(self.MICROVM_IMAGE_INITRD_FILE_SUFFIX):
180-
to_microvm.initrd_file = microvm_dest_path
181-
182-
if resource_key.endswith(self.MICROVM_IMAGE_SSH_KEY_SUFFIX):
183-
# Add the key path to the config dictionary and set
184-
# permissions.
185-
to_microvm.ssh_config["ssh_key_path"] = microvm_dest_path
186-
os.chmod(microvm_dest_path, 0o400)
187-
188146
def list_microvm_images(self, capability_filter: List[str] = None):
189147
"""Return microvm images with the specified capabilities."""
190148
capability_filter = capability_filter or ["*"]

tests/integration_tests/functional/test_concurrency.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,25 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Ensure multiple microVMs work correctly when spawned simultaneously."""
44

5-
from framework import decorators
6-
75
import host_tools.network as net_tools
86

97
NO_OF_MICROVMS = 20
108

119

12-
@decorators.test_context("api", NO_OF_MICROVMS)
13-
def test_run_concurrency(test_multiple_microvms, network_config):
10+
def test_run_concurrency(microvm_factory, network_config, guest_kernel, rootfs):
1411
"""
1512
Check we can spawn multiple microvms.
1613
1714
@type: functional
1815
"""
19-
microvms = test_multiple_microvms
2016

2117
for i in range(NO_OF_MICROVMS):
22-
microvm = microvms[i]
23-
_ = _configure_and_run(microvm, {"config": network_config, "iface_id": str(i)})
18+
microvm = microvm_factory.build(guest_kernel, rootfs)
19+
microvm.spawn()
20+
microvm.basic_config(vcpu_count=1, mem_size_mib=128)
21+
microvm.ssh_network_config(network_config, str(i))
22+
microvm.start()
23+
2424
# We check that the vm is running by testing that the ssh does
2525
# not time out.
26-
_ = net_tools.SSHConnection(microvm.ssh_config)
27-
28-
29-
def _configure_and_run(microvm, network_info):
30-
"""Auxiliary function for configuring and running microVM."""
31-
microvm.spawn()
32-
33-
# Machine configuration specified in the SLA.
34-
config = {"vcpu_count": 1, "mem_size_mib": 128}
35-
36-
microvm.basic_config(**config)
37-
38-
_tap, _, _ = microvm.ssh_network_config(
39-
network_info["config"], network_info["iface_id"]
40-
)
41-
42-
microvm.start()
43-
return _tap
26+
net_tools.SSHConnection(microvm.ssh_config)

0 commit comments

Comments
 (0)