]> git.proxmox.com Git - qemu-server.git/blame - test/run_config2command_tests.pl
update_vm_api: properly wrap arguments
[qemu-server.git] / test / run_config2command_tests.pl
CommitLineData
7b963e57
TL
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use Test::More;
9use Test::MockModule;
38277afc 10use Socket qw(AF_INET AF_INET6);
7b963e57
TL
11
12use PVE::Tools qw(file_get_contents file_set_contents run_command);
38277afc
TL
13use PVE::INotify;
14use PVE::SysFSTools;
15
7b963e57
TL
16use PVE::QemuConfig;
17use PVE::QemuServer;
6db4c69e
SR
18use PVE::QemuServer::Monitor;
19use PVE::QemuServer::Machine;
5bc08470 20use PVE::QemuServer::CPUConfig;
7b963e57
TL
21
22my $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 },
5620282f
TL
33 'btrfs-store' => {
34 content => {
35 images => 1,
36 },
37 path => '/butter/bread',
38 type => 'btrfs',
39 },
7b963e57
TL
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',
188eb9c3 53 fsid => 'fc4181a6-56eb-4f68-b452-8ba1f381ca2a',
7b963e57
TL
54 content => {
55 images => 1
56 },
57 type => 'rbd',
58 pool => 'cpool',
59 username => 'admin',
60 shared => 1
61 },
62 'local-lvm' => {
63 vgname => 'pve',
64 bwlimit => 'restore=1024',
65 type => 'lvmthin',
66 thinpool => 'data',
67 content => {
68 images => 1,
69 }
70 }
71 }
72 },
73 vmid => 8006,
74 real_qemu_version => PVE::QemuServer::kvm_user_version(), # not yet mocked
75};
76
0360faad
DC
77my $pci_devs = [
78 "0000:00:43.1",
79 "0000:00:f4.0",
80 "0000:00:ff.1",
81 "0000:0f:f2.0",
82 "0000:d0:13.0",
83 "0000:d0:15.1",
9b71c34d 84 "0000:d0:15.2",
0360faad
DC
85 "0000:d0:17.0",
86 "0000:f0:42.0",
87 "0000:f0:43.0",
88 "0000:f0:43.1",
89 "1234:f0:43.1",
42ac8180
DC
90 "0000:01:00.4",
91 "0000:01:00.5",
92 "0000:01:00.6",
93 "0000:07:10.0",
94 "0000:07:10.1",
95 "0000:07:10.4",
0360faad
DC
96];
97
42ac8180
DC
98my $pci_map_config = {
99 ids => {
100 someGpu => {
101 type => 'pci',
102 map => [
103 'node=localhost,path=0000:01:00.4,id=10de:2231,iommugroup=1',
104 'node=localhost,path=0000:01:00.5,id=10de:2231,iommugroup=1',
105 'node=localhost,path=0000:01:00.6,id=10de:2231,iommugroup=1',
106 ],
107 },
108 someNic => {
109 type => 'pci',
110 map => [
111 'node=localhost,path=0000:07:10.0,id=8086:1520,iommugroup=2',
112 'node=localhost,path=0000:07:10.1,id=8086:1520,iommugroup=2',
113 'node=localhost,path=0000:07:10.4,id=8086:1520,iommugroup=2',
114 ],
115 },
116 },
117};
118
119my $usb_map_config = {},
120
7b963e57
TL
121my $current_test; # = {
122# description => 'Test description', # if available
123# qemu_version => '2.12',
124# host_arch => 'HOST_ARCH',
1e27bda1
TL
125# expected_error => 'error message',
126# expected_warning => 'warning message',
7b963e57
TL
127# config => { config hash },
128# expected => [ expected outcome cmd line array ],
129# };
130
131# use the config description to allow changing environment, fields are:
132# TEST: A single line describing the test, gets outputted
133# QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
134# HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
1e27bda1 135# EXPECT_ERROR: <error message> For negative tests
7b963e57
TL
136# all fields are optional
137sub parse_test($) {
138 my ($config_fn) = @_;
139
140 $current_test = {}; # reset
141
142 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
143 my $config_raw = file_get_contents($config_fn);
144 my $config = PVE::QemuServer::parse_vm_config($fake_config_fn, $config_raw);
145
146 $current_test->{config} = $config;
147
148 my $description = $config->{description} // '';
149
150 while ($description =~ /^\h*(.*?)\h*$/gm) {
151 my $line = $1;
152 next if !$line || $line =~ /^#/;
153 $line =~ s/^\s+//;
154 $line =~ s/\s+$//;
155
156 if ($line =~ /^TEST:\s*(.*)\s*$/) {
157 $current_test->{description} = "$1";
158 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
159 $current_test->{qemu_version} = "$1";
160 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
161 $current_test->{host_arch} = "$1";
a546da03
TL
162 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
163 $current_test->{expect_error} = "$1";
1e27bda1
TL
164 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
165 $current_test->{expect_warning} = "$1";
7b963e57
TL
166 }
167 }
90d96715
TL
168
169 $config_fn =~ /([^\/]+)$/;
170 my $testname = "$1";
171 if (my $desc = $current_test->{description}) {
172 $testname = "'$testname' - $desc";
173 }
174 $current_test->{testname} = $testname;
7b963e57
TL
175}
176
6db4c69e
SR
177sub get_test_qemu_version {
178 $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
179}
180
7b963e57
TL
181my $qemu_server_module;
182$qemu_server_module = Test::MockModule->new('PVE::QemuServer');
183$qemu_server_module->mock(
184 kvm_user_version => sub {
6db4c69e 185 return get_test_qemu_version();
7b963e57 186 },
66c53994 187 kvm_version => sub {
6db4c69e 188 return get_test_qemu_version();
66c53994 189 },
db70021b
TL
190 kernel_has_vhost_net => sub {
191 return 1; # TODO: make this per-test configurable?
192 },
7b963e57
TL
193 get_host_arch => sub() {
194 return $current_test->{host_arch} // 'x86_64';
195 },
a7740135
WB
196 get_initiator_name => sub {
197 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
198 }
7b963e57
TL
199);
200
201my $qemu_server_config;
202$qemu_server_config = Test::MockModule->new('PVE::QemuConfig');
203$qemu_server_config->mock(
204 load_config => sub {
205 my ($class, $vmid, $node) = @_;
206
207 return $current_test->{config};
208 },
209);
210
6eabfbd1
AD
211my $qemu_server_memory;
212$qemu_server_memory = Test::MockModule->new('PVE::QemuServer::Memory');
213$qemu_server_memory->mock(
214 hugepages_chunk_size_supported => sub {
215 return 1;
216 },
217 host_numanode_exists => sub {
218 my ($id) = @_;
219 return 1;
220 },
221 get_host_phys_address_bits => sub {
222 return 46;
223 }
224);
225
ff9e7dc1
TL
226my $pve_common_tools;
227$pve_common_tools = Test::MockModule->new('PVE::Tools');
228$pve_common_tools->mock(
229 next_vnc_port => sub {
230 my ($family, $address) = @_;
231
232 return '5900';
233 },
234 next_spice_port => sub {
235 my ($family, $address) = @_;
236
237 return '61000';
238 },
38277afc
TL
239 getaddrinfo_all => sub {
240 my ($hostname, @opts) = @_;
241 die "need stable hostname" if $hostname ne 'localhost';
242 return (
243 {
244 addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK),
245 family => AF_INET, # IPv4
246 protocol => 6,
247 socktype => 1,
248 },
249 );
250 },
251);
252
5bc08470
SR
253my $pve_cpuconfig;
254$pve_cpuconfig = Test::MockModule->new('PVE::QemuServer::CPUConfig');
255$pve_cpuconfig->mock(
256 load_custom_model_conf => sub {
257 # mock custom CPU model config
258 return PVE::QemuServer::CPUConfig->parse_config("cpu-models.conf",
259<<EOF
260
261# "qemu64" is also a default CPU, used here to test that this doesn't matter
262cpu-model: qemu64
263 reported-model athlon
264 flags +aes;+avx;-kvm_pv_unhalt
265 hv-vendor-id testvend
266 phys-bits 40
267
268cpu-model: alldefault
269
270EOF
271 )
272 },
273);
274
121e3400
FG
275my $pve_common_network;
276$pve_common_network = Test::MockModule->new('PVE::Network');
277$pve_common_network->mock(
278 read_bridge_mtu => sub {
279 return 1500;
280 },
281);
282
283
38277afc
TL
284my $pve_common_inotify;
285$pve_common_inotify = Test::MockModule->new('PVE::INotify');
286$pve_common_inotify->mock(
287 nodename => sub {
288 return 'localhost';
289 },
ff9e7dc1
TL
290);
291
0360faad
DC
292my $pve_common_sysfstools;
293$pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools');
294$pve_common_sysfstools->mock(
295 lspci => sub {
296 my ($filter, $verbose) = @_;
297
298 return [
299 map { { id => $_ } }
300 grep {
301 !defined($filter)
302 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
303 || (ref($filter) eq 'CODE' && $filter->({ id => $_ }))
304 } sort @$pci_devs
305 ];
306 },
42ac8180
DC
307 pci_device_info => sub {
308 my ($path, $noerr) = @_;
309
310 if ($path =~ m/^0000:01:00/) {
311 return {
312 mdev => 1,
313 iommugroup => 1,
314 mdev => 1,
315 vendor => "0x10de",
316 device => "0x2231",
317 };
318 } elsif ($path =~ m/^0000:07:10/) {
319 return {
320 iommugroup => 2,
321 mdev => 0,
322 vendor => "0x8086",
323 device => "0x1520",
324 };
325 } else {
326 return {};
327 }
328 },
0360faad
DC
329);
330
6db4c69e
SR
331my $qemu_monitor_module;
332$qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor');
333$qemu_monitor_module->mock(
334 mon_cmd => sub {
335 my ($vmid, $cmd) = @_;
336
337 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
338 if $vmid != $base_env->{vmid};
339
340 if ($cmd eq 'query-version') {
341 my $ver = get_test_qemu_version();
342 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
343 return {
344 qemu => {
345 major => $1,
346 minor => $2,
347 micro => $3
348 }
349 }
350 }
351
352 die "unexpected QMP command: '$cmd'";
353 },
354);
355$qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd);
356
42ac8180
DC
357my $mapping_usb_module = Test::MockModule->new("PVE::Mapping::USB");
358$mapping_usb_module->mock(
359 config => sub {
360 return $usb_map_config;
361 },
362);
363
364my $mapping_pci_module = Test::MockModule->new("PVE::Mapping::PCI");
365$mapping_pci_module->mock(
366 config => sub {
367 return $pci_map_config;
368 },
369);
370
371my $pci_module = Test::MockModule->new("PVE::QemuServer::PCI");
372$pci_module->mock(
373 reserve_pci_usage => sub {
374 my ($ids, $vmid, $timeout, $pid, $dryrun) = @_;
375
376 $ids = [$ids] if !ref($ids);
377
378 for my $id (@$ids) {
379 if ($id eq "0000:07:10.1") {
380 die "reserved";
381 }
382 }
383
384 return undef;
385 },
386);
387
3dc780c6
WB
388sub diff($$) {
389 my ($a, $b) = @_;
390 return if $a eq $b;
391
392 my ($ra, $wa) = POSIX::pipe();
393 my ($rb, $wb) = POSIX::pipe();
394 my $ha = IO::Handle->new_from_fd($wa, 'w');
395 my $hb = IO::Handle->new_from_fd($wb, 'w');
396
808a65b5 397 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
3dc780c6
WB
398 or die "failed to run program 'diff': $!";
399 POSIX::close($ra);
400 POSIX::close($rb);
401
402 open my $f1, '<', \$a;
403 open my $f2, '<', \$b;
404 my ($line1, $line2);
405 do {
406 $ha->print($line1) if defined($line1 = <$f1>);
407 $hb->print($line2) if defined($line2 = <$f2>);
408 } while (defined($line1 // $line2));
409 close $f1;
410 close $f2;
411 close $ha;
412 close $hb;
413
414 local $/ = undef;
415 my $diff = <$diffproc>;
416 close $diffproc;
417 die "files differ:\n$diff";
418}
419
1e27bda1
TL
420$SIG{__WARN__} = sub {
421 my $warning = shift;
422 chomp $warning;
423 if (my $warn_expect = $current_test->{expect_warning}) {
424 if ($warn_expect ne $warning) {
425 fail($current_test->{testname});
426 note("warning does not match expected error: '$warning' != '$warn_expect'");
427 } else {
428 note("got expected warning '$warning'");
429 return;
430 }
431 }
432
1e27bda1 433 fail($current_test->{testname});
3eb2f3eb 434 note("got unexpected warning '$warning'");
1e27bda1
TL
435};
436
7b963e57
TL
437sub do_test($) {
438 my ($config_fn) = @_;
439
440 die "no such input test config: $config_fn\n" if ! -f $config_fn;
441
442 parse_test $config_fn;
443
90d96715 444 my $testname = $current_test->{testname};
7b963e57
TL
445
446 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
447
a546da03
TL
448 my $cmdline = eval { PVE::QemuServer::vm_commandline($storecfg, $vmid) };
449 my $err = $@;
450
451 if (my $err_expect = $current_test->{expect_error}) {
452 if (!$err) {
453 fail("$testname");
454 note("did NOT get any error, but expected error: $err_expect");
455 return;
456 }
457 chomp $err;
ac0077cc 458 if ($err ne $err_expect) {
a546da03
TL
459 fail("$testname");
460 note("error does not match expected error: '$err' !~ '$err_expect'");
461 } else {
462 pass("$testname");
463 }
464 return;
465 } elsif ($err) {
466 fail("$testname");
467 note("got unexpected error: $err");
468 return;
469 }
7b963e57 470
6db4c69e
SR
471 # check if QEMU version set correctly and test version_cmp
472 (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//;
473 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
474 if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major);
475
7b963e57
TL
476 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
477 $cmdline .= "\n";
478
479 my $cmd_fn = "$config_fn.cmd";
480
481 if (-f $cmd_fn) {
482 my $cmdline_expected = file_get_contents($cmd_fn);
483
69f4bd34
TL
484 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
485 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
7b963e57 486
8b26544e 487 # uncomment for easier debugging
7b963e57
TL
488 #file_set_contents("$cmd_fn.tmp", $cmdline);
489
3dc780c6
WB
490 my $exp = join("\n", @$cmd_expected);
491 my $got = join("\n", @$cmd);
82562200
TL
492 eval { diff($exp, $got) };
493 if (my $err = $@) {
494 fail("$testname");
495 note($err);
496 } else {
497 pass("$testname");
498 }
7b963e57
TL
499 } else {
500 file_set_contents($cmd_fn, $cmdline);
501 }
502}
503
504print "testing config to command stabillity\n";
505
506# exec tests
507if (my $file = shift) {
508 do_test $file;
509} else {
ce11958a 510 while (my $file = <cfg2cmd/*.conf>) {
7b963e57
TL
511 do_test $file;
512 }
513}
514
515done_testing();