]>
Commit | Line | Data |
---|---|---|
5221635a AD |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use lib qw(..); | |
7 | use File::Slurp; | |
8 | ||
9 | use Test::More; | |
10 | use Test::MockModule; | |
11 | ||
12 | use PVE::Network::SDN; | |
13 | use PVE::Network::SDN::Zones; | |
14 | use PVE::Network::SDN::Controllers; | |
15 | use PVE::INotify; | |
16 | use JSON; | |
17 | ||
18 | use Data::Dumper qw(Dumper); | |
19 | $Data::Dumper::Sortkeys = 1; | |
20 | ||
21 | sub read_sdn_config { | |
22 | my ($file) = @_; | |
23 | # Read structure back in again | |
24 | open my $in, '<', $file or die $!; | |
25 | my $sdn_config; | |
26 | { | |
27 | local $/; # slurp mode | |
28 | $sdn_config = eval <$in>; | |
29 | } | |
30 | close $in; | |
31 | ||
32 | return $sdn_config; | |
33 | } | |
34 | ||
35 | ||
36 | my @plugins = read_dir( './subnets/', prefix => 1 ) ; | |
37 | ||
38 | foreach my $path (@plugins) { | |
39 | ||
40 | my (undef, $testid) = split(/\//, $path); | |
41 | ||
bf8761e9 | 42 | print "test: $testid\n"; |
5221635a AD |
43 | my $sdn_config = read_sdn_config ("$path/sdn_config"); |
44 | ||
45 | ||
46 | my $pve_sdn_subnets; | |
47 | $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); | |
48 | $pve_sdn_subnets->mock( | |
49 | config => sub { | |
50 | return $sdn_config->{subnets}; | |
51 | }, | |
52 | verify_dns_zone => sub { | |
53 | return; | |
54 | }, | |
55 | add_dns_record => sub { | |
56 | return; | |
57 | } | |
58 | ); | |
59 | ||
60 | ||
61 | my $js = JSON->new; | |
62 | $js->canonical(1); | |
63 | ||
64 | ||
65 | #test params; | |
66 | my $subnets = $sdn_config->{subnets}->{ids}; | |
67 | my $subnetid = (keys %{$subnets})[0]; | |
68 | my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1); | |
69 | ||
70 | my $subnet_cidr = $subnet->{cidr}; | |
71 | my $iplist = NetAddr::IP->new($subnet_cidr); | |
3c0fc96d | 72 | $iplist++ if Net::IP::ip_is_ipv4($iplist->canon()); #skip network address for ipv4 |
5221635a AD |
73 | my $ip = $iplist->canon(); |
74 | $iplist++; | |
75 | my $ipnextfree = $iplist->canon(); | |
76 | $iplist++; | |
77 | my $ip2 = $iplist->canon(); | |
78 | ||
79 | my $ip3 = undef; | |
80 | my $hostname = "myhostname"; | |
81 | my $mac = "da:65:8f:18:9b:6f"; | |
82 | my $description = "mydescription"; | |
83 | my $is_gateway = 1; | |
84 | my $ipamdb = {}; | |
85 | ||
86 | my $zone = $sdn_config->{zones}->{ids}->{"myzone"}; | |
bf8761e9 AD |
87 | my $ipam = $zone->{ipam}; |
88 | ||
89 | my $plugin; | |
90 | my $sdn_ipam_plugin; | |
91 | if($ipam) { | |
92 | $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam); | |
93 | $sdn_ipam_plugin = Test::MockModule->new($plugin); | |
94 | $sdn_ipam_plugin->mock( | |
95 | read_db => sub { | |
96 | return $ipamdb; | |
97 | }, | |
98 | write_db => sub { | |
99 | my ($cfg) = @_; | |
100 | $ipamdb = $cfg; | |
101 | } | |
102 | ); | |
103 | } | |
5221635a | 104 | |
bf8761e9 AD |
105 | my $pve_sdn_ipams; |
106 | $pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams'); | |
107 | $pve_sdn_ipams->mock( | |
108 | config => sub { | |
109 | my $ipam_config = read_sdn_config ("$path/ipam_config"); | |
110 | return $ipam_config; | |
5221635a | 111 | }, |
5221635a AD |
112 | ); |
113 | ||
114 | ## add_subnet | |
3c0fc96d | 115 | my $test = "add_subnet $subnetid"; |
5221635a AD |
116 | my $name = "$testid $test"; |
117 | my $result = undef; | |
118 | my $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{}}}}}}'; | |
119 | ||
120 | eval { | |
bf8761e9 AD |
121 | PVE::Network::SDN::Subnets::add_subnet($zone, $subnetid, $subnet); |
122 | ||
5221635a AD |
123 | }; |
124 | ||
125 | if ($@) { | |
bf8761e9 AD |
126 | fail("$name : $@"); |
127 | } elsif($ipam) { | |
5221635a AD |
128 | $result = $js->encode($plugin->read_db()); |
129 | is ($result, $expected, $name); | |
bf8761e9 AD |
130 | } else { |
131 | is (undef, undef, $name); | |
5221635a AD |
132 | } |
133 | ||
134 | ## add_ip | |
3c0fc96d | 135 | $test = "add_ip $ip"; |
5221635a AD |
136 | $name = "$testid $test"; |
137 | $result = undef; | |
34c4c6d7 | 138 | $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1}}}}}}}'; |
5221635a AD |
139 | |
140 | eval { | |
34c4c6d7 | 141 | PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway); |
5221635a AD |
142 | }; |
143 | ||
144 | if ($@) { | |
145 | fail("$name : $@"); | |
bf8761e9 | 146 | } elsif($ipam) { |
5221635a AD |
147 | $result = $js->encode($plugin->read_db()); |
148 | is ($result, $expected, $name); | |
bf8761e9 AD |
149 | } else { |
150 | is (undef, undef, $name); | |
5221635a AD |
151 | } |
152 | ||
bf8761e9 AD |
153 | if($ipam) { |
154 | ## add_already_exist_ip | |
3c0fc96d | 155 | $test = "add_already_exist_ip $ip"; |
bf8761e9 | 156 | $name = "$testid $test"; |
5221635a | 157 | |
bf8761e9 AD |
158 | eval { |
159 | PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description); | |
160 | }; | |
5221635a | 161 | |
bf8761e9 AD |
162 | if ($@) { |
163 | is (undef, undef, $name); | |
164 | } else { | |
165 | fail("$name : $@"); | |
166 | } | |
5221635a AD |
167 | } |
168 | ||
169 | ## add_second_ip | |
3c0fc96d | 170 | $test = "add_second_ip $ip2"; |
5221635a AD |
171 | $name = "$testid $test"; |
172 | $result = undef; | |
34c4c6d7 | 173 | $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1},"'.$ip2.'":{}}}}}}}'; |
5221635a AD |
174 | |
175 | eval { | |
176 | PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip2, $hostname, $mac, $description); | |
177 | }; | |
178 | ||
179 | if ($@) { | |
180 | fail("$name : $@"); | |
bf8761e9 | 181 | } elsif($ipam) { |
5221635a AD |
182 | $result = $js->encode($plugin->read_db()); |
183 | is ($result, $expected, $name); | |
bf8761e9 AD |
184 | } else { |
185 | is (undef, undef, $name); | |
5221635a AD |
186 | } |
187 | ||
5221635a | 188 | ## add_next_free |
3c0fc96d | 189 | $test = "find_next_freeip ($ipnextfree)"; |
5221635a AD |
190 | $name = "$testid $test"; |
191 | $result = undef; | |
34c4c6d7 | 192 | $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ip.'":{"gateway":1},"'.$ipnextfree.'":{},"'.$ip2.'":{}}}}}}}'; |
5221635a AD |
193 | |
194 | eval { | |
195 | $ip3 = PVE::Network::SDN::Subnets::next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $description); | |
196 | }; | |
197 | ||
198 | if ($@) { | |
199 | fail("$name : $@"); | |
bf8761e9 | 200 | } elsif($ipam) { |
5221635a AD |
201 | $result = $js->encode($plugin->read_db()); |
202 | is ($result, $expected, $name); | |
203 | } | |
204 | ||
205 | ## del_ip | |
3c0fc96d | 206 | $test = "del_ip $ip"; |
5221635a AD |
207 | $name = "$testid $test"; |
208 | $result = undef; | |
209 | $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ipnextfree.'":{},"'.$ip2.'":{}}}}}}}'; | |
210 | ||
211 | eval { | |
212 | PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname); | |
213 | }; | |
214 | ||
215 | if ($@) { | |
216 | fail("$name : $@"); | |
bf8761e9 | 217 | } elsif($ipam) { |
5221635a AD |
218 | $result = $js->encode($plugin->read_db()); |
219 | is ($result, $expected, $name); | |
5221635a | 220 | } else { |
bf8761e9 | 221 | is (undef, undef, $name); |
5221635a AD |
222 | } |
223 | ||
bf8761e9 AD |
224 | if($ipam){ |
225 | ## del_subnet_not_empty | |
3c0fc96d | 226 | $test = "del_subnet_not_empty $subnetid"; |
bf8761e9 AD |
227 | $name = "$testid $test"; |
228 | $result = undef; | |
229 | $expected = undef; | |
230 | ||
231 | eval { | |
232 | PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet); | |
233 | }; | |
234 | ||
235 | if ($@) { | |
236 | is ($result, $expected, $name); | |
237 | } else { | |
238 | fail("$name : $@"); | |
239 | } | |
240 | } | |
5221635a AD |
241 | |
242 | ||
243 | ## add_ip_rollback_failing_dns | |
244 | $test = "add_ip_rollback_failing_dns"; | |
245 | ||
246 | $pve_sdn_subnets->mock( | |
247 | config => sub { | |
248 | return $sdn_config->{subnets}; | |
249 | }, | |
250 | verify_dns_zone => sub { | |
251 | return; | |
252 | }, | |
253 | add_dns_record => sub { | |
254 | die "error add dns record"; | |
255 | return; | |
256 | } | |
257 | ); | |
258 | ||
259 | $name = "$testid $test"; | |
260 | $result = undef; | |
261 | $expected = '{"zones":{"myzone":{"subnets":{"'.$subnet_cidr.'":{"ips":{"'.$ipnextfree.'":{},"'.$ip2.'":{}}}}}}}'; | |
262 | ||
263 | eval { | |
264 | PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $description); | |
265 | }; | |
266 | ||
267 | if ($@) { | |
bf8761e9 AD |
268 | if($ipam) { |
269 | $result = $js->encode($plugin->read_db()); | |
270 | is ($result, $expected, $name); | |
271 | } else { | |
272 | is (undef, undef, $name); | |
273 | } | |
5221635a AD |
274 | } else { |
275 | fail("$name : $@"); | |
276 | } | |
277 | ||
278 | ||
279 | ## del_empty_subnet | |
280 | $test = "del_empty_subnet"; | |
281 | $name = "$testid $test"; | |
282 | $result = undef; | |
283 | $expected = '{"zones":{"myzone":{"subnets":{}}}}'; | |
284 | ||
285 | PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip2, $hostname); | |
286 | PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip3, $hostname); | |
287 | ||
288 | eval { | |
289 | PVE::Network::SDN::Subnets::del_subnet($zone, $subnetid, $subnet); | |
290 | }; | |
291 | ||
292 | if ($@) { | |
293 | fail("$name : $@"); | |
bf8761e9 | 294 | } elsif($ipam) { |
5221635a AD |
295 | $result = $js->encode($plugin->read_db()); |
296 | is ($result, $expected, $name); | |
bf8761e9 AD |
297 | } else { |
298 | is (undef, undef, $name); | |
5221635a AD |
299 | } |
300 | ||
301 | } | |
302 | ||
303 | done_testing(); | |
304 | ||
305 |