Commit | Line | Data |
---|---|---|
3c0fc96d AD |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use lib qw(..); | |
7 | use File::Slurp; | |
8 | use NetAddr::IP qw(:lower); | |
9 | ||
10 | use Test::More; | |
11 | use Test::MockModule; | |
12 | ||
13 | use PVE::Network::SDN; | |
14 | use PVE::Network::SDN::Zones; | |
15 | use PVE::Network::SDN::Controllers; | |
16 | use PVE::INotify; | |
17 | use JSON; | |
18 | ||
19 | use Data::Dumper qw(Dumper); | |
20 | $Data::Dumper::Sortkeys = 1; | |
21 | ||
22 | sub read_sdn_config { | |
23 | my ($file) = @_; | |
24 | # Read structure back in again | |
25 | open my $in, '<', $file or die $!; | |
26 | my $sdn_config; | |
27 | { | |
28 | local $/; # slurp mode | |
29 | $sdn_config = eval <$in>; | |
30 | } | |
31 | close $in; | |
32 | return $sdn_config; | |
33 | } | |
34 | ||
35 | ||
36 | my @plugins = read_dir( './vnets/', prefix => 1 ) ; | |
37 | ||
38 | foreach my $path (@plugins) { | |
39 | ||
40 | my (undef, $testid) = split(/\//, $path); | |
41 | ||
42 | print "test: $testid\n"; | |
43 | my $sdn_config = read_sdn_config ("$path/sdn_config"); | |
44 | ||
45 | my $pve_sdn_zones; | |
46 | $pve_sdn_zones = Test::MockModule->new('PVE::Network::SDN::Zones'); | |
47 | $pve_sdn_zones->mock( | |
48 | config => sub { | |
49 | return $sdn_config->{zones}; | |
50 | }, | |
51 | ); | |
52 | ||
53 | my $pve_sdn_vnets; | |
54 | $pve_sdn_vnets = Test::MockModule->new('PVE::Network::SDN::Vnets'); | |
55 | $pve_sdn_vnets->mock( | |
56 | config => sub { | |
57 | return $sdn_config->{vnets}; | |
58 | }, | |
59 | ); | |
60 | ||
61 | my $pve_sdn_subnets; | |
62 | $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); | |
63 | $pve_sdn_subnets->mock( | |
64 | config => sub { | |
65 | return $sdn_config->{subnets}; | |
66 | }, | |
67 | verify_dns_zone => sub { | |
68 | return; | |
69 | }, | |
70 | add_dns_record => sub { | |
71 | return; | |
72 | } | |
73 | ); | |
74 | ||
75 | my $js = JSON->new; | |
76 | $js->canonical(1); | |
77 | ||
78 | #test params; | |
79 | #test params; | |
80 | my $subnets = $sdn_config->{subnets}->{ids}; | |
81 | ||
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; | |
88 | ||
89 | if (Net::IP::ip_is_ipv4($iplist->canon())){ | |
90 | $iplist++; #skip network address for ipv4 | |
91 | $ipversion = 4; | |
92 | } else { | |
93 | $ipversion = 6; | |
94 | } | |
95 | ||
96 | my $cidr1 = $iplist->canon()."/$mask"; | |
97 | $iplist++; | |
98 | my $cidr2 = $iplist->canon()."/$mask"; | |
99 | my $cidr_outofrange = '8.8.8.8/8'; | |
100 | ||
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); | |
105 | $iplist2++; | |
106 | my $cidr3 = $iplist2->canon()."/$mask"; | |
107 | $iplist2++; | |
108 | my $cidr4 = $iplist2->canon()."/$mask"; | |
109 | ||
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"); | |
114 | ||
115 | my $zone = $sdn_config->{zones}->{ids}->{"myzone"}; | |
116 | my $ipam = $zone->{ipam}; | |
117 | ||
118 | my $plugin; | |
119 | my $sdn_ipam_plugin; | |
120 | if($ipam) { | |
121 | $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($ipam); | |
122 | $sdn_ipam_plugin = Test::MockModule->new($plugin); | |
123 | $sdn_ipam_plugin->mock( | |
124 | read_db => sub { | |
125 | return $ipamdb; | |
126 | }, | |
127 | write_db => sub { | |
128 | my ($cfg) = @_; | |
129 | $ipamdb = $cfg; | |
130 | } | |
131 | ); | |
132 | } | |
133 | ||
134 | my $pve_sdn_ipams; | |
135 | $pve_sdn_ipams = Test::MockModule->new('PVE::Network::SDN::Ipams'); | |
136 | $pve_sdn_ipams->mock( | |
137 | config => sub { | |
138 | my $ipam_config = read_sdn_config ("$path/ipam_config"); | |
139 | return $ipam_config; | |
140 | }, | |
141 | ); | |
142 | ||
143 | my $vnetid = "myvnet"; | |
144 | ||
145 | ## add_ip | |
146 | my $test = "add_cidr $cidr1"; | |
147 | my $name = "$testid $test"; | |
148 | my $result = undef; | |
149 | my $expected = ''; | |
150 | ||
151 | eval { | |
152 | PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); | |
153 | }; | |
154 | ||
155 | if ($@) { | |
156 | fail("$name : $@"); | |
157 | } else { | |
158 | is (undef, undef, $name); | |
159 | } | |
160 | ||
161 | ## add_ip | |
162 | $test = "add_already_exist_cidr $cidr1"; | |
163 | $name = "$testid $test"; | |
164 | $result = undef; | |
165 | $expected = ''; | |
166 | ||
167 | eval { | |
168 | PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr1, $hostname, $mac, $description); | |
169 | }; | |
170 | ||
171 | if ($@) { | |
172 | is (undef, undef, $name); | |
173 | } elsif($ipam) { | |
174 | fail("$name : $@"); | |
175 | } else { | |
176 | is (undef, undef, $name); | |
177 | } | |
178 | ||
179 | ## add_ip | |
180 | $test = "add_cidr $cidr2"; | |
181 | $name = "$testid $test"; | |
182 | $result = undef; | |
183 | $expected = ''; | |
184 | ||
185 | eval { | |
186 | PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr2, $hostname, $mac, $description); | |
187 | }; | |
188 | ||
189 | if ($@) { | |
190 | fail("$name : $@"); | |
191 | } else { | |
192 | is (undef, undef, $name); | |
193 | } | |
194 | ||
195 | ## add_ip | |
196 | $test = "add_ip_out_of_range_subnets $cidr_outofrange"; | |
197 | $name = "$testid $test"; | |
198 | $result = undef; | |
199 | $expected = ''; | |
200 | ||
201 | eval { | |
202 | PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr_outofrange, $hostname, $mac, $description); | |
203 | }; | |
204 | ||
205 | if ($@) { | |
206 | is (undef, undef, $name); | |
207 | } else { | |
208 | fail("$name : $@"); | |
209 | } | |
210 | ||
211 | ## add_ip | |
212 | $test = "add_cidr $cidr4"; | |
213 | $name = "$testid $test"; | |
214 | $result = undef; | |
215 | $expected = ''; | |
216 | ||
217 | eval { | |
218 | PVE::Network::SDN::Vnets::add_cidr($vnetid, $cidr4, $hostname, $mac, $description); | |
219 | }; | |
220 | ||
221 | if ($@) { | |
222 | fail("$name : $@"); | |
223 | } else { | |
224 | is (undef, undef, $name); | |
225 | } | |
226 | ||
227 | ||
228 | $test = "find_next_free_cidr_in_second_subnet ($cidr3)"; | |
229 | $name = "$testid $test"; | |
230 | $result = undef; | |
231 | $expected = $ipam ? $cidr3 : undef; | |
232 | ||
233 | eval { | |
234 | $result = PVE::Network::SDN::Vnets::get_next_free_cidr($vnetid, $hostname, $mac, $description, $ipversion); | |
235 | }; | |
236 | ||
237 | if ($@) { | |
238 | fail("$name : $@"); | |
239 | } else { | |
240 | is ($result, $expected, $name); | |
241 | } | |
242 | ||
243 | ||
244 | $test = "del_cidr $cidr1"; | |
245 | $name = "$testid $test"; | |
246 | $result = undef; | |
247 | $expected = undef; | |
248 | ||
249 | eval { | |
250 | $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); | |
251 | }; | |
252 | ||
253 | if ($@) { | |
254 | fail("$name : $@"); | |
255 | } else { | |
256 | is (undef, undef, $name); | |
257 | } | |
258 | ||
259 | $test = "del_cidr $cidr3"; | |
260 | $name = "$testid $test"; | |
261 | $result = undef; | |
262 | $expected = undef; | |
263 | ||
264 | eval { | |
265 | $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr3, $hostname); | |
266 | }; | |
267 | ||
268 | if ($@) { | |
269 | fail("$name : $@"); | |
270 | } else { | |
271 | is (undef, undef, $name); | |
272 | } | |
273 | ||
274 | $test = "del_cidr not exist $cidr1"; | |
275 | $name = "$testid $test"; | |
276 | $result = undef; | |
277 | $expected = undef; | |
278 | ||
279 | eval { | |
280 | $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr1, $hostname); | |
281 | }; | |
282 | ||
283 | if ($@) { | |
284 | is (undef, undef, $name); | |
285 | } elsif($ipam) { | |
286 | fail("$name : $@"); | |
287 | } else { | |
288 | is (undef, undef, $name); | |
289 | } | |
290 | ||
291 | $test = "del_cidr outofrange $cidr_outofrange"; | |
292 | $name = "$testid $test"; | |
293 | $result = undef; | |
294 | $expected = undef; | |
295 | ||
296 | eval { | |
297 | $result = PVE::Network::SDN::Vnets::del_cidr($vnetid, $cidr_outofrange, $hostname); | |
298 | }; | |
299 | ||
300 | if ($@) { | |
301 | is (undef, undef, $name); | |
302 | } else { | |
303 | fail("$name : $@"); | |
304 | } | |
305 | ||
306 | $test = "find_next_free_cidr_in_first_subnet ($cidr1)"; | |
307 | $name = "$testid $test"; | |
308 | $result = undef; | |
309 | $expected = $ipam ? $cidr1 : undef; | |
310 | ||
311 | eval { | |
312 | $result = PVE::Network::SDN::Vnets::get_next_free_cidr($vnetid, $hostname, $mac, $description, $ipversion); | |
313 | }; | |
314 | ||
315 | if ($@) { | |
316 | fail("$name : $@"); | |
317 | } else { | |
318 | is ($result, $expected, $name); | |
319 | } | |
320 | ||
321 | $test = "update_cidr $cidr1"; | |
322 | $name = "$testid $test"; | |
323 | $result = undef; | |
324 | $expected = undef; | |
325 | ||
326 | eval { | |
327 | $result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac, $description); | |
328 | }; | |
329 | ||
330 | if ($@) { | |
331 | fail("$name : $@"); | |
332 | } else { | |
333 | is (undef, undef, $name); | |
334 | } | |
335 | ||
336 | $test = "update_cidr deleted $cidr3"; | |
337 | $name = "$testid $test"; | |
338 | $result = undef; | |
339 | $expected = undef; | |
340 | ||
341 | eval { | |
342 | $result = PVE::Network::SDN::Vnets::update_cidr($vnetid, $cidr1, $hostname, $hostname, $mac, $description); | |
343 | }; | |
344 | ||
345 | if ($@) { | |
346 | fail("$name : $@"); | |
347 | } else { | |
348 | is (undef, undef, $name); | |
349 | } | |
350 | ||
351 | } | |
352 | ||
353 | done_testing(); | |
354 | ||
355 |