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