File: test_csv_parser.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 (187 lines) | stat: -rw-r--r-- 6,584 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
#!/usr/bin/env python
#############################################################################
# Copyright (c) 2020 One Identity
# Copyright (c) 2021 Xiaoyu Qiu
#
# 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.
#
#############################################################################
import pytest


test_parameters_raw = [
    {
        "id": "simple",
        "input_message": "foo,bar",
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "quoted",
        "input_message": '''foo,"bar"''',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "simple-quotes",
        "quotes": "~^",
        "input_message": '''~foo~,^bar^''',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "quote-pairs",
        "quote-pairs": "><~~",
        "input_message": '''~foo~,>bar<''',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "escape-none",
        "input_message": "foo,bar",
        "dialect": "escape-none",
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "escape-double-char",
        "input_message": '''foo,"b""a""r"''',
        "dialect": "escape-double-char",
        "expected_value": 'foo=foo bar=b"a"r',
    },
    {
        "id": "escape-backslash",
        "input_message": r'''foo,"b\"a\"r\a"''',
        "dialect": "escape-backslash",
        "expected_value": 'foo=foo bar=b"a"ra',
    },
    {
        "id": "escape-backslash-with-sequences",
        "input_message": r'''foo,"b\"a\"r\a"''',
        "dialect": "escape-backslash-with-sequences",
        "expected_value": 'foo=foo bar=b"a"r\a',
    },
    {
        "id": "null-value",
        "input_message": "foo,NULL",
        "null": "NULL",
        "expected_value": "foo=foo bar=>>unset<<",
    },
    {
        "id": "delimiter-character",
        "input_message": "foo^bar",
        "delimiters": '"^"',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "delimiter-character2",
        "input_message": "foo^bar",
        "delimiters": 'chars("^")',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "delimiter-string",
        "input_message": "foo^^^bar",
        "delimiters": 'strings("^^^", "~~~")',
        "expected_value": "foo=foo bar=bar",
    },
    {
        "id": "delimiter-string2",
        "input_message": "foo~~~bar",
        "delimiters": 'strings("^^^", "~~~")',
        "expected_value": "foo=foo bar=bar",
    },
]


def add_optional_arg(config, args, testcase, key):
    if key in testcase:
        args[key] = testcase[key]


def add_optional_string_arg(config, args, testcase, key):
    if key in testcase:
        args[key] = config.stringify(testcase[key])


@pytest.mark.parametrize(
    "testcase", test_parameters_raw,
    ids=map(lambda tc: tc['id'], test_parameters_raw),
)
def test_csv_parser(config, syslog_ng, testcase):
    config.update_global_options(stats_level=1)
    generator_source = config.create_example_msg_generator_source(num=1, template=config.stringify(testcase["input_message"]))
    csv_parser_args = {}

    csv_parser_args['delimiters'] = config.stringify(',')
    add_optional_arg(config, csv_parser_args, testcase, 'dialect')
    add_optional_string_arg(config, csv_parser_args, testcase, 'quotes')
    add_optional_string_arg(config, csv_parser_args, testcase, 'quote-pairs')
    add_optional_string_arg(config, csv_parser_args, testcase, 'null')
    add_optional_arg(config, csv_parser_args, testcase, 'delimiters')
    csv_parser = config.create_csv_parser(prefix=config.stringify("prefix."), columns='''"foo", "bar"''', **csv_parser_args)

    file_destination = config.create_file_destination(file_name="output.log", template=config.stringify("foo=${prefix.foo:->>unset<<} bar=${prefix.bar:->>unset<<}\n"))
    config.create_logpath(statements=[generator_source, csv_parser, file_destination])

    syslog_ng.start(config)

    assert file_destination.read_log().strip() == testcase['expected_value']
    assert csv_parser.get_query().get('discarded', -1) == 0


def test_csv_parser_into_matches(config, syslog_ng):
    config.update_global_options(stats_level=1)
    generator_source = config.create_example_msg_generator_source(num=1, template=config.stringify("FOO,BAR,BAZ"))

    csv_parser = config.create_csv_parser(delimiters=config.stringify(","))

    file_destination = config.create_file_destination(file_name="output.log", template=config.stringify("$*\n"))
    config.create_logpath(statements=[generator_source, csv_parser, file_destination])

    syslog_ng.start(config)

    assert file_destination.read_log().strip() == "FOO,BAR,BAZ"
    assert csv_parser.get_query().get('discarded', -1) == 0


test_parameters_drop_invalid = [
    {
        "id": "too_many_columns_in_input",
        "input_message": "foo,bar,baz",
        "columns": "foo,bar",
    },
    {
        "id": "type_does_not_match",
        "input_message": "foo,bar",
        "columns": "int(foo),bar",
    },
]


@pytest.mark.parametrize(
    "testcase", test_parameters_drop_invalid,
    ids=map(lambda tc: tc['id'], test_parameters_drop_invalid),
)
def test_csv_parser_drop_invalid(config, syslog_ng, testcase):
    config.update_global_options(stats_level=1)
    generator_source = config.create_example_msg_generator_source(num=1, template=config.stringify(testcase["input_message"]))

    csv_parser = config.create_csv_parser(delimiters=config.stringify(","), drop_invalid="yes", columns=testcase["columns"])

    file_destination = config.create_file_destination(file_name="output.log", template=config.stringify("$*\n"))
    config.create_logpath(statements=[generator_source, csv_parser, file_destination])

    syslog_ng.start(config)

    assert csv_parser.get_query().get('discarded', -1) == 1