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
98 my $pci_map_config = {
103 'node=localhost,path=0000:01:00.4,id=10de:2231,iommugroup=1',
104 'node=localhost,path=0000:01:00.5,id=10de:2231,iommugroup=1',
105 'node=localhost,path=0000:01:00.6,id=10de:2231,iommugroup=1',
111 'node=localhost,path=0000:07:10.0,id=8086:1520,iommugroup=2',
112 'node=localhost,path=0000:07:10.1,id=8086:1520,iommugroup=2',
113 'node=localhost,path=0000:07:10.4,id=8086:1520,iommugroup=2',
119 my $usb_map_config = {},
121 my $current_test; # = {
122 # description => 'Test description', # if available
123 # qemu_version => '2.12',
124 # host_arch => 'HOST_ARCH',
125 # expected_error => 'error message',
126 # expected_warning => 'warning message',
127 # config => { config hash },
128 # expected => [ expected outcome cmd line array ],
131 # use the config description to allow changing environment, fields are:
132 # TEST: A single line describing the test, gets outputted
133 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
134 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
135 # EXPECT_ERROR: <error message> For negative tests
136 # all fields are optional
138 my ($config_fn) = @_;
140 $current_test = {}; # reset
142 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
143 my $config_raw = file_get_contents
($config_fn);
144 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
146 $current_test->{config
} = $config;
148 my $description = $config->{description
} // '';
150 while ($description =~ /^\h*(.*?)\h*$/gm) {
152 next if !$line || $line =~ /^#/;
156 if ($line =~ /^TEST:\s*(.*)\s*$/) {
157 $current_test->{description
} = "$1";
158 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
159 $current_test->{qemu_version
} = "$1";
160 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
161 $current_test->{host_arch
} = "$1";
162 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
163 $current_test->{expect_error
} = "$1";
164 } elsif ($line =~ /^EXPECT_WARN(?:ING)?:\s*(.*)\s*$/) {
165 $current_test->{expect_warning
} = "$1";
169 $config_fn =~ /([^\/]+)$/;
171 if (my $desc = $current_test->{description
}) {
172 $testname = "'$testname' - $desc";
174 $current_test->{testname
} = $testname;
177 sub get_test_qemu_version
{
178 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
181 my $qemu_server_module;
182 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
183 $qemu_server_module->mock(
184 kvm_user_version
=> sub {
185 return get_test_qemu_version
();
188 return get_test_qemu_version
();
190 kernel_has_vhost_net
=> sub {
191 return 1; # TODO: make this per-test configurable?
193 get_host_arch
=> sub() {
194 return $current_test->{host_arch
} // 'x86_64';
196 get_initiator_name
=> sub {
197 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
201 my $qemu_server_config;
202 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
203 $qemu_server_config->mock(
205 my ($class, $vmid, $node) = @_;
207 return $current_test->{config
};
211 my $qemu_server_memory;
212 $qemu_server_memory = Test
::MockModule-
>new('PVE::QemuServer::Memory');
213 $qemu_server_memory->mock(
214 hugepages_chunk_size_supported
=> sub {
217 host_numanode_exists
=> sub {
221 get_host_phys_address_bits
=> sub {
226 my $pve_common_tools;
227 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
228 $pve_common_tools->mock(
229 next_vnc_port
=> sub {
230 my ($family, $address) = @_;
234 next_spice_port
=> sub {
235 my ($family, $address) = @_;
239 getaddrinfo_all
=> sub {
240 my ($hostname, @opts) = @_;
241 die "need stable hostname" if $hostname ne 'localhost';
244 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
245 family
=> AF_INET
, # IPv4
254 $pve_cpuconfig = Test
::MockModule-
>new('PVE::QemuServer::CPUConfig');
255 $pve_cpuconfig->mock(
256 load_custom_model_conf
=> sub {
257 # mock custom CPU model config
258 return PVE
::QemuServer
::CPUConfig-
>parse_config("cpu-models.conf",
261 # "qemu64" is also a default CPU, used here to test that this doesn't matter
263 reported-model athlon
264 flags +aes;+avx;-kvm_pv_unhalt
265 hv-vendor-id testvend
268 cpu-model: alldefault
275 my $pve_common_network;
276 $pve_common_network = Test
::MockModule-
>new('PVE::Network');
277 $pve_common_network->mock(
278 read_bridge_mtu
=> sub {
284 my $pve_common_inotify;
285 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
286 $pve_common_inotify->mock(
292 my $pve_common_sysfstools;
293 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
294 $pve_common_sysfstools->mock(
296 my ($filter, $verbose) = @_;
302 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
303 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
307 pci_device_info
=> sub {
308 my ($path, $noerr) = @_;
310 if ($path =~ m/^0000:01:00/) {
318 } elsif ($path =~ m/^0000:07:10/) {
331 my $qemu_monitor_module;
332 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
333 $qemu_monitor_module->mock(
335 my ($vmid, $cmd) = @_;
337 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
338 if $vmid != $base_env->{vmid
};
340 if ($cmd eq 'query-version') {
341 my $ver = get_test_qemu_version
();
342 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
352 die "unexpected QMP command: '$cmd'";
355 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
357 my $mapping_usb_module = Test
::MockModule-
>new("PVE::Mapping::USB");
358 $mapping_usb_module->mock(
360 return $usb_map_config;
364 my $mapping_pci_module = Test
::MockModule-
>new("PVE::Mapping::PCI");
365 $mapping_pci_module->mock(
367 return $pci_map_config;
371 my $pci_module = Test
::MockModule-
>new("PVE::QemuServer::PCI");
373 reserve_pci_usage
=> sub {
374 my ($ids, $vmid, $timeout, $pid, $dryrun) = @_;
376 $ids = [$ids] if !ref($ids);
379 if ($id eq "0000:07:10.1") {
392 my ($ra, $wa) = POSIX
::pipe();
393 my ($rb, $wb) = POSIX
::pipe();
394 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
395 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
397 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic
398 or die "failed to run program 'diff': $!";
402 open my $f1, '<', \
$a;
403 open my $f2, '<', \
$b;
406 $ha->print($line1) if defined($line1 = <$f1>);
407 $hb->print($line2) if defined($line2 = <$f2>);
408 } while (defined($line1 // $line2));
415 my $diff = <$diffproc>;
417 die "files differ:\n$diff";
420 $SIG{__WARN__
} = sub {
423 if (my $warn_expect = $current_test->{expect_warning
}) {
424 if ($warn_expect ne $warning) {
425 fail
($current_test->{testname
});
426 note
("warning does not match expected error: '$warning' != '$warn_expect'");
428 note
("got expected warning '$warning'");
433 fail
($current_test->{testname
});
434 note
("got unexpected warning '$warning'");
438 my ($config_fn) = @_;
440 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
442 parse_test
$config_fn;
444 my $testname = $current_test->{testname
};
446 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
448 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
451 if (my $err_expect = $current_test->{expect_error
}) {
454 note
("did NOT get any error, but expected error: $err_expect");
458 if ($err ne $err_expect) {
460 note
("error does not match expected error: '$err' !~ '$err_expect'");
467 note
("got unexpected error: $err");
471 # check if QEMU version set correctly and test version_cmp
472 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
473 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
474 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
476 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
479 my $cmd_fn = "$config_fn.cmd";
482 my $cmdline_expected = file_get_contents
($cmd_fn);
484 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
485 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
487 # uncomment for easier debugging
488 #file_set_contents("$cmd_fn.tmp", $cmdline);
490 my $exp = join("\n", @$cmd_expected);
491 my $got = join("\n", @$cmd);
492 eval { diff
($exp, $got) };
500 file_set_contents
($cmd_fn, $cmdline);
504 print "testing config to command stabillity\n";
507 if (my $file = shift) {
510 while (my $file = <cfg2cmd
/*.conf
>) {