Skip to content

Commit 85622c6

Browse files
roypatzulinx86
authored andcommitted
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 d28f380 commit 85622c6

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

tests/framework/microvm.py

Lines changed: 18 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,35 @@ 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+
# When doing diff snapshots, we continuously overwrite the same base snapshot file from the first
1189+
# iteration in-place with successive snapshots, so don't delete it!
1190+
if last_snapshot is not None and not last_snapshot.is_diff:
1191+
last_snapshot.delete()
11881192

1189-
if snapshot.is_diff:
1190-
new_snapshot = new_snapshot.rebase_snapshot(
1191-
snapshot, use_snapshot_editor
1193+
next_snapshot = microvm.make_snapshot(current_snapshot.snapshot_type)
1194+
1195+
if current_snapshot.is_diff:
1196+
next_snapshot = next_snapshot.rebase_snapshot(
1197+
current_snapshot, use_snapshot_editor
11921198
)
11931199

1194-
snapshot = new_snapshot
1200+
last_snapshot = current_snapshot
1201+
current_snapshot = next_snapshot
11951202

11961203
microvm.kill()
11971204
snapshot_copy.delete()
11981205

1199-
snapshot.delete()
1206+
if last_snapshot is not None and not last_snapshot.is_diff:
1207+
last_snapshot.delete()
1208+
current_snapshot.delete()
12001209

12011210
def kill(self):
12021211
"""Clean up all built VMs"""

0 commit comments

Comments
 (0)