summaryrefslogtreecommitdiff
path: root/slog/slog_filter_tests.py
blob: 401d6a0552527f9e5b2218b6024a76f3c2a4f8c5 (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
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
# slog_filter_tests.py - unit tests for structured logging tests
#
# Copyright 2017  QvarnLabs Ab
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import unittest

import slog


class FilterHasFieldTests(unittest.TestCase):

    def test_allows_if_field_is_there(self):
        log_obj = {
            'foo': False,
        }
        rule = slog.FilterHasField('foo')
        self.assertTrue(rule.allow(log_obj))

    def test_doesnt_allow_if_field_is_not_there(self):
        log_obj = {
            'foo': False,
        }
        rule = slog.FilterHasField('bar')
        self.assertFalse(rule.allow(log_obj))


class FilterFieldHasValueTests(unittest.TestCase):

    def test_allows_when_field_has_wanted_value(self):
        log_obj = {
            'foo': 'bar',
        }
        rule = slog.FilterFieldHasValue('foo', 'bar')
        self.assertTrue(rule.allow(log_obj))

    def test_doesnt_allow_when_field_has_unwanted_value(self):
        log_obj = {
            'foo': 'bar',
        }
        rule = slog.FilterFieldHasValue('foo', 'yo')
        self.assertFalse(rule.allow(log_obj))


class FilterFieldValueRegexpTests(unittest.TestCase):

    def test_allows_when_value_matches_regexp(self):
        log_obj = {
            'foo': 'bar',
        }
        rule = slog.FilterFieldValueRegexp('foo', 'b.r')
        self.assertTrue(rule.allow(log_obj))

    def test_allows_when_value_matches_regexp_if_stringified(self):
        log_obj = {
            'foo': 400,
        }
        rule = slog.FilterFieldValueRegexp('foo', '4.*')
        self.assertTrue(rule.allow(log_obj))

    def test_doesnt_allow_when_field_isnt_there(self):
        log_obj = {
            'blarf': 'yo',
        }
        rule = slog.FilterFieldValueRegexp('foo', 'b.r')
        self.assertFalse(rule.allow(log_obj))

    def test_doesnt_allow_when_value_doesnt_match_regexp(self):
        log_obj = {
            'foo': 'yo',
        }
        rule = slog.FilterFieldValueRegexp('foo', 'b.r')
        self.assertFalse(rule.allow(log_obj))


class FilterAllowTests(unittest.TestCase):

    def test_allows_always(self):
        rule = slog.FilterAllow()
        self.assertTrue(rule.allow(None))


class FilterDenyTests(unittest.TestCase):

    def test_allows_denies(self):
        rule = slog.FilterDeny()
        self.assertFalse(rule.allow(None))


class FilterIncludeTests(unittest.TestCase):

    def test_allows_if_rule_allows_and_no_include(self):
        allow = slog.FilterAllow()
        include = slog.FilterInclude({}, allow)
        self.assertTrue(include.allow(None))

    def test_denies_if_rule_denies_and_no_include(self):
        deny = slog.FilterDeny()
        include = slog.FilterInclude({}, deny)
        self.assertFalse(include.allow(None))

    def test_allows_if_rule_allows_and_include_is_true(self):
        allow = slog.FilterAllow()
        include = slog.FilterInclude({'include': True}, allow)
        self.assertTrue(include.allow({}))

    def test_denies_if_rule_denies_and_include_is_true(self):
        deny = slog.FilterDeny()
        include = slog.FilterInclude({'include': True}, deny)
        self.assertFalse(include.allow({}))

    def test_denies_if_rule_allows_and_include_is_false(self):
        allow = slog.FilterAllow()
        include = slog.FilterInclude({'include': False}, allow)
        self.assertFalse(include.allow({}))

    def test_allows_if_rule_denies_and_include_is_false(self):
        deny = slog.FilterDeny()
        include = slog.FilterInclude({'include': False}, deny)
        self.assertTrue(include.allow({}))


class FilterAnyTests(unittest.TestCase):

    def test_allows_if_any_rule_allows(self):
        rules = [slog.FilterAllow()]
        any_rule = slog.FilterAny(rules)
        self.assertTrue(any_rule.allow(None))

    def test_denies_if_all_rules_deny(self):
        rules = [slog.FilterDeny(), slog.FilterAllow()]
        any_rule = slog.FilterAny(rules)
        self.assertTrue(any_rule.allow(None))


class ConstructFilterTests(unittest.TestCase):

    def test_raises_error_if_no_filters(self):
        filters = []
        with self.assertRaises(Exception):
            slog.construct_log_filter(filters)

    def test_handles_field_wanted(self):
        filters = [
            {
                'field': 'msg_type',
            },
        ]

        rule = slog.construct_log_filter(filters)

        allowed = {
            'msg_type': 'info',
        }
        self.assertTrue(rule.allow(allowed))

        denied = {
            'blah_blah': 'http-response',
        }
        self.assertFalse(rule.allow(denied))

    def test_handles_field_value_wanted(self):
        filters = [
            {
                'field': 'msg_type',
                'value': 'info',
            },
        ]

        rule = slog.construct_log_filter(filters)

        allowed = {
            'msg_type': 'info',
        }
        self.assertTrue(rule.allow(allowed))

        denied = {
            'msg_type': 'http-response',
        }
        self.assertFalse(rule.allow(denied))

    def test_handles_regexp_match_wanted(self):
        filters = [
            {
                'field': 'status',
                'regexp': '^4'
            },
        ]

        rule = slog.construct_log_filter(filters)

        allowed = {
            'status': 400,
        }
        self.assertTrue(rule.allow(allowed))

        denied = {
            'status': 200,
        }
        self.assertFalse(rule.allow(denied))

    def test_handles_not_included(self):
        filters = [
            {
                'field': 'status',
                'value': '400',
                'include': False,
            },
        ]

        rule = slog.construct_log_filter(filters)

        allowed = {
            'status': '200',
        }
        self.assertTrue(rule.allow(allowed))

        denied = {
            'status': '400',
        }
        self.assertFalse(rule.allow(denied))

    def test_returns_compound_filter(self):
        filters = [
            {
                'field': 'msg_type',
            },
            {
                'field': 'msg_type',
                'value': 'debug',
            },
            {
                'field': 'status',
                'regexp': '^4',
                'include': False,
            },
        ]

        rule = slog.construct_log_filter(filters)

        allowed = {
            'msg_type': 'info',
        }
        self.assertTrue(rule.allow(allowed))

        also_allowed = {
            'msg_type': 'debug',
        }
        self.assertTrue(rule.allow(also_allowed))

        denied = {
            'status': '400',
        }
        self.assertFalse(rule.allow(denied))