[pve-devel] [PATCH network 4/4] tests: use Test::Differences to make test assertions

Gabriel Goller g.goller at proxmox.com
Fri Sep 19 11:41:20 CEST 2025


Test::Differences provides a much better output. Instead of dumping the
whole file, eq_or_diff shows a pretty diff table with the changes
side-by-side. It also shows a small context and not the whole
expected/got file. This is very useful when especially when tests fails
on bigger config files.

Signed-off-by: Gabriel Goller <g.goller at proxmox.com>
---
 debian/control                      |  1 +
 src/test/run_test_dns.pl            | 15 +++++++-------
 src/test/run_test_ipams.pl          | 13 ++++++------
 src/test/run_test_subnets.pl        | 31 +++++++++++++++--------------
 src/test/run_test_vnets_blackbox.pl | 17 ++++++++--------
 src/test/run_test_zones.pl          |  5 +++--
 6 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/debian/control b/debian/control
index b45d12621aab..0501391ef282 100644
--- a/debian/control
+++ b/debian/control
@@ -8,6 +8,7 @@ Build-Depends: debhelper-compat (= 13),
                libnet-subnet-perl,
                libpve-rs-perl (>= 0.10.3),
                libtest-mockmodule-perl,
+               libtest-differences-perl,
                perl,
                pve-cluster (>= 9.0.1),
                pve-firewall (>= 5.1.0~),
diff --git a/src/test/run_test_dns.pl b/src/test/run_test_dns.pl
index 26fbfaf035d2..f099da04a777 100755
--- a/src/test/run_test_dns.pl
+++ b/src/test/run_test_dns.pl
@@ -9,6 +9,7 @@ use Net::IP;
 
 use Test::More;
 use Test::MockModule;
+use Test::Differences;
 
 use PVE::Network::SDN;
 use PVE::Network::SDN::Zones;
