Skip to content

Commit 41f4359

Browse files
committed
tests: add helper method MicrovmFactory.build_from_snapshot
This is a fairly common repeated pattern, so extract it into a method to make writing tests simpler. Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent f4f7536 commit 41f4359

File tree

9 files changed

+31
-65
lines changed

9 files changed

+31
-65
lines changed

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,7 @@ def uvm_restored(microvm_factory, guest_kernel, rootfs, cpu_template):
477477
uvm = uvm_booted(microvm_factory, guest_kernel, rootfs, cpu_template)
478478
snapshot = uvm.snapshot_full()
479479
uvm.kill()
480-
uvm2 = microvm_factory.build()
481-
uvm2.spawn()
482-
uvm2.restore_from_snapshot(snapshot, resume=True)
480+
uvm2 = microvm_factory.build_from_snapshot(snapshot)
483481
uvm2.cpu_template_name = uvm.cpu_template_name
484482
return uvm2
485483

tests/framework/microvm.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,13 @@ def build(self, kernel=None, rootfs=None, **kwargs):
10841084
vm.ssh_key = ssh_key
10851085
return vm
10861086

1087+
def build_from_snapshot(self, snapshot: Snapshot):
1088+
"""Build a microvm from a snapshot"""
1089+
vm = self.build()
1090+
vm.spawn()
1091+
vm.restore_from_snapshot(snapshot, resume=True)
1092+
return vm
1093+
10871094
def kill(self):
10881095
"""Clean up all built VMs"""
10891096
for vm in self.vms:

tests/integration_tests/functional/test_api.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,7 @@ def test_get_full_config_after_restoring_snapshot(microvm_factory, uvm_nano):
11661166
]
11671167

11681168
snapshot = uvm_nano.snapshot_full()
1169-
uvm2 = microvm_factory.build()
1170-
uvm2.spawn()
1171-
uvm2.restore_from_snapshot(snapshot, resume=True)
1172-
1169+
uvm2 = microvm_factory.build_from_snapshot(snapshot)
11731170
expected_cfg = setup_cfg.copy()
11741171

11751172
# We expect boot-source to be set with the following values

tests/integration_tests/functional/test_balloon.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ def test_balloon_snapshot(microvm_factory, guest_kernel, rootfs):
484484
assert first_reading > second_reading
485485

486486
snapshot = vm.snapshot_full()
487-
microvm = microvm_factory.build()
488-
microvm.spawn()
489-
microvm.restore_from_snapshot(snapshot, resume=True)
487+
microvm = microvm_factory.build_from_snapshot(snapshot)
490488

491489
# Get the firecracker from snapshot pid, and open an ssh connection.
492490
firecracker_pid = microvm.firecracker_pid

tests/integration_tests/functional/test_cmd_line_parameters.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ def test_cli_metrics_if_resume_no_metrics(uvm_plain, microvm_factory):
9696
snapshot = uvm1.snapshot_full()
9797

9898
# When: restoring from the snapshot
99-
uvm2 = microvm_factory.build()
100-
uvm2.spawn()
101-
uvm2.restore_from_snapshot(snapshot)
99+
uvm2 = microvm_factory.build_from_snapshot(snapshot)
102100

103101
# Then: the old metrics configuration does not exist
104102
metrics2 = Path(uvm2.jailer.chroot_path()) / metrics_path.name

tests/integration_tests/functional/test_snapshot_basic.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,25 @@ def _get_guest_drive_size(ssh_connection, guest_dev_name="/dev/vdb"):
5050
return lines[1].strip()
5151

5252

