File: munin_node_config.t

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (361 lines) | stat: -rw-r--r-- 9,089 bytes parent folder | download | duplicates (4)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# vim: ts=4 : et

use warnings;
use strict;

use Test::More tests => 43;

use FindBin;
use English qw(-no_match_vars);
use Munin::Common::Defaults;

use_ok('Munin::Node::Config');

my $conf = Munin::Node::Config->instance();
isa_ok($conf, 'Munin::Node::Config');


###############################################################################
#                       _ P A R S E _ L I N E

### Corner cases
{
    is($conf->_parse_line(""), undef, "Empty line is undef");

    eval { $conf->_parse_line("foo") };
    like($@, qr{Line is not well formed}, "Need a name and a value");

    is($conf->_parse_line("#foo"), undef, "Comment is undef");
}


### Hostname
{
    my @res = $conf->_parse_line("hostname foo");
    is_deeply(\@res, [fqdn => 'foo'], 'Parsing host name');

    # The parser is quite forgiving ...
    @res = $conf->_parse_line("hostname foo bar");
    is_deeply(\@res, [fqdn => 'foo bar'],
              'Parsing invalid host name gives no error');
}


### Default user
{
    my $uname = getpwuid $UID;

    my @res = $conf->_parse_line("default_client_user $uname");
    is_deeply(\@res, [defuser => $UID], 'Parsing default user name');

    @res = $conf->_parse_line("default_client_user $UID");
    is_deeply(\@res, [defuser => $UID], 'Parsing default user ID');
    
    eval { $conf->_parse_line("default_client_user xxxyyyzzz") };
    like($@, qr{Default user does not exist}, "Default user exists");
}


### Default group
{
    my $gid   = (split / /, $GID)[0];
    my $gname = getgrgid $gid;

    my @res = $conf->_parse_line("default_client_group $gname");
    is_deeply(\@res, [defgroup => $gid], 'Parsing default group');

    eval { $conf->_parse_line("default_client_group xxxyyyzzz") };
    like($@, qr{Default group does not exist}, "Default group exists");
}


### Paranoia
{
    my @res = $conf->_parse_line("paranoia off");
    is_deeply(\@res, [paranoia => 0], 'Parsing paranoia');
}


### allow_deny
{
    my @res = $conf->_parse_line('allow 127\.0\.0\.1');
    is_deeply(\@res, [], 'Parsing: allow is ignored');
}

{
    my @res = $conf->_parse_line('deny 127\.0\.0\.1');
    is_deeply(\@res, [], 'Parsing: deny is ignored');
}


### tls
{
    my @res = $conf->_parse_line('tls paranoid');
    is_deeply(\@res, [tls => 'paranoid'], 'Parsing tls');

}

###############################################################################
#  _strip_comment
{
    my $str = "#Foo" ;
    $conf->_strip_comment($str);
    is($str, "", "Strip comment");
}

{
    my $str = "foo #Foo" ;
    $conf->_strip_comment($str);
    is($str, "foo ", "Strip comment 2");
}

{
    my $str = "foo" ;
    $conf->_strip_comment($str);
    is($str, "foo", "Strip comment 3");
}


###############################################################################
#  reinitialize
{
    my $expected = {foo => 'bar'};
    $conf->reinitialize($expected);
    is_deeply($conf, $expected, "Reinitialize with new values");

    my $oldconf = $conf;

    $conf->reinitialize();
    is_deeply($conf, {}, "Reinitialize to empty state");

    is($conf, $oldconf, "Reinitialize preserves config references");
}


###############################################################################
#  parse_config
{
    $conf->reinitialize();
    $conf->parse_config(*DATA);
    my $expected = {
        fqdn => 'foo.example.com',
        tls => 'enabled',
        sconf => {
            'setsid' => 'yes',
            'background' => '1',
            'log_file' => '/var/log/munin/munin-node.log',
            'host' => '*',
            'setsid' => '1',
            'pid_file' => '/var/run/munin/munin-node.pid',
            'group' => 'root',
            'log_level' => '4',
            'user' => 'root',
        },
        ignores => [
            '~$',
            '\\.bak$',
            '%$',
            '\\.dpkg-(tmp|new|old|dist)$',
            '\\.rpm(save|new)$',
            '\\.pod$',
        ],
    };
    is_deeply($conf, $expected, "Parsing a test config");
}


###############################################################################
# _parse_plugin_line

### malformed line
{
    eval { $conf->_parse_plugin_line("") };
    like($@, qr{Line is not well formed}, "Empty line is an error");
}
{
    eval { $conf->_parse_plugin_line("blah") };
    like($@, qr{Line is not well formed}, "line without a value is an error");
}
{
    eval { $conf->_parse_plugin_line("blah blah blah") };
    like($@, qr{Failed to parse line}, "Unknown variable name is an error");
}


