]> git.proxmox.com Git - qemu-server.git/blob - test/run_config2command_tests.pl
qemu-server: make nodename mock-able for tests
[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 }
121 }
122 }
123
124 sub get_test_qemu_version {
125 $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12';
126 }
127
128 my $qemu_server_module;
129 $qemu_server_module = Test::MockModule->new('PVE::QemuServer');
130 $qemu_server_module->mock(
131 kvm_user_version => sub {
132 return get_test_qemu_version();
133 },
134 kvm_version => sub {
135 return get_test_qemu_version();
136 },
137 kernel_has_vhost_net => sub {
138 return 1; # TODO: make this per-test configurable?
139 },
140 get_host_arch => sub() {
141 return $current_test->{host_arch} // 'x86_64';
142 },
143 get_initiator_name => sub {
144 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
145 }
146 );
147
148 my $qemu_server_config;
149 $qemu_server_config = Test::MockModule->new('PVE::QemuConfig');
150 $qemu_server_config->mock(
151 load_config => sub {
152 my ($class, $vmid, $node) = @_;
153
154 return $current_test->{config};
155 },
156 );
157
158 my $pve_common_tools;
159 $pve_common_tools = Test::MockModule->new('PVE::Tools');
160 $pve_common_tools->mock(
161 next_vnc_port => sub {
162 my ($family, $address) = @_;
163
164 return '5900';
165 },
166 next_spice_port => sub {
167 my ($family, $address) = @_;
168
169 return '61000';
170 },
171 getaddrinfo_all => sub {
172 my ($hostname, @opts) = @_;
173 die "need stable hostname" if $hostname ne 'localhost';
174 return (
175 {
176 addr => Socket::pack_sockaddr_in(0, Socket::INADDR_LOOPBACK),
177 family => AF_INET, # IPv4
178 protocol => 6,
179 socktype => 1,
180 },
181 );
182 },
183 );
184
185 my $pve_common_inotify;
186 $pve_common_inotify = Test::MockModule->new('PVE::INotify');
187 $pve_common_inotify->mock(
188 nodename => sub {
189 return 'localhost';
190 },
191 );
192
193 my $pve_common_sysfstools;
194 $pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools');
195 $pve_common_sysfstools->mock(
196 lspci => sub {
197 my ($filter, $verbose) = @_;
198
199 return [
200 map { { id => $_ } }
201 grep {
202 !defined($filter)
203 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
204 || (ref($filter) eq 'CODE' && $filter->({ id => $_ }))
205 } sort @$pci_devs
206 ];
207 },
208 );
209
210 my $qemu_monitor_module;
211 $qemu_monitor_module = Test::MockModule->new('PVE::QemuServer::Monitor');
212 $qemu_monitor_module->mock(
213 mon_cmd => sub {
214 my ($vmid, $cmd) = @_;
215
216 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
217 if $vmid != $base_env->{vmid};
218
219 if ($cmd eq 'query-version') {
220 my $ver = get_test_qemu_version();
221 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
222 return {
223 qemu => {
224 major => $1,
225 minor => $2,
226 micro => $3
227 }
228 }
229 }
230
231 die "unexpected QMP command: '$cmd'";
232 },
233 );
234 $qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd);
235
236 sub diff($$) {
237 my ($a, $b) = @_;
238 return if $a eq $b;
239
240 my ($ra, $wa) = POSIX::pipe();
241 my ($rb, $wb) = POSIX::pipe();
242 my $ha = IO::Handle->new_from_fd($wa, 'w');
243 my $hb = IO::Handle->new_from_fd($wb, 'w');
244
245 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
246 or die "failed to run program 'diff': $!";
247 POSIX::close($ra);
248 POSIX::close($rb);
249
250 open my $f1, '<', \$a;
251 open my $f2, '<', \$b;
252 my ($line1, $line2);
253 do {
254 $ha->print($line1) if defined($line1 = <$f1>);
255 $hb->print($line2) if defined($line2 = <$f2>);
256 } while (defined($line1 // $line2));
257 close $f1;
258 close $f2;
259 close $ha;
260 close $hb;
261
262 local $/ = undef;
263 my $diff = <$diffproc>;
264 close $diffproc;
265 die "files differ:\n$diff";
266 }
267
268 sub do_test($) {
269 my ($config_fn) = @_;
270
271 die "no such input test config: $config_fn\n" if ! -f $config_fn;
272
273 parse_test $config_fn;
274
275 $config_fn =~ /([^\/]+)$/;
276 my $testname = "$1";
277 if (my $desc = $current_test->{description}) {
278 $testname = "'$testname' - $desc";
279 }
280
281 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
282
283 my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $vmid);
284
285 # check if QEMU version set correctly and test version_cmp
286 (my $qemu_major = get_test_qemu_version()) =~ s/\..*$//;
287 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
288 if !PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, $qemu_major);
289
290 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
291 $cmdline .= "\n";
292
293 my $cmd_fn = "$config_fn.cmd";
294
295 if (-f $cmd_fn) {
296 my $cmdline_expected = file_get_contents($cmd_fn);
297
298 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
299 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
300
301 # uncomment for easier debugging
302 #file_set_contents("$cmd_fn.tmp", $cmdline);
303
304 my $exp = join("\n", @$cmd_expected);
305 my $got = join("\n", @$cmd);
306 eval { diff($exp, $got) };
307 if (my $err = $@) {
308 fail("$testname");
309 note($err);
310 } else {
311 pass("$testname");
312 }
313 } else {
314 file_set_contents($cmd_fn, $cmdline);
315 }
316 }
317
318 print "testing config to command stabillity\n";
319
320 # exec tests
321 if (my $file = shift) {
322 do_test $file;
323 } else {
324 foreach my $file (<cfg2cmd/*.conf>) {
325 do_test $file;
326 }
327 }
328
329 done_testing();