From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A957F66048 for ; Tue, 5 Jan 2021 10:36:23 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A35042594F for ; Tue, 5 Jan 2021 10:35:52 +0100 (CET) Received: from kvmformation1.odiso.net (globalOdiso.M6Lille.odiso.net [89.248.211.242]) by firstgate.proxmox.com (Proxmox) with ESMTP id A15B2254D0 for ; Tue, 5 Jan 2021 10:35:37 +0100 (CET) Received: by kvmformation1.odiso.net (Postfix, from userid 0) id 791FC168B9E; Tue, 5 Jan 2021 10:35:37 +0100 (CET) From: Alexandre Derumier To: pve-devel@lists.proxmox.com Date: Tue, 5 Jan 2021 10:35:30 +0100 Message-Id: <20210105093536.1727641-10-aderumier@odiso.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210105093536.1727641-1-aderumier@odiso.com> References: <20210105093536.1727641-1-aderumier@odiso.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 AWL -0.262 Adjusted score from AWL reputation of From: address HEADER_FROM_DIFFERENT_DOMAINS 0.248 From and EnvelopeFrom 2nd level mail domains are different KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods KAM_LOTSOFHASH 0.25 Emails with lots of hash-like gibberish KHOP_HELO_FCRDNS 0.287 Relay HELO differs from its IP's reverse DNS NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pve-devel] [PATCH pve-network 09/15] tests: add ipams tests X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jan 2021 09:36:23 -0000 Signed-off-by: Alexandre Derumier --- test/Makefile | 5 +- test/ipams/netbox/expected.add_ip | 9 + test/ipams/netbox/expected.add_next_freeip | 9 + test/ipams/netbox/expected.add_subnet | 9 + test/ipams/netbox/expected.del_ip | 9 + test/ipams/netbox/expected.del_subnet | 9 + test/ipams/netbox/expected.update_ip | 9 + test/ipams/netbox/ipam_config | 18 ++ test/ipams/netbox/sdn_config | 20 +++ test/ipams/phpipam/expected.add_ip | 12 ++ test/ipams/phpipam/expected.add_next_freeip | 12 ++ test/ipams/phpipam/expected.add_subnet | 12 ++ test/ipams/phpipam/expected.del_ip | 12 ++ test/ipams/phpipam/expected.del_subnet | 12 ++ test/ipams/phpipam/expected.update_ip | 12 ++ test/ipams/phpipam/ipam_config | 18 ++ test/ipams/phpipam/sdn_config | 20 +++ test/run_test_ipams.pl | 181 ++++++++++++++++++++ 18 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 test/ipams/netbox/expected.add_ip create mode 100644 test/ipams/netbox/expected.add_next_freeip create mode 100644 test/ipams/netbox/expected.add_subnet create mode 100644 test/ipams/netbox/expected.del_ip create mode 100644 test/ipams/netbox/expected.del_subnet create mode 100644 test/ipams/netbox/expected.update_ip create mode 100644 test/ipams/netbox/ipam_config create mode 100644 test/ipams/netbox/sdn_config create mode 100644 test/ipams/phpipam/expected.add_ip create mode 100644 test/ipams/phpipam/expected.add_next_freeip create mode 100644 test/ipams/phpipam/expected.add_subnet create mode 100644 test/ipams/phpipam/expected.del_ip create mode 100644 test/ipams/phpipam/expected.del_subnet create mode 100644 test/ipams/phpipam/expected.update_ip create mode 100644 test/ipams/phpipam/ipam_config create mode 100644 test/ipams/phpipam/sdn_config create mode 100755 test/run_test_ipams.pl diff --git a/test/Makefile b/test/Makefile index b8e5de1..6eb88a1 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,9 @@ all: test -test: test_zones +test: test_zones test_ipams test_zones: run_test_zones.pl ./run_test_zones.pl + +test_ipams: run_test_ipams.pl + ./run_test_ipams.pl diff --git a/test/ipams/netbox/expected.add_ip b/test/ipams/netbox/expected.add_ip new file mode 100644 index 0000000..ae876f2 --- /dev/null +++ b/test/ipams/netbox/expected.add_ip @@ -0,0 +1,9 @@ +bless( { + '_content' => '{"address":"10.0.0.1/24","description":"mydescription mac:da:65:8f:18:9b:6f","dns_name":"myhostname"}', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/ip-addresses/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/expected.add_next_freeip b/test/ipams/netbox/expected.add_next_freeip new file mode 100644 index 0000000..7f80f4c --- /dev/null +++ b/test/ipams/netbox/expected.add_next_freeip @@ -0,0 +1,9 @@ +bless( { + '_content' => '{"description":"mydescription mac:da:65:8f:18:9b:6f","dns_name":"myhostname"}', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/prefixes/1/available-ips/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/expected.add_subnet b/test/ipams/netbox/expected.add_subnet new file mode 100644 index 0000000..62ca823 --- /dev/null +++ b/test/ipams/netbox/expected.add_subnet @@ -0,0 +1,9 @@ +bless( { + '_content' => '{"prefix":"10.0.0.0/24"}', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/prefixes/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/expected.del_ip b/test/ipams/netbox/expected.del_ip new file mode 100644 index 0000000..3c41de4 --- /dev/null +++ b/test/ipams/netbox/expected.del_ip @@ -0,0 +1,9 @@ +bless( { + '_content' => '', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'DELETE', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/ip-addresses/1/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/expected.del_subnet b/test/ipams/netbox/expected.del_subnet new file mode 100644 index 0000000..bdadb71 --- /dev/null +++ b/test/ipams/netbox/expected.del_subnet @@ -0,0 +1,9 @@ +bless( { + '_content' => '{"address":"192.168.0.1/24","description":null,"dns_name":"toto"}', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/ip-addresses/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/expected.update_ip b/test/ipams/netbox/expected.update_ip new file mode 100644 index 0000000..a1202ad --- /dev/null +++ b/test/ipams/netbox/expected.update_ip @@ -0,0 +1,9 @@ +bless( { + '_content' => '{"address":"10.0.0.1/24","description":"mydescription mac:da:65:8f:18:9b:6f","dns_name":"myhostname"}', + '_headers' => bless( { + 'authorization' => 'token 0123456789abcdef0123456789abcdef01234567', + 'content-type' => 'application/json; charset=UTF-8' + }, 'HTTP::Headers' ), + '_method' => 'PATCH', + '_uri' => bless( do{\(my $o = 'http://localhost:8000/api/ipam/ip-addresses/1/')}, 'URI::http' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/netbox/ipam_config b/test/ipams/netbox/ipam_config new file mode 100644 index 0000000..a33be30 --- /dev/null +++ b/test/ipams/netbox/ipam_config @@ -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/ipams/netbox/sdn_config b/test/ipams/netbox/sdn_config new file mode 100644 index 0000000..c31847b --- /dev/null +++ b/test/ipams/netbox/sdn_config @@ -0,0 +1,20 @@ +{ + version => 1, + vnets => { + ids => { + myvnet => { type => "vnet", zone => "myzone" }, + }, + }, + + zones => { + ids => { myzone => { ipam => "netbox" } }, + }, + + subnets => { + ids => { 'myzone-10.0.0.0-24' => { + 'type' => 'subnet', + 'vnet' => 'myvnet', + } + } + } +} diff --git a/test/ipams/phpipam/expected.add_ip b/test/ipams/phpipam/expected.add_ip new file mode 100644 index 0000000..50af460 --- /dev/null +++ b/test/ipams/phpipam/expected.add_ip @@ -0,0 +1,12 @@ +bless( { + '_content' => '{"description":"mydescription","hostname":"myhostname","ip":"10.0.0.1","is_gateway":1,"mac":"da:65:8f:18:9b:6f","subnetId":1}', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/addresses/')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/expected.add_next_freeip b/test/ipams/phpipam/expected.add_next_freeip new file mode 100644 index 0000000..d72f94f --- /dev/null +++ b/test/ipams/phpipam/expected.add_next_freeip @@ -0,0 +1,12 @@ +bless( { + '_content' => '{"description":"mydescription","hostname":"myhostname","mac":"da:65:8f:18:9b:6f"}', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/addresses/first_free/1/')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/expected.add_subnet b/test/ipams/phpipam/expected.add_subnet new file mode 100644 index 0000000..b10cc5a --- /dev/null +++ b/test/ipams/phpipam/expected.add_subnet @@ -0,0 +1,12 @@ +bless( { + '_content' => '{"mask":"24","sectionId":1,"subnet":"10.0.0.0"}', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/subnets/')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/expected.del_ip b/test/ipams/phpipam/expected.del_ip new file mode 100644 index 0000000..72e83cb --- /dev/null +++ b/test/ipams/phpipam/expected.del_ip @@ -0,0 +1,12 @@ +bless( { + '_content' => '', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'DELETE', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/addresses/1')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/expected.del_subnet b/test/ipams/phpipam/expected.del_subnet new file mode 100644 index 0000000..349a34f --- /dev/null +++ b/test/ipams/phpipam/expected.del_subnet @@ -0,0 +1,12 @@ +bless( { + '_content' => '{"description":null,"hostname":"toto","ip":"192.168.0.1","is_gateway":null,"subnetId":1}', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'POST', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/addresses/')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/expected.update_ip b/test/ipams/phpipam/expected.update_ip new file mode 100644 index 0000000..96c219b --- /dev/null +++ b/test/ipams/phpipam/expected.update_ip @@ -0,0 +1,12 @@ +bless( { + '_content' => '{"description":"mydescription","hostname":"myhostname","is_gateway":1,"mac":"da:65:8f:18:9b:6f"}', + '_headers' => bless( { + '::std_case' => { + 'token' => 'Token' + }, + 'content-type' => 'application/json; charset=UTF-8', + 'token' => 'JPHkPSLB4O_XL-GQz4qtEFmNpx-99Htw' + }, 'HTTP::Headers' ), + '_method' => 'PATCH', + '_uri' => bless( do{\(my $o = 'https://localhost/api/apiadmin/addresses/1')}, 'URI::https' ) + }, 'HTTP::Request' ); diff --git a/test/ipams/phpipam/ipam_config b/test/ipams/phpipam/ipam_config new file mode 100644 index 0000000..a33be30 --- /dev/null +++ b/test/ipams/phpipam/ipam_config @@ -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/ipams/phpipam/sdn_config b/test/ipams/phpipam/sdn_config new file mode 100644 index 0000000..c774807 --- /dev/null +++ b/test/ipams/phpipam/sdn_config @@ -0,0 +1,20 @@ +{ + version => 1, + vnets => { + ids => { + myvnet => { type => "vnet", zone => "myzone" }, + }, + }, + + zones => { + ids => { myzone => { ipam => "phpipam" } }, + }, + + subnets => { + ids => { 'myzone-10.0.0.0-24' => { + 'type' => 'subnet', + 'vnet' => 'myvnet', + } + } + } +} diff --git a/test/run_test_ipams.pl b/test/run_test_ipams.pl new file mode 100755 index 0000000..6743eff --- /dev/null +++ b/test/run_test_ipams.pl @@ -0,0 +1,181 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib qw(..); +use File::Slurp; + +use Test::More; +use Test::MockModule; + +use PVE::Network::SDN; +use PVE::Network::SDN::Zones; +use PVE::Network::SDN::Controllers; +use PVE::INotify; +use JSON; + +use Data::Dumper qw(Dumper); +$Data::Dumper::Sortkeys = 1; + +sub read_sdn_config { + my ($file) = @_; + # Read structure back in again + open my $in, '<', $file or die $!; + my $sdn_config; + { + local $/; # slurp mode + $sdn_config = eval <$in>; + } + close $in; + + return $sdn_config; +} + + +#my @plugins = <./ipams/*>; +my @plugins = read_dir( './ipams/', prefix => 1 ) ; + +foreach my $path (@plugins) { + + my (undef, $ipamid) = split(/\//, $path); + my $sdn_config = read_sdn_config ("$path/sdn_config"); + + + my $pve_sdn_subnets; + $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); + $pve_sdn_subnets->mock( + config => sub { + return $sdn_config->{subnets}; + }, + ); + + my $pve_sdn_ipam; + $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Ipams'); + $pve_sdn_subnets->mock( + config => sub { + my $ipam_config = read_sdn_config ("$path/ipam_config"); + return $ipam_config; + }, + ); + + my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); + $sdn_module->mock( + config => sub { + return $sdn_config; + }, + api_request => sub { + my ($method, $url, $headers, $data) = @_; + + my $js = JSON->new; + $js->canonical(1); + + my $encoded_data = $js->encode($data) if $data; + my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); + die Dumper($req); + } + ); + + + + #test params; + my $subnetid = "myzone-10.0.0.0-24"; + my $ip = "10.0.0.1"; + my $hostname = "myhostname"; + my $mac = "da:65:8f:18:9b:6f"; + my $description = "mydescription"; + my $is_gateway = 1; + + + my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1); + + my $ipam_cfg = PVE::Network::SDN::Ipams::config(); + my $plugin_config = $ipam_cfg->{ids}->{$ipamid}; + my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type}); + my $sdn_ipam_plugin = Test::MockModule->new($plugin); + $sdn_ipam_plugin->mock( + get_prefix_id => sub { + return 1; + }, + get_ip_id => sub { + return 1; + } + ); + + ## add_ip + my $test = "add_ip"; + my $expected = Dumper read_sdn_config("$path/expected.$test"); + my $name = "$ipamid $test"; + + $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1); + + if ($@) { + is ($@, $expected, $name); + } else { + fail($name); + } + + ## add_next_freeip + $test = "add_next_freeip"; + $expected = Dumper read_sdn_config("$path/expected.$test"); + $name = "$ipamid $test"; + + $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1); + + if ($@) { + is ($@, $expected, $name); + } else { + fail($name); + } + + + ## del_ip + $test = "del_ip"; + $expected = Dumper read_sdn_config("$path/expected.$test"); + $name = "$ipamid $test"; + + $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1); + + if ($@) { + is ($@, $expected, $name); + } else { + fail($name); + } + + ## update_ip + $test = "update_ip"; + $expected = Dumper read_sdn_config("$path/expected.$test"); + $name = "$ipamid $test"; + + $plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1); + + if ($@) { + is ($@, $expected, $name); + } else { + fail($name); + } + + $sdn_ipam_plugin->mock( + get_prefix_id => sub { + return undef; + }, + ); + + ## add_subnet + $test = "add_subnet"; + $expected = Dumper read_sdn_config("$path/expected.$test"); + $name = "$ipamid $test"; + + $plugin->add_subnet($plugin_config, $subnetid, $subnet, 1); + + if ($@) { + is ($@, $expected, $name); + } else { + fail($name); + } + +} + +done_testing(); + + -- 2.20.1