]>
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; | |
14 | ||
15 | my $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 | ||
62 | my $current_test; # = { | |
63 | # description => 'Test description', # if available | |
64 | # qemu_version => '2.12', | |
65 | # host_arch => 'HOST_ARCH', | |
66 | # config => { config hash }, | |
67 | # expected => [ expected outcome cmd line array ], | |
68 | # }; | |
69 | ||
70 | # use the config description to allow changing environment, fields are: | |
71 | # TEST: A single line describing the test, gets outputted | |
72 | # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version) | |
73 | # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable) | |
74 | # all fields are optional | |
75 | sub parse_test($) { | |
76 | my ($config_fn) = @_; | |
77 | ||
78 | $current_test = {}; # reset | |
79 | ||
80 | my $fake_config_fn ="$config_fn/qemu-server/8006.conf"; | |
81 | my $config_raw = file_get_contents($config_fn); | |
82 | my $config = PVE::QemuServer::parse_vm_config($fake_config_fn, $config_raw); | |
83 | ||
84 | $current_test->{config} = $config; | |
85 | ||
86 | my $description = $config->{description} // ''; | |
87 | ||
88 | while ($description =~ /^\h*(.*?)\h*$/gm) { | |
89 | my $line = $1; | |
90 | next if !$line || $line =~ /^#/; | |
91 | $line =~ s/^\s+//; | |
92 | $line =~ s/\s+$//; | |
93 | ||
94 | if ($line =~ /^TEST:\s*(.*)\s*$/) { | |
95 | $current_test->{description} = "$1"; | |
96 | } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) { | |
97 | $current_test->{qemu_version} = "$1"; | |
98 | } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) { | |
99 | $current_test->{host_arch} = "$1"; | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | my $qemu_server_module; | |
105 | $qemu_server_module = Test::MockModule->new('PVE::QemuServer'); | |
106 | $qemu_server_module->mock( | |
107 | kvm_user_version => sub { | |
987e7ab4 | 108 | return $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12'; |
7b963e57 | 109 | }, |
66c53994 | 110 | kvm_version => sub { |
987e7ab4 | 111 | return $current_test->{qemu_version} // $base_env->{real_qemu_version} // '2.12'; |
66c53994 | 112 | }, |
db70021b TL |
113 | kernel_has_vhost_net => sub { |
114 | return 1; # TODO: make this per-test configurable? | |
115 | }, | |
7b963e57 TL |
116 | get_host_arch => sub() { |
117 | return $current_test->{host_arch} // 'x86_64'; | |
118 | }, | |
a7740135 WB |
119 | get_initiator_name => sub { |
120 | return 'iqn.1993-08.org.debian:01:aabbccddeeff'; | |
121 | } | |
7b963e57 TL |
122 | ); |
123 | ||
124 | my $qemu_server_config; | |
125 | $qemu_server_config = Test::MockModule->new('PVE::QemuConfig'); | |
126 | $qemu_server_config->mock( | |
127 | load_config => sub { | |
128 | my ($class, $vmid, $node) = @_; | |
129 | ||
130 | return $current_test->{config}; | |
131 | }, | |
132 | ); | |
133 | ||
ff9e7dc1 TL |
134 | my $pve_common_tools; |
135 | $pve_common_tools = Test::MockModule->new('PVE::Tools'); | |
136 | $pve_common_tools->mock( | |
137 | next_vnc_port => sub { | |
138 | my ($family, $address) = @_; | |
139 | ||
140 | return '5900'; | |
141 | }, | |
142 | next_spice_port => sub { | |
143 | my ($family, $address) = @_; | |
144 | ||
145 | return '61000'; | |
146 | }, | |
147 | ); | |
148 | ||
3dc780c6 WB |
149 | sub diff($$) { |
150 | my ($a, $b) = @_; | |
151 | return if $a eq $b; | |
152 | ||
153 | my ($ra, $wa) = POSIX::pipe(); | |
154 | my ($rb, $wb) = POSIX::pipe(); | |
155 | my $ha = IO::Handle->new_from_fd($wa, 'w'); | |
156 | my $hb = IO::Handle->new_from_fd($wb, 'w'); | |
157 | ||
e1bdc0de | 158 | open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" |
3dc780c6 WB |
159 | or die "failed to run program 'diff': $!"; |
160 | POSIX::close($ra); | |
161 | POSIX::close($rb); | |
162 | ||
163 | open my $f1, '<', \$a; | |
164 | open my $f2, '<', \$b; | |
165 | my ($line1, $line2); | |
166 | do { | |
167 | $ha->print($line1) if defined($line1 = <$f1>); | |
168 | $hb->print($line2) if defined($line2 = <$f2>); | |
169 | } while (defined($line1 // $line2)); | |
170 | close $f1; | |
171 | close $f2; | |
172 | close $ha; | |
173 | close $hb; | |
174 | ||
175 | local $/ = undef; | |
176 | my $diff = <$diffproc>; | |
177 | close $diffproc; | |
178 | die "files differ:\n$diff"; | |
179 | } | |
180 | ||
7b963e57 TL |
181 | sub do_test($) { |
182 | my ($config_fn) = @_; | |
183 | ||
184 | die "no such input test config: $config_fn\n" if ! -f $config_fn; | |
185 | ||
186 | parse_test $config_fn; | |
187 | ||
188 | $config_fn =~ /([^\/]+)$/; | |
189 | my $testname = "$1"; | |
190 | if (my $desc = $current_test->{description}) { | |
191 | $testname = "'$testname' - $desc"; | |
192 | } | |
193 | ||
194 | my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)}; | |
195 | ||
196 | my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $vmid); | |
197 | ||
198 | $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty | |
199 | $cmdline .= "\n"; | |
200 | ||
201 | my $cmd_fn = "$config_fn.cmd"; | |
202 | ||
203 | if (-f $cmd_fn) { | |
204 | my $cmdline_expected = file_get_contents($cmd_fn); | |
205 | ||
69f4bd34 TL |
206 | my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ]; |
207 | my $cmd = [ split /\s*\\?\n\s*/, $cmdline ]; | |
7b963e57 TL |
208 | |
209 | # comment out for easier debugging | |
210 | #file_set_contents("$cmd_fn.tmp", $cmdline); | |
211 | ||
3dc780c6 WB |
212 | my $exp = join("\n", @$cmd_expected); |
213 | my $got = join("\n", @$cmd); | |
82562200 TL |
214 | eval { diff($exp, $got) }; |
215 | if (my $err = $@) { | |
216 | fail("$testname"); | |
217 | note($err); | |
218 | } else { | |
219 | pass("$testname"); | |
220 | } | |
7b963e57 TL |
221 | } else { |
222 | file_set_contents($cmd_fn, $cmdline); | |
223 | } | |
224 | } | |
225 | ||
226 | print "testing config to command stabillity\n"; | |
227 | ||
228 | # exec tests | |
229 | if (my $file = shift) { | |
230 | do_test $file; | |
231 | } else { | |
232 | foreach my $file (<cfg2cmd/*.conf>) { | |
233 | do_test $file; | |
234 | } | |
235 | } | |
236 | ||
237 | done_testing(); |