1 package PVE
::QemuServer
; ## no critic
9 use PVE
::Storage
::Plugin
;
13 use PVE
::ReplicationConfig
;
19 my $snapshot_possible;
20 my $vol_snapshot_possible = {};
21 my $vol_snapshot_delete_possible = {};
22 my $vol_snapshot_rollback_possible = {};
23 my $vol_snapshot_rollback_enabled = {};
24 my $vol_snapshot = {};
25 my $vol_snapshot_delete = {};
26 my $vol_snapshot_rollback = {};
30 my $save_vmstate_works;
35 sub mocked_volume_snapshot
{
36 my ($storecfg, $volid, $snapname) = @_;
37 die "Storage config not mocked! aborting\n"
38 if defined($storecfg);
39 die "volid undefined\n"
41 die "snapname undefined\n"
42 if !defined($snapname);
43 if ($vol_snapshot_possible->{$volid}) {
44 if (defined($vol_snapshot->{$volid})) {
45 $vol_snapshot->{$volid} .= ",$snapname";
47 $vol_snapshot->{$volid} = $snapname;
51 die "volume snapshot disabled\n";
55 sub mocked_volume_snapshot_delete
{
56 my ($storecfg, $volid, $snapname) = @_;
57 die "Storage config not mocked! aborting\n"
58 if defined($storecfg);
59 die "volid undefined\n"
61 die "snapname undefined\n"
62 if !defined($snapname);
63 if ($vol_snapshot_delete_possible->{$volid}) {
64 if (defined($vol_snapshot_delete->{$volid})) {
65 $vol_snapshot_delete->{$volid} .= ",$snapname";
67 $vol_snapshot_delete->{$volid} = $snapname;
71 die "volume snapshot delete disabled\n";
75 sub mocked_volume_snapshot_rollback
{
76 my ($storecfg, $volid, $snapname) = @_;
77 die "Storage config not mocked! aborting\n"
78 if defined($storecfg);
79 die "volid undefined\n"
81 die "snapname undefined\n"
82 if !defined($snapname);
83 if ($vol_snapshot_rollback_enabled->{$volid}) {
84 if (defined($vol_snapshot_rollback->{$volid})) {
85 $vol_snapshot_rollback->{$volid} .= ",$snapname";
87 $vol_snapshot_rollback->{$volid} = $snapname;
91 die "volume snapshot rollback disabled\n";
95 sub mocked_volume_rollback_is_possible
{
96 my ($storecfg, $volid, $snapname) = @_;
97 die "Storage config not mocked! aborting\n"
98 if defined($storecfg);
99 die "volid undefined\n"
101 die "snapname undefined\n"
102 if !defined($snapname);
103 return $vol_snapshot_rollback_possible->{$volid}
104 if ($vol_snapshot_rollback_possible->{$volid});
105 die "volume_rollback_is_possible failed\n";
108 sub mocked_activate_volumes
{
109 my ($storecfg, $volumes) = @_;
110 die "Storage config not mocked! aborting\n"
111 if defined($storecfg);
112 die "wrong volume - fake vmstate expected!\n"
113 if ((scalar @$volumes != 1) || @$volumes[0] ne "somestorage:state-volume");
117 sub mocked_deactivate_volumes
{
118 my ($storecfg, $volumes) = @_;
119 die "Storage config not mocked! aborting\n"
120 if defined($storecfg);
121 die "wrong volume - fake vmstate expected!\n"
122 if ((scalar @$volumes != 1) || @$volumes[0] ne "somestorage:state-volume");
126 sub mocked_vdisk_free
{
127 my ($storecfg, $vmstate) = @_;
128 die "Storage config not mocked! aborting\n"
129 if defined($storecfg);
130 die "wrong vdisk - fake vmstate expected!\n"
131 if ($vmstate ne "somestorage:state-volume");
135 sub mocked_run_command
{
136 my ($cmd, %param) = @_;
138 if (my $ref = ref($cmd)) {
139 $cmdstring = PVE
::Tools
::cmd2string
($cmd);
140 if ($cmdstring =~ m/.*\/qemu-
(un
)?freeze
.*/) {
141 return 1 if $freeze_possible;
142 die "qemu-[un]freeze disabled\n";
144 if ($cmdstring =~ m/.*\/qemu-stop
.*--kill.*/) {
145 if ($stop_possible) {
153 die "unexpected run_command call: '$cmdstring', aborting\n";
159 my ($exp_fn, $real_fn) = @_;
162 $ret = system("diff -u '$exp_fn' '$real_fn'");
168 sub testcase_prepare
{
169 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err) = @_;
170 subtest
"Preparing snapshot '$snapname' for vm '$vmid'" => sub {
174 PVE
::QemuConfig-
>__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
176 is($@, $exp_err, "\$@ correct");
177 ok
(test_file
("snapshot-expected/prepare/qemu-server/$vmid.conf", "snapshot-working/prepare/qemu-server/$vmid.conf"), "config file correct");
181 sub testcase_commit
{
182 my ($vmid, $snapname, $exp_err) = @_;
183 subtest
"Committing snapshot '$snapname' for vm '$vmid'" => sub {
187 PVE
::QemuConfig-
>__snapshot_commit($vmid, $snapname);
189 is($@, $exp_err, "\$@ correct");
190 ok
(test_file
("snapshot-expected/commit/qemu-server/$vmid.conf", "snapshot-working/commit/qemu-server/$vmid.conf"), "config file correct");
194 sub testcase_create
{
195 my ($vmid, $snapname, $save_vmstate, $comment, $exp_err, $exp_vol_snap, $exp_vol_snap_delete) = @_;
196 subtest
"Creating snapshot '$snapname' for vm '$vmid'" => sub {
199 $vol_snapshot_delete = {};
200 $exp_vol_snap = {} if !defined($exp_vol_snap);
201 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
204 PVE
::QemuConfig-
>snapshot_create($vmid, $snapname, $save_vmstate, $comment);
206 is($@, $exp_err, "\$@ correct");
207 is_deeply
($vol_snapshot, $exp_vol_snap, "created correct volume snapshots");
208 is_deeply
($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
209 ok
(test_file
("snapshot-expected/create/qemu-server/$vmid.conf", "snapshot-working/create/qemu-server/$vmid.conf"), "config file correct");
213 sub testcase_delete
{
214 my ($vmid, $snapname, $force, $exp_err, $exp_vol_snap_delete) = @_;
215 subtest
"Deleting snapshot '$snapname' of vm '$vmid'" => sub {
217 $vol_snapshot_delete = {};
218 $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
221 PVE
::QemuConfig-
>snapshot_delete($vmid, $snapname, $force);
223 is($@, $exp_err, "\$@ correct");
224 is_deeply
($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
225 ok
(test_file
("snapshot-expected/delete/qemu-server/$vmid.conf", "snapshot-working/delete/qemu-server/$vmid.conf"), "config file correct");
229 sub testcase_rollback
{
230 my ($vmid, $snapname, $exp_err, $exp_vol_snap_rollback) = @_;
231 subtest
"Rolling back to snapshot '$snapname' of vm '$vmid'" => sub {
233 $vol_snapshot_rollback = {};
235 $exp_vol_snap_rollback = {} if !defined($exp_vol_snap_rollback);
238 PVE
::QemuConfig-
>snapshot_rollback($vmid, $snapname);
240 is($@, $exp_err, "\$@ correct");
241 is_deeply
($vol_snapshot_rollback, $exp_vol_snap_rollback, "rolled back to correct volume snapshots");
242 ok
(test_file
("snapshot-expected/rollback/qemu-server/$vmid.conf", "snapshot-working/rollback/qemu-server/$vmid.conf"), "config file correct");
246 # BEGIN mocked PVE::QemuConfig methods
247 sub config_file_lock
{
248 return "snapshot-working/pve-test.lock";
251 sub cfs_config_path
{
252 my ($class, $vmid, $node) = @_;
254 $node = $nodename if !$node;
255 return "snapshot-working/$node/qemu-server/$vmid.conf";
259 my ($class, $vmid, $node) = @_;
261 my $filename = $class->cfs_config_path($vmid, $node);
263 my $raw = PVE
::Tools
::file_get_contents
($filename);
265 my $conf = PVE
::QemuServer
::parse_vm_config
($filename, $raw);
270 my ($class, $vmid, $conf) = @_;
272 my $filename = $class->cfs_config_path($vmid);
274 if ($conf->{snapshots
}) {
275 foreach my $snapname (keys %{$conf->{snapshots
}}) {
276 $conf->{snapshots
}->{$snapname}->{snaptime
} = "1234567890"
277 if $conf->{snapshots
}->{$snapname}->{snaptime
};
281 my $raw = PVE
::QemuServer
::write_vm_config
($filename, $conf);
283 PVE
::Tools
::file_set_contents
($filename, $raw);
287 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
288 return $snapshot_possible;
291 sub __snapshot_save_vmstate
{
292 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
293 die "save_vmstate failed\n"
294 if !$save_vmstate_works;
296 my $snap = $conf->{snapshots
}->{$snapname};
297 $snap->{vmstate
} = "somestorage:state-volume";
298 $snap->{runningmachine
} = "somemachine"
301 sub assert_config_exists_on_node
{
302 my ($vmid, $node) = @_;
303 return -f cfs_config_path
("PVE::QemuConfig", $vmid, $node);
305 # END mocked PVE::QemuConfig methods
307 # BEGIN mocked PVE::QemuServer::Helpers methods
309 sub vm_running_locally
{
313 # END mocked PVE::QemuServer::Helpers methods
315 # BEGIN mocked PVE::QemuServer::Monitor methods
318 my ($vmid, $cmd) = @_;
320 my $exec = $cmd->{execute
};
321 if ($exec eq "delete-drive-snapshot") {
324 if ($exec eq "guest-ping") {
325 die "guest-ping disabled\n"
326 if !$vm_mon->{guest_ping
};
329 if ($exec eq "guest-fsfreeze-freeze" || $exec eq "guest-fsfreeze-thaw") {
330 die "freeze disabled\n"
331 if !$freeze_possible;
334 if ($exec eq "savevm-start") {
335 die "savevm-start disabled\n"
336 if !$vm_mon->{savevm_start
};
339 if ($exec eq "savevm-end") {
340 die "savevm-end disabled\n"
341 if !$vm_mon->{savevm_end
};
344 if ($exec eq "query-savevm") {
346 "status" => "completed",
347 "bytes" => 1024*1024*1024,
348 "total-time" => 5000,
351 die "unexpected vm_qmp_command!\n";
354 # END mocked PVE::QemuServer::Monitor methods
356 # BEGIN redefine PVE::QemuServer methods
358 sub do_snapshots_with_qemu
{
363 my ($storecfg, $vmid, $params, $migrate_opts) = @_;
365 die "Storage config not mocked! aborting\n"
366 if defined($storecfg);
368 die "statefile and forcemachine must be both defined or undefined! aborting\n"
369 if defined($params->{statefile
}) xor defined($params->{forcemachine
});
375 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
383 sub set_migration_caps
{} # ignored
385 # END redefine PVE::QemuServer methods
387 PVE
::Tools
::run_command
("rm -rf snapshot-working");
388 PVE
::Tools
::run_command
("cp -a snapshot-input snapshot-working");
390 my $qemu_helpers_module = Test
::MockModule-
>new('PVE::QemuServer::Helpers');
391 $qemu_helpers_module->mock('vm_running_locally', \
&vm_running_locally
);
393 my $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
394 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
396 my $qemu_config_module = Test
::MockModule-
>new('PVE::QemuConfig');
397 $qemu_config_module->mock('config_file_lock', \
&config_file_lock
);
398 $qemu_config_module->mock('cfs_config_path', \
&cfs_config_path
);
399 $qemu_config_module->mock('load_config', \
&load_config
);
400 $qemu_config_module->mock('write_config', \
&write_config
);
401 $qemu_config_module->mock('has_feature', \
&has_feature
);
402 $qemu_config_module->mock('__snapshot_save_vmstate', \
&__snapshot_save_vmstate
);
403 $qemu_config_module->mock('assert_config_exists_on_node', \
&assert_config_exists_on_node
);
405 # ignore existing replication config
406 my $repl_config_module = Test
::MockModule-
>new('PVE::ReplicationConfig');
407 $repl_config_module->mock('new' => sub { return bless {}, "PVE::ReplicationConfig" });
408 $repl_config_module->mock('check_for_existing_jobs' => sub { return });
410 my $storage_module = Test
::MockModule-
>new('PVE::Storage');
411 $storage_module->mock('config', sub { return; });
412 $storage_module->mock('path', sub { return "/some/store/statefile/path"; });
413 $storage_module->mock('activate_volumes', \
&mocked_activate_volumes
);
414 $storage_module->mock('deactivate_volumes', \
&mocked_deactivate_volumes
);
415 $storage_module->mock('vdisk_free', \
&mocked_vdisk_free
);
416 $storage_module->mock('volume_snapshot', \
&mocked_volume_snapshot
);
417 $storage_module->mock('volume_snapshot_delete', \
&mocked_volume_snapshot_delete
);
418 $storage_module->mock('volume_snapshot_rollback', \
&mocked_volume_snapshot_rollback
);
419 $storage_module->mock('volume_rollback_is_possible', \
&mocked_volume_rollback_is_possible
);
422 $freeze_possible = 1;
423 $save_vmstate_works = 1;
426 printf("Running prepare tests\n");
428 $nodename = "prepare";
431 printf("Setting has_feature to return true\n");
433 $snapshot_possible = 1;
435 printf("Successful snapshot_prepare with no existing snapshots\n");
436 testcase_prepare
("101", "test", 0, "test comment", '');
438 printf("Successful snapshot_prepare with no existing snapshots, including vmstate\n");
439 testcase_prepare
("102", "test", 1, "test comment", '');
441 printf("Successful snapshot_prepare with one existing snapshot\n");
442 testcase_prepare
("103", "test2", 0, "test comment", "");
444 printf("Successful snapshot_prepare with one existing snapshot, including vmstate\n");
445 testcase_prepare
("104", "test2", 1, "test comment", "");
447 printf("Expected error for snapshot_prepare on locked container\n");
448 testcase_prepare
("200", "test", 0, "test comment", "VM is locked (snapshot)\n");
450 printf("Expected error for snapshot_prepare with duplicate snapshot name\n");
451 testcase_prepare
("201", "test", 0, "test comment", "snapshot name 'test' already used\n");
453 $save_vmstate_works = 0;
455 printf("Expected error for snapshot_prepare with failing save_vmstate\n");
456 testcase_prepare
("202", "test", 1, "test comment", "save_vmstate failed\n");
458 $save_vmstate_works = 1;
461 printf("Setting has_feature to return false\n");
463 $snapshot_possible = 0;
465 printf("Expected error for snapshot_prepare if snapshots not possible\n");
466 testcase_prepare
("300", "test", 0, "test comment", "snapshot feature is not available\n");
469 printf("Running commit tests\n");
471 $nodename = "commit";
474 printf("Setting has_feature to return true\n");
476 $snapshot_possible = 1;
478 printf("Successful snapshot_commit with one prepared snapshot\n");
479 testcase_commit
("101", "test", "");
481 printf("Successful snapshot_commit with one committed and one prepared snapshot\n");
482 testcase_commit
("102", "test2", "");
484 printf("Expected error for snapshot_commit with no snapshot lock\n");
485 testcase_commit
("201", "test", "missing snapshot lock\n");
487 printf("Expected error for snapshot_commit with invalid snapshot name\n");
488 testcase_commit
("202", "test", "snapshot 'test' does not exist\n");
490 printf("Expected error for snapshot_commit with invalid snapshot state\n");
491 testcase_commit
("203", "test", "wrong snapshot state\n");
493 $vol_snapshot_possible->{"local:snapshotable-disk-1"} = 1;
494 $vol_snapshot_possible->{"local:snapshotable-disk-2"} = 1;
495 $vol_snapshot_possible->{"local:snapshotable-disk-3"} = 1;
496 $vol_snapshot_delete_possible->{"local:snapshotable-disk-1"} = 1;
497 $vol_snapshot_delete_possible->{"local:snapshotable-disk-3"} = 1;
498 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-1"} = 1;
499 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-2"} = 1;
500 $vol_snapshot_rollback_enabled->{"local:snapshotable-disk-3"} = 1;
501 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-1"} = 1;
502 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-2"} = 1;
503 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-3"} = 1;
504 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
505 $vm_mon->{guest_ping
} = 1;
506 $vm_mon->{savevm_start
} = 1;
507 $vm_mon->{savevm_end
} = 1;
509 # possible, but fails
510 $vol_snapshot_rollback_possible->{"local:snapshotable-disk-4"} = 1;
514 #printf("Setting up Mocking for PVE::Tools\n");
515 #my $tools_module = Test::MockModule->new('PVE::Tools');
516 #$tools_module->mock('run_command' => \&mocked_run_command);
517 #printf("\trun_command() mocked\n");
519 $nodename = "create";
521 printf("Running create tests\n");
524 printf("Successful snapshot_create with no existing snapshots\n");
525 testcase_create
("101", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" });
527 printf("Successful snapshot_create with no existing snapshots, including vmstate\n");
528 testcase_create
("102", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
530 printf("Successful snapshot_create with one existing snapshots\n");
531 testcase_create
("103", "test2", 0, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
533 printf("Successful snapshot_create with one existing snapshots, including vmstate\n");
534 testcase_create
("104", "test2", 1, "test comment", "", { "local:snapshotable-disk-1" => "test2" });
536 printf("Successful snapshot_create with multiple mps\n");
537 testcase_create
("105", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
539 $freeze_possible = 0;
540 printf("Successful snapshot_create with no existing snapshots and broken freeze\n");
541 testcase_create
("106", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
542 $freeze_possible = 1;
544 printf("Expected error for snapshot_create when volume snapshot is not possible\n");
545 testcase_create
("201", "test", 0, "test comment", "volume snapshot disabled\n\n");
547 printf("Expected error for snapshot_create when volume snapshot is not possible for one drive\n");
548 testcase_create
("202", "test", 0, "test comment", "volume snapshot disabled\n\n", { "local:snapshotable-disk-1" => "test" }, { "local:snapshotable-disk-1" => "test" });
550 $vm_mon->{savevm_start
} = 0;
551 printf("Expected error for snapshot_create when Qemu mon command 'savevm-start' fails\n");
552 testcase_create
("203", "test", 0, "test comment", "savevm-start disabled\n\n");
553 $vm_mon->{savevm_start
} = 1;
555 printf("Successful snapshot_create with no existing snapshots but set machine type\n");
556 testcase_create
("301", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
558 $nodename = "delete";
560 printf("Running delete tests\n");
563 printf("Successful snapshot_delete of only existing snapshot\n");
564 testcase_delete
("101", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
566 printf("Successful snapshot_delete of leaf snapshot\n");
567 testcase_delete
("102", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
569 printf("Successful snapshot_delete of root snapshot\n");
570 testcase_delete
("103", "test", 0, "", { "local:snapshotable-disk-1" => "test" });
572 printf("Successful snapshot_delete of intermediate snapshot\n");
573 testcase_delete
("104", "test2", 0, "", { "local:snapshotable-disk-1" => "test2" });
575 printf("Successful snapshot_delete with broken volume_snapshot_delete and force=1\n");
576 testcase_delete
("105", "test", 1, "");
578 printf("Successful snapshot_delete with mp broken volume_snapshot_delete and force=1\n");
579 testcase_delete
("106", "test", 1, "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-3" => "test" });
581 printf("Expected error when snapshot_delete fails with broken volume_snapshot_delete and force=0\n");
582 testcase_delete
("201", "test", 0, "volume snapshot delete disabled\n");
584 printf("Expected error when snapshot_delete fails with broken mp volume_snapshot_delete and force=0\n");
585 testcase_delete
("202", "test", 0, "volume snapshot delete disabled\n", { "local:snapshotable-disk-1" => "test" });
587 printf("Expected error for snapshot_delete with locked config\n");
588 testcase_delete
("203", "test", 0, "VM is locked (backup)\n");
590 $nodename = "rollback";
592 printf("Running rollback tests\n");
597 printf("Successful snapshot_rollback to only existing snapshot\n");
598 testcase_rollback
("101", "test", "", { "local:snapshotable-disk-1" => "test" });
600 printf("Successful snapshot_rollback to leaf snapshot\n");
601 testcase_rollback
("102", "test2", "", { "local:snapshotable-disk-1" => "test2" });
603 printf("Successful snapshot_rollback to root snapshot\n");
604 testcase_rollback
("103", "test", "", { "local:snapshotable-disk-1" => "test" });
606 printf("Successful snapshot_rollback to intermediate snapshot\n");
607 testcase_rollback
("104", "test2", "", { "local:snapshotable-disk-1" => "test2" });
609 printf("Successful snapshot_rollback with multiple mp\n");
610 testcase_rollback
("105", "test", "", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test", "local:snapshotable-disk-3" => "test" });
612 printf("Successful snapshot_rollback to only existing snapshot, with saved vmstate and machine config\n");
613 testcase_rollback
("106", "test", "", { "local:snapshotable-disk-1" => "test" });
615 printf("Expected error for snapshot_rollback with non-existing snapshot\n");
616 testcase_rollback
("201", "test2", "snapshot 'test2' does not exist\n");
618 printf("Expected error for snapshot_rollback if volume rollback not possible\n");
619 testcase_rollback
("202", "test", "volume_rollback_is_possible failed\n");
621 printf("Expected error for snapshot_rollback with incomplete snapshot\n");
622 testcase_rollback
("203", "test", "unable to rollback to incomplete snapshot (snapstate = delete)\n");
624 printf("Expected error for snapshot_rollback with lock\n");
625 testcase_rollback
("204", "test", "VM is locked (backup)\n");
629 printf("Expected error for snapshot_rollback with unkillable container\n");
630 testcase_rollback
("205", "test", "unable to rollback vm 205: vm is running\n");
634 printf("Expected error for snapshot_rollback with mp rollback_is_possible failure\n");
635 testcase_rollback
("206", "test", "volume_rollback_is_possible failed\n");
637 printf("Expected error for snapshot_rollback with mp rollback failure (results in inconsistent state)\n");
638 testcase_rollback
("207", "test", "volume snapshot rollback disabled\n", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test" });
640 printf("Successful snapshot_rollback with saved vmstate and machine config only in snapshot\n");
641 testcase_rollback
("301", "test", "", { "local:snapshotable-disk-1" => "test" });
643 printf("Successful snapshot_rollback with saved vmstate and machine config and runningmachine \n");
644 testcase_rollback
("302", "test", "", { "local:snapshotable-disk-1" => "test" });