]>
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 | { | |
24 | local $/; # slurp mode | |
25 | $sdn_config = eval <$in>; | |
26 | } | |
27 | close $in; | |
28 | ||
29 | return $sdn_config; | |
30 | } | |
31 | ||
32 | ||
33 | my @tests = grep { -d } glob './zones/*/*'; | |
34 | ||
35 | foreach my $test (@tests) { | |
36 | ||
37 | my $sdn_config = read_sdn_config ("./$test/sdn_config"); | |
38 | ||
39 | open my $fh1, '<', "./$test/interfaces" or die "can't read interfaces file"; | |
40 | my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef); | |
41 | close $fh1; | |
42 | ||
43 | my $pve_common_inotify; | |
44 | $pve_common_inotify = Test::MockModule->new('PVE::INotify'); | |
45 | $pve_common_inotify->mock( | |
46 | nodename => sub { | |
47 | return 'localhost'; | |
48 | }, | |
49 | read_file => sub { | |
50 | return $interfaces_config; | |
51 | }, | |
52 | ); | |
53 | ||
54 | my $pve_sdn_subnets; | |
55 | $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets'); | |
56 | $pve_sdn_subnets->mock( | |
57 | config => sub { | |
58 | return $sdn_config->{subnets}; | |
59 | }, | |
60 | ); | |
61 | ||
62 | my $pve_sdn_zones_plugin; | |
63 | $pve_sdn_zones_plugin = Test::MockModule->new('PVE::Network::SDN::Zones::Plugin'); | |
64 | $pve_sdn_zones_plugin->mock( | |
65 | get_local_route_ip => sub { | |
66 | my $outiface = "vmbr0"; | |
67 | my $outip = $interfaces_config->{ifaces}->{$outiface}->{address}; | |
68 | return ($outip, $outiface); | |
69 | }, | |
70 | is_vlanaware => sub { | |
71 | return $interfaces_config->{ifaces}->{vmbr0}->{'bridge_vlan_aware'}; | |
72 | }, | |
73 | is_ovs => sub { | |
74 | return 1 if $interfaces_config->{ifaces}->{vmbr0}->{'type'} eq 'OVSBridge'; | |
75 | }, | |
76 | get_bridge_ifaces => sub { | |
77 | return ('eth0'); | |
78 | }, | |
79 | find_bridge => sub { | |
80 | return; | |
81 | } | |
82 | ); | |
83 | ||
84 | my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); | |
85 | $sdn_module->mock( | |
86 | config => sub { | |
87 | return $sdn_config; | |
88 | }, | |
89 | ); | |
90 | ||
91 | my $name = $test; | |
92 | my $expected = read_file("./$test/expected_sdn_interfaces"); | |
93 | ||
94 | my $result = ""; | |
95 | eval { | |
96 | $result = PVE::Network::SDN::Zones::generate_etc_network_config(); | |
97 | }; | |
98 | ||
99 | if (my $err = $@) { | |
100 | fail($name); | |
101 | } else { | |
102 | is ($result, $expected, $name); | |
103 | } | |
104 | ||
105 | if ($sdn_config->{controllers}) { | |
106 | my $expected = read_file("./$test/expected_controller_config"); | |
107 | my $controller_rawconfig = ""; | |
108 | ||
109 | eval { | |
110 | my $config = PVE::Network::SDN::Controllers::generate_controller_config(); | |
111 | $controller_rawconfig = PVE::Network::SDN::Controllers::generate_controller_rawconfig($config); | |
112 | }; | |
113 | ||
114 | if (my $err = $@) { | |
115 | fail($name); | |
116 | } else { | |
117 | is ($controller_rawconfig, $expected, $name); | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | done_testing(); | |
123 | ||
124 |