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