]> git.proxmox.com Git - pve-network.git/blame - test/run_test_zones.pl
add zones && controllers unit tests
[pve-network.git] / test / run_test_zones.pl
CommitLineData
82f6b09f
AD
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib qw(..);
7use File::Slurp;
8
9use Test::More;
10use Test::MockModule;
11
12use PVE::Network::SDN;
13use PVE::Network::SDN::Zones;
14use PVE::Network::SDN::Controllers;
15use PVE::INotify;
16
17sub 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
33my @tests = grep { -d } glob './zones/*/*';
34
35foreach 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
122done_testing();
123
124