]>
Commit | Line | Data |
---|---|---|
7b963e57 TL |
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; | |
38277afc | 10 | use Socket qw(AF_INET AF_INET6); |
7b963e57 TL |
11 | |
12 | use PVE::Tools qw(file_get_contents file_set_contents run_command); | |
38277afc TL |
13 | use PVE::INotify; |
14 | use PVE::SysFSTools; | |
15 | ||
7b963e57 TL |
16 | use PVE::QemuConfig; |
17 | use PVE::QemuServer; | |
6db4c69e SR |
18 | use PVE::QemuServer::Monitor; |
19 | use PVE::QemuServer::Machine; | |
5bc08470 | 20 | use PVE::QemuServer::CPUConfig; |
7b963e57 TL |
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 | }, | |
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 |
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 | ||
7b963e57 TL |
91 | my $current_test; # = { |
92 | # description => 'Test description', # if available | |
93 | # qemu_version => '2.12', | |
94 | # host_arch => 'HOST_ARCH', | |
1e27bda1 TL |
95 | # expected_error => 'error message', |
96 | # expected_warning => 'warning message', | |
7b963e57 TL |
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) | |
1e27bda1 | 105 | # EXPECT_ERROR: <error message> For negative tests |
7b963e57 TL |
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"; | |
a546da03 TL |
132 | } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) { |
133 | $current_test->{expect_error} = "$1"; | |
1e27bda1 TL |
134 | } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) { |
135 | $current_test->{expect_warning} = "$1"; | |
7b963e57 TL |
136 | } |
137 | } | |
90d96715 TL |
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; | |
7b963e57 TL |
145 | } |
146 | ||
6db4c69e SR |
147 | sub get_test_qemu_version { |
148 | $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12'; | |
149 | } | |
150 | ||
7b963e57 TL |
151 | my $qemu_server_module; |
152 | $qemu_server_module = Test::MockModule->new('PVE::QemuServer'); | |
153 | $qemu_server_module->mock( | |
154 | kvm_user_version => sub { | |
6db4c69e | 155 | return get_test_qemu_version(); |
7b963e57 | 156 | }, |
66c53994 | 157 | kvm_version => sub { |
6db4c69e | 158 | return get_test_qemu_version(); |
66c53994 | 159 | }, |
db70021b TL |
160 | kernel_has_vhost_net => sub { |
161 | return 1; # TODO: make this per-test configurable? | |
162 | }, | |
7b963e57 TL |
163 | get_host_arch => sub() { |
164 | return $current_test->{host_arch} // 'x86_64'; | |
165 | }, | |
a7740135 WB |
166 | get_initiator_name => sub { |
167 | return 'iqn.1993-08.org.debian:01:aabbccddeeff'; | |
168 | } | |
7b963e57 TL |
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 | ||
ff9e7dc1 TL |
181 | my $pve_common_tools; |
182 | $pve_common_tools = Test::MockModule->new('PVE::Tools'); | |
183 | $pve_common_tools->mock( | |
184 | next_vnc_port => sub { | |
185 | my ($family, $address) = @_; | |
186 | ||
187 | return '5900'; | |
188 | }, | |
189 | next_spice_port => sub { | |
190 | my ($family, $address) = @_; | |
191 | ||
192 | return '61000'; | |
193 | }, | |
38277afc TL |
194 | getaddrinfo_all => sub { |
195 | my ($hostname, @opts) = @_; | |
196 | die "need stable hostname" if $hostname ne 'localhost'; | |
197 | return ( | |
198 | { | |
199 | addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK), | |
200 | family => AF_INET, # IPv4 | |
201 | protocol => 6, | |
202 | socktype => 1, | |
203 | }, | |
204 | ); | |
205 | }, | |
206 | ); | |
207 | ||
5bc08470 SR |
208 | my $pve_cpuconfig; |
209 | $pve_cpuconfig = Test::MockModule->new('PVE::QemuServer::CPUConfig'); | |
210 | $pve_cpuconfig->mock( | |
211 | load_custom_model_conf => sub { | |
212 | # mock custom CPU model config | |
213 | return PVE::QemuServer::CPUConfig->parse_config("cpu-models.conf", | |
214 | <<EOF | |
215 | ||
216 | # "qemu64" is also a default CPU, used here to test that this doesn't matter | |
217 | cpu-model: qemu64 | |
218 | reported-model athlon | |
219 | flags +aes;+avx;-kvm_pv_unhalt | |
220 | hv-vendor-id testvend | |
221 | phys-bits 40 | |
222 | ||
223 | cpu-model: alldefault | |
224 | ||
225 | EOF | |
226 | ) | |
227 | }, | |
228 | ); | |
229 | ||
121e3400 FG |
230 | my $pve_common_network; |
231 | $pve_common_network = Test::MockModule->new('PVE::Network'); | |
232 | $pve_common_network->mock( | |
233 | read_bridge_mtu => sub { | |
234 | return 1500; | |
235 | }, | |
236 | ); | |
237 | ||
238 | ||
38277afc TL |
239 | my $pve_common_inotify; |
240 | $pve_common_inotify = Test::MockModule->new('PVE::INotify'); | |
241 | $pve_common_inotify->mock( | |
242 | nodename => sub { | |
243 | return 'localhost'; | |
244 | }, | |
ff9e7dc1 TL |
245 | ); |
246 | ||
0360faad DC |
247 | my $pve_common_sysfstools; |
248 | $pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools'); | |
249 | $pve_common_sysfstools->mock( | |
250 | lspci => sub { | |
251 | my ($filter, $verbose) = @_; | |
252 | ||
253 | return [ | |
254 | map { { id => $_ } } | |
255 | grep { | |
256 | !defined($filter) | |
257 | || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/) | |
258 | || (ref($filter) eq 'CODE' && $filter->({ id => $_ })) | |
259 | } sort @$pci_devs | |
260 | ]; | |
261 | }, | |
262 | ); | |
263 | ||
6db4c69e SR |
264 | my $qemu_monitor_module; |
265 | $qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor'); | |
266 | $qemu_monitor_module->mock( | |
267 | mon_cmd => sub { | |
268 | my ($vmid, $cmd) = @_; | |
269 | ||
270 | die "invalid vmid: $vmid (expected: $base_env->{vmid})" | |
271 | if $vmid != $base_env->{vmid}; | |
272 | ||
273 | if ($cmd eq 'query-version') { | |
274 | my $ver = get_test_qemu_version(); | |
275 | $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/; | |
276 | return { | |
277 | qemu => { | |
278 | major => $1, | |
279 | minor => $2, | |
280 | micro => $3 | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | die "unexpected QMP command: '$cmd'"; | |
286 | }, | |
287 | ); | |
288 | $qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd); | |
289 | ||
3dc780c6 WB |
290 | sub diff($$) { |
291 | my ($a, $b) = @_; | |
292 | return if $a eq $b; | |
293 | ||
294 | my ($ra, $wa) = POSIX::pipe(); | |
295 | my ($rb, $wb) = POSIX::pipe(); | |
296 | my $ha = IO::Handle->new_from_fd($wa, 'w'); | |
297 | my $hb = IO::Handle->new_from_fd($wb, 'w'); | |
298 | ||
808a65b5 | 299 | open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic |
3dc780c6 WB |
300 | or die "failed to run program 'diff': $!"; |
301 | POSIX::close($ra); | |
302 | POSIX::close($rb); | |
303 | ||
304 | open my $f1, '<', \$a; | |
305 | open my $f2, '<', \$b; | |
306 | my ($line1, $line2); | |
307 | do { | |
308 | $ha->print($line1) if defined($line1 = <$f1>); | |
309 | $hb->print($line2) if defined($line2 = <$f2>); | |
310 | } while (defined($line1 // $line2)); | |
311 | close $f1; | |
312 | close $f2; | |
313 | close $ha; | |
314 | close $hb; | |
315 | ||
316 | local $/ = undef; | |
317 | my $diff = <$diffproc>; | |
318 | close $diffproc; | |
319 | die "files differ:\n$diff"; | |
320 | } | |
321 | ||
1e27bda1 TL |
322 | $SIG{__WARN__} = sub { |
323 | my $warning = shift; | |
324 | chomp $warning; | |
325 | if (my $warn_expect = $current_test->{expect_warning}) { | |
326 | if ($warn_expect ne $warning) { | |
327 | fail($current_test->{testname}); | |
328 | note("warning does not match expected error: '$warning' != '$warn_expect'"); | |
329 | } else { | |
330 | note("got expected warning '$warning'"); | |
331 | return; | |
332 | } | |
333 | } | |
334 | ||
1e27bda1 | 335 | fail($current_test->{testname}); |
3eb2f3eb | 336 | note("got unexpected warning '$warning'"); |
1e27bda1 TL |
337 | }; |
338 | ||
7b963e57 TL |
339 | sub do_test($) { |
340 | my ($config_fn) = @_; | |
341 | ||
342 | die "no such input test config: $config_fn\n" if ! -f $config_fn; | |
343 | ||
344 | parse_test $config_fn; | |
345 | ||
90d96715 | 346 | my $testname = $current_test->{testname}; |
7b963e57 TL |
347 | |
348 | my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)}; | |
349 | ||
a546da03 TL |
350 | my $cmdline = eval { PVE::QemuServer::vm_commandline($storecfg, $vmid) }; |
351 | my $err = $@; | |
352 | ||
353 | if (my $err_expect = $current_test->{expect_error}) { | |
354 | if (!$err) { | |
355 | fail("$testname"); | |
356 | note("did NOT get any error, but expected error: $err_expect"); | |
357 | return; | |
358 | } | |
359 | chomp $err; | |
ac0077cc | 360 | if ($err ne $err_expect) { |
a546da03 TL |
361 | fail("$testname"); |
362 | note("error does not match expected error: '$err' !~ '$err_expect'"); | |
363 | } else { | |
364 | pass("$testname"); | |
365 | } | |
366 | return; | |
367 | } elsif ($err) { | |
368 | fail("$testname"); | |
369 | note("got unexpected error: $err"); | |
370 | return; | |
371 | } | |
7b963e57 | 372 | |
6db4c69e SR |
373 | # check if QEMU version set correctly and test version_cmp |
374 | (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//; | |
375 | die "runs_at_least_qemu_version returned false, maybe error in version_cmp?" | |
376 | if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major); | |
377 | ||
7b963e57 TL |
378 | $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty |
379 | $cmdline .= "\n"; | |
380 | ||
381 | my $cmd_fn = "$config_fn.cmd"; | |
382 | ||
383 | if (-f $cmd_fn) { | |
384 | my $cmdline_expected = file_get_contents($cmd_fn); | |
385 | ||
69f4bd34 TL |
386 | my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ]; |
387 | my $cmd = [ split /\s*\\?\n\s*/, $cmdline ]; | |
7b963e57 | 388 | |
8b26544e | 389 | # uncomment for easier debugging |
7b963e57 TL |
390 | #file_set_contents("$cmd_fn.tmp", $cmdline); |
391 | ||
3dc780c6 WB |
392 | my $exp = join("\n", @$cmd_expected); |
393 | my $got = join("\n", @$cmd); | |
82562200 TL |
394 | eval { diff($exp, $got) }; |
395 | if (my $err = $@) { | |
396 | fail("$testname"); | |
397 | note($err); | |
398 | } else { | |
399 | pass("$testname"); | |
400 | } | |
7b963e57 TL |
401 | } else { |
402 | file_set_contents($cmd_fn, $cmdline); | |
403 | } | |
404 | } | |
405 | ||
406 | print "testing config to command stabillity\n"; | |
407 | ||
408 | # exec tests | |
409 | if (my $file = shift) { | |
410 | do_test $file; | |
411 | } else { | |
ce11958a | 412 | while (my $file = <cfg2cmd/*.conf>) { |
7b963e57 TL |
413 | do_test $file; |
414 | } | |
415 | } | |
416 | ||
417 | done_testing(); |