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',
35 path
=> '/mnt/pve/cifs-store',
37 server
=> '127.0.0.42',
45 monhost
=> '127.0.0.42,127.0.0.21,::1',
56 bwlimit
=> 'restore=1024',
66 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
83 my $current_test; # = {
84 # description => 'Test description', # if available
85 # qemu_version => '2.12',
86 # host_arch => 'HOST_ARCH',
87 # config => { config hash },
88 # expected => [ expected outcome cmd line array ],
91 # use the config description to allow changing environment, fields are:
92 # TEST: A single line describing the test, gets outputted
93 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
94 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
95 # all fields are optional
99 $current_test = {}; # reset
101 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
102 my $config_raw = file_get_contents
($config_fn);
103 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
105 $current_test->{config
} = $config;
107 my $description = $config->{description
} // '';
109 while ($description =~ /^\h*(.*?)\h*$/gm) {
111 next if !$line || $line =~ /^#/;
115 if ($line =~ /^TEST:\s*(.*)\s*$/) {
116 $current_test->{description
} = "$1";
117 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
118 $current_test->{qemu_version
} = "$1";
119 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
120 $current_test->{host_arch
} = "$1";
121 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
122 $current_test->{expect_error
} = "$1";
127 sub get_test_qemu_version
{
128 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
131 my $qemu_server_module;
132 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
133 $qemu_server_module->mock(
134 kvm_user_version
=> sub {
135 return get_test_qemu_version
();
138 return get_test_qemu_version
();
140 kernel_has_vhost_net
=> sub {
141 return 1; # TODO: make this per-test configurable?
143 get_host_arch
=> sub() {
144 return $current_test->{host_arch
} // 'x86_64';
146 get_initiator_name
=> sub {
147 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
151 my $qemu_server_config;
152 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
153 $qemu_server_config->mock(
155 my ($class, $vmid, $node) = @_;
157 return $current_test->{config
};
161 my $pve_common_tools;
162 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
163 $pve_common_tools->mock(
164 next_vnc_port
=> sub {
165 my ($family, $address) = @_;
169 next_spice_port
=> sub {
170 my ($family, $address) = @_;
174 getaddrinfo_all
=> sub {
175 my ($hostname, @opts) = @_;
176 die "need stable hostname" if $hostname ne 'localhost';
179 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
180 family
=> AF_INET
, # IPv4
189 $pve_cpuconfig = Test
::MockModule-
>new('PVE::QemuServer::CPUConfig');
190 $pve_cpuconfig->mock(
191 load_custom_model_conf
=> sub {
192 # mock custom CPU model config
193 return PVE
::QemuServer
::CPUConfig-
>parse_config("cpu-models.conf",
196 # "qemu64" is also a default CPU, used here to test that this doesn't matter
198 reported-model athlon
199 flags +aes;+avx;-kvm_pv_unhalt
200 hv-vendor-id testvend
203 cpu-model: alldefault
210 my $pve_common_network;
211 $pve_common_network = Test
::MockModule-
>new('PVE::Network');
212 $pve_common_network->mock(
213 read_bridge_mtu
=> sub {
219 my $pve_common_inotify;
220 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
221 $pve_common_inotify->mock(
227 my $pve_common_sysfstools;
228 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
229 $pve_common_sysfstools->mock(
231 my ($filter, $verbose) = @_;
237 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
238 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
244 my $qemu_monitor_module;
245 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
246 $qemu_monitor_module->mock(
248 my ($vmid, $cmd) = @_;
250 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
251 if $vmid != $base_env->{vmid
};
253 if ($cmd eq 'query-version') {
254 my $ver = get_test_qemu_version
();
255 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
265 die "unexpected QMP command: '$cmd'";
268 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
274 my ($ra, $wa) = POSIX
::pipe();
275 my ($rb, $wb) = POSIX
::pipe();
276 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
277 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
279 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
280 or die "failed to run program 'diff': $!";
284 open my $f1, '<', \
$a;
285 open my $f2, '<', \
$b;
288 $ha->print($line1) if defined($line1 = <$f1>);
289 $hb->print($line2) if defined($line2 = <$f2>);
290 } while (defined($line1 // $line2));
297 my $diff = <$diffproc>;
299 die "files differ:\n$diff";
303 my ($config_fn) = @_;
305 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
307 parse_test
$config_fn;
309 $config_fn =~ /([^\/]+)$/;
311 if (my $desc = $current_test->{description
}) {
312 $testname = "'$testname' - $desc";
315 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
317 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
320 if (my $err_expect = $current_test->{expect_error
}) {
323 note
("did NOT get any error, but expected error: $err_expect");
327 if ($err ne $err_expect) {
329 note
("error does not match expected error: '$err' !~ '$err_expect'");
336 note
("got unexpected error: $err");
340 # check if QEMU version set correctly and test version_cmp
341 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
342 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
343 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
345 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
348 my $cmd_fn = "$config_fn.cmd";
351 my $cmdline_expected = file_get_contents
($cmd_fn);
353 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
354 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
356 # uncomment for easier debugging
357 #file_set_contents("$cmd_fn.tmp", $cmdline);
359 my $exp = join("\n", @$cmd_expected);
360 my $got = join("\n", @$cmd);
361 eval { diff
($exp, $got) };
369 file_set_contents
($cmd_fn, $cmdline);
373 print "testing config to command stabillity\n";
376 if (my $file = shift) {
379 foreach my $file (<cfg2cmd
/*.conf
>) {