test: run through perltidy

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2024-03-22 10:08:31 +01:00
parent 74dcbe9c08
commit ddd3d0f726
5 changed files with 297 additions and 254 deletions

View file

@ -20,11 +20,12 @@ $Data::Dumper::Sortkeys = 1;
sub read_sdn_config { sub read_sdn_config {
my ($file) = @_; my ($file) = @_;
# Read structure back in again # Read structure back in again
open my $in, '<', $file or die $!; open my $in, '<', $file or die $!;
my $sdn_config; my $sdn_config;
{ {
local $/; # slurp mode local $/; # slurp mode
$sdn_config = eval <$in>; $sdn_config = eval <$in>;
} }
close $in; close $in;
@ -32,20 +33,18 @@ sub read_sdn_config {
return $sdn_config; return $sdn_config;
} }
my @plugins = read_dir('./dns/', prefix => 1);
my @plugins = read_dir( './dns/', prefix => 1 ) ;
foreach my $path (@plugins) { foreach my $path (@plugins) {
my (undef, $dnsid) = split(/\//, $path); my (undef, $dnsid) = split(/\//, $path);
my $sdn_config = read_sdn_config ("$path/sdn_config"); my $sdn_config = read_sdn_config("$path/sdn_config");
my $pve_sdn_dns; my $pve_sdn_dns;
$pve_sdn_dns = Test::MockModule->new('PVE::Network::SDN::Dns'); $pve_sdn_dns = Test::MockModule->new('PVE::Network::SDN::Dns');
$pve_sdn_dns->mock( $pve_sdn_dns->mock(
config => sub { config => sub {
my $dns_config = read_sdn_config ("$path/dns_config"); my $dns_config = read_sdn_config("$path/dns_config");
return $dns_config; return $dns_config;
}, },
); );
@ -56,23 +55,21 @@ foreach my $path (@plugins) {
return $sdn_config; return $sdn_config;
}, },
api_request => sub { api_request => sub {
my ($method, $url, $headers, $data) = @_; my ($method, $url, $headers, $data) = @_;
my $js = JSON->new; my $js = JSON->new;
$js->canonical(1); $js->canonical(1);
my $encoded_data = $js->encode($data) if $data; my $encoded_data = $js->encode($data) if $data;
my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); my $req = HTTP::Request->new($method, $url, $headers, $encoded_data);
die Dumper($req); die Dumper($req);
} },
); );
my $dns_cfg = PVE::Network::SDN::Dns::config(); my $dns_cfg = PVE::Network::SDN::Dns::config();
my $plugin_config = $dns_cfg->{ids}->{$dnsid}; my $plugin_config = $dns_cfg->{ids}->{$dnsid};
my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type}); my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type});
#test params; #test params;
my @ips = ("10.0.0.1", "2001:4860:4860::8888"); my @ips = ("10.0.0.1", "2001:4860:4860::8888");
my $zone = "domain.com"; my $zone = "domain.com";
@ -83,9 +80,9 @@ foreach my $path (@plugins) {
my $ipversion = Net::IP::ip_is_ipv6($ip) ? "ipv6" : "ipv4"; my $ipversion = Net::IP::ip_is_ipv6($ip) ? "ipv6" : "ipv4";
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A"; my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
my $ip2 = $type eq 'AAAA' ? '2001:4860:4860::8844' : '127.0.0.1'; my $ip2 = $type eq 'AAAA' ? '2001:4860:4860::8844' : '127.0.0.1';
my $fqdn = $hostname.".".$zone."."; my $fqdn = $hostname . "." . $zone . ".";
my $sdn_dns_plugin = Test::MockModule->new($plugin); my $sdn_dns_plugin = Test::MockModule->new($plugin);
$sdn_dns_plugin->mock( $sdn_dns_plugin->mock(
get_zone_content => sub { get_zone_content => sub {
@ -93,7 +90,7 @@ foreach my $path (@plugins) {
}, },
get_zone_rrset => sub { get_zone_rrset => sub {
return undef; return undef;
} },
); );
## add_a_record ## add_a_record
@ -104,7 +101,7 @@ foreach my $path (@plugins) {
$plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1); $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -117,12 +114,11 @@ foreach my $path (@plugins) {
$plugin->add_ptr_record($plugin_config, $zone, $hostname, $ip, 1); $plugin->add_ptr_record($plugin_config, $zone, $hostname, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
## del_ptr_record ## del_ptr_record
$test = "del_ptr_record"; $test = "del_ptr_record";
$expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion");
@ -131,12 +127,11 @@ foreach my $path (@plugins) {
$plugin->del_ptr_record($plugin_config, $zone, $ip, 1); $plugin->del_ptr_record($plugin_config, $zone, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
## del_a_record ## del_a_record
$sdn_dns_plugin->mock( $sdn_dns_plugin->mock(
@ -147,18 +142,22 @@ foreach my $path (@plugins) {
get_zone_rrset => sub { get_zone_rrset => sub {
my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A"; my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A";
my $fqdn = $hostname.".".$zone."."; my $fqdn = $hostname . "." . $zone . ".";
my $record = { content => $ip, my $record = {
disabled => JSON::false, content => $ip,
name => $fqdn, disabled => JSON::false,
type => $type }; name => $fqdn,
type => $type,
};
my $rrset = { name => $fqdn, my $rrset = {
type => $type, name => $fqdn,
ttl => '3600', type => $type,
records => [ $record ] }; ttl => '3600',
records => [$record],
};
return $rrset; return $rrset;
} },
); );
$test = "del_a_record"; $test = "del_a_record";
@ -168,7 +167,7 @@ foreach my $path (@plugins) {
$plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1); $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -182,22 +181,28 @@ foreach my $path (@plugins) {
}, },
get_zone_rrset => sub { get_zone_rrset => sub {
my $record = { content => $ip, my $record = {
disabled => JSON::false, content => $ip,
name => $fqdn, disabled => JSON::false,
type => $type }; name => $fqdn,
type => $type,
};
my $record2 = { content => $ip2, my $record2 = {
disabled => JSON::false, content => $ip2,
name => $fqdn, disabled => JSON::false,
type => $type }; name => $fqdn,
type => $type,
};
my $rrset = { name => $fqdn, my $rrset = {
type => $type, name => $fqdn,
ttl => '3600', type => $type,
records => [ $record, $record2 ] }; ttl => '3600',
records => [$record, $record2],
};
return $rrset; return $rrset;
} },
); );
$test = "del_a_multiple_record"; $test = "del_a_multiple_record";
@ -207,7 +212,7 @@ foreach my $path (@plugins) {
$plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1); $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -221,17 +226,21 @@ foreach my $path (@plugins) {
}, },
get_zone_rrset => sub { get_zone_rrset => sub {
my $record2 = { content => $ip2, my $record2 = {
disabled => JSON::false, content => $ip2,
name => $fqdn, disabled => JSON::false,
type => $type }; name => $fqdn,
type => $type,
};
my $rrset = { name => $fqdn, my $rrset = {
type => $type, name => $fqdn,
ttl => '3600', type => $type,
records => [ $record2 ] }; ttl => '3600',
records => [$record2],
};
return $rrset; return $rrset;
} },
); );
$test = "add_a_multiple_record"; $test = "add_a_multiple_record";
@ -241,7 +250,7 @@ foreach my $path (@plugins) {
$plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1); $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -255,7 +264,7 @@ foreach my $path (@plugins) {
$plugin->verify_zone($plugin_config, $zone, 1); $plugin->verify_zone($plugin_config, $zone, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -264,4 +273,3 @@ foreach my $path (@plugins) {
done_testing(); done_testing();

View file

@ -20,11 +20,12 @@ $Data::Dumper::Sortkeys = 1;
sub read_sdn_config { sub read_sdn_config {
my ($file) = @_; my ($file) = @_;
# Read structure back in again # Read structure back in again
open my $in, '<', $file or die $!; open my $in, '<', $file or die $!;
my $sdn_config; my $sdn_config;
{ {
local $/; # slurp mode local $/; # slurp mode
$sdn_config = eval <$in>; $sdn_config = eval <$in>;
} }
close $in; close $in;
@ -32,15 +33,13 @@ sub read_sdn_config {
return $sdn_config; return $sdn_config;
} }
#my @plugins = <./ipams/*>; #my @plugins = <./ipams/*>;
my @plugins = read_dir( './ipams/', prefix => 1 ) ; my @plugins = read_dir('./ipams/', prefix => 1);
foreach my $path (@plugins) { foreach my $path (@plugins) {
my (undef, $ipamid) = split(/\//, $path); my (undef, $ipamid) = split(/\//, $path);
my $sdn_config = read_sdn_config ("$path/sdn_config"); my $sdn_config = read_sdn_config("$path/sdn_config");
my $pve_sdn_subnets; my $pve_sdn_subnets;
$pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets');
@ -54,7 +53,7 @@ foreach my $path (@plugins) {
$pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Ipams'); $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Ipams');
$pve_sdn_subnets->mock( $pve_sdn_subnets->mock(
config => sub { config => sub {
my $ipam_config = read_sdn_config ("$path/ipam_config"); my $ipam_config = read_sdn_config("$path/ipam_config");
return $ipam_config; return $ipam_config;
}, },
); );
@ -65,19 +64,17 @@ foreach my $path (@plugins) {
return $sdn_config; return $sdn_config;
}, },
api_request => sub { api_request => sub {
my ($method, $url, $headers, $data) = @_; my ($method, $url, $headers, $data) = @_;
my $js = JSON->new; my $js = JSON->new;
$js->canonical(1); $js->canonical(1);
my $encoded_data = $js->encode($data) if $data; my $encoded_data = $js->encode($data) if $data;
my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); my $req = HTTP::Request->new($method, $url, $headers, $encoded_data);
die Dumper($req); die Dumper($req);
} },
); );
#test params; #test params;
my $subnetid = "myzone-10.0.0.0-24"; my $subnetid = "myzone-10.0.0.0-24";
my $ip = "10.0.0.1"; my $ip = "10.0.0.1";
@ -86,10 +83,10 @@ foreach my $path (@plugins) {
my $description = "mydescription"; my $description = "mydescription";
my $is_gateway = 1; my $is_gateway = 1;
my $subnet =
PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1);
my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1); my $ipam_cfg = PVE::Network::SDN::Ipams::config();
my $ipam_cfg = PVE::Network::SDN::Ipams::config();
my $plugin_config = $ipam_cfg->{ids}->{$ipamid}; my $plugin_config = $ipam_cfg->{ids}->{$ipamid};
my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type}); my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
my $sdn_ipam_plugin = Test::MockModule->new($plugin); my $sdn_ipam_plugin = Test::MockModule->new($plugin);
@ -102,7 +99,7 @@ foreach my $path (@plugins) {
}, },
is_ip_gateway => sub { is_ip_gateway => sub {
return 1; return 1;
} },
); );
## add_ip ## add_ip
@ -110,10 +107,11 @@ foreach my $path (@plugins) {
my $expected = Dumper read_sdn_config("$path/expected.$test"); my $expected = Dumper read_sdn_config("$path/expected.$test");
my $name = "$ipamid $test"; my $name = "$ipamid $test";
$plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1); $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description,
$is_gateway, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -126,12 +124,11 @@ foreach my $path (@plugins) {
$plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1); $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
## del_ip ## del_ip
$test = "del_ip"; $test = "del_ip";
$expected = Dumper read_sdn_config("$path/expected.$test"); $expected = Dumper read_sdn_config("$path/expected.$test");
@ -140,7 +137,7 @@ foreach my $path (@plugins) {
$plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1); $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -149,10 +146,11 @@ foreach my $path (@plugins) {
$test = "update_ip"; $test = "update_ip";
$expected = Dumper read_sdn_config("$path/expected.$test"); $expected = Dumper read_sdn_config("$path/expected.$test");
$name = "$ipamid $test"; $name = "$ipamid $test";
$plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1); $plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description,
$is_gateway, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -163,15 +161,16 @@ foreach my $path (@plugins) {
$expected = Dumper read_sdn_config("$path/expected.$test"); $expected = Dumper read_sdn_config("$path/expected.$test");
$name = "$ipamid $test"; $name = "$ipamid $test";
$plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1); $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description,
$is_gateway, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
$sdn_ipam_plugin->mock( $sdn_ipam_plugin->mock(
get_prefix_id => sub { get_prefix_id => sub {
return undef; return undef;
}, },
@ -185,7 +184,7 @@ foreach my $path (@plugins) {
$plugin->add_subnet($plugin_config, $subnetid, $subnet, 1); $plugin->add_subnet($plugin_config, $subnetid, $subnet, 1);
if ($@) { if ($@) {
is ($@, $expected, $name); is($@, $expected, $name);
} else { } else {
fail($name); fail($name);
} }
@ -194,4 +193,3 @@ foreach my $path (@plugins) {
done_testing(); done_testing();

View file

@ -18,7 +18,6 @@ use JSON;
use Data::Dumper qw(Dumper); use Data::Dumper qw(Dumper);
$Data::Dumper::Sortkeys = 1; $Data::Dumper::Sortkeys = 1;
my $locks = {}; my $locks = {};
my $mocked_cfs_lock_file = sub { my $mocked_cfs_lock_file = sub {
@ -37,11 +36,12 @@ my $mocked_cfs_lock_file = sub {
sub read_sdn_config { sub read_sdn_config {
my ($file) = @_; my ($file) = @_;
# Read structure back in again # Read structure back in again
open my $in, '<', $file or die $!; open my $in, '<', $file or die $!;
my $sdn_config; my $sdn_config;
{ {
local $/; # slurp mode local $/; # slurp mode
$sdn_config = eval <$in>; $sdn_config = eval <$in>;
} }
close $in; close $in;
@ -49,16 +49,14 @@ sub read_sdn_config {
return $sdn_config; return $sdn_config;
} }
my @plugins = read_dir('./subnets/', prefix => 1);
my @plugins = read_dir( './subnets/', prefix => 1 ) ;
foreach my $path (@plugins) { foreach my $path (@plugins) {
my (undef, $testid) = split(/\//, $path); my (undef, $testid) = split(/\//, $path);
print "test: $testid\n"; print "test: $testid\n";
my $sdn_config = read_sdn_config ("$path/sdn_config"); my $sdn_config = read_sdn_config("$path/sdn_config");
my $pve_sdn_subnets; my $pve_sdn_subnets;
$pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets');
@ -71,18 +69,17 @@ foreach my $path (@plugins) {
}, },
add_dns_record => sub { add_dns_record => sub {
return; return;
} },
); );
my $js = JSON->new; my $js = JSON->new;
$js->canonical(1); $js->canonical(1);
#test params; #test params;
my $subnets = $sdn_config->{subnets}->{ids}; my $subnets = $sdn_config->{subnets}->{ids};
my $subnetid = (keys %{$subnets})[0]; my $subnetid = (keys %{$subnets})[0];
my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1); my $subnet =
PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1);
my $subnet_cidr = $subnet->{cidr}; my $subnet_cidr = $subnet->{cidr};
my $iplist = NetAddr::IP->new($subnet_cidr); my $iplist = NetAddr::IP->new($subnet_cidr);
@ -105,7 +102,7 @@ foreach my $path (@plugins) {
my $plugin; my $plugin;
my $sdn_ipam_plugin; my $sdn_ipam_plugin;
if($ipam) { if ($ipam) {
$plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam); $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam);
$sdn_ipam_plugin = Test::MockModule->new($plugin); $sdn_ipam_plugin = Test::MockModule->new($plugin);
$sdn_ipam_plugin->mock( $sdn_ipam_plugin->mock(
@ -124,7 +121,7 @@ foreach my $path (@plugins) {
$pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams'); $pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams');
$pve_sdn_ipams->mock( $pve_sdn_ipams->mock(
config => sub { config => sub {
my $ipam_config = read_sdn_config ("$path/ipam_config"); my $ipam_config = read_sdn_config("$path/ipam_config");
return $ipam_config; return $ipam_config;
}, },
add_cache_mac_ip => sub { add_cache_mac_ip => sub {
@ -132,59 +129,66 @@ foreach my $path (@plugins) {
}, },
del_cache_mac_ip => sub { del_cache_mac_ip => sub {
return; return;
} },
); );
## add_subnet ## add_subnet
my $test = "add_subnet $subnetid"; my $test = "add_subnet $subnetid";
my $name = "$testid $test"; my $name = "$testid $test";
my $result = undef; my $result = undef;
my $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{}}}}}}'; my $expected = '{"zones":{"myzone":{"subnets":{"' . $subnet_cidr . '":{"ips":{}}}}}}';
eval { eval {
PVE::Network::SDN::Subnets::add_subnet($zone, $subnetid, $subnet); PVE::Network::SDN::Subnets::add_subnet($zone, $subnetid, $subnet);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
## add_ip ## add_ip
$test = "add_ip $ip"; $test = "add_ip $ip";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1}}}}}}}'; $expected =
'{"zones":{"myzone":{"subnets":{"'
. $subnet_cidr
. '":{"ips":{"'
. $ip
. '":{"gateway":1}}}}}}}';
eval { eval {
PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, $is_gateway); PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid,
$is_gateway);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
if($ipam) { if ($ipam) {
## add_already_exist_ip ## add_already_exist_ip
$test = "add_already_exist_ip $ip"; $test = "add_already_exist_ip $ip";
$name = "$testid $test"; $name = "$testid $test";
eval { eval {
PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid); PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac,
$vmid);
}; };
if ($@) { if ($@) {
is (undef, undef, $name); is(undef, undef, $name);
} else { } else {
fail("$name : $@"); fail("$name : $@");
} }
@ -194,76 +198,113 @@ foreach my $path (@plugins) {
$test = "add_second_ip $ip2"; $test = "add_second_ip $ip2";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1},"'.$ip2.'":{"hostname":"'.$hostname.'","mac":"'.$mac.'","vmid":"'.$vmid.'"}}}}}}}'; $expected =
'{"zones":{"myzone":{"subnets":{"'
. $subnet_cidr
. '":{"ips":{"'
. $ip
. '":{"gateway":1},"'
. $ip2
. '":{"hostname":"'
. $hostname
. '","mac":"'
. $mac
. '","vmid":"'
. $vmid
. '"}}}}}}}';
eval { eval {
PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip2, $hostname, $mac, $vmid); PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip2, $hostname, $mac, $vmid);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
## add_next_free ## add_next_free
$test = "find_next_freeip ($ipnextfree)"; $test = "find_next_freeip ($ipnextfree)";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1},"'.$ipnextfree.'":{},"'.$ip2.'":{"hostname":"'.$hostname.'","mac":"'.$mac.'","vmid":"'.$vmid.'"}}}}}}}'; $expected =
'{"zones":{"myzone":{"subnets":{"'
. $subnet_cidr
. '":{"ips":{"'
. $ip
. '":{"gateway":1},"'
. $ipnextfree
. '":{},"'
. $ip2
. '":{"hostname":"'
. $hostname
. '","mac":"'
. $mac
. '","vmid":"'
. $vmid
. '"}}}}}}}';
eval { eval {
$ip3 = PVE::Network::SDN::Subnets::add_next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $vmid); $ip3 = PVE::Network::SDN::Subnets::add_next_free_ip($zone, $subnetid, $subnet, $hostname,
$mac, $vmid);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} }
## del_ip ## del_ip
$test = "del_ip $ip"; $test = "del_ip $ip";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ipnextfree.'":{},"'.$ip2.'":{"hostname":"'.$hostname.'","mac":"'.$mac.'","vmid":"'.$vmid.'"}}}}}}}'; $expected =
'{"zones":{"myzone":{"subnets":{"'
. $subnet_cidr
. '":{"ips":{"'
. $ipnextfree
. '":{},"'
. $ip2
. '":{"hostname":"'
. $hostname
. '","mac":"'
. $mac
. '","vmid":"'
. $vmid
. '"}}}}}}}';
eval { eval { PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname); };
PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
if($ipam){ if ($ipam) {
## del_subnet_not_empty ## del_subnet_not_empty
$test = "del_subnet_not_empty $subnetid"; $test = "del_subnet_not_empty $subnetid";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = undef; $expected = undef;
eval { eval { PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet); };
PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet);
};
if ($@) { if ($@) {
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
fail("$name : $@"); fail("$name : $@");
} }
} }
## add_ip_rollback_failing_dns ## add_ip_rollback_failing_dns
$test = "add_ip_rollback_failing_dns"; $test = "add_ip_rollback_failing_dns";
@ -277,29 +318,41 @@ foreach my $path (@plugins) {
add_dns_record => sub { add_dns_record => sub {
die "error add dns record"; die "error add dns record";
return; return;
} },
); );
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ipnextfree.'":{},"'.$ip2.'":{"hostname":"'.$hostname.'","mac":"'.$mac.'","vmid":"'.$vmid.'"}}}}}}}'; $expected =
'{"zones":{"myzone":{"subnets":{"'
. $subnet_cidr
. '":{"ips":{"'
. $ipnextfree
. '":{},"'
. $ip2
. '":{"hostname":"'
. $hostname
. '","mac":"'
. $mac
. '","vmid":"'
. $vmid
. '"}}}}}}}';
eval { eval {
PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid); PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid);
}; };
if ($@) { if ($@) {
if($ipam) { if ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
} else { } else {
fail("$name : $@"); fail("$name : $@");
} }
## del_empty_subnet ## del_empty_subnet
$test = "del_empty_subnet"; $test = "del_empty_subnet";
$name = "$testid $test"; $name = "$testid $test";
@ -309,21 +362,18 @@ foreach my $path (@plugins) {
PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip2, $hostname); PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip2, $hostname);
PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip3, $hostname); PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip3, $hostname);
eval { eval { PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet); };
PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} elsif($ipam) { } elsif ($ipam) {
$result = $js->encode($plugin->read_db()); $result = $js->encode($plugin->read_db());
is ($result, $expected, $name); is($result, $expected, $name);
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
} }
done_testing(); done_testing();

