forked from quattor/configuration-modules-core
/
nmstate_simple.t
137 lines (112 loc) · 3.65 KB
/
nmstate_simple.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use strict;
use warnings;
BEGIN {
*CORE::GLOBAL::sleep = sub {};
}
use Test::More;
use Test::Quattor qw(nmstate_simple);
use Test::MockModule;
use Readonly;
use NCM::Component::nmstate;
my $mock = Test::MockModule->new('NCM::Component::nmstate');
my %executables;
$mock->mock('_is_executable', sub {diag "executables $_[1] ",explain \%executables;return $executables{$_[1]};});
my $cfg = get_config_for_profile('nmstate_simple');
my $cmp = NCM::Component::nmstate->new('network');
# test a few valid filenames
foreach my $fn (qw(/a/b/c/eth0.yml eth1.yml ens1f2p3.yml bond0.0.yml test123.yml)) {
my $name = $fn;
$name =~ s/^.*\///;
$name =~ s/\.yml$//;
is_deeply([$name, $name], $cmp->is_valid_interface($fn), "$fn is a valid interface filename");
}
# test a few invalid filenames
foreach my $fn (qw(/etc/nmstate/resolv.yml eth0.json notaninterface.yml)) {
ok(!defined($cmp->is_valid_interface($fn)), "$fn not a valid interface filename");
}
Readonly my $NETWORK => 'x' x 100;
Readonly my $RESOLV => <<EOF;
managed by something else
EOF
Readonly my $NODNS => <<EOF;
[main]
dns=none
EOF
Readonly my $ETH0_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
interfaces:
- ipv4:
address:
- ip: 4.3.2.1
prefix-length: 24
dhcp: false
enabled: true
mac-address: 6e:a5:1b:55:77:0a
name: eth0
profile-name: eth0
state: up
type: ethernet
routes:
config:
- next-hop-interface: eth0
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: eth0
EOF
Readonly my $NOTTOREMOVE => <<EOF;
something not to remove
EOF
# TODO: there should be no reason for this. we can assume it's there in EL9
$executables{'/usr/bin/hostnamectl'} = 1;
set_file_contents("/etc/resolv.conf", "$RESOLV");
set_file_contents("/etc/nmstate/toremove0.yml", "something to remove");
set_file_contents("/etc/nmstate/nottoremove.yml", "$NOTTOREMOVE");
command_history_reset();
is($cmp->Configure($cfg), 1, "Component runs correctly with a test profile");
my $fh;
# resolv.conf is unchanged
is(get_file_contents("/etc/resolv.conf"), $RESOLV, "Exact network config");
# set nm config to disable dns mgmt
is(get_file_contents("/etc/NetworkManager/conf.d/90-quattor.conf"), $NODNS, "disable NM dns mgmt");
# unconfigure nmstate yml is removed
ok(!$cmp->file_exists("/etc/nmstate/toremove0.yml"), "unconfigured yml nmstate is removed");
# keep non-interface files
is(get_file_contents("/etc/nmstate/nottoremove.yml"), $NOTTOREMOVE, "disable NM dns mgmt");
my $eth0yml = get_file_contents("/etc/nmstate/eth0.yml");
is($eth0yml, $ETH0_YML, "Exact eth0 yml config");
diag "all history commands ", explain \@Test::Quattor::command_history;
ok(command_history_ok([
'ls -ltr /etc/nmstate',
'ip addr show',
'ip route show',
'/usr/bin/nmstatectl show',
'/usr/bin/nmcli dev status',
'/usr/bin/nmcli connection',
'ip addr show',
'systemctl enable NetworkManager',
'/usr/bin/hostnamectl set-hostname somehost.test.domain --static',
'ls -ltr /etc/nmstate',
'ip addr show',
'ip route show',
'/usr/bin/nmstatectl show',
'/usr/bin/nmcli dev status',
'/usr/bin/nmcli connection',
'service NetworkManager reload',
'systemctl disable nmstate',
'/usr/bin/nmstatectl apply /etc/nmstate/eth0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/resolv.yml',
'service NetworkManager reload',
'/usr/bin/nmcli connection delete toremove0',
'ls -ltr /etc/nmstate',
'ip addr show',
'ip route show',
'/usr/bin/nmstatectl show',
'/usr/bin/nmcli dev status',
'/usr/bin/nmcli connection',
'/usr/bin/nmcli -t -f uuid,device,name,state,active conn',
'ccm-fetch'
], []));
command_history_reset();
done_testing();