/
simple.t
337 lines (278 loc) · 9.89 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# -*- mode: cperl -*-
use strict;
use warnings;
BEGIN {
*CORE::GLOBAL::sleep = sub {};
}
use Test::More;
use Test::Quattor qw(simple simple_ethtool simple_noethtool simple_realhostname simple_shortrealhostname simple_nobroadcast simple_tun);
use Test::MockModule;
use NCM::Component::network;
my $mock = Test::MockModule->new('NCM::Component::network');
my %executables;
$mock->mock('_is_executable', sub {diag "executables $_[1] ",explain \%executables;return $executables{$_[1]};});
use Readonly;
Readonly my $RT => <<EOF;
#
# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
4 manual
80 someold # managed by Quattor
200 custom
EOF
Readonly my $NETWORK => <<EOF;
NETWORKING=yes
HOSTNAME=somehost.test.domain
GATEWAY=4.3.2.254
EOF
Readonly my $NETWORK_HOSTNAMECTL => <<EOF;
NETWORKING=yes
GATEWAY=4.3.2.254
EOF
Readonly my $ETH0 => <<EOF;
ONBOOT=yes
NM_CONTROLLED='no'
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=static
IPADDR=4.3.2.1
NETMASK=255.255.255.0
BROADCAST=4.3.2.255
RESOLV_MODS=no
PEERDNS=no
EOF
Readonly my $ETH0_NO_RESOLV_MODS_PEERDNS => <<EOF;
ONBOOT=yes
NM_CONTROLLED='no'
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=static
IPADDR=4.3.2.1
NETMASK=255.255.255.0
BROADCAST=4.3.2.255
EOF
Readonly my $ETHTOOL_ETH0 => <<EOF;
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: Symmetric
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: Symmetric
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: pumbg
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
EOF
Readonly my $ETHTOOL_ETH0_CHANNELS => <<EOF;
Channel parameters for eth0:
Pre-set maximums:
RX: 0
TX: 0
Other: 1
Combined: 8
Current hardware settings:
RX: 0
TX: 0
Other: 1
Combined: 8
EOF
Readonly my $TUN0 => <<EOF;
ONBOOT=yes
NM_CONTROLLED='no'
DEVICE=tun0
TYPE=IPIP
BOOTPROTO=none
MY_INNER_IPADDR=5.6.7.8/20
MY_OUTER_IPADDR=5.6.8.8
RESOLV_MODS=no
PEERDNS=no
EOF
Readonly my $TUN1 => <<EOF;
ONBOOT=yes
NM_CONTROLLED='no'
DEVICE=tun1
TYPE=IPIP
BOOTPROTO=none
MY_INNER_IPADDR=5.6.7.9/21
MY_OUTER_IPADDR=5.6.8.9
PEER_OUTER_IPADDR=5.6.9.9
RESOLV_MODS=no
PEERDNS=no
EOF
=head1 DESCRIPTION
Test the C<Configure> method of the component for bridge configuration.
=cut
ok($NETWORK ne $NETWORK_HOSTNAMECTL,
"expected network config is different for case with and without hostnamectl");
ok($ETH0 ne $ETH0_NO_RESOLV_MODS_PEERDNS,
"expected network ifcfg eth0 config is different for case with and without RESOLV_MODS/PEERDNS");
# File must exist
set_file_contents("/etc/sysconfig/network", 'x' x 1000);
set_file_contents("/etc/iproute2/rt_tables", $RT);
my $cfg = get_config_for_profile('simple');
my $cmp = NCM::Component::network->new('network');
set_desired_output('/usr/sbin/ethtool eth0', $ETHTOOL_ETH0);
set_desired_output('/usr/sbin/ethtool --show-channels eth0', $ETHTOOL_ETH0_CHANNELS);
is($cmp->Configure($cfg), 1, "Component runs correctly with a test profile");
is(get_file_contents("/etc/iproute2/rt_tables"), $RT, "Exact (unmodified) routing table");
# generic
my $fh;
$fh = get_file($cmp->testcfg_filename("/etc/sysconfig/network"));
ok(! defined($fh), "testcfg network file was cleaned up");
# on success, this is hardlink of a cleaned up testcfg; can't use get_file
is(get_file_contents("/etc/sysconfig/network"), $NETWORK, "Exact network config");
$fh = get_file($cmp->testcfg_filename("/etc/sysconfig/network-scripts/ifcfg-eth0"));
ok(! defined($fh), "testcfg network/ifcfg-eth0 was cleaned up");
is(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0"), $ETH0, "Exact network config");
ok(command_history_ok([
'ip addr show',
'/sbin/chkconfig --level 2345 network on',
'service network stop',
'service network start',
'ccm-fetch',
], ['hostnamectl']),
"network stop/start called on network config change (and no hostnamectl)");
command_history_reset();
is($cmp->Configure($cfg), 1, "Component runs correctly 2nd time with same test profile");
ok(command_history_ok([
'ip addr show',
], [
'service network stop',
'service network start',
'ccm-fetch',
'hostnamectl'
]), "network stop/start not called with same config 2nd run");
# Test RESOLV_MODS change
command_history_reset();
set_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0", $ETH0_NO_RESOLV_MODS_PEERDNS);
is($cmp->Configure($cfg), 1, "Component runs correctly 3rd time with same test profile");
is(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0"), $ETH0, "Exact network config 3rd run");
ok(command_history_ok([
'ip addr show',
], [
'service network stop',
'service network start',
'ifup',
'ifdown',
'ccm-fetch',
'hostnamectl'
]), "network stop/start or ifup/ifdown not called with same config and only RESOLV_MODS/PEERDNS changed");
# enable hostnamectl
$executables{'/usr/bin/hostnamectl'} = 1;
# current network config has old legacy format
# new format be different, but should be keeps_state
command_history_reset();
is($cmp->Configure($cfg), 1, "Component runs correctly 4th time with same test profile but with hostnamectl");
# if the contents here is a hardlink, it means the cleanup of the backup files failed
is(get_file_contents("/etc/sysconfig/network"), $NETWORK_HOSTNAMECTL, "Exact network config with hostnamectl");
ok(command_history_ok([
'ip addr show',
'/usr/bin/hostnamectl set-hostname somehost.test.domain --static',
], [
'service network stop',
'service network start',
'ccm-fetch',
]), "network stop/start not called with same config with hostnamectl (KEEPS_STATE set) 3rd run");
# add ethtool options
# shouldn't trigger an ifup/ifdown cycle
command_history_reset();
$cfg = get_config_for_profile('simple_ethtool');
is($cmp->Configure($cfg), 1, "Component runs correctly w/o ethtool");
like(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0"),
qr/^ETHTOOL_OPTS='--set-channels eth0 combined 7 other 1 ; autoneg on speed 10000 wol b'$/m,
"no ethtool opts");
# also no ethtool, as this config has no ethtool configured
ok(command_history_ok([
'/usr/sbin/ethtool --show-channels eth0',
'/usr/sbin/ethtool eth0',
'/usr/sbin/ethtool --set-channels eth0 combined 7', # no other, is already 1
'/usr/sbin/ethtool --change eth0 speed 10000 wol b', # no autoneg, is already on
], ['ifup', 'ifdown', 'restart', 'autoneg on', 'other 1']),
"changes in ethtool do not trigger ifup/ifdown");
# check that removal ethtool_opts does not trigger ifup/ifdown
command_history_reset();
$cfg = get_config_for_profile('simple_noethtool');
is($cmp->Configure($cfg), 1, "Component runs correctly w/o ethtool");
unlike(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0"),
qr/ETHTOOL/m,
"no ethtool opts");
# also no ethtool, as this config has no ethtool configured
ok(command_history_ok(undef, ['ethtool', 'ifup', 'ifdown', 'restart']),
"changes in ethtool do not trigger ifup/ifdown");
# Check that realhostname is used correctly
delete $executables{'/usr/bin/hostnamectl'};
command_history_reset();
$cfg = get_config_for_profile('simple_realhostname');
is($cmp->Configure($cfg), 1, "Component runs correctly with realhostname test profile");
like(get_file_contents("/etc/sysconfig/network"),
qr/^HOSTNAME=realhost.example.com$/m,
"realhostname correctly used as hostname");
ok(command_history_ok(undef,[
'hostnamectl',
]), "hostnamectl not called with realhostname");
command_history_reset();
$cfg = get_config_for_profile('simple_realhostname');
$executables{'/usr/bin/hostnamectl'} = 1;
is($cmp->Configure($cfg), 1, "Component runs correctly with realhostname test profile w hostnamectl");
unlike(get_file_contents("/etc/sysconfig/network"),
qr/HOSTNAME=/m,
"realhostname not used as hostname w hostnamectl");
ok(command_history_ok([
'/usr/bin/hostnamectl set-hostname realhost.example.com --static',
]), "hostnamectl called with realhostname");
command_history_reset();
$cfg = get_config_for_profile('simple_shortrealhostname');
$executables{'/usr/bin/hostnamectl'} = 1;
is($cmp->Configure($cfg), 1, "Component runs correctly with shortrealhostname test profile w hostnamectl");
unlike(get_file_contents("/etc/sysconfig/network"),
qr/HOSTNAME=/m,
"shortrealhostname not used as hostname w hostnamectl");
ok(command_history_ok([
'/usr/bin/hostnamectl set-hostname shortrealhost --static',
]), "hostnamectl called with shortrealhostname");
# removing broadcast that was same as computed default is ok (triggers no network restart)
set_desired_output('ipcalc --broadcast 4.3.2.1 255.255.255.0', "BROADCAST=4.3.2.255\n");
command_history_reset();
$cfg = get_config_for_profile('simple_nobroadcast');
is($cmp->Configure($cfg), 1, "Component runs correctly with nobroadcast test profile w/o broadcast");
unlike(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-eth0"),
qr/BROADCAST=/m,
"no broadcast set");
ok(command_history_ok([
'ipcalc --broadcast 4.3.2.1 255.255.255.0',
], [
'service network stop',
'service network start',
'ccm-fetch',
]), "network stop/start not called with same config w/o broadcast (KEEPS_STATE set) 2nd run");
# test tun0 / tun1 IPIP
command_history_reset();
$cfg = get_config_for_profile('simple_tun');
is($cmp->Configure($cfg), 1, "Component runs correctly with nobroadcast test profile w/o broadcast");
is(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-tun0"), $TUN0, "Exact network config tun0");
is(get_file_contents("/etc/sysconfig/network-scripts/ifcfg-tun1"), $TUN1, "Exact network config tun1");
done_testing();