View file

@ -21,26 +21,26 @@ $Data::Dumper::Sortkeys = 1;
sub read_sdn_config { sub read_sdn_config {
my ($file) = @_; my ($file) = @_;
# Read structure back in again # Read structure back in again
open my $in, '<', $file or die $!; open my $in, '<', $file or die $!;
my $sdn_config; my $sdn_config;
{ {
local $/; # slurp mode local $/; # slurp mode
$sdn_config = eval <$in>; $sdn_config = eval <$in>;
} }
close $in; close $in;
return $sdn_config; return $sdn_config;
} }
my @plugins = read_dir('./vnets/', prefix => 1);
my @plugins = read_dir( './vnets/', prefix => 1 ) ;
foreach my $path (@plugins) { foreach my $path (@plugins) {
my (undef, $testid) = split(/\//, $path); my (undef, $testid) = split(/\//, $path);
print "test: $testid\n"; print "test: $testid\n";
my $sdn_config = read_sdn_config ("$path/sdn_config"); my $sdn_config = read_sdn_config("$path/sdn_config");
my $pve_sdn_zones; my $pve_sdn_zones;
$pve_sdn_zones = Test::MockModule->new('PVE::Network::SDN::Zones'); $pve_sdn_zones = Test::MockModule->new('PVE::Network::SDN::Zones');
@ -69,55 +69,57 @@ foreach my $path (@plugins) {
}, },
add_dns_record => sub { add_dns_record => sub {
return; return;
} },
); );
my $js = JSON->new; my $js = JSON->new;
$js->canonical(1); $js->canonical(1);
#test params; #test params;
#test params; #test params;
my $subnets = $sdn_config->{subnets}->{ids}; my $subnets = $sdn_config->{subnets}->{ids};
my $subnetid = (sort keys %{$subnets})[0]; my $subnetid = (sort keys %{$subnets})[0];
my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1); my $subnet =
PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1);
my $subnet_cidr = $subnet->{cidr}; my $subnet_cidr = $subnet->{cidr};
my $iplist = NetAddr::IP->new($subnet_cidr); my $iplist = NetAddr::IP->new($subnet_cidr);
my $mask = $iplist->masklen(); my $mask = $iplist->masklen();
my $ipversion = undef; my $ipversion = undef;
if (Net::IP::ip_is_ipv4($iplist->canon())){ if (Net::IP::ip_is_ipv4($iplist->canon())) {
$iplist++; #skip network address for ipv4 $iplist++; #skip network address for ipv4
$ipversion = 4; $ipversion = 4;
} else { } else {
$ipversion = 6; $ipversion = 6;
} }
my $cidr1 = $iplist->canon()."/$mask"; my $cidr1 = $iplist->canon() . "/$mask";
$iplist++; $iplist++;
my $cidr2 = $iplist->canon()."/$mask"; my $cidr2 = $iplist->canon() . "/$mask";
my $cidr_outofrange = '8.8.8.8/8'; my $cidr_outofrange = '8.8.8.8/8';
my $subnetid2 = (sort keys %{$subnets})[1]; my $subnetid2 = (sort keys %{$subnets})[1];
my $subnet2 = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid2, 1); my $subnet2 =
PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid2, 1);
my $subnet2_cidr = $subnet2->{cidr}; my $subnet2_cidr = $subnet2->{cidr};
my $iplist2 = NetAddr::IP->new($subnet2_cidr); my $iplist2 = NetAddr::IP->new($subnet2_cidr);
$iplist2++; $iplist2++;
my $cidr3 = $iplist2->canon()."/$mask"; my $cidr3 = $iplist2->canon() . "/$mask";
$iplist2++; $iplist2++;
my $cidr4 = $iplist2->canon()."/$mask"; my $cidr4 = $iplist2->canon() . "/$mask";
my $hostname = "myhostname"; my $hostname = "myhostname";
my $mac = "da:65:8f:18:9b:6f"; my $mac = "da:65:8f:18:9b:6f";
my $description = "mydescription"; my $description = "mydescription";
my $ipamdb = read_sdn_config ("$path/ipam.db"); my $ipamdb = read_sdn_config("$path/ipam.db");
my $zone = $sdn_config->{zones}->{ids}->{"myzone"}; my $zone = $sdn_config->{zones}->{ids}->{"myzone"};
my $ipam = $zone->{ipam}; my $ipam = $zone->{ipam};
my $plugin; my $plugin;
my $sdn_ipam_plugin; my $sdn_ipam_plugin;
if($ipam) { if ($ipam) {
$plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam); $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam);
$sdn_ipam_plugin = Test::MockModule->new($plugin); $sdn_ipam_plugin = Test::MockModule->new($plugin);
$sdn_ipam_plugin->mock( $sdn_ipam_plugin->mock(
@ -127,7 +129,7 @@ foreach my $path (@plugins) {
write_db => sub { write_db => sub {
my ($cfg) = @_; my ($cfg) = @_;
$ipamdb = $cfg; $ipamdb = $cfg;
} },
); );
} }
@ -135,7 +137,7 @@ foreach my $path (@plugins) {
$pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams'); $pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams');
$pve_sdn_ipams->mock( $pve_sdn_ipams->mock(
config => sub { config => sub {
my $ipam_config = read_sdn_config ("$path/ipam_config"); my $ipam_config = read_sdn_config("$path/ipam_config");
return $ipam_config; return $ipam_config;
}, },
); );
@ -148,14 +150,12 @@ foreach my $path (@plugins) {
my $result = undef; my $result = undef;
my $expected = ''; my $expected = '';
eval { eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); };
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
## add_ip ## add_ip
@ -164,16 +164,14 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = ''; $expected = '';
eval { eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); };
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description);
};
if ($@) { if ($@) {
is (undef, undef, $name); is(undef, undef, $name);
} elsif($ipam) { } elsif ($ipam) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
## add_ip ## add_ip
@ -182,14 +180,12 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = ''; $expected = '';
eval { eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr2, $hostname, $mac, $description); };
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr2, $hostname, $mac, $description);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
## add_ip ## add_ip
@ -199,13 +195,14 @@ foreach my $path (@plugins) {
$expected = ''; $expected = '';
eval { eval {
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr_outofrange, $hostname, $mac, $description); PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr_outofrange, $hostname, $mac,
$description);
}; };
if ($@) { if ($@) {
is (undef, undef, $name); is(undef, undef, $name);
} else { } else {
fail("$name : $@"); fail("$name : $@");
} }
## add_ip ## add_ip
@ -214,46 +211,41 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = ''; $expected = '';
eval { eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr4, $hostname, $mac, $description); };
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr4, $hostname, $mac, $description);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
$test = "find_next_free_cidr_in_second_subnet ($cidr3)"; $test = "find_next_free_cidr_in_second_subnet ($cidr3)";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = $ipam ? $cidr3 : undef; $expected = $ipam ? $cidr3 : undef;
eval { eval {
$result = PVE::Network::SDN::Vnets::add_next_free_cidr($vnetid, $hostname, $mac, $description); $result =
PVE::Network::SDN::Vnets::add_next_free_cidr($vnetid, $hostname, $mac, $description);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is ($result, $expected, $name); is($result, $expected, $name);
} }
$test = "del_cidr $cidr1"; $test = "del_cidr $cidr1";
$name = "$testid $test"; $name = "$testid $test";
$result = undef; $result = undef;
$expected = undef; $expected = undef;
eval { eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); };
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
$test = "del_cidr $cidr3"; $test = "del_cidr $cidr3";
@ -261,14 +253,12 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = undef; $expected = undef;
eval { eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr3, $hostname); };
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr3, $hostname);
};
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
$test = "del_cidr not exist $cidr1"; $test = "del_cidr not exist $cidr1";
@ -276,16 +266,14 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = undef; $expected = undef;
eval { eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); };
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname);
};
if ($@) { if ($@) {
is (undef, undef, $name); is(undef, undef, $name);
} elsif($ipam) { } elsif ($ipam) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
$test = "del_cidr outofrange $cidr_outofrange"; $test = "del_cidr outofrange $cidr_outofrange";
@ -293,14 +281,12 @@ foreach my $path (@plugins) {
$result = undef; $result = undef;
$expected = undef; $expected = undef;
eval { eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr_outofrange, $hostname); };
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr_outofrange, $hostname);
};
if ($@) { if ($@) {
is (undef, undef, $name); is(undef, undef, $name);
} else { } else {
fail("$name : $@"); fail("$name : $@");
} }
$test = "find_next_free_cidr_in_first_subnet ($cidr1)"; $test = "find_next_free_cidr_in_first_subnet ($cidr1)";
@ -309,13 +295,14 @@ foreach my $path (@plugins) {
$expected = $ipam ? $cidr1 : undef; $expected = $ipam ? $cidr1 : undef;
eval { eval {
$result = PVE::Network::SDN::Vnets::add_next_free_cidr($vnetid, $hostname, $mac, $description); $result =
PVE::Network::SDN::Vnets::add_next_free_cidr($vnetid, $hostname, $mac, $description);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is ($result, $expected, $name); is($result, $expected, $name);
} }
$test = "update_cidr $cidr1"; $test = "update_cidr $cidr1";
@ -324,13 +311,14 @@ foreach my $path (@plugins) {
$expected = undef; $expected = undef;
eval { eval {
$result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac, $description); $result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac,
$description);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
$test = "update_cidr deleted $cidr3"; $test = "update_cidr deleted $cidr3";
@ -339,17 +327,17 @@ foreach my $path (@plugins) {
$expected = undef; $expected = undef;
eval { eval {
$result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac, $description); $result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac,
$description);
}; };
if ($@) { if ($@) {
fail("$name : $@"); fail("$name : $@");
} else { } else {
is (undef, undef, $name); is(undef, undef, $name);
} }
} }
done_testing(); done_testing();

