11 use PVE
::Tools
qw(file_get_contents file_set_contents run_command);
14 use PVE
::QemuServer
::Monitor
;
15 use PVE
::QemuServer
::Machine
;
24 path
=> '/var/lib/vz',
30 path
=> '/mnt/pve/cifs-store',
32 server
=> '127.0.0.42',
40 monhost
=> '127.0.0.42,127.0.0.21,::1',
51 bwlimit
=> 'restore=1024',
61 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
78 my $current_test; # = {
79 # description => 'Test description', # if available
80 # qemu_version => '2.12',
81 # host_arch => 'HOST_ARCH',
82 # config => { config hash },
83 # expected => [ expected outcome cmd line array ],
86 # use the config description to allow changing environment, fields are:
87 # TEST: A single line describing the test, gets outputted
88 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
89 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
90 # all fields are optional
94 $current_test = {}; # reset
96 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
97 my $config_raw = file_get_contents
($config_fn);
98 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
100 $current_test->{config
} = $config;
102 my $description = $config->{description
} // '';
104 while ($description =~ /^\h*(.*?)\h*$/gm) {
106 next if !$line || $line =~ /^#/;
110 if ($line =~ /^TEST:\s*(.*)\s*$/) {
111 $current_test->{description
} = "$1";
112 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
113 $current_test->{qemu_version
} = "$1";
114 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
115 $current_test->{host_arch
} = "$1";
120 sub get_test_qemu_version
{
121 $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
124 my $qemu_server_module;
125 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
126 $qemu_server_module->mock(
127 kvm_user_version
=> sub {
128 return get_test_qemu_version
();
131 return get_test_qemu_version
();
133 kernel_has_vhost_net
=> sub {
134 return 1; # TODO: make this per-test configurable?
136 get_host_arch
=> sub() {
137 return $current_test->{host_arch
} // 'x86_64';
139 get_initiator_name
=> sub {
140 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
144 my $qemu_server_config;
145 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
146 $qemu_server_config->mock(
148 my ($class, $vmid, $node) = @_;
150 return $current_test->{config
};
154 my $pve_common_tools;
155 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
156 $pve_common_tools->mock(
157 next_vnc_port
=> sub {
158 my ($family, $address) = @_;
162 next_spice_port
=> sub {
163 my ($family, $address) = @_;
169 my $pve_common_sysfstools;
170 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
171 $pve_common_sysfstools->mock(
173 my ($filter, $verbose) = @_;
179 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
180 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
186 my $qemu_monitor_module;
187 $qemu_monitor_module = Test
::MockModule-
>new('PVE::QemuServer::Monitor');
188 $qemu_monitor_module->mock(
190 my ($vmid, $cmd) = @_;
192 die "invalid vmid: $vmid (expected: $base_env->{vmid})"
193 if $vmid != $base_env->{vmid
};
195 if ($cmd eq 'query-version') {
196 my $ver = get_test_qemu_version
();
197 $ver =~ m/(\d+)\.(\d+)(?:\.(\d+))?/;
207 die "unexpected QMP command: '$cmd'";
210 $qemu_monitor_module->mock('qmp_cmd', \
&qmp_cmd
);
216 my ($ra, $wa) = POSIX
::pipe();
217 my ($rb, $wb) = POSIX
::pipe();
218 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
219 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
221 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
222 or die "failed to run program 'diff': $!";
226 open my $f1, '<', \
$a;
227 open my $f2, '<', \
$b;
230 $ha->print($line1) if defined($line1 = <$f1>);
231 $hb->print($line2) if defined($line2 = <$f2>);
232 } while (defined($line1 // $line2));
239 my $diff = <$diffproc>;
241 die "files differ:\n$diff";
245 my ($config_fn) = @_;
247 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
249 parse_test
$config_fn;
251 $config_fn =~ /([^\/]+)$/;
253 if (my $desc = $current_test->{description
}) {
254 $testname = "'$testname' - $desc";
257 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
259 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $vmid);
261 # check if QEMU version set correctly and test version_cmp
262 (my $qemu_major = get_test_qemu_version
()) =~ s/\..*$//;
263 die "runs_at_least_qemu_version returned false, maybe error in version_cmp?"
264 if !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, $qemu_major);
266 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
269 my $cmd_fn = "$config_fn.cmd";
272 my $cmdline_expected = file_get_contents
($cmd_fn);
274 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
275 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
277 # uncomment for easier debugging
278 #file_set_contents("$cmd_fn.tmp", $cmdline);
280 my $exp = join("\n", @$cmd_expected);
281 my $got = join("\n", @$cmd);
282 eval { diff
($exp, $got) };
290 file_set_contents
($cmd_fn, $cmdline);
294 print "testing config to command stabillity\n";
297 if (my $file = shift) {
300 foreach my $file (<cfg2cmd
/*.conf
>) {