]> git.proxmox.com Git - pve-network.git/blob - test/run_test_ipams.pl
controllers: evpn : use frr restart if reload fail
[pve-network.git] / test / run_test_ipams.pl
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 = <./ipams/*>;
37 my @plugins = read_dir( './ipams/', prefix => 1 ) ;
38
39 foreach my $path (@plugins) {
40
41 my (undef, $ipamid) = split(/\//, $path);
42 my $sdn_config = read_sdn_config ("$path/sdn_config");
43
44
45 my $pve_sdn_subnets;
46 $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets');
47 $pve_sdn_subnets->mock(
48 config => sub {
49 return $sdn_config->{subnets};
50 },
51 );
52
53 my $pve_sdn_ipam;
54 $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Ipams');
55 $pve_sdn_subnets->mock(
56 config => sub {
57 my $ipam_config = read_sdn_config ("$path/ipam_config");
58 return $ipam_config;
59 },
60 );
61
62 my $sdn_module = Test::MockModule->new("PVE::Network::SDN");
63 $sdn_module->mock(
64 config => sub {
65 return $sdn_config;
66 },
67 api_request => sub {
68 my ($method, $url, $headers, $data) = @_;
69
70 my $js = JSON->new;
71 $js->canonical(1);
72
73 my $encoded_data = $js->encode($data) if $data;
74 my $req = HTTP::Request->new($method,$url, $headers, $encoded_data);
75 die Dumper($req);
76 }
77 );
78
79
80
81 #test params;
82 my $subnetid = "myzone-10.0.0.0-24";
83 my $ip = "10.0.0.1";
84 my $hostname = "myhostname";
85 my $mac = "da:65:8f:18:9b:6f";
86 my $description = "mydescription";
87 my $is_gateway = 1;
88
89
90 my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($sdn_config->{subnets}, $subnetid, 1);
91
92 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
93 my $plugin_config = $ipam_cfg->{ids}->{$ipamid};
94 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
95 my $sdn_ipam_plugin = Test::MockModule->new($plugin);
96 $sdn_ipam_plugin->mock(
97 get_prefix_id => sub {
98 return 1;
99 },
100 get_ip_id => sub {
101 return 1;
102 },
103 is_ip_gateway => sub {
104 return 1;
105 }
106 );
107
108 ## add_ip
109 my $test = "add_ip";
110 my $expected = Dumper read_sdn_config("$path/expected.$test");
111 my $name = "$ipamid $test";
112
113 $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1);
114
115 if ($@) {
116 is ($@, $expected, $name);
117 } else {
118 fail($name);
119 }
120
121 ## add_next_freeip
122 $test = "add_next_freeip";
123 $expected = Dumper read_sdn_config("$path/expected.$test");
124 $name = "$ipamid $test";
125
126 $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1);
127
128 if ($@) {
129 is ($@, $expected, $name);
130 } else {
131 fail($name);
132 }
133
134
135 ## del_ip
136 $test = "del_ip";
137 $expected = Dumper read_sdn_config("$path/expected.$test");
138 $name = "$ipamid $test";
139
140 $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1);
141
142 if ($@) {
143 is ($@, $expected, $name);
144 } else {
145 fail($name);
146 }
147
148 ## update_ip
149 $test = "update_ip";
150 $expected = Dumper read_sdn_config("$path/expected.$test");
151 $name = "$ipamid $test";
152 $plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1);
153
154 if ($@) {
155 is ($@, $expected, $name);
156 } else {
157 fail($name);
158 }
159
160 ## add_ip_notgateway
161 $is_gateway = undef;
162 $test = "add_ip_notgateway";
163 $expected = Dumper read_sdn_config("$path/expected.$test");
164 $name = "$ipamid $test";
165
166 $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1);
167
168 if ($@) {
169 is ($@, $expected, $name);
170 } else {
171 fail($name);
172 }
173
174 $sdn_ipam_plugin->mock(
175 get_prefix_id => sub {
176 return undef;
177 },
178 );
179
180 ## add_subnet
181 $test = "add_subnet";
182 $expected = Dumper read_sdn_config("$path/expected.$test");
183 $name = "$ipamid $test";
184
185 $plugin->add_subnet($plugin_config, $subnetid, $subnet, 1);
186
187 if ($@) {
188 is ($@, $expected, $name);
189 } else {
190 fail($name);
191 }
192
193 }
194
195 done_testing();
196
197