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