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