View file

@ -29,12 +29,11 @@ sub read_sdn_config {
return $sdn_config; return $sdn_config;
} }
my @tests = grep { -d } glob './zones/*/*'; my @tests = grep { -d } glob './zones/*/*';
foreach my $test (@tests) { foreach my $test (@tests) {
my $sdn_config = read_sdn_config ("./$test/sdn_config"); my $sdn_config = read_sdn_config("./$test/sdn_config");
open(my $fh1, '<', "./$test/interfaces") or die "can't read interfaces file - $!"; open(my $fh1, '<', "./$test/interfaces") or die "can't read interfaces file - $!";
my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef); my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef);
@ -78,7 +77,7 @@ foreach my $test (@tests) {
}, },
find_bridge => sub { find_bridge => sub {
return; return;
} },
); );
my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); my $sdn_module = Test::MockModule->new("PVE::Network::SDN");
@ -97,7 +96,7 @@ foreach my $test (@tests) {
diag("got unexpected error - $err"); diag("got unexpected error - $err");
fail($name); fail($name);
} else { } else {
is ($result, $expected, $name); is($result, $expected, $name);
} }
if ($sdn_config->{controllers}) { if ($sdn_config->{controllers}) {
@ -106,17 +105,17 @@ foreach my $test (@tests) {
eval { eval {
my $config = PVE::Network::SDN::Controllers::generate_controller_config(); my $config = PVE::Network::SDN::Controllers::generate_controller_config();
$controller_rawconfig = PVE::Network::SDN::Controllers::generate_controller_rawconfig($config); $controller_rawconfig =
PVE::Network::SDN::Controllers::generate_controller_rawconfig($config);
}; };
if (my $err = $@) { if (my $err = $@) {
diag("got unexpected error - $err"); diag("got unexpected error - $err");
fail($name); fail($name);
} else { } else {
is ($controller_rawconfig, $expected, $name); is($controller_rawconfig, $expected, $name);
} }
} }
} }
done_testing(); done_testing();