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