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

View file

@ -20,11 +20,12 @@ $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
local $/; # slurp mode
$sdn_config = eval <$in>;
}
close $in;
@ -32,15 +33,13 @@ sub read_sdn_config {
return $sdn_config;
}
#my @plugins = <./ipams/*>;
my @plugins = read_dir( './ipams/', prefix => 1 ) ;
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 $sdn_config = read_sdn_config("$path/sdn_config");
my $pve_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->mock(
config => sub {
my $ipam_config = read_sdn_config ("$path/ipam_config");
my $ipam_config = read_sdn_config("$path/ipam_config");
return $ipam_config;
},
);
@ -65,19 +64,17 @@ foreach my $path (@plugins) {
return $sdn_config;
},
api_request => sub {
my ($method, $url, $headers, $data) = @_;
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);
}
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";
@ -86,10 +83,10 @@ foreach my $path (@plugins) {
my $description = "mydescription";
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 = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
my $sdn_ipam_plugin = Test::MockModule->new($plugin);
@ -102,7 +99,7 @@ foreach my $path (@plugins) {
},
is_ip_gateway => sub {
return 1;
}
},
);
## add_ip
@ -110,10 +107,11 @@ foreach my $path (@plugins) {
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);
$plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description,
$is_gateway, 1);
if ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
@ -126,12 +124,11 @@ foreach my $path (@plugins) {
$plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1);
if ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
## del_ip
$test = "del_ip";
$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);
if ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
@ -149,10 +146,11 @@ foreach my $path (@plugins) {
$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);
$plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description,
$is_gateway, 1);
if ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
@ -163,15 +161,16 @@ foreach my $path (@plugins) {
$expected = Dumper read_sdn_config("$path/expected.$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 ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
$sdn_ipam_plugin->mock(
$sdn_ipam_plugin->mock(
get_prefix_id => sub {
return undef;
},
@ -185,7 +184,7 @@ foreach my $path (@plugins) {
$plugin->add_subnet($plugin_config, $subnetid, $subnet, 1);
if ($@) {
is ($@, $expected, $name);
is($@, $expected, $name);
} else {
fail($name);
}
@ -194,4 +193,3 @@ foreach my $path (@plugins) {
done_testing();

View file

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

View file

@ -21,26 +21,26 @@ $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
local $/; # slurp mode
$sdn_config = eval <$in>;
}
close $in;
return $sdn_config;
}
my @plugins = read_dir( './vnets/', prefix => 1 ) ;
my @plugins = read_dir('./vnets/', prefix => 1);
foreach my $path (@plugins) {
my (undef, $testid) = split(/\//, $path);
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;
$pve_sdn_zones = Test::MockModule->new('PVE::Network::SDN::Zones');
@ -69,55 +69,57 @@ foreach my $path (@plugins) {
},
add_dns_record => sub {
return;
}
},
);
my $js = JSON->new;
$js->canonical(1);
#test params;
#test params;
my $subnets = $sdn_config->{subnets}->{ids};
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 $iplist = NetAddr::IP->new($subnet_cidr);
my $mask = $iplist->masklen();
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
$ipversion = 4;
} else {
} else {
$ipversion = 6;
}
my $cidr1 = $iplist->canon()."/$mask";
my $cidr1 = $iplist->canon() . "/$mask";
$iplist++;
my $cidr2 = $iplist->canon()."/$mask";
my $cidr2 = $iplist->canon() . "/$mask";
my $cidr_outofrange = '8.8.8.8/8';
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 $iplist2 = NetAddr::IP->new($subnet2_cidr);
$iplist2++;
my $cidr3 = $iplist2->canon()."/$mask";
my $cidr3 = $iplist2->canon() . "/$mask";
$iplist2++;
my $cidr4 = $iplist2->canon()."/$mask";
my $cidr4 = $iplist2->canon() . "/$mask";
my $hostname = "myhostname";
my $mac = "da:65:8f:18:9b:6f";
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 $ipam = $zone->{ipam};
my $plugin;
my $sdn_ipam_plugin;
if($ipam) {
if ($ipam) {
$plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam);
$sdn_ipam_plugin = Test::MockModule->new($plugin);
$sdn_ipam_plugin->mock(
@ -127,7 +129,7 @@ foreach my $path (@plugins) {
write_db => sub {
my ($cfg) = @_;
$ipamdb = $cfg;
}
},
);
}
@ -135,7 +137,7 @@ foreach my $path (@plugins) {
$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");
my $ipam_config = read_sdn_config("$path/ipam_config");
return $ipam_config;
},
);
@ -148,14 +150,12 @@ foreach my $path (@plugins) {
my $result = undef;
my $expected = '';
eval {
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description);
};
eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); };
if ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
## add_ip
@ -164,16 +164,14 @@ foreach my $path (@plugins) {
$result = undef;
$expected = '';
eval {
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description);
};
eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); };
if ($@) {
is (undef, undef, $name);
} elsif($ipam) {
fail("$name : $@");
is(undef, undef, $name);
} elsif ($ipam) {
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
## add_ip
@ -182,14 +180,12 @@ foreach my $path (@plugins) {
$result = undef;
$expected = '';
eval {
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr2, $hostname, $mac, $description);
};
eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr2, $hostname, $mac, $description); };
if ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
## add_ip
@ -199,13 +195,14 @@ foreach my $path (@plugins) {
$expected = '';
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 ($@) {
is (undef, undef, $name);
is(undef, undef, $name);
} else {
fail("$name : $@");
fail("$name : $@");
}
## add_ip
@ -214,46 +211,41 @@ foreach my $path (@plugins) {
$result = undef;
$expected = '';
eval {
PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr4, $hostname, $mac, $description);
};
eval { PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr4, $hostname, $mac, $description); };
if ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
$test = "find_next_free_cidr_in_second_subnet ($cidr3)";
$name = "$testid $test";
$result = undef;
$expected = $ipam ? $cidr3 : undef;
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 ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is ($result, $expected, $name);
is($result, $expected, $name);
}
$test = "del_cidr $cidr1";
$name = "$testid $test";
$result = undef;
$expected = undef;
eval {
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname);
};
eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); };
if ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
$test = "del_cidr $cidr3";
@ -261,14 +253,12 @@ foreach my $path (@plugins) {
$result = undef;
$expected = undef;
eval {
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr3, $hostname);
};
eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr3, $hostname); };
if ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
$test = "del_cidr not exist $cidr1";
@ -276,16 +266,14 @@ foreach my $path (@plugins) {
$result = undef;
$expected = undef;
eval {
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname);
};
eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); };
if ($@) {
is (undef, undef, $name);
} elsif($ipam) {
fail("$name : $@");
is(undef, undef, $name);
} elsif ($ipam) {
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
$test = "del_cidr outofrange $cidr_outofrange";
@ -293,14 +281,12 @@ foreach my $path (@plugins) {
$result = undef;
$expected = undef;
eval {
$result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr_outofrange, $hostname);
};
eval { $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr_outofrange, $hostname); };
if ($@) {
is (undef, undef, $name);
is(undef, undef, $name);
} else {
fail("$name : $@");
fail("$name : $@");
}
$test = "find_next_free_cidr_in_first_subnet ($cidr1)";
@ -309,13 +295,14 @@ foreach my $path (@plugins) {
$expected = $ipam ? $cidr1 : undef;
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 ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is ($result, $expected, $name);
is($result, $expected, $name);
}
$test = "update_cidr $cidr1";
@ -324,13 +311,14 @@ foreach my $path (@plugins) {
$expected = undef;
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 ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
$test = "update_cidr deleted $cidr3";
@ -339,17 +327,17 @@ foreach my $path (@plugins) {
$expected = undef;
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 ($@) {
fail("$name : $@");
fail("$name : $@");
} else {
is (undef, undef, $name);
is(undef, undef, $name);
}
}
done_testing();

View file

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