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