]>
git.proxmox.com Git - pve-network.git/blob - test/run_test_vnets.pl
8 use NetAddr
::IP
qw(:lower);
13 use PVE
::Network
::SDN
;
14 use PVE
::Network
::SDN
::Zones
;
15 use PVE
::Network
::SDN
::Controllers
;
19 use Data
::Dumper
qw(Dumper);
20 $Data::Dumper
::Sortkeys
= 1;
24 # Read structure back in again
25 open my $in, '<', $file or die $!;
28 local $/; # slurp mode
29 $sdn_config = eval <$in>;
36 my @plugins = read_dir
( './vnets/', prefix
=> 1 ) ;
38 foreach my $path (@plugins) {
40 my (undef, $testid) = split(/\//, $path);
42 print "test: $testid\n";
43 my $sdn_config = read_sdn_config
("$path/sdn_config");
46 $pve_sdn_zones = Test
::MockModule-
>new('PVE::Network::SDN::Zones');
49 return $sdn_config->{zones
};
54 $pve_sdn_vnets = Test
::MockModule-
>new('PVE::Network::SDN::Vnets');
57 return $sdn_config->{vnets
};
62 $pve_sdn_subnets = Test
::MockModule-
>new('PVE::Network::SDN::Subnets');
63 $pve_sdn_subnets->mock(
65 return $sdn_config->{subnets
};
67 verify_dns_zone
=> sub {
70 add_dns_record
=> sub {
80 my $subnets = $sdn_config->{subnets
}->{ids
};
82 my $subnetid = (sort keys %{$subnets})[0];
83 my $subnet = PVE
::Network
::SDN
::Subnets
::sdn_subnets_config
($sdn_config->{subnets
}, $subnetid, 1);
84 my $subnet_cidr = $subnet->{cidr
};
85 my $iplist = NetAddr
::IP-
>new($subnet_cidr);
86 my $mask = $iplist->masklen();
87 my $ipversion = undef;
89 if (Net
::IP
::ip_is_ipv4
($iplist->canon())){
90 $iplist++; #skip network address for ipv4
96 my $cidr1 = $iplist->canon()."/$mask";
98 my $cidr2 = $iplist->canon()."/$mask";
99 my $cidr_outofrange = '8.8.8.8/8';
101 my $subnetid2 = (sort keys %{$subnets})[1];
102 my $subnet2 = PVE
::Network
::SDN
::Subnets
::sdn_subnets_config
($sdn_config->{subnets
}, $subnetid2, 1);
103 my $subnet2_cidr = $subnet2->{cidr
};
104 my $iplist2 = NetAddr
::IP-
>new($subnet2_cidr);
106 my $cidr3 = $iplist2->canon()."/$mask";
108 my $cidr4 = $iplist2->canon()."/$mask";
110 my $hostname = "myhostname";
111 my $mac = "da:65:8f:18:9b:6f";
112 my $description = "mydescription";
113 my $ipamdb = read_sdn_config
("$path/ipam.db");
115 my $zone = $sdn_config->{zones
}->{ids
}->{"myzone"};
116 my $ipam = $zone->{ipam
};
121 $plugin = PVE
::Network
::SDN
::Ipams
::Plugin-
>lookup($ipam);
122 $sdn_ipam_plugin = Test
::MockModule-
>new($plugin);
123 $sdn_ipam_plugin->mock(
135 $pve_sdn_ipams = Test
::MockModule-
>new('PVE::Network::SDN::Ipams');
136 $pve_sdn_ipams->mock(
138 my $ipam_config = read_sdn_config
("$path/ipam_config");
143 my $vnetid = "myvnet";
146 my $test = "add_cidr $cidr1";
147 my $name = "$testid $test";
152 PVE
::Network
::SDN
::Vnets
::add_cidr
($vnetid, $cidr1, $hostname, $mac, $description);
158 is (undef, undef, $name);
162 $test = "add_already_exist_cidr $cidr1";
163 $name = "$testid $test";
168 PVE
::Network
::SDN
::Vnets
::add_cidr
($vnetid, $cidr1, $hostname, $mac, $description);
172 is (undef, undef, $name);
176 is (undef, undef, $name);
180 $test = "add_cidr $cidr2";
181 $name = "$testid $test";
186 PVE
::Network
::SDN
::Vnets
::add_cidr
($vnetid, $cidr2, $hostname, $mac, $description);
192 is (undef, undef, $name);
196 $test = "add_ip_out_of_range_subnets $cidr_outofrange";
197 $name = "$testid $test";
202 PVE
::Network
::SDN
::Vnets
::add_cidr
($vnetid, $cidr_outofrange, $hostname, $mac, $description);
206 is (undef, undef, $name);
212 $test = "add_cidr $cidr4";
213 $name = "$testid $test";
218 PVE
::Network
::SDN
::Vnets
::add_cidr
($vnetid, $cidr4, $hostname, $mac, $description);
224 is (undef, undef, $name);
228 $test = "find_next_free_cidr_in_second_subnet ($cidr3)";
229 $name = "$testid $test";
231 $expected = $ipam ?
$cidr3 : undef;
234 $result = PVE
::Network
::SDN
::Vnets
::get_next_free_cidr
($vnetid, $hostname, $mac, $description, $ipversion);
240 is ($result, $expected, $name);
244 $test = "del_cidr $cidr1";
245 $name = "$testid $test";
250 $result = PVE
::Network
::SDN
::Vnets
::del_cidr
($vnetid, $cidr1, $hostname);
256 is (undef, undef, $name);
259 $test = "del_cidr $cidr3";
260 $name = "$testid $test";
265 $result = PVE
::Network
::SDN
::Vnets
::del_cidr
($vnetid, $cidr3, $hostname);
271 is (undef, undef, $name);
274 $test = "del_cidr not exist $cidr1";
275 $name = "$testid $test";
280 $result = PVE
::Network
::SDN
::Vnets
::del_cidr
($vnetid, $cidr1, $hostname);
284 is (undef, undef, $name);
288 is (undef, undef, $name);
291 $test = "del_cidr outofrange $cidr_outofrange";
292 $name = "$testid $test";
297 $result = PVE
::Network
::SDN
::Vnets
::del_cidr
($vnetid, $cidr_outofrange, $hostname);
301 is (undef, undef, $name);
306 $test = "find_next_free_cidr_in_first_subnet ($cidr1)";
307 $name = "$testid $test";
309 $expected = $ipam ?
$cidr1 : undef;
312 $result = PVE
::Network
::SDN
::Vnets
::get_next_free_cidr
($vnetid, $hostname, $mac, $description, $ipversion);
318 is ($result, $expected, $name);
321 $test = "update_cidr $cidr1";
322 $name = "$testid $test";
327 $result = PVE
::Network
::SDN
::Vnets
::update_cidr
($vnetid, $cidr1, $hostname, $hostname, $mac, $description);
333 is (undef, undef, $name);
336 $test = "update_cidr deleted $cidr3";
337 $name = "$testid $test";
342 $result = PVE
::Network
::SDN
::Vnets
::update_cidr
($vnetid, $cidr1, $hostname, $hostname, $mac, $description);
348 is (undef, undef, $name);