Skip to content

Commit 7ba6f92

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 f903eb0 commit 7ba6f92

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/framework/microvm.py

Lines changed: 45 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

@@ -1116,6 +1117,50 @@ def build_from_snapshot(self, snapshot: Snapshot):
11161117
vm.restore_from_snapshot(snapshot, resume=True)
11171118
return vm
11181119

1120+
def build_n_from_snapshot(
1121+
self,
1122+
snapshot,
1123+
nr_vms,
1124+
*,
1125+
uffd_handler_name=None,
1126+
incremental=False,
1127+
use_snapshot_editor=True,
1128+
):
1129+
"""A generator of `n` microvms restored, either all restored from the same given snapshot
1130+
(incremental=False), or created by taking successive snapshots of restored VMs
1131+
"""
1132+
for _ in range(nr_vms):
1133+
microvm = self.build()
1134+
microvm.spawn(emit_metrics=True)
1135+
1136+
uffd_path = None
1137+
if uffd_handler_name is not None:
1138+
pf_handler = spawn_pf_handler(
1139+
microvm, uffd_handler(uffd_handler_name), snapshot.mem
1140+
)
1141+
uffd_path = pf_handler.socket_path
1142+
1143+
snapshot_copy = microvm.restore_from_snapshot(
1144+
snapshot, resume=True, uffd_path=uffd_path
1145+
)
1146+
1147+
yield microvm
1148+
1149+
if incremental:
1150+
new_snapshot = microvm.make_snapshot(snapshot.snapshot_type)
1151+
1152+
if snapshot.is_diff:
1153+
new_snapshot = new_snapshot.rebase_snapshot(
1154+
snapshot, use_snapshot_editor
1155+
)
1156+
1157+
snapshot = new_snapshot
1158+
1159+
microvm.kill()
1160+
snapshot_copy.delete()
1161+
1162+
snapshot.delete()
1163+
11191164
def kill(self):
11201165
"""Clean up all built VMs"""
11211166
for vm in self.vms:

0 commit comments

Comments
 (0)