]> git.proxmox.com Git - qemu-server.git/blob - test/run_config2command_tests.pl
cfg2cmd: switch off ACPI hotplug on bridges for q35 VMs
[qemu-server.git] / test / run_config2command_tests.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib qw(..);
7
8 use Test::More;
9 use Test::MockModule;
10 use Socket qw(AF_INET AF_INET6);
11
12 use PVE::Tools qw(file_get_contents file_set_contents run_command);
13 use PVE::INotify;
14 use PVE::SysFSTools;
15
16 use PVE::QemuConfig;
17 use PVE::QemuServer;
18 use PVE::QemuServer::Monitor;
19 use PVE::QemuServer::Machine;
20 use PVE::QemuServer::CPUConfig;
21
22 my $base_env = {
23 storage_config => {
24 ids => {
25 local => {
26 content => {
27 images => 1,
28 },
29 path => '/var/lib/vz',
30 type => 'dir',
31 shared => 0,
32 },
33 'btrfs-store' => {
34 content => {
35 images => 1,
36 },
37 path => '/butter/bread',
38 type => 'btrfs',
39 },
40 'cifs-store' => {
41 shared => 1,
42 path => '/mnt/pve/cifs-store',
43 username => 'guest',
44 server => '127.0.0.42',
45 type => 'cifs',
46 share => 'CIFShare',
47 content => {
48 images => 1
49 },
50 },
51 'rbd-store' => {
52 monhost => '127.0.0.42,127.0.0.21,::1',
53 content => {
54 images => 1
55 },
56 type => 'rbd',
57 pool => 'cpool',
58 username => 'admin',
59 shared => 1
60 },
61 'local-lvm' => {
62 vgname => 'pve',
63 bwlimit => 'restore=1024',
64 type => 'lvmthin',
65 thinpool => 'data',
66 content => {
67 images => 1,
68 }
69 }
70 }
71 },
72 vmid => 8006,
73 real_qemu_version => PVE::QemuServer::kvm_user_version(), # not yet mocked
74 };
75
76 my $pci_devs = [
77 "0000:00:43.1",
78 "0000:00:f4.0",
79 "0000:00:ff.1",
80 "0000:0f:f2.0",
81 "0000:d0:13.0",
82 "0000:d0:15.1",
83 "0000:d0:17.0",
84 "0000:f0:42.0",
85 "0000:f0:43.0",
86 "0000:f0:43.1",
87 "1234:f0:43.1",
88 ];
89
90 my $current_test; # = {
91 # description => 'Test description', # if available
92 # qemu_version => '2.12',
93 # host_arch => 'HOST_ARCH',
94 # expected_error => 'error message',
95 # expected_warning => 'warning message',
96 # config => { config hash },
97 # expected => [ expected outcome cmd line array ],
98 # };
99
100 # use the config description to allow changing environment, fields are:
101 # TEST: A single line describing the test, gets outputted
102 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
103 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
104 # EXPECT_ERROR: <error message> For negative tests
105 # all fields are optional
106 sub parse_test($) {
107 my ($config_fn) = @_;
108
109 $current_test = {}; # reset
110
111 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
112 my $config_raw = file_get_contents($config_fn);
113 my $config = PVE::QemuServer::parse_vm_config($fake_config_fn, $config_raw);
114
115 $current_test->{config} = $config;
116
117 my $description = $config->{description} // '';
118
119 while ($description =~ /^\h*(.*?)\h*$/gm) {
120 my $line = $1;
121 next if !$line || $line =~ /^#/;
122 $line =~ s/^\s+//;
123 $line =~ s/\s+$//;
124
125 if ($line =~ /^TEST:\s*(.*)\s*$/) {
126 $current_test->{description} = "$1";
127 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
128 $current_test->{qemu_version} = "$1";
129 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
130 $current_test->{host_arch} = "$1";
131 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
132 $current_test->{expect_error} = "$1";
133 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
134 $current_test->{expect_warning} = "$1";
135 }
136 }
137
138 $config_fn =~ /([^\/]+)$/;
139 my $testname = "$1";
140 if (my $desc = $current_test->{description}) {
141 $testname = "'$testname' - $desc";
142 }
143 $current_test->{testname} = $testname;
144 }
145
146 sub get_test_qemu_version {
147 $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
148 }
149
150 my $qemu_server_module;
151 $qemu_server_module = Test::MockModule->new('PVE::QemuServer');
152 $qemu_server_module->mock(
153 kvm_user_version => sub {
154 return get_test_qemu_version();
155 },
156 kvm_version => sub {
157 return get_test_qemu_version();
158 },
159 kernel_has_vhost_net => sub {
160 return 1; # TODO: make this per-test configurable?
161 },
162 get_host_arch => sub() {
163 return $current_test->{host_arch} // 'x86_64';
164 },
165 get_initiator_name => sub {
166 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
167 }
168 );
169
170 my $qemu_server_config;
171 $qemu_server_config = Test::MockModule->new('PVE::QemuConfig');
172 $qemu_server_config->mock(
173 load_config => sub {
174 my ($class, $vmid, $node) = @_;
175
176 return $current_test->{config};
177 },
178 );
179
180 my $pve_common_tools;
181 $pve_common_tools = Test::MockModule->new('PVE::Tools');
182 $pve_common_tools->mock(
183 next_vnc_port => sub {
184 my ($family, $address) = @_;
185
186 return '5900';
187 },
188 next_spice_port => sub {
189 my ($family, $address) = @_;
190
191 return '61000';
192 },
193 getaddrinfo_all => sub {
194 my ($hostname, @opts) = @_;
195 die "need stable hostname" if $hostname ne 'localhost';
196 return (
197 {
198 addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK),
199 family => AF_INET, # IPv4
200 protocol => 6,
201 socktype => 1,
202 },
203 );
204 },
205 );
206
207 my $pve_cpuconfig;
208 $pve_cpuconfig = Test::MockModule->new('PVE::QemuServer::CPUConfig');
209 $pve_cpuconfig->mock(
210 load_custom_model_conf => sub {
211 # mock custom CPU model config
212 return PVE::QemuServer::CPUConfig->parse_config("cpu-models.conf",
213 <<EOF
214
215 # "qemu64" is also a default CPU, used here to test that this doesn't matter
216 cpu-model: qemu64
217 reported-model athlon
218 flags +aes;+avx;-kvm_pv_unhalt
219 hv-vendor-id testvend
220 phys-bits 40
221
222 cpu-model: alldefault
223
224 EOF
225 )
226 },
227 );
228
229 my $pve_common_network;
230 $pve_common_network = Test::MockModule->new('PVE::Network');
231 $pve_common_network->mock(
232 read_bridge_mtu => sub {
233 return 1500;
234 },
235 );
236
237
238 my $pve_common_inotify;
239 $pve_common_inotify = Test::MockModule->new('PVE::INotify');
240 $pve_common_inotify->mock(
241 nodename => sub {
242 return 'localhost';
243 },
244 );
245
246 my $pve_common_sysfstools;
247 $pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools');
248 $pve_common_sysfstools->mock(
249 lspci => sub {
250 my ($filter, $verbose) = @_;
251
252 return [
253 map { { id => $_ } }
254 grep {
255 !defined($filter)
256 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
257 || (ref($filter) eq 'CODE' && $filter->({ id => $_ }))
258 } sort @$pci_devs
259 ];
260 },
261 );
262
263 my $qemu_monitor_module;
264 $qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor');
265 $qemu_monitor_module->mock(
266 mon_cmd => sub {
267 my ($vmid, $cmd) = @_;
268
269 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
270 if $vmid != $base_env->{vmid};
271
272 if ($cmd eq 'query-version') {
273 my $ver = get_test_qemu_version();
274 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
275 return {
276 qemu => {
277 major => $1,
278 minor => $2,
279 micro => $3
280 }
281 }
282 }
283
284 die "unexpected QMP command: '$cmd'";
285 },
286 );
287 $qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd);
288
289 sub diff($$) {
290 my ($a, $b) = @_;
291 return if $a eq $b;
292
293 my ($ra, $wa) = POSIX::pipe();
294 my ($rb, $wb) = POSIX::pipe();
295 my $ha = IO::Handle->new_from_fd($wa, 'w');
296 my $hb = IO::Handle->new_from_fd($wb, 'w');
297
298 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
299 or die "failed to run program 'diff': $!";
300 POSIX::close($ra);
301 POSIX::close($rb);
302
303 open my $f1, '<', \$a;
304 open my $f2, '<', \$b;
305 my ($line1, $line2);
306 do {
307 $ha->print($line1) if defined($line1 = <$f1>);
308 $hb->print($line2) if defined($line2 = <$f2>);
309 } while (defined($line1 // $line2));
310 close $f1;
311 close $f2;
312 close $ha;
313 close $hb;
314
315 local $/ = undef;
316 my $diff = <$diffproc>;
317 close $diffproc;
318 die "files differ:\n$diff";
319 }
320
321 $SIG{__WARN__} = sub {
322 my $warning = shift;
323 chomp $warning;
324 if (my $warn_expect = $current_test->{expect_warning}) {
325 if ($warn_expect ne $warning) {
326 fail($current_test->{testname});
327 note("warning does not match expected error: '$warning' != '$warn_expect'");
328 } else {
329 note("got expected warning '$warning'");
330 return;
331 }
332 }
333
334 fail($current_test->{testname});
335 note("got unexpected warning '$warning'");
336 };
337
338 sub do_test($) {
339 my ($config_fn) = @_;
340
341 die "no such input test config: $config_fn\n" if ! -f $config_fn;
342
343 parse_test $config_fn;
344
345 my $testname = $current_test->{testname};
346
347 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
348
349 my $cmdline = eval { PVE::QemuServer::vm_commandline($storecfg, $vmid) };
350 my $err = $@;
351
352 if (my $err_expect = $current_test->{expect_error}) {
353 if (!$err) {
354 fail("$testname");
355 note("did NOT get any error, but expected error: $err_expect");
356 return;
357 }
358 chomp $err;
359 if ($err ne $err_expect) {
360 fail("$testname");
361 note("error does not match expected error: '$err' !~ '$err_expect'");
362 } else {
363 pass("$testname");
364 }
365 return;
366 } elsif ($err) {
367 fail("$testname");
368 note("got unexpected error: $err");
369 return;
370 }
371
372 # check if QEMU version set correctly and test version_cmp
373 (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//;
374 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
375 if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major);
376
377 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
378 $cmdline .= "\n";
379
380 my $cmd_fn = "$config_fn.cmd";
381
382 if (-f $cmd_fn) {
383 my $cmdline_expected = file_get_contents($cmd_fn);
384
385 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
386 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
387
388 # uncomment for easier debugging
389 #file_set_contents("$cmd_fn.tmp", $cmdline);
390
391 my $exp = join("\n", @$cmd_expected);
392 my $got = join("\n", @$cmd);
393 eval { diff($exp, $got) };
394 if (my $err = $@) {
395 fail("$testname");
396 note($err);
397 } else {
398 pass("$testname");
399 }
400 } else {
401 file_set_contents($cmd_fn, $cmdline);
402 }
403 }
404
405 print "testing config to command stabillity\n";
406
407 # exec tests
408 if (my $file = shift) {
409 do_test $file;
410 } else {
411 while (my $file = <cfg2cmd/*.conf>) {
412 do_test $file;
413 }
414 }
415
416 done_testing();