Skip to content

Commit 565afda

Browse files
committed
test(build_n_from_snapshot): Delete snapshot n after iteration n+2
If build_n_from_snapshot is asked to build many snapshots incrementally, it doesn't clean up after itself properly: Each iteration ends by creating a new snapshot of the VM, and this snapshot is passed to iteration n+1. Here, a copy is created inside the new VMs chroot, and iteration n+1 ends by creating a new snapshot for iteration n+2, and deleting the copy of the snapshot inside the chroot. However, we never delete the snapshot created in iteration n, and so with each iteration more snapshots accumulate. Fix this by having the function delete the snapshot created in iteration n after iteration n+2 finished successfully. The idea here is that in case of failure, we will have the snapshot created in iteration n+1 (the one which caused a failure in n+2), and also the snapshot created in n (which was the last known snapshot to successfully go through a test iteration, namely iteration n+1). Signed-off-by: Patrick Roy <[email protected]>
1 parent 3103f2f commit 565afda

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tests/framework/microvm.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ def build_from_snapshot(self, snapshot: Snapshot):
11581158

11591159
def build_n_from_snapshot(
11601160
self,
1161-
snapshot,
1161+
current_snapshot,
11621162
nr_vms,
11631163
*,
11641164
uffd_handler_name=None,
@@ -1168,6 +1168,7 @@ def build_n_from_snapshot(
11681168
"""A generator of `n` microvms restored, either all restored from the same given snapshot
11691169
(incremental=False), or created by taking successive snapshots of restored VMs
11701170
"""
1171+
last_snapshot = None
11711172
for _ in range(nr_vms):
11721173
microvm = self.build()
11731174
microvm.spawn()
@@ -1176,27 +1177,33 @@ def build_n_from_snapshot(
11761177
spawn_pf_handler(
11771178
microvm,
11781179
uffd_handler(uffd_handler_name, binary_dir=self.binary_path),
1179-
snapshot,
1180+
current_snapshot,
11801181
)
11811182

1182-
snapshot_copy = microvm.restore_from_snapshot(snapshot, resume=True)
1183+
snapshot_copy = microvm.restore_from_snapshot(current_snapshot, resume=True)
11831184

11841185
yield microvm
11851186

11861187
if incremental:
1187-
new_snapshot = microvm.make_snapshot(snapshot.snapshot_type)
1188+
if last_snapshot is not None:
1189+
last_snapshot.delete()
11881190

1189-
if snapshot.is_diff:
1190-
new_snapshot = new_snapshot.rebase_snapshot(
1191-
snapshot, use_snapshot_editor
1191+
next_snapshot = microvm.make_snapshot(current_snapshot.snapshot_type)
1192+
1193+
if current_snapshot.is_diff:
1194+
next_snapshot = next_snapshot.rebase_snapshot(
1195+
current_snapshot, use_snapshot_editor
11921196
)
11931197

1194-
snapshot = new_snapshot
1198+
last_snapshot = current_snapshot
1199+
current_snapshot = next_snapshot
11951200

11961201
microvm.kill()
11971202
snapshot_copy.delete()
11981203

1199-
snapshot.delete()
1204+
if last_snapshot is not None:
1205+
last_snapshot.delete()
1206+
current_snapshot.delete()
12001207

12011208
def kill(self):
12021209
"""Clean up all built VMs"""

0 commit comments

Comments
 (0)