aboutsummaryrefslogtreecommitdiffstats
path: root/t/add-syslog.t
blob: 638040e29a2c4491ffea17065866212e97512041 (plain)
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
#!/usr/bin/perl
# $Id: gprintify 257533 2009-05-23 12:45:15Z guillomovitch $

use strict;
use warnings;
use Test::More;
use Digest::MD5;
use FindBin qw/$Bin/;
use File::Temp;
use lib "$Bin/../lib";

my @selector_tests = (
    [ [ qw/local1 debug emerg/ ], 'local1.*' , 'debug -> emerg' ],
    [ [ qw/local1 info emerg/ ], 'local1.info' , 'info -> emerg' ],
    [ [ qw/local1 debug alert/ ], 'local1.=debug;local1.=info;local1.=notice;local1.=warning;local1.=err;local1.=crit;local1.=alert' , 'debug -> alert' ],
    [ [ qw/local1 info alert/ ], 'local1.=info;local1.=notice;local1.=warning;local1.=err;local1.=crit;local1.=alert' , 'info -> alert' ],
);

plan tests => 5 + scalar @selector_tests;

# test loading
ok(require("add-syslog"), "loading file OK");

# test string function
foreach my $test (@selector_tests) {
    is(get_selector(@{$test->[0]}), $test->[1], $test->[2]);
}

# test service configuration file modification
my $file;
$file = setup(<<EOF);
SYSLOGD_OPTIONS=
EOF

add_new_source('/tmp/log', $file);

is(
    get_syslog_option($file),
    '"-a /tmp/log"',
    'new source, without prior option'
);
unlink($file) unless $ENV{TEST_DEBUG};

$file = setup(<<EOF);
SYSLOGD_OPTIONS=-x
EOF

add_new_source('/tmp/log', $file);

is(
    get_syslog_option($file),
    '"-x -a /tmp/log"',
    'new source, with prior unquoted option'
);
unlink($file) unless $ENV{TEST_DEBUG};

$file = setup(<<EOF);
SYSLOGD_OPTIONS="-a /dev/log"
EOF

add_new_source('/tmp/log', $file);

is(
    get_syslog_option($file),
    '"-a /dev/log -a /tmp/log"',
    'new source, with prior quoted option'
);
unlink($file) unless $ENV{TEST_DEBUG};

$file = setup(<<EOF);
SYSLOGD_OPTIONS="-a /tmp/log"
EOF

add_new_source('/tmp/log', $file);

is(
    get_syslog_option($file),
    '"-a /tmp/log"',
    'new source, already defined'
);
unlink($file) unless $ENV{TEST_DEBUG};

sub setup {
    my ($content) = @_;

    my $out = File::Temp->new(UNLINK => 0);
    print $out $content;
    close $out;

    return $out->filename();
}

sub get_syslog_option {
    my ($file) = @_;

    my $options;
    open(my $in, '<', $file) or die "can't read $file: $!";
    while (my $line = <$in>) {
        chomp $line;
        next unless $line =~ /^SYSLOGD_OPTIONS=(.+)/;
        $options = $1;
        last;
    }
    close($in);

    return $options;
}