10 use Socket
qw(AF_INET AF_INET6);
12 use PVE
::Tools
qw(file_get_contents file_set_contents run_command);
18 use PVE
::QemuServer
::Monitor
;
19 use PVE
::QemuServer
::Machine
;
20 use PVE
::QemuServer
::CPUConfig
;
29 path
=> '/var/lib/vz',
37 path
=> '/butter/bread',
42 path
=> '/mnt/pve/cifs-store',
44 server
=> '127.0.0.42',
52 monhost
=> '127.0.0.42,127.0.0.21,::1',
53 fsid
=> 'fc4181a6-56eb-4f68-b452-8ba1f381ca2a',
64 bwlimit
=> 'restore=1024',
74 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
91 my $current_test; # = {
92 # description => 'Test description', # if available
93 # qemu_version => '2.12',
94 # host_arch => 'HOST_ARCH',
95 # expected_error => 'error message',
96 # expected_warning => 'warning message',
97 # config => { config hash },
98 # expected => [ expected outcome cmd line array ],
101 # use the config description to allow changing environment, fields are:
102 # TEST: A single line describing the test, gets outputted
103 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
104 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
105 # EXPECT_ERROR: <error message> For negative tests
106 # all fields are optional
108 my ($config_fn) = @_;
110 $current_test = {}; # reset
112 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
113 my $config_raw = file_get_contents
($config_fn);
114 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
116 $current_test->{config
} = $config;
118 my $description = $config->{description
} // '';
120 while ($description =~ /^\h*(.*?)\h*$/gm) {
122 next if !$line || $line =~ /^#/;
126 if ($line =~ /^TEST:\s*(.*)\s*$/) {
127 $current_test->{description
} = "$1";
128 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
129 $current_test->{qemu_version
} = "$1";
130 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
131 $current_test->{host_arch
} = "$1";
132 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
133 $current_test->{expect_error
} = "$1";
134 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
135 $current_test->{expect_warning
} = "$1";
139 $config_fn =~ /([^\/]+)$/;
141 if (my $desc = $current_test->{description
}) {
142 $testname = "'$testname' - $desc";
144 $current_test->{testname
} = $testname;
147 sub get_test_qemu_version
{
148 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
151 my $qemu_server_module;
152 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
153 $qemu_server_module->mock(
154 kvm_user_version
=> sub {
155 return get_test_qemu_version
();
158 return get_test_qemu_version
();
160 kernel_has_vhost_net
=> sub {
161 return 1; # TODO: make this per-test configurable?
163 get_host_arch
=> sub() {
164 return $current_test->{host_arch
} // 'x86_64';
166 get_initiator_name
=> sub {
167 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
171 my $qemu_server_config;
172 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
173 $qemu_server_config->mock(
175 my ($class, $vmid, $node) = @_;
177 return $current_test->{config
};
181 my $qemu_server_memory;
182 $qemu_server_memory = Test
::MockModule-
>new('PVE::QemuServer::Memory');
183 $qemu_server_memory->mock(
184 hugepages_chunk_size_supported
=> sub {
187 host_numanode_exists
=> sub {
191 get_host_phys_address_bits
=> sub {
196 my $pve_common_tools;
197 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
198 $pve_common_tools->mock(
199 next_vnc_port
=> sub {
200 my ($family, $address) = @_;
204 next_spice_port
=> sub {
205 my ($family, $address) = @_;
209 getaddrinfo_all
=> sub {
210 my ($hostname, @opts) = @_;
211 die "need stable hostname" if $hostname ne 'localhost';
214 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
215 family
=> AF_INET
, # IPv4
224 $pve_cpuconfig = Test
::MockModule-
>new('PVE::QemuServer::CPUConfig');
225 $pve_cpuconfig->mock(
226 load_custom_model_conf
=> sub {
227 # mock custom CPU model config
228 return PVE
::QemuServer
::CPUConfig-
>parse_config("cpu-models.conf",
231 # "qemu64" is also a default CPU, used here to test that this doesn't matter
233 reported-model athlon
234 flags +aes;+avx;-kvm_pv_unhalt
235 hv-vendor-id testvend
238 cpu-model: alldefault
245 my $pve_common_network;
246 $pve_common_network = Test
::MockModule-
>new('PVE::Network');
247 $pve_common_network->mock(
248 read_bridge_mtu
=> sub {
254 my $pve_common_inotify;
255 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
256 $pve_common_inotify->mock(
262 my $pve_common_sysfstools;
263 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
264 $pve_common_sysfstools->mock(
266 my ($filter, $verbose) = @_;
272 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
273 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
279 my $qemu_monitor_module;
280 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
281 $qemu_monitor_module->mock(
283 my ($vmid, $cmd) = @_;
285 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
286 if $vmid != $base_env->{vmid
};
288 if ($cmd eq 'query-version') {
289 my $ver = get_test_qemu_version
();
290 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
300 die "unexpected QMP command: '$cmd'";
303 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
309 my ($ra, $wa) = POSIX
::pipe();
310 my ($rb, $wb) = POSIX
::pipe();
311 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
312 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
314 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
315 or die "failed to run program 'diff': $!";
319 open my $f1, '<', \
$a;
320 open my $f2, '<', \
$b;
323 $ha->print($line1) if defined($line1 = <$f1>);
324 $hb->print($line2) if defined($line2 = <$f2>);
325 } while (defined($line1 // $line2));
332 my $diff = <$diffproc>;
334 die "files differ:\n$diff";
337 $SIG{__WARN__
} = sub {
340 if (my $warn_expect = $current_test->{expect_warning
}) {
341 if ($warn_expect ne $warning) {
342 fail
($current_test->{testname
});
343 note
("warning does not match expected error: '$warning' != '$warn_expect'");
345 note
("got expected warning '$warning'");
350 fail
($current_test->{testname
});
351 note
("got unexpected warning '$warning'");
355 my ($config_fn) = @_;
357 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
359 parse_test
$config_fn;
361 my $testname = $current_test->{testname
};
363 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
365 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
368 if (my $err_expect = $current_test->{expect_error
}) {
371 note
("did NOT get any error, but expected error: $err_expect");
375 if ($err ne $err_expect) {
377 note
("error does not match expected error: '$err' !~ '$err_expect'");
384 note
("got unexpected error: $err");
388 # check if QEMU version set correctly and test version_cmp
389 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
390 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
391 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
393 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
396 my $cmd_fn = "$config_fn.cmd";
399 my $cmdline_expected = file_get_contents
($cmd_fn);
401 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
402 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
404 # uncomment for easier debugging
405 #file_set_contents("$cmd_fn.tmp", $cmdline);
407 my $exp = join("\n", @$cmd_expected);
408 my $got = join("\n", @$cmd);
409 eval { diff
($exp, $got) };
417 file_set_contents
($cmd_fn, $cmdline);
421 print "testing config to command stabillity\n";
424 if (my $file = shift) {
427 while (my $file = <cfg2cmd
/*.conf
>) {