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
;
28 path
=> '/var/lib/vz',
34 path
=> '/mnt/pve/cifs-store',
36 server
=> '127.0.0.42',
44 monhost
=> '127.0.0.42,127.0.0.21,::1',
55 bwlimit
=> 'restore=1024',
65 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
82 my $current_test; # = {
83 # description => 'Test description', # if available
84 # qemu_version => '2.12',
85 # host_arch => 'HOST_ARCH',
86 # config => { config hash },
87 # expected => [ expected outcome cmd line array ],
90 # use the config description to allow changing environment, fields are:
91 # TEST: A single line describing the test, gets outputted
92 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
93 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
94 # all fields are optional
98 $current_test = {}; # reset
100 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
101 my $config_raw = file_get_contents
($config_fn);
102 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
104 $current_test->{config
} = $config;
106 my $description = $config->{description
} // '';
108 while ($description =~ /^\h*(.*?)\h*$/gm) {
110 next if !$line || $line =~ /^#/;
114 if ($line =~ /^TEST:\s*(.*)\s*$/) {
115 $current_test->{description
} = "$1";
116 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
117 $current_test->{qemu_version
} = "$1";
118 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
119 $current_test->{host_arch
} = "$1";
120 } elsif ($line =~ /^EXPECT_ERROR:\s*(.*)\s*$/) {
121 $current_test->{expect_error
} = "$1";
126 sub get_test_qemu_version
{
127 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
130 my $qemu_server_module;
131 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
132 $qemu_server_module->mock(
133 kvm_user_version
=> sub {
134 return get_test_qemu_version
();
137 return get_test_qemu_version
();
139 kernel_has_vhost_net
=> sub {
140 return 1; # TODO: make this per-test configurable?
142 get_host_arch
=> sub() {
143 return $current_test->{host_arch
} // 'x86_64';
145 get_initiator_name
=> sub {
146 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
150 my $qemu_server_config;
151 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
152 $qemu_server_config->mock(
154 my ($class, $vmid, $node) = @_;
156 return $current_test->{config
};
160 my $pve_common_tools;
161 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
162 $pve_common_tools->mock(
163 next_vnc_port
=> sub {
164 my ($family, $address) = @_;
168 next_spice_port
=> sub {
169 my ($family, $address) = @_;
173 getaddrinfo_all
=> sub {
174 my ($hostname, @opts) = @_;
175 die "need stable hostname" if $hostname ne 'localhost';
178 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
179 family
=> AF_INET
, # IPv4
187 my $pve_common_inotify;
188 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
189 $pve_common_inotify->mock(
195 my $pve_common_sysfstools;
196 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
197 $pve_common_sysfstools->mock(
199 my ($filter, $verbose) = @_;
205 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
206 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
212 my $qemu_monitor_module;
213 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
214 $qemu_monitor_module->mock(
216 my ($vmid, $cmd) = @_;
218 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
219 if $vmid != $base_env->{vmid
};
221 if ($cmd eq 'query-version') {
222 my $ver = get_test_qemu_version
();
223 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
233 die "unexpected QMP command: '$cmd'";
236 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
242 my ($ra, $wa) = POSIX
::pipe();
243 my ($rb, $wb) = POSIX
::pipe();
244 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
245 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
247 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
248 or die "failed to run program 'diff': $!";
252 open my $f1, '<', \
$a;
253 open my $f2, '<', \
$b;
256 $ha->print($line1) if defined($line1 = <$f1>);
257 $hb->print($line2) if defined($line2 = <$f2>);
258 } while (defined($line1 // $line2));
265 my $diff = <$diffproc>;
267 die "files differ:\n$diff";
271 my ($config_fn) = @_;
273 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
275 parse_test
$config_fn;
277 $config_fn =~ /([^\/]+)$/;
279 if (my $desc = $current_test->{description
}) {
280 $testname = "'$testname' - $desc";
283 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
285 my $cmdline = eval { PVE
::QemuServer
::vm_commandline
($storecfg, $vmid) };
288 if (my $err_expect = $current_test->{expect_error
}) {
291 note
("did NOT get any error, but expected error: $err_expect");
295 if ($err ne $err_expect) {
297 note
("error does not match expected error: '$err' !~ '$err_expect'");
304 note
("got unexpected error: $err");
308 # check if QEMU version set correctly and test version_cmp
309 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
310 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
311 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
313 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
316 my $cmd_fn = "$config_fn.cmd";
319 my $cmdline_expected = file_get_contents
($cmd_fn);
321 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
322 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
324 # uncomment for easier debugging
325 #file_set_contents("$cmd_fn.tmp", $cmdline);
327 my $exp = join("\n", @$cmd_expected);
328 my $got = join("\n", @$cmd);
329 eval { diff
($exp, $got) };
337 file_set_contents
($cmd_fn, $cmdline);
341 print "testing config to command stabillity\n";
344 if (my $file = shift) {
347 foreach my $file (<cfg2cmd
/*.conf
>) {