Skip to content

Commit 9515f0e

Browse files
pb8ozulinx86
authored andcommitted
test: revamp .add_drive support
Track added drives in the Microvm class, and clean up some of the logic. Signed-off-by: Pablo Barbáchano <pablob@amazon.com>
1 parent 9524d75 commit 9515f0e

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

tests/framework/microvm.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,12 @@ def __init__(
157157

158158
# Initalize memory monitor
159159
self.memory_monitor = None
160-
161-
# iface dictionary
162-
self.iface = {}
163-
164-
# Deal with memory monitoring.
165160
if monitor_memory:
166161
self.memory_monitor = mem_tools.MemoryMonitor()
167162

163+
# device dictionaries
164+
self.iface = {}
165+
self.disks = {}
168166
self.vcpus_count = None
169167

170168
# External clone/exec tool, because Python can't into clone
@@ -616,20 +614,18 @@ def basic_config(
616614
), response.text
617615

618616
if add_root_device and self.rootfs_file != "":
619-
jail_fn = self.create_jailed_resource
620-
if self.jailer.uses_ramfs:
621-
jail_fn = self.copy_to_jail_ramfs
622-
# Add the root file system with rw permissions.
623-
response = self.drive.put(
617+
self.rootfs_file = Path(self.rootfs_file)
618+
read_only = self.rootfs_file.suffix == ".squashfs"
619+
620+
# Add the root file system
621+
self.add_drive(
624622
drive_id="rootfs",
625-
path_on_host=jail_fn(self.rootfs_file),
623+
path_on_host=self.rootfs_file,
626624
is_root_device=True,
627-
is_read_only=False,
625+
is_read_only=read_only,
628626
io_engine=rootfs_io_engine,
627+
use_ramdisk=self.jailer.uses_ramfs,
629628
)
630-
assert self._api_session.is_status_no_content(
631-
response.status_code
632-
), response.text
633629

634630
def cpu_config(self, config):
635631
"""Set CPU configuration."""
@@ -669,29 +665,32 @@ def daemonize_jailer(self, jailer_param_list):
669665
def add_drive(
670666
self,
671667
drive_id,
672-
file_path,
673-
root_device=False,
668+
path_on_host,
669+
is_root_device=False,
674670
is_read_only=False,
675671
partuuid=None,
676672
cache_type=None,
677673
io_engine=None,
678674
use_ramdisk=False,
679675
):
680676
"""Add a block device."""
677+
678+
if use_ramdisk:
679+
path_on_jail = self.copy_to_jail_ramfs(path_on_host)
680+
else:
681+
path_on_jail = self.create_jailed_resource(path_on_host)
682+
681683
response = self.drive.put(
682684
drive_id=drive_id,
683-
path_on_host=(
684-
self.copy_to_jail_ramfs(file_path)
685-
if use_ramdisk
686-
else self.create_jailed_resource(file_path)
687-
),
688-
is_root_device=root_device,
685+
path_on_host=path_on_jail,
686+
is_root_device=is_root_device,
689687
is_read_only=is_read_only,
690688
partuuid=partuuid,
691689
cache_type=cache_type,
692690
io_engine=io_engine,
693691
)
694692
assert self.api_session.is_status_no_content(response.status_code)
693+
self.disks[drive_id] = path_on_host
695694

696695
def patch_drive(self, drive_id, file):
697696
"""Modify/patch an existing block device."""
@@ -700,6 +699,7 @@ def patch_drive(self, drive_id, file):
700699
path_on_host=self.create_jailed_resource(file.path),
701700
)
702701
assert self.api_session.is_status_no_content(response.status_code)
702+
self.disks[drive_id] = Path(file.path)
703703

704704
def add_net_iface(self, iface=None, api=True, **kwargs):
705705
"""Add a network interface"""

tests/integration_tests/functional/test_api.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import host_tools.drive as drive_tools
1616
import host_tools.network as net_tools
1717
from framework import utils_cpuid
18-
from framework.artifacts import SnapshotType, NetIfaceConfig
18+
from framework.artifacts import NetIfaceConfig, SnapshotType
1919
from framework.builder import MicrovmBuilder, SnapshotBuilder
2020
from framework.utils import get_firecracker_version_from_toml, is_io_uring_supported
2121
from framework.utils_cpu_templates import skip_on_arm
@@ -793,16 +793,14 @@ def test_drive_patch(test_microvm_with_api):
793793
# a root file system with the rw permission.
794794
test_microvm.basic_config(rootfs_io_engine="Sync")
795795

796-
# The drive to be patched.
797796
fs = drive_tools.FilesystemFile(os.path.join(test_microvm.fsfiles, "scratch"))
798-
response = test_microvm.drive.put(
797+
test_microvm.add_drive(
799798
drive_id="scratch",
800-
path_on_host=test_microvm.create_jailed_resource(fs.path),
799+
path_on_host=fs.path,
801800
is_root_device=False,
802801
is_read_only=False,
803802
io_engine="Async" if is_io_uring_supported() else "Sync",
804803
)
805-
assert test_microvm.api_session.is_status_no_content(response.status_code)
806804

807805
# Patching drive before boot is not allowed.
808806
response = test_microvm.drive.patch(drive_id="scratch", path_on_host="foo.bar")

tests/integration_tests/functional/test_drives.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def test_partuuid_boot(test_microvm_with_partuuid):
191191
test_microvm.add_drive(
192192
"rootfs",
193193
test_microvm.rootfs_file,
194-
root_device=True,
194+
is_root_device=True,
195195
partuuid=PARTUUID[platform.machine()],
196196
)
197197

@@ -219,14 +219,14 @@ def test_partuuid_update(test_microvm_with_api):
219219

220220
# Add the root block device specified through PARTUUID.
221221
test_microvm.add_drive(
222-
"rootfs", test_microvm.rootfs_file, root_device=True, partuuid="0eaa91a0-01"
222+
"rootfs", test_microvm.rootfs_file, is_root_device=True, partuuid="0eaa91a0-01"
223223
)
224224

225225
# Update the root block device to boot from /dev/vda.
226226
test_microvm.add_drive(
227227
"rootfs",
228228
test_microvm.rootfs_file,
229-
root_device=True,
229+
is_root_device=True,
230230
)
231231

232232
test_microvm.start()
@@ -289,7 +289,7 @@ def test_no_flush(test_microvm_with_api):
289289
test_microvm.add_drive(
290290
"rootfs",
291291
test_microvm.rootfs_file,
292-
root_device=True,
292+
is_root_device=True,
293293
)
294294

295295
# Configure the metrics.
@@ -331,7 +331,7 @@ def test_flush(test_microvm_with_api):
331331
test_microvm.add_drive(
332332
"rootfs",
333333
test_microvm.rootfs_file,
334-
root_device=True,
334+
is_root_device=True,
335335
cache_type="Writeback",
336336
)
337337

@@ -369,7 +369,7 @@ def test_block_default_cache_old_version(test_microvm_with_api):
369369
test_microvm.add_drive(
370370
"rootfs",
371371
test_microvm.rootfs_file,
372-
root_device=True,
372+
is_root_device=True,
373373
cache_type="Writeback",
374374
)
375375

0 commit comments

Comments
 (0)