]> git.proxmox.com Git - pve-container.git/blob - src/test/snapshot-test.pm
c0504f65911bd8f3c978f057d35f5db6fcfedb5e
[pve-container.git] / src / test / snapshot-test.pm
1 package PVE::LXC;
2
3 use strict;
4 use warnings;
5
6 use lib qw(..);
7
8 use PVE::Storage;
9 use PVE::Storage::Plugin;
10 use PVE::LXC;
11 use PVE::Tools;
12
13 use Test::MockModule;
14 use Test::More;
15
16 my $nodename;
17 my $snapshot_possible;
18 my $vol_snapshot_possible = {};
19 my $vol_snapshot_delete_possible = {};
20 my $vol_snapshot_rollback_possible = {};
21 my $vol_snapshot_rollback_enabled = {};
22 my $vol_snapshot = {};
23 my $vol_snapshot_delete = {};
24 my $vol_snapshot_rollback = {};
25 my $running;
26 my $freeze_possible;
27 my $kill_possible;
28
29 # Mocked methods
30
31 sub mocked_volume_snapshot {
32 my ($storecfg, $volid, $snapname) = @_;
33 die "Storage config not mocked! aborting\n"
34 if defined($storecfg);
35 die "volid undefined\n"
36 if !defined($volid);
37 die "snapname undefined\n"
38 if !defined($snapname);
39 if ($vol_snapshot_possible->{$volid}) {
40 if (defined($vol_snapshot->{$volid})) {
41 $vol_snapshot->{$volid} .= ",$snapname";
42 } else {
43 $vol_snapshot->{$volid} = $snapname;
44 }
45 return 1;
46 } else {
47 die "volume snapshot disabled\n";
48 }
49 }
50
51 sub mocked_volume_snapshot_delete {
52 my ($storecfg, $volid, $snapname) = @_;
53 die "Storage config not mocked! aborting\n"
54 if defined($storecfg);
55 die "volid undefined\n"
56 if !defined($volid);
57 die "snapname undefined\n"
58 if !defined($snapname);
59 if ($vol_snapshot_delete_possible->{$volid}) {
60 if (defined($vol_snapshot_delete->{$volid})) {
61 $vol_snapshot_delete->{$volid} .= ",$snapname";
62 } else {
63 $vol_snapshot_delete->{$volid} = $snapname;
64 }
65 return 1;
66 } else {
67 die "volume snapshot delete disabled\n";
68 }
69 }
70
71 sub mocked_volume_snapshot_rollback {
72 my ($storecfg, $volid, $snapname) = @_;
73 die "Storage config not mocked! aborting\n"
74 if defined($storecfg);
75 die "volid undefined\n"
76 if !defined($volid);
77 die "snapname undefined\n"
78 if !defined($snapname);
79 if ($vol_snapshot_rollback_enabled->{$volid}) {
80 if (defined($vol_snapshot_rollback->{$volid})) {
81 $vol_snapshot_rollback->{$volid} .= ",$snapname";
82 } else {
83 $vol_snapshot_rollback->{$volid} = $snapname;
84 }
85 return 1;
86 } else {
87 die "volume snapshot rollback disabled\n";
88 }
89 }
90
91 sub mocked_volume_rollback_is_possible {
92 my ($storecfg, $volid, $snapname) = @_;
93 die "Storage config not mocked! aborting\n"
94 if defined($storecfg);
95 die "volid undefined\n"
96 if !defined($volid);
97 die "snapname undefined\n"
98 if !defined($snapname);
99 return $vol_snapshot_rollback_possible->{$volid}
100 if ($vol_snapshot_rollback_possible->{$volid});
101 die "volume_rollback_is_possible failed\n";
102 }
103
104 sub mocked_run_command {
105 my ($cmd, %param) = @_;
106 my $cmdstring;
107 if (my $ref = ref($cmd)) {
108 $cmdstring = PVE::Tools::cmd2string($cmd);
109 if ($cmdstring =~ m/.*\/lxc-(un)?freeze.*/) {
110 return 1 if $freeze_possible;
111 die "lxc-[un]freeze disabled\n";
112 }
113 if ($cmdstring =~ m/.*\/lxc-stop.*--kill.*/) {
114 if ($kill_possible) {
115 $running = 0;
116 return 1;
117 } else {
118 return 0;
119 }
120 }
121 }
122 die "unexpected run_command call: '$cmdstring', aborting\n";
123 }
124
125 # Testing methods
126
127 sub test_file {
128 my ($exp_fn, $real_fn) = @_;
129 my $ret;
130 eval {
131 $ret = system("diff -u '$exp_fn' '$real_fn'");
132 };
133 die if $@;
134 return !$ret;
135 }
136
137 sub testcase_prepare {
138 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_;
139 subtest "Preparing snapshot '$snapname' for vm '$vmid'" => sub {
140 plan tests => 2;
141 $@ = undef;
142 eval {
143 PVE::LXC::snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
144 };
145 is($@, $exp_err, "\$@ correct");
146 ok(test_file("snapshot-expected/prepare/lxc/$vmid.conf", "snapshot-working/prepare/lxc/$vmid.conf"), "config file correct");
147 };
148 }
149
150 sub testcase_commit {
151 my ($vmid, $snapname, $exp_err) = @_;
152 subtest "Committing snapshot '$snapname' for vm '$vmid'" => sub {
153 plan tests => 2;
154 $@ = undef;
155 eval {
156 PVE::LXC::snapshot_commit($vmid, $snapname);
157 };
158 is($@, $exp_err, "\$@ correct");
159 ok(test_file("snapshot-expected/commit/lxc/$vmid.conf", "snapshot-working/commit/lxc/$vmid.conf"), "config file correct");
160 }
161 }
162
163 sub testcase_create {
164 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err, $exp_vol_snap, $exp_vol_snap_delete) = @_;
165 subtest "Creating snapshot '$snapname' for vm '$vmid'" => sub {
166 plan tests => 4;
167 $vol_snapshot = {};
168 $vol_snapshot_delete = {};
169 $exp_vol_snap = {} if !defined($exp_vol_snap);
170 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
171 $@ = undef;
172 eval {
173 PVE::LXC::snapshot_create($vmid, $snapname, $save_vmstate, $comment);
174 };
175 is($@, $exp_err, "\$@ correct");
176 is_deeply($vol_snapshot, $exp_vol_snap, "created correct volume snapshots");
177 is_deeply($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
178 ok(test_file("snapshot-expected/create/lxc/$vmid.conf", "snapshot-working/create/lxc/$vmid.conf"), "config file correct");
179 };
180 }
181
182 sub testcase_delete {
183 my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_;
184 subtest "Deleting snapshot '$snapname' of vm '$vmid'" => sub {
185 plan tests => 3;
186 $vol_snapshot_delete = {};
187 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
188 $@ = undef;
189 eval {
190 PVE::LXC::snapshot_delete($vmid, $snapname, $force);
191 };
192 is($@, $exp_err, "\$@ correct");
193 is_deeply($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
194 ok(test_file("snapshot-expected/delete/lxc/$vmid.conf", "snapshot-working/delete/lxc/$vmid.conf"), "config file correct");
195 };
196 }
197
198 sub testcase_rollback {
199 my ($vmid, $snapname, $exp_err, $exp_vol_snap_rollback) = @_;
200 subtest "Rolling back to snapshot '$snapname' of vm '$vmid'" => sub {
201 plan tests => 3;
202 $vol_snapshot_rollback = {};
203 $running = 1;
204 $exp_vol_snap_rollback = {} if !defined($exp_vol_snap_rollback);
205 $@ = undef;
206 eval {
207 PVE::LXC::snapshot_rollback($vmid, $snapname);
208 };
209 is($@, $exp_err, "\$@ correct");
210 is_deeply($vol_snapshot_rollback, $exp_vol_snap_rollback, "rolled back to correct volume snapshots");
211 ok(test_file("snapshot-expected/rollback/lxc/$vmid.conf", "snapshot-working/rollback/lxc/$vmid.conf"), "config file correct");
212 };
213 }
214
215 # BEGIN redefine PVE::LXC methods
216 sub config_file_lock {
217 return "snapshot-working/pve-test.lock";
218 }
219
220 sub cfs_config_path {
221 my ($vmid, $node) = @_;
222
223 $node = $nodename if !$node;
224 return "snapshot-working/$node/lxc/$vmid.conf";
225 }
226
227 sub load_config {
228 my ($vmid, $node) = @_;
229
230 my $filename = cfs_config_path($vmid, $node);
231
232 my $raw = PVE::Tools::file_get_contents($filename);
233
234 my $conf = PVE::LXC::parse_pct_config($filename, $raw);
235 return $conf;
236 }
237
238 sub write_config {
239 my ($vmid, $conf) = @_;
240
241 my $filename = cfs_config_path($vmid);
242
243 if ($conf->{snapshots}) {
244 foreach my $snapname (keys %{$conf->{snapshots}}) {
245 $conf->{snapshots}->{$snapname}->{snaptime} = "1234567890"
246 if $conf->{snapshots}->{$snapname}->{snaptime};
247 }
248 }
249
250 my $raw = PVE::LXC::write_pct_config($filename, $conf);
251
252 PVE::Tools::file_set_contents($filename, $raw);
253 }
254
255 sub has_feature {
256 my ($feature, $conf, $storecfg, $snapname) = @_;
257 return $snapshot_possible;
258 }
259
260 sub check_running {
261 return $running;
262 }
263
264 sub sync_container_namespace {
265 return;
266 }
267
268 # END redefine PVE::LXC methods
269
270 PVE::Tools::run_command("rm -rf snapshot-working");
271 PVE::Tools::run_command("cp -a snapshot-input snapshot-working");
272
273 $running = 1;
274 $freeze_possible = 1;
275
276 printf("\n");
277 printf("Running prepare tests\n");
278 printf("\n");
279 $nodename = "prepare";
280
281 printf("\n");
282 printf("Setting has_feature to return true\n");
283 printf("\n");
284 $snapshot_possible = 1;
285
286 printf("Successful snapshot_prepare with no existing snapshots\n");
287 testcase_prepare("101", "test", 0, "test comment", '');
288
289 printf("Successful snapshot_prepare with one existing snapshot\n");
290 testcase_prepare("102", "test2", 0, "test comment", "");
291
292 printf("Expected error for snapshot_prepare on locked container\n");
293 testcase_prepare("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
294
295 printf("Expected error for snapshot_prepare with duplicate snapshot name\n");
296 testcase_prepare("201", "test", 0, "test comment", "snapshot name 'test' already used\n");
297
298 printf("Expected error for snapshot_prepare with save_vmstate\n");
299 testcase_prepare("202", "test", 1, "test comment", "implement me - snapshot_save_vmstate\n");
300
301 printf("\n");
302 printf("Setting has_feature to return false\n");
303 printf("\n");
304 $snapshot_possible = 0;
305
306 printf("Expected error for snapshot_prepare if snapshots not possible\n");
307 testcase_prepare("300", "test", 0, "test comment", "snapshot feature is not available\n");
308
309 printf("\n");
310 printf("Running commit tests\n");
311 printf("\n");
312 $nodename = "commit";
313
314 printf("\n");
315 printf("Setting has_feature to return true\n");
316 printf("\n");
317 $snapshot_possible = 1;
318
319 printf("Successful snapshot_commit with one prepared snapshot\n");
320 testcase_commit("101", "test", "");
321
322 printf("Successful snapshot_commit with one committed and one prepared snapshot\n");
323 testcase_commit("102", "test2", "");
324
325 printf("Expected error for snapshot_commit with no snapshot lock\n");
326 testcase_commit("201", "test", "missing snapshot lock\n");
327
328 printf("Expected error for snapshot_commit with invalid snapshot name\n");
329 testcase_commit("202", "test", "snapshot 'test' does not exist\n");
330
331 printf("Expected error for snapshot_commit with invalid snapshot state\n");
332 testcase_commit("203", "test", "wrong snapshot state\n");
333
334 $vol_snapshot_possible->{"local:snapshotable-disk-1"} = 1;
335 $vol_snapshot_possible->{"local:snapshotable-disk-2"} = 1;
336 $vol_snapshot_possible->{"local:snapshotable-disk-3"} = 1;
337 $vol_snapshot_delete_possible->{"local:snapshotable-disk-1"} = 1;
338 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-1"} = 1;
339 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-2"} = 1;
340 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-3"} = 1;
341 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-1"} = 1;
342 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-2"} = 1;
343 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-3"} = 1;
344
345 # possible, but fails
346 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
347
348 printf("\n");
349 printf("Setting up Mocking for PVE::Storage\n");
350 my $storage_module = new Test::MockModule('PVE::Storage');
351 $storage_module->mock('config', sub { return undef; });
352 $storage_module->mock('volume_snapshot', \&mocked_volume_snapshot);
353 $storage_module->mock('volume_snapshot_delete', \&mocked_volume_snapshot_delete);
354 $storage_module->mock('volume_snapshot_rollback', \&mocked_volume_snapshot_rollback);
355 $storage_module->mock('volume_rollback_is_possible', \&mocked_volume_rollback_is_possible);
356 printf("\tconfig(), volume_snapshot(), volume_snapshot_delete(), volume_snapshot_rollback() and volume_rollback_is_possible() mocked\n");
357
358 printf("\n");
359 printf("Setting up Mocking for PVE::Tools\n");
360 my $tools_module = new Test::MockModule('PVE::Tools');
361 $tools_module->mock('run_command' => \&mocked_run_command);
362 printf("\trun_command() mocked\n");
363
364 $nodename = "create";
365 printf("\n");
366 printf("Running create tests\n");
367 printf("\n");
368
369 printf("Successful snapshot_create with no existing snapshots\n");
370 testcase_create("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
371
372 printf("Successful snapshot_create with one existing snapshots\n");
373 testcase_create("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
374
375 printf("Successful snapshot_create with multiple mps\n");
376 testcase_create("103", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
377
378 printf("Expected error for snapshot_create when volume snapshot is not possible\n");
379 testcase_create("201", "test", 0, "test comment", "volume snapshot disabled\n\n");
380
381 printf("Expected error for snapshot_create with broken lxc-freeze\n");
382 $freeze_possible = 0;
383 testcase_create("202", "test", 0, "test comment", "lxc-[un]freeze disabled\n\n");
384 $freeze_possible = 1;
385
386 printf("Expected error for snapshot_create when mp volume snapshot is not possible\n");
387 testcase_create("203", "test", 0, "test comment", "volume snapshot disabled\n\n", { "local:snapshotable-disk-1" => "test" }, { "local:snapshotable-disk-1" => "test" });
388
389 $nodename = "delete";
390 printf("\n");
391 printf("Running delete tests\n");
392 printf("\n");
393
394 printf("Successful snapshot_delete of only existing snapshot\n");
395 testcase_delete("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
396
397 printf("Successful snapshot_delete of leaf snapshot\n");
398 testcase_delete("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
399
400 printf("Successful snapshot_delete of root snapshot\n");
401 testcase_delete("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
402
403 printf("Successful snapshot_delete of intermediate snapshot\n");
404 testcase_delete("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
405
406 printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1\n");
407 testcase_delete("105", "test", 1, "");
408
409 printf("Successful snapshot_delete with mp broken volume_snapshot_delete and force=1\n");
410 testcase_delete("106", "test", 1, "", { "local:snapshotable-disk-1" => "test" });
411
412 printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0\n");
413 testcase_delete("201", "test", 0, "volume snapshot delete disabled\n");
414
415 printf("Expected error when snapshot_delete fails with broken mp volume_snapshot_delete and force=0\n");
416 testcase_delete("203", "test", 0, "volume snapshot delete disabled\n", { "local:snapshotable-disk-1" => "test" });
417
418 printf("Expected error for snapshot_delete with locked config\n");
419 testcase_delete("202", "test", 0, "VM is locked (backup)\n");
420
421 $nodename = "rollback";
422 printf("\n");
423 printf("Running rollback tests\n");
424 printf("\n");
425
426 $kill_possible = 1;
427
428 printf("Successful snapshot_rollback to only existing snapshot\n");
429 testcase_rollback("101", "test", "", { "local:snapshotable-disk-1" => "test" });
430
431 printf("Successful snapshot_rollback to leaf snapshot\n");
432 testcase_rollback("102", "test2", "", { "local:snapshotable-disk-1" => "test2" });
433
434 printf("Successful snapshot_rollback to root snapshot\n");
435 testcase_rollback("103", "test", "", { "local:snapshotable-disk-1" => "test" });
436
437 printf("Successful snapshot_rollback to intermediate snapshot\n");
438 testcase_rollback("104", "test2", "", { "local:snapshotable-disk-1" => "test2" });
439
440 printf("Successful snapshot_rollback with multiple mp\n");
441 testcase_rollback("105", "test", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
442
443 printf("Expected error for snapshot_rollback with non-existing snapshot\n");
444 testcase_rollback("201", "test2", "snapshot 'test2' does not exist\n");
445
446 printf("Expected error for snapshot_rollback if volume rollback not possible\n");
447 testcase_rollback("202", "test", "volume_rollback_is_possible failed\n");
448
449 printf("Expected error for snapshot_rollback with incomplete snapshot\n");
450 testcase_rollback("203", "test", "unable to rollback to incomplete snapshot (snapstate = delete)\n");
451
452 printf("Expected error for snapshot_rollback with lock\n");
453 testcase_rollback("204", "test", "VM is locked (backup)\n");
454
455 printf("Expected error for snapshot_rollback with saved vmstate\n");
456 testcase_rollback("205", "test", "implement me - save vmstate\n", { "local:snapshotable-disk-1" => "test" });
457
458 $kill_possible = 0;
459
460 printf("Expected error for snapshot_rollback with unkillable container\n");
461 testcase_rollback("206", "test", "unable to rollback vm 206: vm is running\n");
462
463 $kill_possible = 1;
464
465 printf("Expected error for snapshot_rollback with mp rollback_is_possible failure\n");
466 testcase_rollback("207", "test", "volume_rollback_is_possible failed\n");
467
468 printf("Expected error for snapshot_rollback with mp rollback failure (results in inconsistent state)\n");
469 testcase_rollback("208", "test", "volume snapshot rollback disabled\n", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test" });
470
471 done_testing();