53-
def test_resume_after_restoration(uvm_nano, microvm_factory):
54-
"""Tests snapshot is resumable after restoration.
53+
@pytest.mark.parametrize("resume_at_restore", [True, False])
54+
def test_resume(uvm_nano, microvm_factory, resume_at_restore):
55+
"""Tests snapshot is resumable at or after restoration.
5556
56-
Check that a restored microVM is resumable by calling PATCH /vm with Resumed
57-
after PUT /snapshot/load with `resume_vm=False`.
57+
Check that a restored microVM is resumable by either
58+
a. PUT /snapshot/load with `resume_vm=False`, then calling PATCH /vm resume=True
59+
b. PUT /snapshot/load with `resume_vm=True`
5860
"""
5961
vm = uvm_nano
6062
vm.add_net_iface()
6163
vm.start()
62-
63-
snapshot = vm.snapshot_full()
64-
65-
restored_vm = microvm_factory.build()
66-
restored_vm.spawn()
67-
restored_vm.restore_from_snapshot(snapshot)
68-
restored_vm.resume()
69-
70-
71-
def test_resume_at_restoration(uvm_nano, microvm_factory):
72-
"""Tests snapshot is resumable at restoration.
73-
74-
Check that a restored microVM is resumable by calling PUT /snapshot/load
75-
with `resume_vm=True`.
76-
"""
77-
vm = uvm_nano
78-
vm.add_net_iface()
79-
vm.start()
80-
8164
snapshot = vm.snapshot_full()
82-
8365
restored_vm = microvm_factory.build()
8466
restored_vm.spawn()
85-
restored_vm.restore_from_snapshot(snapshot, resume=True)
67+
restored_vm.restore_from_snapshot(snapshot, resume=resume_at_restore)
68+
if not resume_at_restore:
69+
assert restored_vm.state == "Paused"
70+
restored_vm.resume()
71+
assert restored_vm.state == "Running"
8672

8773

8874
def test_snapshot_current_version(uvm_nano):
@@ -228,9 +214,7 @@ def test_patch_drive_snapshot(uvm_nano, microvm_factory):
228214

229215
# Load snapshot in a new Firecracker microVM.
230216
logger.info("Load snapshot, mem %s", snapshot.mem)
231-
vm = microvm_factory.build()
232-
vm.spawn()
233-
vm.restore_from_snapshot(snapshot, resume=True)
217+
vm = microvm_factory.build_from_snapshot(snapshot)
234218

235219
# Attempt to connect to resumed microvm and verify the new microVM has the
236220
# right scratch drive.
@@ -319,9 +303,7 @@ def test_negative_postload_api(uvm_plain, microvm_factory):
319303
basevm.kill()
320304

321305
# Do not resume, just load, so we can still call APIs that work.
322-
microvm = microvm_factory.build()
323-
microvm.spawn()
324-
microvm.restore_from_snapshot(snapshot, resume=True)
306+
microvm = microvm_factory.build_from_snapshot(snapshot)
325307

326308
fail_msg = "The requested operation is not supported after starting the microVM"
327309
with pytest.raises(RuntimeError, match=fail_msg):
@@ -486,9 +468,7 @@ def test_diff_snapshot_overlay(guest_kernel, rootfs, microvm_factory):
486468

487469
assert not filecmp.cmp(merged_snapshot.mem, first_snapshot_backup, shallow=False)
488470

489-
new_vm = microvm_factory.build()
490-
new_vm.spawn()
491-
new_vm.restore_from_snapshot(merged_snapshot, resume=True)
471+
_ = microvm_factory.build_from_snapshot(merged_snapshot)
492472

493473
# Check that the restored VM works
494474

@@ -510,9 +490,7 @@ def test_snapshot_overwrite_self(guest_kernel, rootfs, microvm_factory):
510490
snapshot = base_vm.snapshot_full()
511491
base_vm.kill()
512492

513-
vm = microvm_factory.build()
514-
vm.spawn()
515-
vm.restore_from_snapshot(snapshot, resume=True)
493+
vm = microvm_factory.build_from_snapshot(snapshot)
516494

517495
# When restoring a snapshot, vm.restore_from_snapshot first copies
518496
# the memory file (inside of the jailer) to /mem.src

tests/integration_tests/functional/test_snapshot_editor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,4 @@ def test_remove_regs(uvm_nano, microvm_factory):
6868
assert MIDR_EL1 not in stdout
6969

7070
# test that we can restore from a snapshot
71-
new_vm = microvm_factory.build()
72-
new_vm.spawn()
73-
new_vm.restore_from_snapshot(snapshot, resume=True)
71+
_ = microvm_factory.build_from_snapshot(snapshot)

tests/integration_tests/functional/test_snapshot_not_losing_dirty_pages.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ def test_diff_snapshot_works_after_error(
6363

6464
# Now there is enough space for it to work
6565
snap2 = uvm.snapshot_diff()
66-
67-
vm2 = microvm_factory.build()
68-
vm2.spawn()
69-
vm2.restore_from_snapshot(snap2, resume=True)
70-
7166
uvm.kill()
67+
68+
_vm2 = microvm_factory.build_from_snapshot(snap2)

tests/integration_tests/functional/test_vsock.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ def test_vsock_transport_reset_h2g(
199199
test_vm.kill()
200200

201201
# Load snapshot.
202-
203-
vm2 = microvm_factory.build()
204-
vm2.spawn()
205-
vm2.restore_from_snapshot(snapshot, resume=True)
202+
vm2 = microvm_factory.build_from_snapshot(snapshot)
206203

207204
# Check that vsock device still works.
208205
# Test guest-initiated connections.
@@ -231,9 +228,7 @@ def test_vsock_transport_reset_g2h(uvm_nano, microvm_factory):
231228

232229
for _ in range(5):
233230
# Load snapshot.
234-
new_vm = microvm_factory.build()
235-
new_vm.spawn()
236-
new_vm.restore_from_snapshot(snapshot, resume=True)
231+
new_vm = microvm_factory.build_from_snapshot(snapshot)
237232

238233
# After snap restore all vsock connections should be
239234
# dropped. This means guest socat should exit same way

0 commit comments

Comments
 (0)