]> git.proxmox.com Git - pve-network.git/commitdiff
subnets: tests: add noipam test
authorAlexandre Derumier <aderumier@odiso.com>
Sat, 8 May 2021 05:51:35 +0000 (07:51 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 10 May 2021 07:37:16 +0000 (09:37 +0200)
test/run_test_subnets.pl
test/subnets/noipam/ipam_config [new file with mode: 0644]
test/subnets/noipam/sdn_config [new file with mode: 0644]

index 6ffa6a32c23d0d805e0934ddeece5633fe8d70de..364baa6c6e719122cfce4575c7747518e6004f40 100755 (executable)
@@ -39,6 +39,7 @@ foreach my $path (@plugins) {
 
     my (undef, $testid) = split(/\//, $path);
 
+    print "test: $testid\n";
     my $sdn_config = read_sdn_config ("$path/sdn_config");
 
 
@@ -83,17 +84,31 @@ foreach my $path (@plugins) {
     my $ipamdb = {};
 
     my $zone = $sdn_config->{zones}->{ids}->{"myzone"};
+    my $ipam = $zone->{ipam};
+
+    my $plugin;
+    my $sdn_ipam_plugin;
+    if($ipam) {
+       $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam);
+       $sdn_ipam_plugin = Test::MockModule->new($plugin);
+       $sdn_ipam_plugin->mock(
+           read_db => sub {
+               return $ipamdb;
+           },
+           write_db => sub {
+               my ($cfg) = @_;
+               $ipamdb = $cfg;
+           }
+       );
+    }
 
-    my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup('pve');
-    my $sdn_ipam_plugin = Test::MockModule->new($plugin);
-    $sdn_ipam_plugin->mock(
-       read_db => sub {
-           return $ipamdb;
+    my $pve_sdn_ipams;
+    $pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams');
+    $pve_sdn_ipams->mock(
+       config => sub {
+           my $ipam_config = read_sdn_config ("$path/ipam_config");
+           return $ipam_config;
        },
-       write_db => sub {
-           my ($cfg) = @_;
-           $ipamdb = $cfg;
-       }
     );
 
     ## add_subnet
@@ -103,14 +118,17 @@ foreach my $path (@plugins) {
     my $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{}}}}}}';
 
     eval {
-       $plugin->add_subnet(undef, $subnetid, $subnet, 1);
+        PVE::Network::SDN::Subnets::add_subnet($zone, $subnetid, $subnet);
+
     };
 
     if ($@) {
-        fail($name);
-    } else {
+        fail("$name : $@");
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
+    } else {
+        is (undef, undef, $name);
     }
 
     ## add_ip
@@ -125,23 +143,27 @@ foreach my $path (@plugins) {
 
     if ($@) {
         fail("$name : $@");
-    } else {
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
+    } else {
+        is (undef, undef, $name);
     }
 
-    ## add_already_exist_ip
-    $test = "add_already_exist_ip";
-    $name = "$testid $test";
+    if($ipam) {
+       ## add_already_exist_ip
+       $test = "add_already_exist_ip";
+       $name = "$testid $test";
 
-    eval {
-       PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description);
-    };
+       eval {
+           PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description);
+       };
 
-    if ($@) {
-        is (undef, undef, $name);
-    } else {
-        fail("$name : $@");
+       if ($@) {
+           is (undef, undef, $name);
+       } else {
+           fail("$name : $@");
+       }
     }
 
     ## add_second_ip
@@ -156,12 +178,13 @@ foreach my $path (@plugins) {
 
     if ($@) {
         fail("$name : $@");
-    } else {
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
+    } else {
+        is (undef, undef, $name);
     }
 
-
     ## add_next_free
     $test = "add_next_freeip";
     $name = "$testid $test";
@@ -174,7 +197,7 @@ foreach my $path (@plugins) {
 
     if ($@) {
         fail("$name : $@");
-    } else {
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
     }
@@ -191,27 +214,30 @@ foreach my $path (@plugins) {
 
     if ($@) {
         fail("$name : $@");
-    } else {
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
-    }
-
-    ## del_subnet_not_empty
-    $test = "del_subnet_not_empty";
-    $name = "$testid $test";
-    $result = undef;
-    $expected = undef;
-
-    eval {
-       PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet);
-    };
-
-    if ($@) {
-        is ($result, $expected, $name);
     } else {
-        fail("$name : $@");
+        is (undef, undef, $name);
     }
 
+    if($ipam){
+       ## del_subnet_not_empty
+       $test = "del_subnet_not_empty";
+       $name = "$testid $test";
+       $result = undef;
+       $expected = undef;
+
+       eval {
+           PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet);
+       };
+
+       if ($@) {
+           is ($result, $expected, $name);
+       } else {
+           fail("$name : $@");
+       }
+    }
 
 
     ## add_ip_rollback_failing_dns
@@ -239,8 +265,12 @@ foreach my $path (@plugins) {
     };
 
     if ($@) {
-        $result = $js->encode($plugin->read_db());
-        is ($result, $expected, $name);
+       if($ipam) {
+           $result = $js->encode($plugin->read_db());
+           is ($result, $expected, $name);
+       } else {
+           is (undef, undef, $name);
+       }
     } else {
         fail("$name : $@");
     }
@@ -261,9 +291,11 @@ foreach my $path (@plugins) {
 
     if ($@) {
         fail("$name : $@");
-    } else {
+    } elsif($ipam) {
         $result = $js->encode($plugin->read_db());
         is ($result, $expected, $name);
+    } else {
+        is (undef, undef, $name);
     }
 
 }
diff --git a/test/subnets/noipam/ipam_config b/test/subnets/noipam/ipam_config
new file mode 100644 (file)
index 0000000..a33be30
--- /dev/null
@@ -0,0 +1,18 @@
+{
+          'ids' => {
+                     'phpipam' => {
+                                    'url' => 'https://localhost/api/apiadmin',
+                                    'type' => 'phpipam',
+                                    'section' => 1,
+                                    'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw'
+                                  },
+                     'pve' => {
+                                'type' => 'pve'
+                              },
+                     'netbox' => {
+                                   'token' => '0123456789abcdef0123456789abcdef01234567',
+                                   'type' => 'netbox',
+                                   'url' => 'http://localhost:8000/api'
+                                 }
+                   },
+}
diff --git a/test/subnets/noipam/sdn_config b/test/subnets/noipam/sdn_config
new file mode 100644 (file)
index 0000000..55107d6
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  version => 1,
+  vnets   => {
+               ids => {
+                        myvnet => { type => "vnet", zone => "myzone" },
+                      },
+             },
+
+  zones   => {
+               ids => { myzone => { type =>"simple" } },
+             },
+
+  subnets => {
+              ids => { 'myzone-10.0.0.0-24' => {
+                                                        'type' => 'subnet',
+                                                        'vnet' => 'myvnet',
+                                                  }
+                     }
+             }
+}