9 use PVE
::Storage
::Plugin
;
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 = {};
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"
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";
43 $vol_snapshot->{$volid} = $snapname;
47 die "volume snapshot disabled\n";
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"
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";
63 $vol_snapshot_delete->{$volid} = $snapname;
67 die "volume snapshot delete disabled\n";
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"
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";
83 $vol_snapshot_rollback->{$volid} = $snapname;
87 die "volume snapshot rollback disabled\n";
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"
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";
104 sub mocked_run_command
{
105 my ($cmd, %param) = @_;
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";
113 if ($cmdstring =~ m/.*\/lxc-stop
.*--kill.*/) {
114 if ($kill_possible) {
122 die "unexpected run_command call: '$cmdstring', aborting\n";
128 my ($exp_fn, $real_fn) = @_;
131 $ret = system("diff -u '$exp_fn' '$real_fn'");
137 sub testcase_prepare
{
138 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_;
139 subtest
"Preparing snapshot '$snapname' for vm '$vmid'" => sub {
143 PVE
::LXC
::snapshot_prepare
($vmid, $snapname, $save_vmstate, $comment);
145 is($@, $exp_err, "\$@ correct");
146 ok
(test_file
("snapshot-expected/prepare/lxc/$vmid.conf", "snapshot-working/prepare/lxc/$vmid.conf"), "config file correct");
150 sub testcase_commit
{
151 my ($vmid, $snapname, $exp_err) = @_;
152 subtest
"Committing snapshot '$snapname' for vm '$vmid'" => sub {
156 PVE
::LXC
::snapshot_commit
($vmid, $snapname);
158 is($@, $exp_err, "\$@ correct");
159 ok
(test_file
("snapshot-expected/commit/lxc/$vmid.conf", "snapshot-working/commit/lxc/$vmid.conf"), "config file correct");
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 {
168 $vol_snapshot_delete = {};
169 $exp_vol_snap = {} if !defined($exp_vol_snap);
170 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
173 PVE
::LXC
::snapshot_create
($vmid, $snapname, $save_vmstate, $comment);
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");
182 sub testcase_delete
{
183 my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_;
184 subtest
"Deleting snapshot '$snapname' of vm '$vmid'" => sub {
186 $vol_snapshot_delete = {};
187 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
190 PVE
::LXC
::snapshot_delete
($vmid, $snapname, $force);
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");
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 {
202 $vol_snapshot_rollback = {};
204 $exp_vol_snap_rollback = {} if !defined($exp_vol_snap_rollback);
207 PVE
::LXC
::snapshot_rollback
($vmid, $snapname);
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");
215 # BEGIN redefine PVE::LXC methods
216 sub config_file_lock
{
217 return "snapshot-working/pve-test.lock";
220 sub cfs_config_path
{
221 my ($vmid, $node) = @_;
223 $node = $nodename if !$node;
224 return "snapshot-working/$node/lxc/$vmid.conf";
228 my ($vmid, $node) = @_;
230 my $filename = cfs_config_path
($vmid, $node);
232 my $raw = PVE
::Tools
::file_get_contents
($filename);
234 my $conf = PVE
::LXC
::parse_pct_config
($filename, $raw);
239 my ($vmid, $conf) = @_;
241 my $filename = cfs_config_path
($vmid);
243 if ($conf->{snapshots
}) {
244 foreach my $snapname (keys %{$conf->{snapshots
}}) {
245 $conf->{snapshots
}->{$snapname}->{snaptime
} = "1234567890"
246 if $conf->{snapshots
}->{$snapname}->{snaptime
};
250 my $raw = PVE
::LXC
::write_pct_config
($filename, $conf);
252 PVE
::Tools
::file_set_contents
($filename, $raw);
256 my ($feature, $conf, $storecfg, $snapname) = @_;
257 return $snapshot_possible;
264 sub sync_container_namespace
{
268 # END redefine PVE::LXC methods
270 PVE
::Tools
::run_command
("rm -rf snapshot-working");
271 PVE
::Tools
::run_command
("cp -a snapshot-input snapshot-working");
274 $freeze_possible = 1;
277 printf("Running prepare tests\n");
279 $nodename = "prepare";
282 printf("Setting has_feature to return true\n");
284 $snapshot_possible = 1;
286 printf("Successful snapshot_prepare with no existing snapshots\n");
287 testcase_prepare
("101", "test", 0, "test comment", '');
289 printf("Successful snapshot_prepare with one existing snapshot\n");
290 testcase_prepare
("102", "test2", 0, "test comment", "");
292 printf("Expected error for snapshot_prepare on locked container\n");
293 testcase_prepare
("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
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");
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");
302 printf("Setting has_feature to return false\n");
304 $snapshot_possible = 0;
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");
310 printf("Running commit tests\n");
312 $nodename = "commit";
315 printf("Setting has_feature to return true\n");
317 $snapshot_possible = 1;
319 printf("Successful snapshot_commit with one prepared snapshot\n");
320 testcase_commit
("101", "test", "");
322 printf("Successful snapshot_commit with one committed and one prepared snapshot\n");
323 testcase_commit
("102", "test2", "");
325 printf("Expected error for snapshot_commit with no snapshot lock\n");
326 testcase_commit
("201", "test", "missing snapshot lock\n");
328 printf("Expected error for snapshot_commit with invalid snapshot name\n");
329 testcase_commit
("202", "test", "snapshot 'test' does not exist\n");
331 printf("Expected error for snapshot_commit with invalid snapshot state\n");
332 testcase_commit
("203", "test", "wrong snapshot state\n");
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;
345 # possible, but fails
346 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
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");
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");
364 $nodename = "create";
366 printf("Running create tests\n");
369 printf("Successful snapshot_create with no existing snapshots\n");
370 testcase_create
("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
372 printf("Successful snapshot_create with one existing snapshots\n");
373 testcase_create
("102", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
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" });
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");
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;
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" });
389 $nodename = "delete";
391 printf("Running delete tests\n");
394 printf("Successful snapshot_delete of only existing snapshot\n");
395 testcase_delete
("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
397 printf("Successful snapshot_delete of leaf snapshot\n");
398 testcase_delete
("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
400 printf("Successful snapshot_delete of root snapshot\n");
401 testcase_delete
("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
403 printf("Successful snapshot_delete of intermediate snapshot\n");
404 testcase_delete
("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
406 printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1\n");
407 testcase_delete
("105", "test", 1, "");
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" });
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");
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" });
418 printf("Expected error for snapshot_delete with locked config\n");
419 testcase_delete
("202", "test", 0, "VM is locked (backup)\n");
421 $nodename = "rollback";
423 printf("Running rollback tests\n");
428 printf("Successful snapshot_rollback to only existing snapshot\n");
429 testcase_rollback
("101", "test", "", { "local:snapshotable-disk-1" => "test" });
431 printf("Successful snapshot_rollback to leaf snapshot\n");
432 testcase_rollback
("102", "test2", "", { "local:snapshotable-disk-1" => "test2" });
434 printf("Successful snapshot_rollback to root snapshot\n");
435 testcase_rollback
("103", "test", "", { "local:snapshotable-disk-1" => "test" });
437 printf("Successful snapshot_rollback to intermediate snapshot\n");
438 testcase_rollback
("104", "test2", "", { "local:snapshotable-disk-1" => "test2" });
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" });
443 printf("Expected error for snapshot_rollback with non-existing snapshot\n");
444 testcase_rollback
("201", "test2", "snapshot 'test2' does not exist\n");
446 printf("Expected error for snapshot_rollback if volume rollback not possible\n");
447 testcase_rollback
("202", "test", "volume_rollback_is_possible failed\n");
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");
452 printf("Expected error for snapshot_rollback with lock\n");
453 testcase_rollback
("204", "test", "VM is locked (backup)\n");
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" });
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");
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");
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" });