Debian Codegraph @

ansible_collections/community/docker/tests/unit/plugins/module_utils/test__logfmt.py

raw
1 # Copyright (c) 2024, Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import typing as t
8
9 import pytest
10
11 from ansible_collections.community.docker.plugins.module_utils._logfmt import (
12 InvalidLogFmt,
13 parse_line,
14 )
15
16 SUCCESS_TEST_CASES: list[tuple[str, dict[str, t.Any], dict[str, t.Any]]] = [
17 (
18 'time="2024-02-02T08:14:10+01:00" level=warning msg="a network with name influxNetwork exists but was not'
19 ' created for project \\"influxdb\\".\\nSet `external: true` to use an existing network"',
20 {},
21 {
22 "time": "2024-02-02T08:14:10+01:00",
23 "level": "warning",
24 "msg": 'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
25 },
26 ),
27 (
28 'time="2024-02-02T08:14:10+01:00" level=warning msg="a network with name influxNetwork exists but was not'
29 ' created for project \\"influxdb\\".\\nSet `external: true` to use an existing network"',
30 {"logrus_mode": True},
31 {
32 "time": "2024-02-02T08:14:10+01:00",
33 "level": "warning",
34 "msg": 'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
35 },
36 ),
37 (
38 'foo=bar a=14 baz="hello kitty" cool%story=bro f %^asdf',
39 {},
40 {
41 "foo": "bar",
42 "a": "14",
43 "baz": "hello kitty",
44 "cool%story": "bro",
45 "f": None,
46 "%^asdf": None,
47 },
48 ),
49 (
50 '{"foo":"bar"}',
51 {},
52 {
53 "{": None,
54 "foo": None,
55 ":": None,
56 "bar": None,
57 "}": None,
58 },
59 ),
60 ]
61
62
63 FAILURE_TEST_CASES: list[tuple[str, dict[str, t.Any], str]] = [
64 (
65 'foo=bar a=14 baz="hello kitty" cool%story=bro f %^asdf',
66 {"logrus_mode": True},
67 'Key must always be followed by "=" in logrus mode',
68 ),
69 (
70 "{}",
71 {"logrus_mode": True},
72 'Key must always be followed by "=" in logrus mode',
73 ),
74 (
75 "[]",
76 {"logrus_mode": True},
77 'Key must always be followed by "=" in logrus mode',
78 ),
79 (
80 '{"foo=bar": "baz=bam"}',
81 {"logrus_mode": True},
82 'Key must always be followed by "=" in logrus mode',
83 ),
84 ]
85
86
87 @pytest.mark.parametrize("line, kwargs, result", SUCCESS_TEST_CASES)
88 def test_parse_line_success(
89 line: str, kwargs: dict[str, t.Any], result: dict[str, t.Any]
90 ) -> None:
91 res = parse_line(line, **kwargs)
92 print(repr(res))
93 assert res == result
94
95
96 @pytest.mark.parametrize("line, kwargs, message", FAILURE_TEST_CASES)
97 def test_parse_line_failure(line: str, kwargs: dict[str, t.Any], message: str) -> None:
98 with pytest.raises(InvalidLogFmt) as exc:
99 parse_line(line, **kwargs)
100
101 print(repr(exc.value.args[0]))
102 assert exc.value.args[0] == message