1 package PVE
::QemuServer
;
9 use PVE
::Storage
::Plugin
;
18 my $snapshot_possible;
19 my $vol_snapshot_possible = {};
20 my $vol_snapshot_delete_possible = {};
21 my $vol_snapshot_rollback_possible = {};
22 my $vol_snapshot_rollback_enabled = {};
23 my $vol_snapshot = {};
24 my $vol_snapshot_delete = {};
25 my $vol_snapshot_rollback = {};
29 my $save_vmstate_works;
34 sub mocked_volume_snapshot
{
35 my ($storecfg, $volid, $snapname) = @_;
36 die "Storage config not mocked! aborting\n"
37 if defined($storecfg);
38 die "volid undefined\n"
40 die "snapname undefined\n"
41 if !defined($snapname);
42 if ($vol_snapshot_possible->{$volid}) {
43 if (defined($vol_snapshot->{$volid})) {
44 $vol_snapshot->{$volid} .= ",$snapname";
46 $vol_snapshot->{$volid} = $snapname;
50 die "volume snapshot disabled\n";
54 sub mocked_volume_snapshot_delete
{
55 my ($storecfg, $volid, $snapname) = @_;
56 die "Storage config not mocked! aborting\n"
57 if defined($storecfg);
58 die "volid undefined\n"
60 die "snapname undefined\n"
61 if !defined($snapname);
62 if ($vol_snapshot_delete_possible->{$volid}) {
63 if (defined($vol_snapshot_delete->{$volid})) {
64 $vol_snapshot_delete->{$volid} .= ",$snapname";
66 $vol_snapshot_delete->{$volid} = $snapname;
70 die "volume snapshot delete disabled\n";
74 sub mocked_volume_snapshot_rollback
{
75 my ($storecfg, $volid, $snapname) = @_;
76 die "Storage config not mocked! aborting\n"
77 if defined($storecfg);
78 die "volid undefined\n"
80 die "snapname undefined\n"
81 if !defined($snapname);
82 if ($vol_snapshot_rollback_enabled->{$volid}) {
83 if (defined($vol_snapshot_rollback->{$volid})) {
84 $vol_snapshot_rollback->{$volid} .= ",$snapname";
86 $vol_snapshot_rollback->{$volid} = $snapname;
90 die "volume snapshot rollback disabled\n";
94 sub mocked_volume_rollback_is_possible
{
95 my ($storecfg, $volid, $snapname) = @_;
96 die "Storage config not mocked! aborting\n"
97 if defined($storecfg);
98 die "volid undefined\n"
100 die "snapname undefined\n"
101 if !defined($snapname);
102 return $vol_snapshot_rollback_possible->{$volid}
103 if ($vol_snapshot_rollback_possible->{$volid});
104 die "volume_rollback_is_possible failed\n";
107 sub mocked_vdisk_free
{
108 my ($storecfg, $vmstate) = @_;
109 die "Storage config not mocked! aborting\n"
110 if defined($storecfg);
111 die "wrong vdisk - fake vmstate expected!\n"
112 if ($vmstate ne "somestorage:state-volume");
116 sub mocked_run_command
{
117 my ($cmd, %param) = @_;
119 if (my $ref = ref($cmd)) {
120 $cmdstring = PVE
::Tools
::cmd2string
($cmd);
121 if ($cmdstring =~ m/.*\/qemu-
(un
)?freeze
.*/) {
122 return 1 if $freeze_possible;
123 die "qemu-[un]freeze disabled\n";
125 if ($cmdstring =~ m/.*\/qemu-stop
.*--kill.*/) {
126 if ($stop_possible) {
134 die "unexpected run_command call: '$cmdstring', aborting\n";
140 my ($exp_fn, $real_fn) = @_;
143 $ret = system("diff -u '$exp_fn' '$real_fn'");
149 sub testcase_prepare
{
150 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_;
151 subtest
"Preparing snapshot '$snapname' for vm '$vmid'" => sub {
155 PVE
::QemuServer
::snapshot_prepare
($vmid, $snapname, $save_vmstate, $comment);
157 is($@, $exp_err, "\$@ correct");
158 ok
(test_file
("snapshot-expected/prepare/qemu-server/$vmid.conf", "snapshot-working/prepare/qemu-server/$vmid.conf"), "config file correct");
162 sub testcase_commit
{
163 my ($vmid, $snapname, $exp_err) = @_;
164 subtest
"Committing snapshot '$snapname' for vm '$vmid'" => sub {
168 PVE
::QemuServer
::snapshot_commit
($vmid, $snapname);
170 is($@, $exp_err, "\$@ correct");
171 ok
(test_file
("snapshot-expected/commit/qemu-server/$vmid.conf", "snapshot-working/commit/qemu-server/$vmid.conf"), "config file correct");
175 sub testcase_create
{
176 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err, $exp_vol_snap, $exp_vol_snap_delete) = @_;
177 subtest
"Creating snapshot '$snapname' for vm '$vmid'" => sub {
180 $vol_snapshot_delete = {};
181 $exp_vol_snap = {} if !defined($exp_vol_snap);
182 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
185 PVE
::QemuServer
::snapshot_create
($vmid, $snapname, $save_vmstate, $comment);
187 is($@, $exp_err, "\$@ correct");
188 is_deeply
($vol_snapshot, $exp_vol_snap, "created correct volume snapshots");
189 is_deeply
($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
190 ok
(test_file
("snapshot-expected/create/qemu-server/$vmid.conf", "snapshot-working/create/qemu-server/$vmid.conf"), "config file correct");
194 sub testcase_delete
{
195 my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_;
196 subtest
"Deleting snapshot '$snapname' of vm '$vmid'" => sub {
198 $vol_snapshot_delete = {};
199 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
202 PVE
::QemuServer
::snapshot_delete
($vmid, $snapname, $force);
204 is($@, $exp_err, "\$@ correct");
205 is_deeply
($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
206 ok
(test_file
("snapshot-expected/delete/qemu-server/$vmid.conf", "snapshot-working/delete/qemu-server/$vmid.conf"), "config file correct");
210 sub testcase_rollback
{
211 my ($vmid, $snapname, $exp_err, $exp_vol_snap_rollback) = @_;
212 subtest
"Rolling back to snapshot '$snapname' of vm '$vmid'" => sub {
214 $vol_snapshot_rollback = {};
216 $exp_vol_snap_rollback = {} if !defined($exp_vol_snap_rollback);
219 PVE
::QemuServer
::snapshot_rollback
($vmid, $snapname);
221 is($@, $exp_err, "\$@ correct");
222 is_deeply
($vol_snapshot_rollback, $exp_vol_snap_rollback, "rolled back to correct volume snapshots");
223 ok
(test_file
("snapshot-expected/rollback/qemu-server/$vmid.conf", "snapshot-working/rollback/qemu-server/$vmid.conf"), "config file correct");
227 # BEGIN mocked PVE::QemuConfig methods
228 sub config_file_lock
{
229 return "snapshot-working/pve-test.lock";
232 sub cfs_config_path
{
233 my ($class, $vmid, $node) = @_;
235 $node = $nodename if !$node;
236 return "snapshot-working/$node/qemu-server/$vmid.conf";
240 my ($class, $vmid, $node) = @_;
242 my $filename = $class->cfs_config_path($vmid, $node);
244 my $raw = PVE
::Tools
::file_get_contents
($filename);
246 my $conf = PVE
::QemuServer
::parse_vm_config
($filename, $raw);
251 my ($class, $vmid, $conf) = @_;
253 my $filename = $class->cfs_config_path($vmid);
255 if ($conf->{snapshots
}) {
256 foreach my $snapname (keys %{$conf->{snapshots
}}) {
257 $conf->{snapshots
}->{$snapname}->{snaptime
} = "1234567890"
258 if $conf->{snapshots
}->{$snapname}->{snaptime
};
262 my $raw = PVE
::QemuServer
::write_vm_config
($filename, $conf);
264 PVE
::Tools
::file_set_contents
($filename, $raw);
266 # END mocked PVE::QemuConfig methods
268 # BEGIN redefine PVE::QemuServer methods
270 my ($feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
271 return $snapshot_possible;
278 sub snapshot_save_vmstate
{
279 my ($vmid, $conf, $snapname, $storecfg) = @_;
280 die "save_vmstate failed\n"
281 if !$save_vmstate_works;
283 my $snap = $conf->{snapshots
}->{$snapname};
284 $snap->{vmstate
} = "somestorage:state-volume";
285 $snap->{machine
} = "somemachine";
288 sub do_snapshots_with_qemu
{
293 my ($vmid, $cmd, $nocheck) = @_;
295 my $exec = $cmd->{execute
};
296 if ($exec eq "delete-drive-snapshot") {
299 if ($exec eq "guest-ping") {
300 die "guest-ping disabled\n"
301 if !$vm_mon->{guest_ping
};
304 if ($exec eq "guest-fsfreeze-freeze" || $exec eq "guest-fsfreeze-thaw") {
305 die "freeze disabled\n"
306 if !$freeze_possible;
309 if ($exec eq "savevm-start") {
310 die "savevm-start disabled\n"
311 if !$vm_mon->{savevm_start
};
314 if ($exec eq "savevm-end") {
315 die "savevm-end disabled\n"
316 if !$vm_mon->{savevm_end
};
319 if ($exec eq "query-savevm") {
320 return { "status" => "completed" };
322 die "unexpected vm_qmp_command!\n";
326 my ($storecfg, $vmid, $statefile, $skiplock, $migratedfrom, $paused, $forcemachine) = @_;
328 die "Storage config not mocked! aborting\n"
329 if defined($storecfg);
331 die "statefile and forcemachine must be both defined or undefined! aborting\n"
332 if defined($statefile) xor defined($forcemachine);
338 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
346 # END redefine PVE::QemuServer methods
348 PVE
::Tools
::run_command
("rm -rf snapshot-working");
349 PVE
::Tools
::run_command
("cp -a snapshot-input snapshot-working");
351 my $qemu_config_module = new Test
::MockModule
('PVE::QemuConfig');
352 $qemu_config_module->mock('config_file_lock', \
&config_file_lock
);
353 $qemu_config_module->mock('cfs_config_path', \
&cfs_config_path
);
354 $qemu_config_module->mock('load_config', \
&load_config
);
355 $qemu_config_module->mock('write_config', \
&write_config
);
358 $freeze_possible = 1;
359 $save_vmstate_works = 1;
362 printf("Running prepare tests\n");
364 $nodename = "prepare";
367 printf("Setting has_feature to return true\n");
369 $snapshot_possible = 1;
371 printf("Successful snapshot_prepare with no existing snapshots\n");
372 testcase_prepare
("101", "test", 0, "test comment", '');
374 printf("Successful snapshot_prepare with no existing snapshots, including vmstate\n");
375 testcase_prepare
("102", "test", 1, "test comment", '');
377 printf("Successful snapshot_prepare with one existing snapshot\n");
378 testcase_prepare
("103", "test2", 0, "test comment", "");
380 printf("Successful snapshot_prepare with one existing snapshot, including vmstate\n");
381 testcase_prepare
("104", "test2", 1, "test comment", "");
383 printf("Expected error for snapshot_prepare on locked container\n");
384 testcase_prepare
("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
386 printf("Expected error for snapshot_prepare with duplicate snapshot name\n");
387 testcase_prepare
("201", "test", 0, "test comment", "snapshot name 'test' already used\n");
389 $save_vmstate_works = 0;
391 printf("Expected error for snapshot_prepare with failing save_vmstate\n");
392 testcase_prepare
("202", "test", 1, "test comment", "save_vmstate failed\n");
394 $save_vmstate_works = 1;
397 printf("Setting has_feature to return false\n");
399 $snapshot_possible = 0;
401 printf("Expected error for snapshot_prepare if snapshots not possible\n");
402 testcase_prepare
("300", "test", 0, "test comment", "snapshot feature is not available\n");
405 printf("Running commit tests\n");
407 $nodename = "commit";
410 printf("Setting has_feature to return true\n");
412 $snapshot_possible = 1;
414 printf("Successful snapshot_commit with one prepared snapshot\n");
415 testcase_commit
("101", "test", "");
417 printf("Successful snapshot_commit with one committed and one prepared snapshot\n");
418 testcase_commit
("102", "test2", "");
420 printf("Expected error for snapshot_commit with no snapshot lock\n");
421 testcase_commit
("201", "test", "missing snapshot lock\n");
423 printf("Expected error for snapshot_commit with invalid snapshot name\n");
424 testcase_commit
("202", "test", "snapshot 'test' does not exist\n");
426 printf("Expected error for snapshot_commit with invalid snapshot state\n");
427 testcase_commit
("203", "test", "wrong snapshot state\n");
429 $vol_snapshot_possible->{"local:snapshotable-disk-1"} = 1;
430 $vol_snapshot_possible->{"local:snapshotable-disk-2"} = 1;
431 $vol_snapshot_possible->{"local:snapshotable-disk-3"} = 1;
432 $vol_snapshot_delete_possible->{"local:snapshotable-disk-1"} = 1;
433 $vol_snapshot_delete_possible->{"local:snapshotable-disk-3"} = 1;
434 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-1"} = 1;
435 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-2"} = 1;
436 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-3"} = 1;
437 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-1"} = 1;
438 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-2"} = 1;
439 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-3"} = 1;
440 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
441 $vm_mon->{guest_ping
} = 1;
442 $vm_mon->{savevm_start
} = 1;
443 $vm_mon->{savevm_end
} = 1;
445 # possible, but fails
446 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
449 printf("Setting up Mocking for PVE::Storage\n");
450 my $storage_module = new Test
::MockModule
('PVE::Storage');
451 $storage_module->mock('config', sub { return undef; });
452 $storage_module->mock('path', sub { return "/some/store/statefile/path"; });
453 $storage_module->mock('vdisk_free', \
&mocked_vdisk_free
);
454 $storage_module->mock('volume_snapshot', \
&mocked_volume_snapshot
);
455 $storage_module->mock('volume_snapshot_delete', \
&mocked_volume_snapshot_delete
);
456 $storage_module->mock('volume_snapshot_rollback', \
&mocked_volume_snapshot_rollback
);
457 $storage_module->mock('volume_rollback_is_possible', \
&mocked_volume_rollback_is_possible
);
458 printf("\tconfig(), volume_snapshot(), volume_snapshot_delete(), volume_snapshot_rollback() and volume_rollback_is_possible() mocked\n");
461 #printf("Setting up Mocking for PVE::Tools\n");
462 #my $tools_module = new Test::MockModule('PVE::Tools');
463 #$tools_module->mock('run_command' => \&mocked_run_command);
464 #printf("\trun_command() mocked\n");
466 $nodename = "create";
468 printf("Running create tests\n");
471 printf("Successful snapshot_create with no existing snapshots\n");
472 testcase_create
("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
474 printf("Successful snapshot_create with no existing snapshots, including vmstate\n");
475 testcase_create
("102", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
477 printf("Successful snapshot_create with one existing snapshots\n");
478 testcase_create
("103", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
480 printf("Successful snapshot_create with one existing snapshots, including vmstate\n");
481 testcase_create
("104", "test2", 1, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
483 printf("Successful snapshot_create with multiple mps\n");
484 testcase_create
("105", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
486 $freeze_possible = 0;
487 printf("Successful snapshot_create with no existing snapshots and broken freeze\n");
488 testcase_create
("106", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
489 $freeze_possible = 1;
491 printf("Expected error for snapshot_create when volume snapshot is not possible\n");
492 testcase_create
("201", "test", 0, "test comment", "volume snapshot disabled\n");
494 printf("Expected error for snapshot_create when volume snapshot is not possible for one drive\n");
495 testcase_create
("202", "test", 0, "test comment", "volume snapshot disabled\n", { "local:snapshotable-disk-1" => "test" }, { "local:snapshotable-disk-1" => "test" });
497 $vm_mon->{savevm_start
} = 0;
498 printf("Expected error for snapshot_create when Qemu mon command 'savevm-start' fails\n");
499 testcase_create
("203", "test", 0, "test comment", "savevm-start disabled\n");
500 $vm_mon->{savevm_start
} = 1;
503 $nodename = "delete";
505 printf("Running delete tests\n");
508 printf("Successful snapshot_delete of only existing snapshot\n");
509 testcase_delete
("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
511 printf("Successful snapshot_delete of leaf snapshot\n");
512 testcase_delete
("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
514 printf("Successful snapshot_delete of root snapshot\n");
515 testcase_delete
("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
517 printf("Successful snapshot_delete of intermediate snapshot\n");
518 testcase_delete
("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
520 printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1\n");
521 testcase_delete
("105", "test", 1, "");
523 printf("Successful snapshot_delete with mp broken volume_snapshot_delete and force=1\n");
524 testcase_delete
("106", "test", 1, "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-3" => "test" });
526 printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0\n");
527 testcase_delete
("201", "test", 0, "volume snapshot delete disabled\n");
529 printf("Expected error when snapshot_delete fails with broken mp volume_snapshot_delete and force=0\n");
530 testcase_delete
("202", "test", 0, "volume snapshot delete disabled\n", { "local:snapshotable-disk-1" => "test" });
532 printf("Expected error for snapshot_delete with locked config\n");
533 testcase_delete
("203", "test", 0, "VM is locked (backup)\n");
535 $nodename = "rollback";
537 printf("Running rollback tests\n");
542 printf("Successful snapshot_rollback to only existing snapshot\n");
543 testcase_rollback
("101", "test", "", { "local:snapshotable-disk-1" => "test" });
545 printf("Successful snapshot_rollback to leaf snapshot\n");
546 testcase_rollback
("102", "test2", "", { "local:snapshotable-disk-1" => "test2" });
548 printf("Successful snapshot_rollback to root snapshot\n");
549 testcase_rollback
("103", "test", "", { "local:snapshotable-disk-1" => "test" });
551 printf("Successful snapshot_rollback to intermediate snapshot\n");
552 testcase_rollback
("104", "test2", "", { "local:snapshotable-disk-1" => "test2" });
554 printf("Successful snapshot_rollback with multiple mp\n");
555 testcase_rollback
("105", "test", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
557 printf("Successful snapshot_rollback to only existing snapshot, with saved vmstate and machine config\n");
558 testcase_rollback
("106", "test", "", { "local:snapshotable-disk-1" => "test" });
560 printf("Expected error for snapshot_rollback with non-existing snapshot\n");
561 testcase_rollback
("201", "test2", "snapshot 'test2' does not exist\n");
563 printf("Expected error for snapshot_rollback if volume rollback not possible\n");
564 testcase_rollback
("202", "test", "volume_rollback_is_possible failed\n");
566 printf("Expected error for snapshot_rollback with incomplete snapshot\n");
567 testcase_rollback
("203", "test", "unable to rollback to incomplete snapshot (snapstate = delete)\n");
569 printf("Expected error for snapshot_rollback with lock\n");
570 testcase_rollback
("204", "test", "VM is locked (backup)\n");
574 printf("Expected error for snapshot_rollback with unkillable container\n");
575 testcase_rollback
("205", "test", "unable to rollback vm 205: vm is running\n");
579 printf("Expected error for snapshot_rollback with mp rollback_is_possible failure\n");
580 testcase_rollback
("206", "test", "volume_rollback_is_possible failed\n");
582 printf("Expected error for snapshot_rollback with mp rollback failure (results in inconsistent state)\n");
583 testcase_rollback
("207", "test", "volume snapshot rollback disabled\n", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test" });