]> git.proxmox.com Git - pve-network.git/blob - src/test/run_test_zones.pl
5354e4d30fe59eee4ebf2e93474e8718d4b1e048
[pve-network.git] / src / test / run_test_zones.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
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 my @tests = grep { -d } glob './zones/*/*';
33
34 foreach my $test (@tests) {
35
36 my $sdn_config = read_sdn_config("./$test/sdn_config");
37
38 open(my $fh1, '<', "./$test/interfaces") or die "can't read interfaces file - $!";
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 {
49 return $interfaces_config;
50 },
51 );
52
53 my $pve_sdn_subnets;
54 $pve_sdn_subnets = Test::MockModule->new('PVE::Network::SDN::Subnets');
55 $pve_sdn_subnets->mock(
56 config => sub {
57 return $sdn_config->{subnets};
58 },
59 );
60
61 my $pve_sdn_zones_plugin;
62 $pve_sdn_zones_plugin = Test::MockModule->new('PVE::Network::SDN::Zones::Plugin');
63 $pve_sdn_zones_plugin->mock(
64 get_local_route_ip => sub {
65 my $outiface = "vmbr0";
66 my $outip = $interfaces_config->{ifaces}->{$outiface}->{address};
67 return ($outip, $outiface);
68 },
69 is_vlanaware => sub {
70 return $interfaces_config->{ifaces}->{vmbr0}->{'bridge_vlan_aware'};
71 },
72 is_ovs => sub {
73 return 1 if $interfaces_config->{ifaces}->{vmbr0}->{'type'} eq 'OVSBridge';
74 },
75 get_bridge_ifaces => sub {
76 return ('eth0');
77 },
78 find_bridge => sub {
79 return;
80 },
81 );
82
83 my $sdn_module = Test::MockModule->new("PVE::Network::SDN");
84 $sdn_module->mock(
85 running_config => sub {
86 return $sdn_config;
87 },
88 );
89
90 my $name = $test;
91 my $expected = read_file("./$test/expected_sdn_interfaces");
92
93 my $result = eval { PVE::Network::SDN::Zones::generate_etc_network_config() };
94
95 if (my $err = $@) {
96 diag("got unexpected error - $err");
97 fail($name);
98 } else {
99 is($result, $expected, $name);
100 }
101
102 if ($sdn_config->{controllers}) {
103 my $expected = read_file("./$test/expected_controller_config");
104 my $controller_rawconfig = "";
105
106 eval {
107 my $config = PVE::Network::SDN::Controllers::generate_controller_config();
108 $controller_rawconfig =
109 PVE::Network::SDN::Controllers::generate_controller_rawconfig($config);
110 };
111 if (my $err = $@) {
112 diag("got unexpected error - $err");
113 fail($name);
114 } else {
115 is($controller_rawconfig, $expected, $name);
116 }
117 }
118 }
119
120 done_testing();
121