[pve-devel] [PATCH pve-network 3/4] tests: mocking more functions to avoid access to system files

Stefan Lendl s.lendl at proxmox.com
Tue Dec 5 14:12:09 CET 2023


previously extracted functions are now mocked in the zone tests

mocked PVE::Network::SDN::Controllers::read_etc_network_interfaces and also
INotify::read_etc_network_interfaces to return the same $interfaces_config

created a mock for several PVE::Network::SDN::Controllers::<type>Plugin methods
that would require system access. Just to be sure.

Added a note that mocking INotify::read_file is a HACK and not garanteed to work
if anything other than /etc/network/interfaces is read.

Signed-off-by: Stefan Lendl <s.lendl at proxmox.com>
---
 src/test/run_test_zones.pl | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/test/run_test_zones.pl b/src/test/run_test_zones.pl
index ce8edc2..2d9be88 100755
--- a/src/test/run_test_zones.pl
+++ b/src/test/run_test_zones.pl
@@ -14,6 +14,10 @@ use PVE::Network::SDN::Zones;
 use PVE::Network::SDN::Controllers;
 use PVE::INotify;
 
+use Data::Dumper qw(Dumper);
+$Data::Dumper::Sortkeys = 1;
+$Data::Dumper::Indent = 1;
+
 sub read_sdn_config {
     my ($file) = @_;
 
@@ -29,7 +33,6 @@ sub read_sdn_config {
     return $sdn_config;
 }
 
-
 my @tests = grep { -d } glob './zones/*/*';
 
 foreach my $test (@tests) {
@@ -47,8 +50,20 @@ foreach my $test (@tests) {
 	    return 'localhost';
 	},
 	read_file => sub {
+	    # HACK this assumes we are always calling PVE::INotify::read_file('interfaces');
 	    return $interfaces_config;
 	},
+	read_etc_network_interfaces => sub {
+	    return $interfaces_config;
+	},
+    );
+
+    my $mocked_pve_sdn_controllers;
+    $mocked_pve_sdn_controllers = Test::MockModule->new('PVE::Network::SDN::Controllers');
+    $mocked_pve_sdn_controllers->mock(
+	read_etc_network_interfaces => sub {
+	    return $interfaces_config;
+	}
     );
 
     my $pve_sdn_subnets;
@@ -88,6 +103,25 @@ foreach my $test (@tests) {
 	},
     );
 
+    my ($first_plugin) = %{$sdn_config->{controllers}->{ids}} if defined $sdn_config->{controllers};
+    if ($first_plugin) {
+	my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup(
+	    $sdn_config->{controllers}->{ids}->{$first_plugin}->{type}
+	);
+	my $mocked_controller_plugin = Test::MockModule->new($controller_plugin);
+	$mocked_controller_plugin->mock(
+	    write_controller_config => sub {
+		return;
+	    },
+	    reload_controller => sub {
+		return;
+	    },
+	    read_local_frr_config => sub {
+		return;
+	    },
+	);
+    }
+
     my $name = $test;
     my $expected = read_file("./$test/expected_sdn_interfaces");
 
-- 
2.42.0





More information about the pve-devel mailing list