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