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";
124 sub get_test_qemu_version
{
125 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
128 my $qemu_server_module;
129 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
130 $qemu_server_module->mock(
131 kvm_user_version
=> sub {
132 return get_test_qemu_version
();
135 return get_test_qemu_version
();
137 kernel_has_vhost_net
=> sub {
138 return 1; # TODO: make this per-test configurable?
140 get_host_arch
=> sub() {
141 return $current_test->{host_arch
} // 'x86_64';
143 get_initiator_name
=> sub {
144 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
148 my $qemu_server_config;
149 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
150 $qemu_server_config->mock(
152 my ($class, $vmid, $node) = @_;
154 return $current_test->{config
};
158 my $pve_common_tools;
159 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
160 $pve_common_tools->mock(
161 next_vnc_port
=> sub {
162 my ($family, $address) = @_;
166 next_spice_port
=> sub {
167 my ($family, $address) = @_;
171 getaddrinfo_all
=> sub {
172 my ($hostname, @opts) = @_;
173 die "need stable hostname" if $hostname ne 'localhost';
176 addr
=> Socket
::pack_sockaddr_in
(0, Socket
::INADDR_LOOPBACK
),
177 family
=> AF_INET
, # IPv4
185 my $pve_common_inotify;
186 $pve_common_inotify = Test
::MockModule-
>new('PVE::INotify');
187 $pve_common_inotify->mock(
193 my $pve_common_sysfstools;
194 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
195 $pve_common_sysfstools->mock(
197 my ($filter, $verbose) = @_;
203 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
204 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
210 my $qemu_monitor_module;
211 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
212 $qemu_monitor_module->mock(
214 my ($vmid, $cmd) = @_;
216 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
217 if $vmid != $base_env->{vmid
};
219 if ($cmd eq 'query-version') {
220 my $ver = get_test_qemu_version
();
221 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
231 die "unexpected QMP command: '$cmd'";
234 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
240 my ($ra, $wa) = POSIX
::pipe();
241 my ($rb, $wb) = POSIX
::pipe();
242 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
243 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
245 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
246 or die "failed to run program 'diff': $!";
250 open my $f1, '<', \
$a;
251 open my $f2, '<', \
$b;
254 $ha->print($line1) if defined($line1 = <$f1>);
255 $hb->print($line2) if defined($line2 = <$f2>);
256 } while (defined($line1 // $line2));
263 my $diff = <$diffproc>;
265 die "files differ:\n$diff";
269 my ($config_fn) = @_;
271 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
273 parse_test
$config_fn;
275 $config_fn =~ /([^\/]+)$/;
277 if (my $desc = $current_test->{description
}) {
278 $testname = "'$testname' - $desc";
281 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
283 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $vmid);
285 # check if QEMU version set correctly and test version_cmp
286 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
287 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
288 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
290 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
293 my $cmd_fn = "$config_fn.cmd";
296 my $cmdline_expected = file_get_contents
($cmd_fn);
298 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
299 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
301 # uncomment for easier debugging
302 #file_set_contents("$cmd_fn.tmp", $cmdline);
304 my $exp = join("\n", @$cmd_expected);
305 my $got = join("\n", @$cmd);
306 eval { diff
($exp, $got) };
314 file_set_contents
($cmd_fn, $cmdline);
318 print "testing config to command stabillity\n";
321 if (my $file = shift) {
324 foreach my $file (<cfg2cmd
/*.conf
>) {