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',
53 monhost
=> '127.0.0.42,127.0.0.21,::1',
54 fsid
=> 'fc4181a6-56eb-4f68-b452-8ba1f381ca2a',
65 bwlimit
=> 'restore=1024',
75 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
99 my $pci_map_config = {
104 'node=localhost,path=0000:01:00.4,id=10de:2231,iommugroup=1',
105 'node=localhost,path=0000:01:00.5,id=10de:2231,iommugroup=1',
106 'node=localhost,path=0000:01:00.6,id=10de:2231,iommugroup=1',
112 'node=localhost,path=0000:07:10.0,id=8086:1520,iommugroup=2',
113 'node=localhost,path=0000:07:10.1,id=8086:1520,iommugroup=2',
114 'node=localhost,path=0000:07:10.4,id=8086:1520,iommugroup=2',
120 my $usb_map_config = {},
122 my $current_test; # = {
123 # description => 'Test description', # if available
124 # qemu_version => '2.12',
125 # host_arch => 'HOST_ARCH',
126 # expected_error => 'error message',
127 # expected_warning => 'warning message',
128 # config => { config hash },
129 # expected => [ expected outcome cmd line array ],
132 # use the config description to allow changing environment, fields are:
133 # TEST: A single line describing the test, gets outputted
134 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
135 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
136 # EXPECT_ERROR: <error message> For negative tests
137 # all fields are optional
139 my ($config_fn) = @_;
141 $current_test = {}; # reset
143 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
144 my $config_raw = file_get_contents
($config_fn);
145 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
147 $current_test->{config
} = $config;
149 my $description = $config->{description
} // '';
151 while ($description =~ /^\h*(.*?)\h*$/gm) {
153 next if !$line || $line =~ /^#/;
157 if ($line =~ /^TEST:\s*(.*)\s*$/) {
158 $current_test->{description
} = "$1";
159 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
160 $current_test->{qemu_version
} = "$1";
161 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
162 $current_test->{host_arch
} = "$1";
163 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
164 $current_test->{expect_error
} = "$1";
165 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
166 $current_test->{expect_warning
} = "$1";
170 $config_fn =~ /([^\/]+)$/;
172 if (my $desc = $current_test->{description
}) {
173 $testname = "'$testname' - $desc";
175 $current_test->{testname
} = $testname;
178 sub get_test_qemu_version
{
179 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
182 my $qemu_server_module;
183 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
184 $qemu_server_module->mock(
185 kvm_user_version
=> sub {
186 return get_test_qemu_version
();
189 return get_test_qemu_version
();
191 kernel_has_vhost_net
=> sub {
192 return 1; # TODO: make this per-test configurable?
194 get_host_arch
=> sub() {
195 return $current_test->{host_arch
} // 'x86_64';
197 get_initiator_name
=> sub {
198 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
202 my $qemu_server_config;
203 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
204 $qemu_server_config->mock(
206 my ($class, $vmid, $node) = @_;
208 return $current_test->{config
};
212 my $qemu_server_memory;
213 $qemu_server_memory = Test
::MockModule-
>new('PVE::QemuServer::Memory');
214 $qemu_server_memory->mock(
215 hugepages_chunk_size_supported
=> sub {
218 host_numanode_exists
=> sub {
222 get_host_phys_address_bits
=> sub {
227 my $pve_common_tools;
228 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
229 $pve_common_tools->mock(
230 next_vnc_port
=> sub {
231 my ($family, $address) = @_;
235 next_spice_port
=> sub {
236 my ($family, $address) = @_;
240 getaddrinfo_all
=> sub {
241 my ($hostname, @opts) = @_;
242 die "need stable hostname" if $hostname ne 'localhost';
245 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
246 family
=> AF_INET
, # IPv4
255 $pve_cpuconfig = Test
::MockModule-
>new('PVE::QemuServer::CPUConfig');
256 $pve_cpuconfig->mock(
257 load_custom_model_conf
=> sub {
258 # mock custom CPU model config
259 return PVE
::QemuServer
::CPUConfig-
>parse_config("cpu-models.conf",
262 # "qemu64" is also a default CPU, used here to test that this doesn't matter
264 reported-model athlon
265 flags +aes;+avx;-kvm_pv_unhalt
266 hv-vendor-id testvend
269 cpu-model: alldefault
276 my $pve_common_network;
277 $pve_common_network = Test
::MockModule-
>new('PVE::Network');
278 $pve_common_network->mock(
279 read_bridge_mtu
=> sub {
285 my $pve_common_inotify;
286 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
287 $pve_common_inotify->mock(
293 my $pve_common_sysfstools;
294 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
295 $pve_common_sysfstools->mock(
297 my ($filter, $verbose) = @_;
303 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
304 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
308 pci_device_info
=> sub {
309 my ($path, $noerr) = @_;
311 if ($path =~ m/^0000:01:00/) {
319 } elsif ($path =~ m/^0000:07:10/) {
332 my $qemu_monitor_module;
333 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
334 $qemu_monitor_module->mock(
336 my ($vmid, $cmd) = @_;
338 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
339 if $vmid != $base_env->{vmid
};
341 if ($cmd eq 'query-version') {
342 my $ver = get_test_qemu_version
();
343 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
353 die "unexpected QMP command: '$cmd'";
356 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
358 my $mapping_usb_module = Test
::MockModule-
>new("PVE::Mapping::USB");
359 $mapping_usb_module->mock(
361 return $usb_map_config;
365 my $mapping_pci_module = Test
::MockModule-
>new("PVE::Mapping::PCI");
366 $mapping_pci_module->mock(
368 return $pci_map_config;
372 my $pci_module = Test
::MockModule-
>new("PVE::QemuServer::PCI");
374 reserve_pci_usage
=> sub {
375 my ($ids, $vmid, $timeout, $pid, $dryrun) = @_;
377 $ids = [$ids] if !ref($ids);
380 if ($id eq "0000:07:10.1") {
393 my ($ra, $wa) = POSIX
::pipe();
394 my ($rb, $wb) = POSIX
::pipe();
395 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
396 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
398 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
399 or die "failed to run program 'diff': $!";
403 open my $f1, '<', \
$a;
404 open my $f2, '<', \
$b;
407 $ha->print($line1) if defined($line1 = <$f1>);
408 $hb->print($line2) if defined($line2 = <$f2>);
409 } while (defined($line1 // $line2));
416 my $diff = <$diffproc>;
418 die "files differ:\n$diff";
421 $SIG{__WARN__
} = sub {
424 if (my $warn_expect = $current_test->{expect_warning
}) {
425 if ($warn_expect ne $warning) {
426 fail
($current_test->{testname
});
427 note
("warning does not match expected error: '$warning' != '$warn_expect'");
429 note
("got expected warning '$warning'");
434 fail
($current_test->{testname
});
435 note
("got unexpected warning '$warning'");
439 my ($config_fn) = @_;
441 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
443 parse_test
$config_fn;
445 my $testname = $current_test->{testname
};
447 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
449 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
452 if (my $err_expect = $current_test->{expect_error
}) {
455 note
("did NOT get any error, but expected error: $err_expect");
459 if ($err ne $err_expect) {
461 note
("error does not match expected error: '$err' !~ '$err_expect'");
468 note
("got unexpected error: $err");
472 # check if QEMU version set correctly and test version_cmp
473 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
474 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
475 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
477 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
480 my $cmd_fn = "$config_fn.cmd";
483 my $cmdline_expected = file_get_contents
($cmd_fn);
485 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
486 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
488 # uncomment for easier debugging
489 #file_set_contents("$cmd_fn.tmp", $cmdline);
491 my $exp = join("\n", @$cmd_expected);
492 my $got = join("\n", @$cmd);
493 eval { diff
($exp, $got) };
501 file_set_contents
($cmd_fn, $cmdline);
505 print "testing config to command stabillity\n";
508 if (my $file = shift) {
511 while (my $file = <cfg2cmd
/*.conf
>) {