10 use Socket
qw(AF_INET AF_INET6);
12 use PVE
::Tools
qw(file_get_contents file_set_contents run_command);
18 use PVE
::QemuServer
::Monitor
;
19 use PVE
::QemuServer
::Machine
;
20 use PVE
::QemuServer
::CPUConfig
;
29 path
=> '/var/lib/vz',
35 path
=> '/mnt/pve/cifs-store',
37 server
=> '127.0.0.42',
45 monhost
=> '127.0.0.42,127.0.0.21,::1',
56 bwlimit
=> 'restore=1024',
66 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
83 my $current_test; # = {
84 # description => 'Test description', # if available
85 # qemu_version => '2.12',
86 # host_arch => 'HOST_ARCH',
87 # expected_error => 'error message',
88 # expected_warning => 'warning message',
89 # config => { config hash },
90 # expected => [ expected outcome cmd line array ],
93 # use the config description to allow changing environment, fields are:
94 # TEST: A single line describing the test, gets outputted
95 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
96 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
97 # EXPECT_ERROR: <error message> For negative tests
98 # all fields are optional
100 my ($config_fn) = @_;
102 $current_test = {}; # reset
104 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
105 my $config_raw = file_get_contents
($config_fn);
106 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
108 $current_test->{config
} = $config;
110 my $description = $config->{description
} // '';
112 while ($description =~ /^\h*(.*?)\h*$/gm) {
114 next if !$line || $line =~ /^#/;
118 if ($line =~ /^TEST:\s*(.*)\s*$/) {
119 $current_test->{description
} = "$1";
120 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
121 $current_test->{qemu_version
} = "$1";
122 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
123 $current_test->{host_arch
} = "$1";
124 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
125 $current_test->{expect_error
} = "$1";
126 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
127 $current_test->{expect_warning
} = "$1";
131 $config_fn =~ /([^\/]+)$/;
133 if (my $desc = $current_test->{description
}) {
134 $testname = "'$testname' - $desc";
136 $current_test->{testname
} = $testname;
139 sub get_test_qemu_version
{
140 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
143 my $qemu_server_module;
144 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
145 $qemu_server_module->mock(
146 kvm_user_version
=> sub {
147 return get_test_qemu_version
();
150 return get_test_qemu_version
();
152 kernel_has_vhost_net
=> sub {
153 return 1; # TODO: make this per-test configurable?
155 get_host_arch
=> sub() {
156 return $current_test->{host_arch
} // 'x86_64';
158 get_initiator_name
=> sub {
159 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
163 my $qemu_server_config;
164 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
165 $qemu_server_config->mock(
167 my ($class, $vmid, $node) = @_;
169 return $current_test->{config
};
173 my $pve_common_tools;
174 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
175 $pve_common_tools->mock(
176 next_vnc_port
=> sub {
177 my ($family, $address) = @_;
181 next_spice_port
=> sub {
182 my ($family, $address) = @_;
186 getaddrinfo_all
=> sub {
187 my ($hostname, @opts) = @_;
188 die "need stable hostname" if $hostname ne 'localhost';
191 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
192 family
=> AF_INET
, # IPv4
201 $pve_cpuconfig = Test
::MockModule-
>new('PVE::QemuServer::CPUConfig');
202 $pve_cpuconfig->mock(
203 load_custom_model_conf
=> sub {
204 # mock custom CPU model config
205 return PVE
::QemuServer
::CPUConfig-
>parse_config("cpu-models.conf",
208 # "qemu64" is also a default CPU, used here to test that this doesn't matter
210 reported-model athlon
211 flags +aes;+avx;-kvm_pv_unhalt
212 hv-vendor-id testvend
215 cpu-model: alldefault
222 my $pve_common_network;
223 $pve_common_network = Test
::MockModule-
>new('PVE::Network');
224 $pve_common_network->mock(
225 read_bridge_mtu
=> sub {
231 my $pve_common_inotify;
232 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
233 $pve_common_inotify->mock(
239 my $pve_common_sysfstools;
240 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
241 $pve_common_sysfstools->mock(
243 my ($filter, $verbose) = @_;
249 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
250 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
256 my $qemu_monitor_module;
257 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
258 $qemu_monitor_module->mock(
260 my ($vmid, $cmd) = @_;
262 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
263 if $vmid != $base_env->{vmid
};
265 if ($cmd eq 'query-version') {
266 my $ver = get_test_qemu_version
();
267 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
277 die "unexpected QMP command: '$cmd'";
280 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
286 my ($ra, $wa) = POSIX
::pipe();
287 my ($rb, $wb) = POSIX
::pipe();
288 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
289 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
291 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
292 or die "failed to run program 'diff': $!";
296 open my $f1, '<', \
$a;
297 open my $f2, '<', \
$b;
300 $ha->print($line1) if defined($line1 = <$f1>);
301 $hb->print($line2) if defined($line2 = <$f2>);
302 } while (defined($line1 // $line2));
309 my $diff = <$diffproc>;
311 die "files differ:\n$diff";
314 $SIG{__WARN__
} = sub {
317 if (my $warn_expect = $current_test->{expect_warning
}) {
318 if ($warn_expect ne $warning) {
319 fail
($current_test->{testname
});
320 note
("warning does not match expected error: '$warning' != '$warn_expect'");
322 note
("got expected warning '$warning'");
327 fail
($current_test->{testname
});
328 note
("got unexpected warning '$warning'");
332 my ($config_fn) = @_;
334 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
336 parse_test
$config_fn;
338 my $testname = $current_test->{testname
};
340 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
342 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
345 if (my $err_expect = $current_test->{expect_error
}) {
348 note
("did NOT get any error, but expected error: $err_expect");
352 if ($err ne $err_expect) {
354 note
("error does not match expected error: '$err' !~ '$err_expect'");
361 note
("got unexpected error: $err");
365 # check if QEMU version set correctly and test version_cmp
366 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
367 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
368 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
370 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
373 my $cmd_fn = "$config_fn.cmd";
376 my $cmdline_expected = file_get_contents
($cmd_fn);
378 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
379 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
381 # uncomment for easier debugging
382 #file_set_contents("$cmd_fn.tmp", $cmdline);
384 my $exp = join("\n", @$cmd_expected);
385 my $got = join("\n", @$cmd);
386 eval { diff
($exp, $got) };
394 file_set_contents
($cmd_fn, $cmdline);
398 print "testing config to command stabillity\n";
401 if (my $file = shift) {
404 while (my $file = <cfg2cmd
/*.conf
>) {