From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id B55971FF0AF for ; Fri, 23 Oct 2026 15:03:37 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A80812168D; Fri, 23 Oct 2026 15:03:29 +0200 (CEST) From: Dominik Csapak To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v4 2/9] tests: factor out the diff generation Date: Fri, 23 Oct 2026 15:01:05 +0200 Message-ID: <20261023130324.451252-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261023130324.451252-1-d.csapak@proxmox.com> References: <20261023130324.451252-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.350 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: R27PC233ZKQXI4QW75IMO5A5WJPFJEFX X-Message-ID-Hash: R27PC233ZKQXI4QW75IMO5A5WJPFJEFX X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: instead of having two separate implementations, factor that out and avoid writing both parts into a file. This avoids creating an output dir for the parse config tests. Signed-off-by: Dominik Csapak --- src/test/Makefile | 4 +-- src/test/QemuServerTest/Utils.pm | 31 ++++++++++++++++++ src/test/run_config2command_tests.pl | 47 +++++----------------------- src/test/run_parse_config_tests.pl | 25 +++++++-------- 4 files changed, 51 insertions(+), 56 deletions(-) create mode 100644 src/test/QemuServerTest/Utils.pm diff --git a/src/test/Makefile b/src/test/Makefile index 8906be58..33e1c891 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -31,7 +31,7 @@ $(MIGRATION_TEST_TARGETS): test_restore_config: run_qemu_restore_config_tests.pl ./run_qemu_restore_config_tests.pl -test_parse_config: run_parse_config_tests.pl +test_parse_config: run_parse_config_tests.pl QemuServerTest/Utils.pm ./run_parse_config_tests.pl test_memory: run_memory_tests.pl @@ -39,4 +39,4 @@ test_memory: run_memory_tests.pl .PHONY: clean clean: - rm -rf MigrationTest/run parse-config-output + rm -rf MigrationTest/run diff --git a/src/test/QemuServerTest/Utils.pm b/src/test/QemuServerTest/Utils.pm new file mode 100644 index 00000000..6c88d95c --- /dev/null +++ b/src/test/QemuServerTest/Utils.pm @@ -0,0 +1,31 @@ +package QemuServerTest::Utils; + +use v5.36; + +use File::Temp; + +use PVE::Cmd; + +use base 'Exporter'; + +our @EXPORT_OK = qw( + diff +); + +sub diff($expected, $got) { + return if $expected eq $got; + + my $tmp = File::Temp->new(); + print $tmp $expected; + $tmp->flush(); + + my $diff = ''; + PVE::Cmd::run( + ['diff', '-up', '--label', 'expected', $tmp->filename(), '--label', 'got', '-'], + input => $got, + outfunc => sub { $diff .= "$_[0]\n"; }, + noerr => 1, + ); + + return $diff || "inputs differ, but `diff` produced no output."; +} diff --git a/src/test/run_config2command_tests.pl b/src/test/run_config2command_tests.pl index 2c553260..a3b2ad06 100755 --- a/src/test/run_config2command_tests.pl +++ b/src/test/run_config2command_tests.pl @@ -16,6 +16,7 @@ use PVE::QemuServer::CPUConfig; use PVE::Storage; use QemuServerTest::CommandLineMocks qw(get_storage_config get_test_qemu_version set_test_env); +use QemuServerTest::Utils qw(diff); my $vmid = 8006; @@ -120,37 +121,6 @@ $qemu_monitor_module->mock( }, ); -sub diff($a, $b) { - return if $a eq $b; - - my ($ra, $wa) = POSIX::pipe(); - my ($rb, $wb) = POSIX::pipe(); - my $ha = IO::Handle->new_from_fd($wa, 'w'); - my $hb = IO::Handle->new_from_fd($wb, 'w'); - - open my $diffproc, '-|', 'diff', '-up', "/proc/self/fd/$ra", "/proc/self/fd/$rb" ## no critic - or die "failed to run program 'diff': $!"; - POSIX::close($ra); - POSIX::close($rb); - - open my $f1, '<', \$a; - open my $f2, '<', \$b; - my ($line1, $line2); - do { - $ha->print($line1) if defined($line1 = <$f1>); - $hb->print($line2) if defined($line2 = <$f2>); - } while (defined($line1 // $line2)); - close $f1; - close $f2; - close $ha; - close $hb; - - local $/ = undef; - my $diff = <$diffproc>; - close $diffproc; - die "files differ:\n$diff"; -} - $SIG{__WARN__} = sub { my $warning = shift; chomp $warning; @@ -211,26 +181,23 @@ sub do_test($config_fn) { my $cmd_fn = "$config_fn.cmd"; - if (-f $cmd_fn) { + if (!-f $cmd_fn) { + file_set_contents($cmd_fn, $cmdline); + } else { my $cmdline_expected = file_get_contents($cmd_fn); my $cmd_expected = [split /\s*\\?\n\s*/, $cmdline_expected]; my $cmd = [split /\s*\\?\n\s*/, $cmdline]; - # uncomment for easier debugging - #file_set_contents("$cmd_fn.tmp", $cmdline); - my $exp = join("\n", @$cmd_expected); my $got = join("\n", @$cmd); - eval { diff($exp, $got) }; - if (my $err = $@) { + + if (my $diff = diff($exp, $got)) { fail("$testname"); - note($err); + note("files differ:\n$diff"); } else { pass("$testname"); } - } else { - file_set_contents($cmd_fn, $cmdline); } } diff --git a/src/test/run_parse_config_tests.pl b/src/test/run_parse_config_tests.pl index 62e36ee0..f68abc06 100755 --- a/src/test/run_parse_config_tests.pl +++ b/src/test/run_parse_config_tests.pl @@ -11,9 +11,7 @@ use strict; use warnings; -use lib qw(..); - -use File::Path qw(make_path remove_tree); +use lib qw(.. .); use Test::MockModule; use Test::More; @@ -21,8 +19,9 @@ use Test::More; use PVE::QemuServer; use PVE::Tools; +use QemuServerTest::Utils qw(diff); + 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 @@ -72,25 +71,23 @@ sub run_tests { 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 $expected = $input; + if (-f $expected_file) { + $expected = PVE::Tools::file_get_contents($expected_file); + } - my $cmd = ['diff', '-u', $expected_file, $output_file]; - if (system(@$cmd) == 0) { - pass($file); - } else { + if (my $diff = diff($expected, $output)) { + note($diff); fail($file); + } else { + pass($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(); -- 2.47.3