File: test_midpoint_destinations.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 (247 lines) | stat: -rw-r--r-- 7,863 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#############################################################################
# Copyright (c) 2023 Balazs Scheidler <bazsi77@gmail.com>
#
# 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 src.syslog_ng_config.renderer import render_statement


def create_config(config, test_config, msg="foobar"):
    file_true = config.create_file_destination(file_name="dest-true.log", template="'$MSG\n'")
    file_false = config.create_file_destination(file_name="dest-false.log", template="'$MSG\n'")
    file_after = config.create_file_destination(file_name="dest-after.log", template="'$MSG\n'")
    file_fallback = config.create_file_destination(file_name="dest-fallback.log", template="'$MSG\n'")
    file_dropall = config.create_file_destination(file_name="dest-dropall.log", template="'$MSG\n'")

    preamble = f"""
@version: {config.get_version()}

options {{ stats-level(1); }};

block filter true() {{
    "1" eq "1"
}};

block filter false() {{
    "0" eq "1"
}};

source genmsg {{
    example-msg-generator(num(1) template("{msg}"));
}};

destination dest_after {{
    {render_statement(file_after)};
}};

destination dest_true {{
    {render_statement(file_true)};
}};

destination dest_false {{
    {render_statement(file_false)};
}};

destination dest_fallback {{
    {render_statement(file_fallback)};
}};

destination dest_dropall {{
    channel {{
        filter {{ false(); }};
        destination {{ {render_statement(file_dropall)}; }};
    }};
}};

log {{
    source(genmsg);
    destination(dest_fallback);
    flags(fallback);
}};

"""
    config.set_raw_config(preamble + test_config)
    return (file_true, file_false, file_after, file_fallback, file_dropall)


def test_midpoint_destination_that_drops_all_messages_does_not_cause_message_to_be_unmatched(config, syslog_ng):
    # Rationale:
    #
    #   midpoint destinations which drop messages completely do it in their
    #   own context which should not affect the log path as a whole.  As far
    #   as the log path is concerned, the message was successfully
    #   dispatched to the destination.
    test_config = """

log {
    source(genmsg);
    destination(dest_dropall);
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, file_dropall) = create_config(config, test_config)
    syslog_ng.start(config)

    assert "processed" not in file_dropall.get_stats()
    assert file_after.get_stats()["processed"] == 1
    assert "processed" not in file_fallback.get_stats()

    assert file_after.read_log() == "foobar\n"


def test_midpoint_inline_destination_that_drops_all_messages_does_not_cause_message_to_be_unmatched(config, syslog_ng):
    # Rationale:
    #
    #   midpoint destinations which drop messages completely do it in their
    #   own context which should not affect the log path as a whole.  As far
    #   as the log path is concerned, the message was successfully
    #   dispatched to the destination.
    test_config = """

log {
    source(genmsg);
    destination {
        channel { filter { false(); }; };
    };
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, file_dropall) = create_config(config, test_config)
    syslog_ng.start(config)

    assert "processed" not in file_dropall.get_stats()
    assert file_after.get_stats()["processed"] == 1
    assert "processed" not in file_fallback.get_stats()

    assert file_after.read_log() == "foobar\n"


def test_filter_between_destinations_that_drops_all_messages_causes_the_message_to_be_unmatched(config, syslog_ng):
    # Rationale:
    #
    #   In this case the filter() is part of the log path and the path is
    #   not processed completely because the filter dropped the message.
    #   The anticipated use-case here is that the first destination serves
    #   debugging purposes only, and the delivery of the message to that
    #   destination does not mean that the log path was successfully
    #   finished.

    test_config = """

log {
    source(genmsg);
    destination(dest_true);
    filter { false(); };
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, _) = create_config(config, test_config)
    syslog_ng.start(config)

    assert file_true.get_stats()["processed"] == 1
    assert "processed" not in file_after.get_stats()
    assert file_fallback.get_stats()["processed"] == 1

    assert file_true.read_log() == "foobar\n"
    assert file_fallback.read_log() == "foobar\n"


def test_junction_that_drops_messages_in_all_branches_causes_the_message_to_be_unmatched(config, syslog_ng):

    test_config = """

log {
    source(genmsg);
    destination(dest_true);
    junction {
        channel { filter { false(); }; };
        channel { filter { false(); }; };
    };
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, _) = create_config(config, test_config)
    syslog_ng.start(config)

    assert file_true.get_stats()["processed"] == 1
    assert "processed" not in file_after.get_stats()
    assert file_fallback.get_stats()["processed"] == 1

    assert file_true.read_log() == "foobar\n"
    assert file_fallback.read_log() == "foobar\n"


def test_junction_that_drops_messages_in_all_branches_causes_the_message_to_be_unmatched_even_if_they_reference_destinations(config, syslog_ng):

    test_config = """

log {
    source(genmsg);
    junction {
        channel {
            destination(dest_true);
            filter { false(); };
            destination(dest_false);
        };
        channel { filter { false(); }; };
    };
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, _) = create_config(config, test_config)
    syslog_ng.start(config)

    assert file_true.get_stats()["processed"] == 1
    assert "processed" not in file_false.get_stats()
    assert "processed" not in file_after.get_stats()
    assert file_fallback.get_stats()["processed"] == 1

    assert file_true.read_log() == "foobar\n"
    assert file_fallback.read_log() == "foobar\n"


def test_junction_that_contains_message_dropping_destination_continues_to_deliver_messages_on_the_same_path(config, syslog_ng):

    test_config = """

log {
    source(genmsg);
    junction {
        channel {
            destination(dest_dropall);
            destination(dest_true);
        };
        channel { filter { false(); }; destination(dest_false); };
    };
    destination(dest_after);
};
"""
    (file_true, file_false, file_after, file_fallback, file_dropall) = create_config(config, test_config)
    syslog_ng.start(config)

    assert "processed" not in file_dropall.get_stats()
    assert file_true.get_stats()["processed"] == 1
    assert "processed" not in file_false.get_stats()

    assert file_after.get_stats()["processed"] == 1
    assert "processed" not in file_fallback.get_stats()

    assert file_true.read_log() == "foobar\n"
    assert file_after.read_log() == "foobar\n"