public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
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	[thread overview]
Message-ID: <20261023130324.451252-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261023130324.451252-1-d.csapak@proxmox.com>

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 <d.csapak@proxmox.com>
---
 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





  parent reply	other threads:[~2026-10-23 13:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23 13:01 [PATCH qemu-server v4 0/9] add hotplug tests and fix uncovered bugs Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 1/9] tests: factor out useful commandline mocks for reuse Dominik Csapak
2026-10-23 13:01 ` Dominik Csapak [this message]
2026-10-23 13:01 ` [PATCH qemu-server v4 3/9] machine: factor out machine expanding Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 4/9] tests: hotplug: add initial hotplug test harness Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 5/9] tests: hotplug: add some test cases Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 6/9] tests: hotplug: add test case for adding scsi14 on qemu 11.1 Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 7/9] tests: hotplug: add cases for known defects Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 8/9] hotplug: fix hot unplugging tablet on aarch64 Dominik Csapak
2026-10-23 13:01 ` [PATCH qemu-server v4 9/9] hotplug: remove iothread if adding drive device failed Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261023130324.451252-3-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal