]>
git.proxmox.com Git - qemu-server.git/blob - test/run_config2command_tests.pl
11 use PVE
::Tools
qw(file_get_contents file_set_contents run_command);
22 path
=> '/var/lib/vz',
28 path
=> '/mnt/pve/cifs-store',
30 server
=> '127.0.0.42',
38 monhost
=> '127.0.0.42,127.0.0.21,::1',
49 bwlimit
=> 'restore=1024',
59 real_qemu_version
=> PVE
::QemuServer
::kvm_user_version
(), # not yet mocked
76 my $current_test; # = {
77 # description => 'Test description', # if available
78 # qemu_version => '2.12',
79 # host_arch => 'HOST_ARCH',
80 # config => { config hash },
81 # expected => [ expected outcome cmd line array ],
84 # use the config description to allow changing environment, fields are:
85 # TEST: A single line describing the test, gets outputted
86 # QEMU_VERSION: \d+\.\d+(\.\d+)? (defaults to current version)
87 # HOST_ARCH: x86_64 | aarch64 (default to x86_64, to make tests stable)
88 # all fields are optional
92 $current_test = {}; # reset
94 my $fake_config_fn ="$config_fn/qemu-server/8006.conf";
95 my $config_raw = file_get_contents
($config_fn);
96 my $config = PVE
::QemuServer
::parse_vm_config
($fake_config_fn, $config_raw);
98 $current_test->{config
} = $config;
100 my $description = $config->{description
} // '';
102 while ($description =~ /^\h*(.*?)\h*$/gm) {
104 next if !$line || $line =~ /^#/;
108 if ($line =~ /^TEST:\s*(.*)\s*$/) {
109 $current_test->{description
} = "$1";
110 } elsif ($line =~ /^QEMU_VERSION:\s*(.*)\s*$/) {
111 $current_test->{qemu_version
} = "$1";
112 } elsif ($line =~ /^HOST_ARCH:\s*(.*)\s*$/) {
113 $current_test->{host_arch
} = "$1";
118 my $qemu_server_module;
119 $qemu_server_module = Test
::MockModule-
>new('PVE::QemuServer');
120 $qemu_server_module->mock(
121 kvm_user_version
=> sub {
122 return $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
125 return $current_test->{qemu_version
} // $base_env->{real_qemu_version
} // '2.12';
127 kernel_has_vhost_net
=> sub {
128 return 1; # TODO: make this per-test configurable?
130 get_host_arch
=> sub() {
131 return $current_test->{host_arch
} // 'x86_64';
133 get_initiator_name
=> sub {
134 return 'iqn.1993-08.org.debian:01:aabbccddeeff';
138 my $qemu_server_config;
139 $qemu_server_config = Test
::MockModule-
>new('PVE::QemuConfig');
140 $qemu_server_config->mock(
142 my ($class, $vmid, $node) = @_;
144 return $current_test->{config
};
148 my $pve_common_tools;
149 $pve_common_tools = Test
::MockModule-
>new('PVE::Tools');
150 $pve_common_tools->mock(
151 next_vnc_port
=> sub {
152 my ($family, $address) = @_;
156 next_spice_port
=> sub {
157 my ($family, $address) = @_;
163 my $pve_common_sysfstools;
164 $pve_common_sysfstools = Test
::MockModule-
>new('PVE::SysFSTools');
165 $pve_common_sysfstools->mock(
167 my ($filter, $verbose) = @_;
173 || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
174 || (ref($filter) eq 'CODE' && $filter->({ id
=> $_ }))
184 my ($ra, $wa) = POSIX
::pipe();
185 my ($rb, $wb) = POSIX
::pipe();
186 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
187 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
189 open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb"
190 or die "failed to run program 'diff': $!";
194 open my $f1, '<', \
$a;
195 open my $f2, '<', \
$b;
198 $ha->print($line1) if defined($line1 = <$f1>);
199 $hb->print($line2) if defined($line2 = <$f2>);
200 } while (defined($line1 // $line2));
207 my $diff = <$diffproc>;
209 die "files differ:\n$diff";
213 my ($config_fn) = @_;
215 die "no such input test config: $config_fn\n" if ! -f
$config_fn;
217 parse_test
$config_fn;
219 $config_fn =~ /([^\/]+)$/;
221 if (my $desc = $current_test->{description
}) {
222 $testname = "'$testname' - $desc";
225 my ($vmid, $storecfg) = $base_env->@{qw(vmid storage_config)};
227 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $vmid);
229 $cmdline =~ s/ -/ \\\n -/g; # same as qm showcmd --pretty
232 my $cmd_fn = "$config_fn.cmd";
235 my $cmdline_expected = file_get_contents
($cmd_fn);
237 my $cmd_expected = [ split /\s*\\?\n\s*/, $cmdline_expected ];
238 my $cmd = [ split /\s*\\?\n\s*/, $cmdline ];
240 # comment out for easier debugging
241 #file_set_contents("$cmd_fn.tmp", $cmdline);
243 my $exp = join("\n", @$cmd_expected);
244 my $got = join("\n", @$cmd);
245 eval { diff
($exp, $got) };
253 file_set_contents
($cmd_fn, $cmdline);
257 print "testing config to command stabillity\n";
260 if (my $file = shift) {
263 foreach my $file (<cfg2cmd
/*.conf
>) {