qemu-server/test/run_parse_config_tests.pl
Fiona Ebner df6d255b0f parse config: be precise about section type checks
There are checks for custom parsing behavior inside certain sections
relying only on the section name. While the name 'pending' cannot be
used by snapshots, the name 'cloudinit' can. Introduce an associated
section type to make the checks precise.

The test was not added in a separate commit, because it would fail
when writing the config before the fix, and failure in writing is
never expected by the test script. So there is no easy way to
highlight just the difference in behavior together with the fix and
the git history should stay bisectable.

Compare with the verify-snapshot.conf testcase without the actual fix
applied to see the difference.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Link: https://lore.proxmox.com/20250127112923.31703-5-f.ebner@proxmox.com
2025-04-07 14:13:01 +02:00

93 lines
2.4 KiB
Perl
Executable File

#!/usr/bin/perl
# Tests parsing and writing VM configuration files.
# The parsing part is already covered by the config2command test too, but that only focuses on the
# main section, not other section types and does not also test parsing in strict mode.
#
# If no expected file exists, the input is assumed to be equal to the expected output.
# If $file.strict.error (respectively $file.non-strict.error) exists, it is assumed to be the
# expected error when parsing the config in strict (respectively non-strict) mode.
use strict;
use warnings;
use lib qw(..);
use File::Path qw(make_path remove_tree);
use Test::MockModule;
use Test::More;
use PVE::QemuServer;
use PVE::Tools;
my $INPUT_DIR = './parse-config-input';
my $OUTPUT_DIR = './parse-config-output';
my $EXPECTED_DIR = './parse-config-expected';
# NOTE update when you add/remove tests
plan tests => 2 * 7;
sub run_tests {
my ($strict) = @_;
PVE::Tools::dir_glob_foreach('./parse-config-input', '.*\.conf', sub {
my ($file) = @_;
my $strict_mode = $strict ? 'strict' : 'non-strict';
my $expected_err_file = "${EXPECTED_DIR}/${file}.${strict_mode}.error";
my $expected_err;
$expected_err = PVE::Tools::file_get_contents($expected_err_file) if -f $expected_err_file;
my $fake_config_fn ="$file/qemu-server/8006.conf";
my $input_file = "${INPUT_DIR}/${file}";
my $input = PVE::Tools::file_get_contents($input_file);
my $conf = eval {
PVE::QemuServer::parse_vm_config($fake_config_fn, $input, $strict);
};
if (my $err = $@) {
if ($expected_err) {
is($err, $expected_err, $file);
} else {
note("got unexpected error '$err'");
fail($file);
}
return;
}
if ($expected_err) {
note("expected error for strict mode did not occur: '$expected_err'");
fail($file);
return;
}
my $output = eval { PVE::QemuServer::write_vm_config($fake_config_fn, $conf); };
if (my $err = $@) {
note("got unexpected error '$err'");
fail($file);
return;
}
my $output_file = "${OUTPUT_DIR}/${file}";
PVE::Tools::file_set_contents($output_file, $output);
my $expected_file = "${EXPECTED_DIR}/${file}";
$expected_file = $input_file if !-f $expected_file;
my $cmd = ['diff', '-u', $expected_file, $output_file];
if (system(@$cmd) == 0) {
pass($file);
} else {
fail($file);
}
});
}
make_path(${OUTPUT_DIR});
run_tests(0);
run_tests(1);
remove_tree(${OUTPUT_DIR}) or die "failed to remove output directory\n";
done_testing();