File: test_filters.py

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (144 lines) | stat: -rw-r--r-- 4,998 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
#############################################################################
# Copyright (c) 2007-2015 Balabit
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################

from globals import *
from log import *
from messagegen import *
from messagecheck import *

config = """@version: %(syslog_ng_version)s

options { ts_format(iso); chain_hostnames(no); keep_hostname(yes); threaded(yes); };

source s_int { internal(); };
source s_unix { unix-stream("log-stream" flags(expect-hostname)); };

filter f_facility { message("facility"); };
filter f_facility1 { facility(syslog); };
filter f_facility2 { facility(kern); };
filter f_facility3 { facility(mail); };
filter f_facility4 { facility(daemon,auth,lpr); };

destination d_facility1 { file("test-facility1.log"); logstore("test-facility1.lgs"); };
destination d_facility2 { file("test-facility2.log"); logstore("test-facility2.lgs"); };
destination d_facility3 { file("test-facility3.log"); logstore("test-facility3.lgs"); };
destination d_facility4 { file("test-facility4.log"); logstore("test-facility4.lgs"); };

log { source(s_unix);
    filter(f_facility);
    log { filter(f_facility1); destination(d_facility1); };
    log { filter(f_facility2); destination(d_facility2); };
    log { filter(f_facility3); destination(d_facility3); };
    log { filter(f_facility4); destination(d_facility4); };
};

filter f_level { message("level"); };
filter f_level1 { level(debug); };
filter f_level2 { level(info); };
filter f_level3 { level(notice); };
filter f_level4 { level(warning..crit); };

destination d_level1 { file("test-level1.log"); logstore("test-level1.lgs"); };
destination d_level2 { file("test-level2.log"); logstore("test-level2.lgs"); };
destination d_level3 { file("test-level3.log"); logstore("test-level3.lgs"); };
destination d_level4 { file("test-level4.log"); logstore("test-level4.lgs"); };

log { source(s_unix);
    filter(f_level);
    log { filter(f_level1); destination(d_level1); };
    log { filter(f_level2); destination(d_level2); };
    log { filter(f_level3); destination(d_level3); };
    log { filter(f_level4); destination(d_level4); };
};

""" % locals()

def test_facility_single():
    messages = (
      (41, 'facility1'),
      (1, 'facility2'),
      (17, 'facility3'),
    )
    expected = [None,] * len(messages)

    s = SocketSender(AF_UNIX, 'log-stream', dgram=0, repeat=10)
    for ndx in range(0, len(messages)):
        if not expected[ndx]:
            expected[ndx] = []
        expected[ndx].extend(s.sendMessages(messages[ndx][1], pri=messages[ndx][0]))

    for ndx in range(0, len(messages)):
        if not check_file_expected('test-facility%d' % (ndx + 1,), expected[ndx]):
            return False
    return True

def test_facility_multi():
    messages = (
      (25, 'facility4'),
      (33, 'facility4'),
      (49, 'facility4'),
    )
    expected = []

    s = SocketSender(AF_UNIX, 'log-stream', dgram=0, repeat=10)
    for ndx in range(0, len(messages)):
        expected.extend(s.sendMessages(messages[ndx][1], pri=messages[ndx][0]))

    if not check_file_expected('test-facility4', expected):
        return False
    return True


def test_level_single():
    messages = (
      (7, 'level1'),
      (6, 'level2'),
      (5, 'level3'),
    )
    expected = [None,] * len(messages)

    s = SocketSender(AF_UNIX, 'log-stream', dgram=0, repeat=10)
    for ndx in range(0, len(messages)):
        if not expected[ndx]:
            expected[ndx] = []
        expected[ndx].extend(s.sendMessages(messages[ndx][1], pri=messages[ndx][0]))

    for ndx in range(0, len(messages)):
        if not check_file_expected('test-level%d' % (ndx + 1,), expected[ndx]):
            return False
    return True

def test_level_multi():
    messages = (
      (4, 'level4'),
      (3, 'level4'),
      (2, 'level4'),
    )
    expected = []

    s = SocketSender(AF_UNIX, 'log-stream', dgram=0, repeat=10)
    for ndx in range(0, len(messages)):
        expected.extend(s.sendMessages(messages[ndx][1], pri=messages[ndx][0]))

    if not check_file_expected('test-level4', expected):
        return False
    return True