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