Skip to content

Commit cf48f33

Browse files
committed
test: Add MicroVMFactory.build_n_from_snapshot
Add a utility function that allows building multiple microvms from the same snapshot sequentially. Signed-off-by: Patrick Roy <[email protected]>
1 parent 11b2675 commit cf48f33

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/framework/microvm.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from framework.microvm_helpers import MicrovmHelpers
3939
from framework.properties import global_props
4040
from framework.utils_drive import VhostUserBlkBackend, VhostUserBlkBackendType
41+
from framework.utils_uffd import spawn_pf_handler, uffd_handler
4142
from host_tools.fcmetrics import FCMetricsMonitor
4243
from host_tools.memory import MemoryMonitor
4344

@@ -1128,6 +1129,52 @@ def build_from_snapshot(self, snapshot: Snapshot):
11281129
vm.restore_from_snapshot(snapshot, resume=True)
11291130
return vm
11301131

1132+
def build_n_from_snapshot(
1133+
self,
1134+
snapshot,
1135+
nr_vms,
1136+
*,
1137+
uffd_handler_name=None,
1138+
incremental=False,
1139+
use_snapshot_editor=True,
1140+
):
1141+
"""A generator of `n` microvms restored, either all restored from the same given snapshot
1142+
(incremental=False), or created by taking successive snapshots of restored VMs
1143+
"""
1144+
for _ in range(nr_vms):
1145+
microvm = self.build()
1146+
microvm.spawn(emit_metrics=True)
1147+
1148+
uffd_path = None
1149+
if uffd_handler_name is not None:
1150+
pf_handler = spawn_pf_handler(
1151+
microvm,
1152+
uffd_handler(uffd_handler_name, binary_dir=self.binary_path),
1153+
snapshot.mem,
1154+
)
1155+
uffd_path = pf_handler.socket_path
1156+
1157+
snapshot_copy = microvm.restore_from_snapshot(
1158+
snapshot, resume=True, uffd_path=uffd_path
1159+
)
1160+
1161+
yield microvm
1162+
1163+
if incremental:
1164+
new_snapshot = microvm.make_snapshot(snapshot.snapshot_type)
1165+
1166+
if snapshot.is_diff:
1167+
new_snapshot = new_snapshot.rebase_snapshot(
1168+
snapshot, use_snapshot_editor
1169+
)
1170+
1171+
snapshot = new_snapshot
1172+
1173+
microvm.kill()
1174+
snapshot_copy.delete()
1175+
1176+
snapshot.delete()
1177+
11311178
def kill(self):
11321179
"""Clean up all built VMs"""
11331180
for vm in self.vms:

0 commit comments

Comments
 (0)