@@ -101,7 +102,7 @@ foreach my $path (@plugins) {
         $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -114,7 +115,7 @@ foreach my $path (@plugins) {
         $plugin->add_ptr_record($plugin_config, $zone, $hostname, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -127,7 +128,7 @@ foreach my $path (@plugins) {
         $plugin->del_ptr_record($plugin_config, $zone, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -167,7 +168,7 @@ foreach my $path (@plugins) {
         $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -212,7 +213,7 @@ foreach my $path (@plugins) {
         $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -250,7 +251,7 @@ foreach my $path (@plugins) {
         $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1);
 
         if ($@) {
-            is($@, $expected, $name);
+            eq_or_diff($@, $expected, $name);
         } else {
             fail($name);
         }
@@ -264,7 +265,7 @@ foreach my $path (@plugins) {
     $plugin->verify_zone($plugin_config, $zone, 1);
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
diff --git a/src/test/run_test_ipams.pl b/src/test/run_test_ipams.pl
index 193b34bb98d9..f9c44fe1a3b9 100755
--- a/src/test/run_test_ipams.pl
+++ b/src/test/run_test_ipams.pl
@@ -8,6 +8,7 @@ use File::Slurp;
 
 use Test::More;
 use Test::MockModule;
+use Test::Differences;
 
 use PVE::Network::SDN;
 use PVE::Network::SDN::Zones;
@@ -120,7 +121,7 @@ foreach my $path (@plugins) {
     );
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
@@ -133,7 +134,7 @@ foreach my $path (@plugins) {
     $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1);
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
@@ -146,7 +147,7 @@ foreach my $path (@plugins) {
     $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1);
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
@@ -168,7 +169,7 @@ foreach my $path (@plugins) {
     );
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
@@ -192,7 +193,7 @@ foreach my $path (@plugins) {
     );
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
@@ -211,7 +212,7 @@ foreach my $path (@plugins) {
     $plugin->add_subnet($plugin_config, $subnetid, $subnet, 1);
 
     if ($@) {
-        is($@, $expected, $name);
+        eq_or_diff($@, $expected, $name);
     } else {
         fail($name);
     }
diff --git a/src/test/run_test_subnets.pl b/src/test/run_test_subnets.pl
index d4f8d6616de3..16443bf7ee26 100755
--- a/src/test/run_test_subnets.pl
+++ b/src/test/run_test_subnets.pl
@@ -8,6 +8,7 @@ use File::Slurp;
 
 use Test::More;
 use Test::MockModule;
+use Test::Differences;
 
 use PVE::Network::SDN;
 use PVE::Network::SDN::Zones;
@@ -147,9 +148,9 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     } else {
-        is(undef, undef, $name);
+        eq_or_diff(undef, undef, $name);
     }
 
     ## add_ip
@@ -173,9 +174,9 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     } else {
-        is(undef, undef, $name);
+        eq_or_diff(undef, undef, $name);
     }
 
     if ($ipam) {
@@ -190,7 +191,7 @@ foreach my $path (@plugins) {
         };
 
         if ($@) {
-            is(undef, undef, $name);
+            eq_or_diff(undef, undef, $name);
         } else {
             fail("$name : $@");
         }
@@ -225,9 +226,9 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     } else {
-        is(undef, undef, $name);
+        eq_or_diff(undef, undef, $name);
     }
 
     ## add_next_free
@@ -266,7 +267,7 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     }
 
     ## del_ip
@@ -299,9 +300,9 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     } else {
-        is(undef, undef, $name);
+        eq_or_diff(undef, undef, $name);
     }
 
     if ($ipam) {
@@ -314,7 +315,7 @@ foreach my $path (@plugins) {
         eval { PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet); };
 
         if ($@) {
-            is($result, $expected, $name);
+            eq_or_diff($result, $expected, $name);
         } else {
             fail("$name : $@");
         }
@@ -367,9 +368,9 @@ foreach my $path (@plugins) {
     if ($@) {
         if ($ipam) {
             $result = $js->encode($plugin->read_db());
-            is($result, $expected, $name);
+            eq_or_diff($result, $expected, $name);
         } else {
-            is(undef, undef, $name);
+            eq_or_diff(undef, undef, $name);
         }
     } else {
         fail("$name : $@");
@@ -390,9 +391,9 @@ foreach my $path (@plugins) {
         fail("$name : $@");
     } elsif ($ipam) {
         $result = $js->encode($plugin->read_db());
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     } else {
-        is(undef, undef, $name);
+        eq_or_diff(undef, undef, $name);
     }
 
 }
diff --git a/src/test/run_test_vnets_blackbox.pl b/src/test/run_test_vnets_blackbox.pl
index 468b1ede17e2..a7025dc36e75 100755
--- a/src/test/run_test_vnets_blackbox.pl
+++ b/src/test/run_test_vnets_blackbox.pl
@@ -10,6 +10,7 @@ use NetAddr::IP qw(:lower);
 
 use Test::More;
 use Test::MockModule;
+use Test::Differences;
 
 use PVE::Tools qw(extract_param file_set_contents);
 
@@ -416,7 +417,7 @@ sub test_without_subnet {
 
     my @ips = get_ips_from_mac($mac);
     my $num_ips = scalar @ips;
-    is($num_ips, 0, "$test_name: No IP allocated in IPAM");
+    eq_or_diff($num_ips, 0, "$test_name: No IP allocated in IPAM");
 }
 run_test(\&test_without_subnet);
 
@@ -463,7 +464,7 @@ sub test_nic_join {
 
     my @ips = get_ips_from_mac($mac);
     my $num_ips = scalar @ips;
-    is($num_ips, $num_subnets, "$test_name: Expecting $num_subnets IPs, found $num_ips");
+    eq_or_diff($num_ips, $num_subnets, "$test_name: Expecting $num_subnets IPs, found $num_ips");
     ok(
         (all { ($_->{vnet} eq $vnetid && $_->{zone} eq $zoneid) } @ips),
         "$test_name: all IPs in correct vnet and zone",
@@ -622,7 +623,7 @@ sub test_nic_join_full_dhcp_range {
 
     my @ips = get_ips_from_mac($mac);
     my $num_ips = scalar @ips;
-    is($num_ips, 0, "$test_name: No IP allocated in IPAM");
+    eq_or_diff($num_ips, 0, "$test_name: No IP allocated in IPAM");
 }
 
 run_test(
@@ -770,9 +771,9 @@ sub test_nic_start {
         });
     }
     my @current_ips = get_ips_from_mac($mac);
-    is(get_ip4(@current_ips), $current_ip4, "$test_name: setup current IPv4: $current_ip4")
+    eq_or_diff(get_ip4(@current_ips), $current_ip4, "$test_name: setup current IPv4: $current_ip4")
         if defined $current_ip4;
-    is(get_ip6(@current_ips), $current_ip6, "$test_name: setup current IPv6: $current_ip6")
+    eq_or_diff(get_ip6(@current_ips), $current_ip6, "$test_name: setup current IPv6: $current_ip6")
         if defined $current_ip6;
 
     eval { nic_start($vnetid, $mac, $hostname, $vmid); };
@@ -784,14 +785,14 @@ sub test_nic_start {
 
     my @ips = get_ips_from_mac($mac);
     my $num_ips = scalar @ips;
-    is($num_ips, $num_expected_ips, "$test_name: Expecting $num_expected_ips IPs, found $num_ips");
+    eq_or_diff($num_ips, $num_expected_ips, "$test_name: Expecting $num_expected_ips IPs, found $num_ips");
     ok(
         (all { ($_->{vnet} eq $vnetid && $_->{zone} eq $zoneid) } @ips),
         "$test_name: all IPs in correct vnet and zone",
     );
 
-    is(get_ip4(@ips), $current_ip4, "$test_name: still current IPv4: $current_ip4") if $current_ip4;
-    is(get_ip6(@ips), $current_ip6, "$test_name: still current IPv6: $current_ip6") if $current_ip6;
+    eq_or_diff(get_ip4(@ips), $current_ip4, "$test_name: still current IPv4: $current_ip4") if $current_ip4;
+    eq_or_diff(get_ip6(@ips), $current_ip6, "$test_name: still current IPv6: $current_ip6") if $current_ip6;
 }
 
 run_test(
diff --git a/src/test/run_test_zones.pl b/src/test/run_test_zones.pl
index 917f40a90069..905b2f42e1dc 100755
--- a/src/test/run_test_zones.pl
+++ b/src/test/run_test_zones.pl
@@ -8,6 +8,7 @@ use File::Slurp;
 
 use Test::More;
 use Test::MockModule;
+use Test::Differences;
 
 use PVE::Network::SDN;
 use PVE::Network::SDN::Zones;
@@ -140,7 +141,7 @@ foreach my $test (@tests) {
         diag("got unexpected error - $err");
         fail($name);
     } else {
-        is($result, $expected, $name);
+        eq_or_diff($result, $expected, $name);
     }
 
     if ($sdn_config->{controllers}) {
@@ -155,7 +156,7 @@ foreach my $test (@tests) {
             diag("got unexpected error - $err");
             fail($name);
         } else {
-            is($config, $expected, $name);
+            eq_or_diff($config, $expected, $name);
         }
     }
 }
-- 
2.47.3





More information about the pve-devel mailing list