]> git.proxmox.com Git - qemu-server.git/blobdiff - test/snapshot-test.pm
cfg2cmd tests: config with q35, pci passthrough, NUMA, EFI
[qemu-server.git] / test / snapshot-test.pm
index 4dc2897cc0ef94ffc1b8e934303fca53656c5fed..c95e7f37b3e0fc0eddd0f8d819a3c84ffbb54727 100644 (file)
@@ -8,7 +8,9 @@ use lib qw(..);
 use PVE::Storage;
 use PVE::Storage::Plugin;
 use PVE::QemuServer;
+use PVE::QemuConfig;
 use PVE::Tools;
+use PVE::ReplicationConfig;
 
 use Test::MockModule;
 use Test::More;
@@ -103,6 +105,24 @@ sub mocked_volume_rollback_is_possible {
     die "volume_rollback_is_possible failed\n";
 }
 
+sub mocked_activate_volumes {
+    my ($storecfg, $volumes) = @_;
+    die "Storage config not mocked! aborting\n"
+       if defined($storecfg);
+    die "wrong volume - fake vmstate expected!\n"
+       if ((scalar @$volumes != 1) || @$volumes[0] ne "somestorage:state-volume");
+    return;
+}
+
+sub mocked_deactivate_volumes {
+    my ($storecfg, $volumes) = @_;
+    die "Storage config not mocked! aborting\n"
+       if defined($storecfg);
+    die "wrong volume - fake vmstate expected!\n"
+       if ((scalar @$volumes != 1) || @$volumes[0] ne "somestorage:state-volume");
+    return;
+}
+
 sub mocked_vdisk_free {
     my ($storecfg, $vmstate) = @_;
     die "Storage config not mocked! aborting\n"
@@ -151,7 +171,7 @@ sub testcase_prepare {
        plan tests => 2;
        $@ = undef;
        eval {
-           PVE::QemuServer::snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
+           PVE::QemuConfig->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
        };
        is($@, $exp_err, "\$@ correct");
        ok(test_file("snapshot-expected/prepare/qemu-server/$vmid.conf", "snapshot-working/prepare/qemu-server/$vmid.conf"), "config file correct");
@@ -164,7 +184,7 @@ sub testcase_commit {
        plan tests => 2;
        $@ = undef;
        eval {
-           PVE::QemuServer::snapshot_commit($vmid, $snapname);
+           PVE::QemuConfig->__snapshot_commit($vmid, $snapname);
        };
        is($@, $exp_err, "\$@ correct");
        ok(test_file("snapshot-expected/commit/qemu-server/$vmid.conf", "snapshot-working/commit/qemu-server/$vmid.conf"), "config file correct");
@@ -181,7 +201,7 @@ sub testcase_create {
        $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
        $@ = undef;
        eval {
-           PVE::QemuServer::snapshot_create($vmid, $snapname, $save_vmstate, $comment);
+           PVE::QemuConfig->snapshot_create($vmid, $snapname, $save_vmstate, $comment);
        };
        is($@, $exp_err, "\$@ correct");
        is_deeply($vol_snapshot, $exp_vol_snap, "created correct volume snapshots");
@@ -198,7 +218,7 @@ sub testcase_delete {
        $exp_vol_snap_delete = {} if !defined($exp_vol_snap_delete);
        $@ = undef;
        eval {
-           PVE::QemuServer::snapshot_delete($vmid, $snapname, $force);
+           PVE::QemuConfig->snapshot_delete($vmid, $snapname, $force);
        };
        is($@, $exp_err, "\$@ correct");
        is_deeply($vol_snapshot_delete, $exp_vol_snap_delete, "deleted correct volume snapshots");
@@ -215,7 +235,7 @@ sub testcase_rollback {
        $exp_vol_snap_rollback = {} if !defined($exp_vol_snap_rollback);
        $@ = undef;
        eval {
-           PVE::QemuServer::snapshot_rollback($vmid, $snapname);
+           PVE::QemuConfig->snapshot_rollback($vmid, $snapname);
        };
        is($@, $exp_err, "\$@ correct");
        is_deeply($vol_snapshot_rollback, $exp_vol_snap_rollback, "rolled back to correct volume snapshots");
@@ -223,22 +243,22 @@ sub testcase_rollback {
     };
 }
 
-# BEGIN redefine PVE::QemuServer methods 
+# BEGIN mocked PVE::QemuConfig methods
 sub config_file_lock {
     return "snapshot-working/pve-test.lock";
 }
 
 sub cfs_config_path {
-    my ($vmid, $node) = @_;
+    my ($class, $vmid, $node) = @_;
 
     $node = $nodename if !$node;
     return "snapshot-working/$node/qemu-server/$vmid.conf";
 }
 
 sub load_config {
-    my ($vmid, $node) = @_;
+    my ($class, $vmid, $node) = @_;
 
-    my $filename = cfs_config_path($vmid, $node);
+    my $filename = $class->cfs_config_path($vmid, $node);
 
     my $raw = PVE::Tools::file_get_contents($filename);
 
@@ -247,9 +267,9 @@ sub load_config {
 }
 
 sub write_config {
-    my ($vmid, $conf) = @_;
+    my ($class, $vmid, $conf) = @_;
 
-    my $filename = cfs_config_path($vmid);
+    my $filename = $class->cfs_config_path($vmid);
 
     if ($conf->{snapshots}) {
        foreach my $snapname (keys %{$conf->{snapshots}}) {
@@ -264,24 +284,28 @@ sub write_config {
 }
 
 sub has_feature {
-    my ($feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
+    my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
     return $snapshot_possible;
 }
 
-sub check_running {
-    return $running;
-}
-
-sub snapshot_save_vmstate {
-    my ($vmid, $conf, $snapname, $storecfg) = @_;
+sub __snapshot_save_vmstate {
+    my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
     die "save_vmstate failed\n"
        if !$save_vmstate_works;
 
     my $snap = $conf->{snapshots}->{$snapname};
     $snap->{vmstate} = "somestorage:state-volume";
-    $snap->{machine} = "somemachine";
+    $snap->{runningmachine} = "somemachine"
+}
+# END mocked PVE::QemuConfig methods
+
+# BEGIN redefine PVE::QemuServer methods
+
+sub check_running {
+    return $running;
 }
 
+
 sub do_snapshots_with_qemu {
     return 0;
 }
@@ -345,6 +369,18 @@ sub vm_stop {
 PVE::Tools::run_command("rm -rf snapshot-working");
 PVE::Tools::run_command("cp -a snapshot-input snapshot-working");
 
+my $qemu_config_module = new Test::MockModule('PVE::QemuConfig');
+$qemu_config_module->mock('config_file_lock', \&config_file_lock);
+$qemu_config_module->mock('cfs_config_path', \&cfs_config_path);
+$qemu_config_module->mock('load_config', \&load_config);
+$qemu_config_module->mock('write_config', \&write_config);
+$qemu_config_module->mock('has_feature', \&has_feature);
+$qemu_config_module->mock('__snapshot_save_vmstate', \&__snapshot_save_vmstate);
+
+# ignore existing replication config
+my $repl_config_module = new Test::MockModule('PVE::ReplicationConfig');
+$repl_config_module->mock('check_for_existing_jobs' => sub { return undef });
+
 $running = 1;
 $freeze_possible = 1;
 $save_vmstate_works = 1;
@@ -441,6 +477,8 @@ printf("Setting up Mocking for PVE::Storage\n");
 my $storage_module = new Test::MockModule('PVE::Storage');
 $storage_module->mock('config', sub { return undef; });
 $storage_module->mock('path', sub { return "/some/store/statefile/path"; });
+$storage_module->mock('activate_volumes', \&mocked_activate_volumes);
+$storage_module->mock('deactivate_volumes', \&mocked_deactivate_volumes);
 $storage_module->mock('vdisk_free', \&mocked_vdisk_free);
 $storage_module->mock('volume_snapshot', \&mocked_volume_snapshot);
 $storage_module->mock('volume_snapshot_delete', \&mocked_volume_snapshot_delete);
@@ -480,16 +518,18 @@ testcase_create("106", "test", 1, "test comment", "", { "local:snapshotable-disk
 $freeze_possible = 1;
 
 printf("Expected error for snapshot_create when volume snapshot is not possible\n");
-testcase_create("201", "test", 0, "test comment", "volume snapshot disabled\n");
+testcase_create("201", "test", 0, "test comment", "volume snapshot disabled\n\n");
 
 printf("Expected error for snapshot_create when volume snapshot is not possible for one drive\n");
-testcase_create("202", "test", 0, "test comment", "volume snapshot disabled\n", { "local:snapshotable-disk-1" => "test" }, { "local:snapshotable-disk-1" => "test" });
+testcase_create("202", "test", 0, "test comment", "volume snapshot disabled\n\n", { "local:snapshotable-disk-1" => "test" }, { "local:snapshotable-disk-1" => "test" });
 
 $vm_mon->{savevm_start} = 0;
 printf("Expected error for snapshot_create when Qemu mon command 'savevm-start' fails\n");
-testcase_create("203", "test", 0, "test comment", "savevm-start disabled\n");
+testcase_create("203", "test", 0, "test comment", "savevm-start disabled\n\n");
 $vm_mon->{savevm_start} = 1;
 
+printf("Successful snapshot_create with no existing snapshots but set machine type\n");
+testcase_create("301", "test", 1, "test comment", "", { "local:snapshotable-disk-1" => "test" });
 
 $nodename = "delete";
 printf("\n");
@@ -573,4 +613,10 @@ testcase_rollback("206", "test", "volume_rollback_is_possible failed\n");
 printf("Expected error for snapshot_rollback with mp rollback failure (results in inconsistent state)\n");
 testcase_rollback("207", "test", "volume snapshot rollback disabled\n", { "local:snapshotable-disk-1" => "test", "local:snapshotable-disk-2" => "test" });
 
+printf("Successful snapshot_rollback with saved vmstate and machine config only in snapshot\n");
+testcase_rollback("301", "test", "", { "local:snapshotable-disk-1" => "test" });
+
+printf("Successful snapshot_rollback with saved vmstate and machine config and runningmachine \n");
+testcase_rollback("302", "test", "", { "local:snapshotable-disk-1" => "test" });
+
 done_testing();