### user
{
    my $uname = getpwuid $UID;

    my @res = $conf->_parse_plugin_line("user $uname");
    is_deeply(\@res, [user => $uname], 'Parsing plugin user name');

    @res = $conf->_parse_plugin_line("user $UID");
    is_deeply(\@res, [user => $UID], 'Parsing plugin user ID');
}

### group
my @gids = split / /, $GID;
my $gid  = $gids[0];

(my $gid_list = $GID) =~ tr/ /,/;
my $gname = getgrgid $gid;

{
    my @res = $conf->_parse_plugin_line("group $gname");
    is_deeply(\@res, [group => [ $gname ] ], 'Parsing plugin group');
}
{
    my @res = $conf->_parse_plugin_line("group $gid_list");
    is_deeply(\@res, [group => [ @gids ] ], 'Parsing plugin group (many)');
}
{
    my @res = $conf->_parse_plugin_line("group $gid_list, (999999999)");
    is_deeply(\@res, [group => [ @gids, '(999999999)' ] ],
        'Parsing plugin group (many with optional nonexistent)');
}
{
    my @res = $conf->_parse_plugin_line("group xxxyyyzzz");
    is_deeply(\@res, [group => [ 'xxxyyyzzz' ]], 'Parsing unknown group');
}

### command
{
    my @res = $conf->_parse_plugin_line('command shutdown -h now');
    is_deeply(\@res, [command => [ 'shutdown', '-h', 'now' ] ], 'command line');
}
{
    my @res = $conf->_parse_plugin_line('command sudo -u root %c');
    is_deeply(\@res, [command => [ 'sudo', '-u', 'root', '%c' ] ],
        'command line with %c expansion'
    );
}

### host_name
{
    my @res = $conf->_parse_plugin_line('host_name server.example.com');
    is_deeply(\@res, [host_name => 'server.example.com'], 'parsing host_name');
}

### timeout
{
    my @res = $conf->_parse_plugin_line('timeout 20');
    is_deeply(\@res, [timeout => 20], 'parsing timeout');
}
{
    my @res = $conf->_parse_plugin_line('timeout aeons');
    is_deeply(\@res, [timeout => 'aeons'], 'non-numeric timeout is valid');
}


### environment
{
    my @res = $conf->_parse_plugin_line("env.foo fnord");
    is_deeply(\@res, [ env => { foo => 'fnord' } ], 'Parsing environment variable');
}
{
    eval { $conf->_parse_plugin_line("env foo = fnord") };
    like($@, qr{Deprecated.*'env\.foo fnord'},
         "Old way of configuring plugin environment variables throws exception");
}


###############################################################################
#     P R O C E S S _ P L U G I N _ C O N F I G U R A T I O N _ F I L E S

{
    # Capture STDERR in a string
    my $stderr;
    open my $olderr, '>&', *STDERR;
    close STDERR;
    open STDERR, '>', \$stderr or die $!;

    my $sconfdir = "$FindBin::Bin/config/plugin-conf.d";

    eval {
        $conf->reinitialize({
            sconfdir => $sconfdir,
        });
        $conf->process_plugin_configuration_files();
    };

    # Close and restore STDERR to original condition.
    close STDERR;
    open STDERR, '>&', $olderr;

    is($@, '', "No exceptions");
    like($stderr, qr{Clutter before section start}, "Clutter file is skipped");

    is_deeply($conf, {
        sconfdir => $sconfdir,
        sconf=>{
            Foo    => {user => 'root', env => {baz => 'zing'}, update_rate => 86400 },
            'Foo*' => {group => [ 'root' ], env => {bar => 'zap'}},
            'F*'   => {env => {bar => 'zoo'}},
        },
    }, "Checking sconf");

    $conf->apply_wildcards(qw| Foo Fnord boF |);
    is_deeply($conf, {
        sconfdir => $sconfdir,
        sconf=>{
            Foo => {
                user => 'root',
                group => [ 'root' ],
                env => {
                    baz => 'zing',
                    bar => 'zap',
                },
                update_rate => 86400,
            },
            Fnord => {
                env => {
                    bar => 'zoo',
                },
            },
        },
    }, "Checking sconf wildcards");
}

__DATA__
#
# Example config-file for munin-node
#

log_level 4
log_file /var/log/munin/munin-node.log
pid_file /var/run/munin/munin-node.pid

background 1
setsid 1

user root
group root
setsid yes

# Regexps for files to ignore

ignore_file ~$
ignore_file \.bak$
ignore_file %$
ignore_file \.dpkg-(tmp|new|old|dist)$
ignore_file \.rpm(save|new)$
ignore_file \.pod$

# Set this if the client doesn't report the correct hostname when
# telnetting to localhost, port 4948
#
host_name foo.example.com

# A list of addresses that are allowed to connect.  This must be a
# regular expression, due to brain damage in Net::Server, which
# doesn't understand CIDR-style network notation.  You may repeat
# the allow line as many times as you'd like

allow ^127\.0\.0\.\d+$
allow ^10\.0\.0\.\d+$

# Which address to bind to;
host *
# host 127.0.0.1

tls enabled