]> git.proxmox.com Git - pve-network.git/blob - test/run_test_ipams.pl
qinq: code cleanup and whitespace/indendation fixes
[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 );
104
105 ## add_ip
106 my $test = "add_ip";
107 my $expected = Dumper read_sdn_config("$path/expected.$test");
108 my $name = "$ipamid $test";
109
110 $plugin->add_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1);
111
112 if ($@) {
113 is ($@, $expected, $name);
114 } else {
115 fail($name);
116 }
117
118 ## add_next_freeip
119 $test = "add_next_freeip";
120 $expected = Dumper read_sdn_config("$path/expected.$test");
121 $name = "$ipamid $test";
122
123 $plugin->add_next_freeip($plugin_config, $subnetid, $subnet, $hostname, $mac, $description, 1);
124
125 if ($@) {
126 is ($@, $expected, $name);
127 } else {
128 fail($name);
129 }
130
131
132 ## del_ip
133 $test = "del_ip";
134 $expected = Dumper read_sdn_config("$path/expected.$test");
135 $name = "$ipamid $test";
136
137 $plugin->del_ip($plugin_config, $subnetid, $subnet, $ip, 1);
138
139 if ($@) {
140 is ($@, $expected, $name);
141 } else {
142 fail($name);
143 }
144
145 ## update_ip
146 $test = "update_ip";
147 $expected = Dumper read_sdn_config("$path/expected.$test");
148 $name = "$ipamid $test";
149
150 $plugin->update_ip($plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway, 1);
151
152 if ($@) {
153 is ($@, $expected, $name);
154 } else {
155 fail($name);
156 }
157
158 $sdn_ipam_plugin->mock(
159 get_prefix_id => sub {
160 return undef;
161 },
162 );
163
164 ## add_subnet
165 $test = "add_subnet";
166 $expected = Dumper read_sdn_config("$path/expected.$test");
167 $name = "$ipamid $test";
168
169 $plugin->add_subnet($plugin_config, $subnetid, $subnet, 1);
170
171 if ($@) {
172 is ($@, $expected, $name);
173 } else {
174 fail($name);
175 }
176
177 }
178
179 done_testing();
180
181