]>
Commit | Line | Data |
---|---|---|
82f6b09f 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 | ||
17 | sub read_sdn_config { | |
18 | my ($file) = @_; | |
19 | ||
20 | # Read structure back in again | |
21 | open my $in, '<', $file or die $!; | |
22 | my $sdn_config; | |
23 | { | |
65723efd | 24 | local $/; # slurp mode |
82f6b09f AD |
25 | $sdn_config = eval <$in>; |
26 | } | |
27 | close $in; | |
28 | ||
29 | return $sdn_config; | |
30 | } | |
31 | ||
82f6b09f AD |
32 | my @tests = grep { -d } glob './zones/*/*'; |
33 | ||
34 | foreach my $test (@tests) { | |
35 | ||
ddd3d0f7 | 36 | my $sdn_config = read_sdn_config("./$test/sdn_config"); |
82f6b09f | 37 | |
1ed61845 | 38 | open(my $fh1, '<', "./$test/interfaces") or die "can't read interfaces file - $!"; |
82f6b09f AD |
39 | my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef); |
40 | close $fh1; | |
41 | ||
42 | my $pve_common_inotify; | |
43 | $pve_common_inotify = Test::MockModule->new('PVE::INotify'); | |
44 | $pve_common_inotify->mock( | |
45 | nodename => sub { | |
46 | return 'localhost'; | |
47 | }, | |
48 | read_file => sub { | |
1c3f1a7e | 49 | # HACK this assumes we are always calling PVE::INotify::read_file('interfaces'); |
82f6b09f AD |
50 | return $interfaces_config; |
51 | }, | |
1c3f1a7e SL |
52 | read_etc_network_interfaces => sub { |
53 | return $interfaces_config; | |
54 | }, | |
55 | ); | |
56 | ||
57 | my $mocked_pve_sdn_controllers; | |
58 | $mocked_pve_sdn_controllers = Test::MockModule->new('PVE::Network::SDN::Controllers'); | |
59 | $mocked_pve_sdn_controllers->mock( | |
60 | read_etc_network_interfaces => sub { | |
61 | return $interfaces_config; | |
62 | } | |
82f6b09f AD |
63 | ); |
64 | ||
65 | my $pve_sdn_subnets; | |
66 | $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); | |
67 | $pve_sdn_subnets->mock( | |
68 | config => sub { | |
69 | return $sdn_config->{subnets}; | |
70 | }, | |
71 | ); | |
72 | ||
73 | my $pve_sdn_zones_plugin; | |
74 | $pve_sdn_zones_plugin = Test::MockModule->new('PVE::Network::SDN::Zones::Plugin'); | |
75 | $pve_sdn_zones_plugin->mock( | |
76 | get_local_route_ip => sub { | |
77 | my $outiface = "vmbr0"; | |
78 | my $outip = $interfaces_config->{ifaces}->{$outiface}->{address}; | |
79 | return ($outip, $outiface); | |
80 | }, | |
81 | is_vlanaware => sub { | |
82 | return $interfaces_config->{ifaces}->{vmbr0}->{'bridge_vlan_aware'}; | |
83 | }, | |
84 | is_ovs => sub { | |
85 | return 1 if $interfaces_config->{ifaces}->{vmbr0}->{'type'} eq 'OVSBridge'; | |
86 | }, | |
87 | get_bridge_ifaces => sub { | |
88 | return ('eth0'); | |
89 | }, | |
90 | find_bridge => sub { | |
91 | return; | |
ddd3d0f7 | 92 | }, |
82f6b09f AD |
93 | ); |
94 | ||
95 | my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); | |
96 | $sdn_module->mock( | |
d73c7c36 | 97 | running_config => sub { |
82f6b09f AD |
98 | return $sdn_config; |
99 | }, | |
100 | ); | |
101 | ||
7638f253 AD |
102 | my $pve_sdn_controllers_plugin; |
103 | $pve_sdn_controllers_plugin = Test::MockModule->new('PVE::Network::SDN::Controllers::Plugin'); | |
104 | $pve_sdn_controllers_plugin->mock( | |
105 | read_iface_mac => sub { | |
106 | return "bc:24:11:1d:69:60"; | |
107 | }, | |
108 | ); | |
109 | ||
1c3f1a7e SL |
110 | my ($first_plugin) = %{$sdn_config->{controllers}->{ids}} if defined $sdn_config->{controllers}; |
111 | if ($first_plugin) { | |
112 | my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup( | |
113 | $sdn_config->{controllers}->{ids}->{$first_plugin}->{type} | |
114 | ); | |
115 | my $mocked_controller_plugin = Test::MockModule->new($controller_plugin); | |
116 | $mocked_controller_plugin->mock( | |
117 | write_controller_config => sub { | |
118 | return; | |
119 | }, | |
120 | reload_controller => sub { | |
121 | return; | |
122 | }, | |
123 | read_local_frr_config => sub { | |
124 | return; | |
125 | }, | |
126 | ); | |
127 | } | |
128 | ||
82f6b09f AD |
129 | my $name = $test; |
130 | my $expected = read_file("./$test/expected_sdn_interfaces"); | |
131 | ||
764dba50 | 132 | my $result = eval { PVE::Network::SDN::Zones::generate_etc_network_config() }; |
82f6b09f AD |
133 | |
134 | if (my $err = $@) { | |
677e12ca | 135 | diag("got unexpected error - $err"); |
82f6b09f AD |
136 | fail($name); |
137 | } else { | |
ddd3d0f7 | 138 | is($result, $expected, $name); |
82f6b09f AD |
139 | } |
140 | ||
141 | if ($sdn_config->{controllers}) { | |
142 | my $expected = read_file("./$test/expected_controller_config"); | |
143 | my $controller_rawconfig = ""; | |
144 | ||
65723efd | 145 | eval { |
82f6b09f | 146 | my $config = PVE::Network::SDN::Controllers::generate_controller_config(); |
ddd3d0f7 TL |
147 | $controller_rawconfig = |
148 | PVE::Network::SDN::Controllers::generate_controller_rawconfig($config); | |
82f6b09f | 149 | }; |
82f6b09f | 150 | if (my $err = $@) { |
677e12ca | 151 | diag("got unexpected error - $err"); |
82f6b09f AD |
152 | fail($name); |
153 | } else { | |
ddd3d0f7 | 154 | is($controller_rawconfig, $expected, $name); |
82f6b09f AD |
155 | } |
156 | } | |
157 | } | |
158 | ||
159 | done_testing(); | |
160 |