]> git.proxmox.com Git - qemu-server.git/blobdiff - test/run_config2command_tests.pl
snapshot-test: mock query-savevm better
[qemu-server.git] / test / run_config2command_tests.pl
index a826419842de57955fe2c913e8473686a1c1126b..8011265ab4a62ec558c45300c0948a9026b8e651 100755 (executable)
@@ -7,10 +7,17 @@ use lib qw(..);
 
 use Test::More;
 use Test::MockModule;
+use Socket qw(AF_INET AF_INET6);
 
 use PVE::Tools qw(file_get_contents file_set_contents run_command);
+use PVE::INotify;
+use PVE::SysFSTools;
+
 use PVE::QemuConfig;
 use PVE::QemuServer;
+use PVE::QemuServer::Monitor;
+use PVE::QemuServer::Machine;
+use PVE::QemuServer::CPUConfig;
 
 my $base_env = {
     storage_config => {
@@ -77,6 +84,8 @@ my $current_test; # = {
 #   description => 'Test description', # if available
 #   qemu_version => '2.12',
 #   host_arch => 'HOST_ARCH',
+#   expected_error => 'error message',
+#   expected_warning => 'warning message',
 #   config => { config hash },
 #   expected => [ expected outcome cmd line array ],
 # };
@@ -85,6 +94,7 @@ my $current_test; # = {
 #   TEST: A single line describing the test, gets outputted
 #   QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
 #   HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
+#   EXPECT_ERROR: <error message> For negative tests
 # all fields are optional
 sub parse_test($) {
     my ($config_fn) = @_;
@@ -111,18 +121,33 @@ sub parse_test($) {
            $current_test->{qemu_version} = "$1";
        } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
            $current_test->{host_arch} = "$1";
+       } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
+           $current_test->{expect_error} = "$1";
+       } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
+           $current_test->{expect_warning} = "$1";
        }
     }
+
+    $config_fn =~ /([^\/]+)$/;
+    my $testname = "$1";
+    if (my $desc = $current_test->{description}) {
+       $testname = "'$testname' - $desc";
+    }
+    $current_test->{testname} = $testname;
+}
+
+sub get_test_qemu_version {
+    $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
 }
 
 my $qemu_server_module;
 $qemu_server_module = Test::MockModule->new('PVE::QemuServer');
 $qemu_server_module->mock(
     kvm_user_version => sub {
-       return $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
+       return get_test_qemu_version();
     },
     kvm_version => sub {
-       return $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
+       return get_test_qemu_version();
     },
     kernel_has_vhost_net => sub {
        return 1; # TODO: make this per-test configurable?
@@ -158,6 +183,57 @@ $pve_common_tools->mock(
 
        return '61000';
     },
+    getaddrinfo_all => sub {
+       my ($hostname, @opts) = @_;
+       die "need stable hostname" if $hostname ne 'localhost';
+       return (
+           {
+               addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK),
+               family => AF_INET, # IPv4
+               protocol => 6,
+               socktype => 1,
+           },
+       );
+    },
+);
+
+my $pve_cpuconfig;
+$pve_cpuconfig = Test::MockModule->new('PVE::QemuServer::CPUConfig');
+$pve_cpuconfig->mock(
+    load_custom_model_conf => sub {
+       # mock custom CPU model config
+       return PVE::QemuServer::CPUConfig->parse_config("cpu-models.conf",
+<<EOF
+
+# "qemu64" is also a default CPU, used here to test that this doesn't matter
+cpu-model: qemu64
+    reported-model athlon
+    flags +aes;+avx;-kvm_pv_unhalt
+    hv-vendor-id testvend
+    phys-bits 40
+
+cpu-model: alldefault
+
+EOF
+       )
+    },
+);
+
+my $pve_common_network;
+$pve_common_network = Test::MockModule->new('PVE::Network');
+$pve_common_network->mock(
+    read_bridge_mtu => sub {
+       return 1500;
+    },
+);
+
+
+my $pve_common_inotify;
+$pve_common_inotify = Test::MockModule->new('PVE::INotify');
+$pve_common_inotify->mock(
+    nodename => sub {
+       return 'localhost';
+    },
 );
 
 my $pve_common_sysfstools;
@@ -177,6 +253,32 @@ $pve_common_sysfstools->mock(
     },
 );
 
+my $qemu_monitor_module;
+$qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor');
+$qemu_monitor_module->mock(
+    mon_cmd => sub {
+       my ($vmid, $cmd) = @_;
+
+       die "invalid vmid: $vmid (expected: $base_env->{vmid})"
+           if $vmid != $base_env->{vmid};
+
+       if ($cmd eq 'query-version') {
+           my $ver = get_test_qemu_version();
+           $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
+           return {
+               qemu => {
+                   major => $1,
+                   minor => $2,
+                   micro => $3
+               }
+           }
+       }
+
+       die "unexpected QMP command: '$cmd'";
+    },
+);
+$qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd);
+
 sub diff($$) {
     my ($a, $b) = @_;
     return if $a eq $b;
@@ -186,7 +288,7 @@ sub diff($$) {
     my $ha = IO::Handle->new_from_fd($wa, 'w');
     my $hb = IO::Handle->new_from_fd($wb, 'w');
 
-    open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
+    open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
        or die "failed to run program 'diff': $!";
     POSIX::close($ra);
     POSIX::close($rb);
@@ -209,6 +311,23 @@ sub diff($$) {
     die "files differ:\n$diff";
 }
 
+$SIG{__WARN__} = sub {
+    my $warning = shift;
+    chomp $warning;
+    if (my $warn_expect = $current_test->{expect_warning}) {
+       if ($warn_expect ne $warning) {
+           fail($current_test->{testname});
+           note("warning does not match expected error: '$warning' != '$warn_expect'");
+       } else {
+           note("got expected warning '$warning'");
+           return;
+       }
+    }
+
+    fail($current_test->{testname});
+    note("got unexpected warning '$warning'");
+};
+
 sub do_test($) {
     my ($config_fn) = @_;
 
@@ -216,15 +335,37 @@ sub do_test($) {
 
     parse_test $config_fn;
 
-    $config_fn =~ /([^\/]+)$/;
-    my $testname = "$1";
-    if (my $desc = $current_test->{description}) {
-       $testname = "'$testname' - $desc";
-    }
+    my $testname = $current_test->{testname};
 
     my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
 
-    my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $vmid);
+    my $cmdline = eval { PVE::QemuServer::vm_commandline($storecfg, $vmid) };
+    my $err = $@;
+
+    if (my $err_expect = $current_test->{expect_error}) {
+       if (!$err) {
+           fail("$testname");
+           note("did NOT get any error, but expected error: $err_expect");
+           return;
+       }
+       chomp $err;
+       if ($err ne $err_expect) {
+           fail("$testname");
+           note("error does not match expected error: '$err' !~ '$err_expect'");
+       } else {
+           pass("$testname");
+       }
+       return;
+    } elsif ($err) {
+       fail("$testname");
+       note("got unexpected error: $err");
+       return;
+    }
+
+    # check if QEMU version set correctly and test version_cmp
+    (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//;
+    die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
+       if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major);
 
     $cmdline =~ s/ -/ \\\n  -/g; # same as qm showcmd --pretty
     $cmdline .= "\n";
@@ -260,7 +401,7 @@ print "testing config to command stabillity\n";
 if (my $file = shift) {
     do_test $file;
 } else {
-    foreach my $file (<cfg2cmd/*.conf>) {
+    while (my $file = <cfg2cmd/*.conf>) {
        do_test $file;
     }
 }