]>
Commit | Line | Data |
---|---|---|
718ac053 AD |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use lib qw(..); | |
7 | use File::Slurp; | |
8 | use Net::IP; | |
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 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( './dns/', prefix => 1 ) ; | |
37 | ||
38 | foreach my $path (@plugins) { | |
39 | ||
40 | my (undef, $dnsid) = split(/\//, $path); | |
41 | my $sdn_config = read_sdn_config ("$path/sdn_config"); | |
42 | ||
43 | ||
44 | my $pve_sdn_dns; | |
45 | $pve_sdn_dns = Test::MockModule->new('PVE::Network::SDN::Dns'); | |
46 | $pve_sdn_dns->mock( | |
47 | config => sub { | |
48 | my $dns_config = read_sdn_config ("$path/dns_config"); | |
49 | return $dns_config; | |
50 | }, | |
51 | ); | |
52 | ||
53 | my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); | |
54 | $sdn_module->mock( | |
55 | config => sub { | |
56 | return $sdn_config; | |
57 | }, | |
58 | api_request => sub { | |
59 | my ($method, $url, $headers, $data) = @_; | |
60 | ||
61 | my $js = JSON->new; | |
62 | $js->canonical(1); | |
63 | ||
64 | my $encoded_data = $js->encode($data) if $data; | |
65 | my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); | |
66 | die Dumper($req); | |
67 | } | |
68 | ); | |
69 | ||
70 | ||
71 | ||
72 | my $dns_cfg = PVE::Network::SDN::Dns::config(); | |
73 | my $plugin_config = $dns_cfg->{ids}->{$dnsid}; | |
74 | my $plugin = PVE::Network::SDN::Dns::Plugin->lookup($plugin_config->{type}); | |
75 | ||
76 | #test params; | |
77 | my @ips = ("10.0.0.1", "2001:4860:4860::8888"); | |
78 | my $zone = "domain.com"; | |
79 | my $hostname = "myhostname"; | |
80 | ||
81 | foreach my $ip (@ips) { | |
82 | ||
83 | my $ipversion = Net::IP::ip_is_ipv6($ip) ? "ipv6" : "ipv4"; | |
84 | my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A"; | |
85 | my $ip2 = $type eq 'AAAA' ? '2001:4860:4860::8844' : '127.0.0.1'; | |
86 | my $fqdn = $hostname.".".$zone."."; | |
87 | ||
88 | my $sdn_dns_plugin = Test::MockModule->new($plugin); | |
89 | $sdn_dns_plugin->mock( | |
90 | ||
91 | get_zone_content => sub { | |
92 | return undef; | |
93 | }, | |
94 | get_zone_rrset => sub { | |
95 | return undef; | |
96 | } | |
97 | ); | |
98 | ||
99 | ## add_a_record | |
100 | my $test = "add_a_record"; | |
101 | my $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
102 | my $name = "$dnsid $test"; | |
103 | ||
104 | $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1); | |
105 | ||
106 | if ($@) { | |
107 | is ($@, $expected, $name); | |
108 | } else { | |
109 | fail($name); | |
110 | } | |
111 | ||
112 | ## add_ptr_record | |
113 | $test = "add_ptr_record"; | |
114 | $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
115 | $name = "$dnsid $test"; | |
116 | ||
117 | $plugin->add_ptr_record($plugin_config, $zone, $hostname, $ip, 1); | |
118 | ||
119 | if ($@) { | |
120 | is ($@, $expected, $name); | |
121 | } else { | |
122 | fail($name); | |
123 | } | |
124 | ||
125 | ||
126 | ## del_ptr_record | |
127 | $test = "del_ptr_record"; | |
128 | $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
129 | $name = "$dnsid $test"; | |
130 | ||
131 | $plugin->del_ptr_record($plugin_config, $zone, $ip, 1); | |
132 | ||
133 | if ($@) { | |
134 | is ($@, $expected, $name); | |
135 | } else { | |
136 | fail($name); | |
137 | } | |
138 | ||
139 | ||
140 | ## del_a_record | |
141 | ||
142 | $sdn_dns_plugin->mock( | |
143 | ||
144 | get_zone_content => sub { | |
145 | return undef; | |
146 | }, | |
147 | get_zone_rrset => sub { | |
148 | ||
149 | my $type = Net::IP::ip_is_ipv6($ip) ? "AAAA" : "A"; | |
150 | my $fqdn = $hostname.".".$zone."."; | |
151 | my $record = { content => $ip, | |
152 | disabled => JSON::false, | |
153 | name => $fqdn, | |
154 | type => $type, | |
155 | priority => 0 }; | |
156 | ||
157 | my $rrset = { name => $fqdn, | |
158 | type => $type, | |
159 | ttl => '3600', | |
160 | records => [ $record ] }; | |
161 | return $rrset; | |
162 | } | |
163 | ); | |
164 | ||
165 | $test = "del_a_record"; | |
166 | $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
167 | $name = "$dnsid $test"; | |
168 | ||
169 | $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1); | |
170 | ||
171 | if ($@) { | |
172 | is ($@, $expected, $name); | |
173 | } else { | |
174 | fail($name); | |
175 | } | |
176 | ||
177 | ## del_a_multiple_record | |
178 | ||
179 | $sdn_dns_plugin->mock( | |
180 | ||
181 | get_zone_content => sub { | |
182 | return undef; | |
183 | }, | |
184 | get_zone_rrset => sub { | |
185 | ||
186 | my $record = { content => $ip, | |
187 | disabled => JSON::false, | |
188 | name => $fqdn, | |
189 | type => $type, | |
190 | priority => 0 }; | |
191 | ||
192 | my $record2 = { content => $ip2, | |
193 | disabled => JSON::false, | |
194 | name => $fqdn, | |
195 | type => $type, | |
196 | priority => 0 }; | |
197 | ||
198 | my $rrset = { name => $fqdn, | |
199 | type => $type, | |
200 | ttl => '3600', | |
201 | records => [ $record, $record2 ] }; | |
202 | return $rrset; | |
203 | } | |
204 | ); | |
205 | ||
206 | $test = "del_a_multiple_record"; | |
207 | $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
208 | $name = "$dnsid $test"; | |
209 | ||
210 | $plugin->del_a_record($plugin_config, $zone, $hostname, $ip, 1); | |
211 | ||
212 | if ($@) { | |
213 | is ($@, $expected, $name); | |
214 | } else { | |
215 | fail($name); | |
216 | } | |
217 | ||
218 | ## add_a_multiple_record | |
219 | ||
220 | $sdn_dns_plugin->mock( | |
221 | ||
222 | get_zone_content => sub { | |
223 | return undef; | |
224 | }, | |
225 | get_zone_rrset => sub { | |
226 | ||
227 | my $record2 = { content => $ip2, | |
228 | disabled => JSON::false, | |
229 | name => $fqdn, | |
230 | type => $type, | |
231 | priority => 0 }; | |
232 | ||
233 | my $rrset = { name => $fqdn, | |
234 | type => $type, | |
235 | ttl => '3600', | |
236 | records => [ $record2 ] }; | |
237 | return $rrset; | |
238 | } | |
239 | ); | |
240 | ||
241 | $test = "add_a_multiple_record"; | |
242 | $expected = Dumper read_sdn_config("$path/expected.$test.$ipversion"); | |
243 | $name = "$dnsid $test"; | |
244 | ||
245 | $plugin->add_a_record($plugin_config, $zone, $hostname, $ip, 1); | |
246 | ||
247 | if ($@) { | |
248 | is ($@, $expected, $name); | |
249 | } else { | |
250 | fail($name); | |
251 | } | |
252 | } | |
253 | ||
254 | ## verify_zone | |
255 | my $test = "verify_zone"; | |
256 | my $expected = Dumper read_sdn_config("$path/expected.$test"); | |
257 | my $name = "$dnsid $test"; | |
258 | ||
259 | $plugin->verify_zone($plugin_config, $zone, 1); | |
260 | ||
261 | if ($@) { | |
262 | is ($@, $expected, $name); | |
263 | } else { | |
264 | fail($name); | |
265 | } | |
266 | ||
267 | } | |
268 | ||
269 | done_testing(); | |
270 | ||
271 |