community.docker/tests/unit/plugins/module_utils/test__logfmt.py
Felix Fontein 6ad4bfcd40
Add typing information, 2/n (#1178)
* Add typing to Docker Stack modules. Clean modules up.

* Add typing to Docker Swarm modules.

* Add typing to unit tests.

* Add more typing.

* Add ignore.txt entries.
2025-10-25 01:16:04 +02:00

104 lines
3.1 KiB
Python

# Copyright (c) 2024, Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
import typing as t
import pytest
from ansible_collections.community.docker.plugins.module_utils._logfmt import (
InvalidLogFmt,
parse_line,
)
SUCCESS_TEST_CASES: list[tuple[str, dict[str, t.Any], dict[str, t.Any]]] = [
(
'time="2024-02-02T08:14:10+01:00" level=warning msg="a network with name influxNetwork exists but was not'
' created for project \\"influxdb\\".\\nSet `external: true` to use an existing network"',
{},
{
"time": "2024-02-02T08:14:10+01:00",
"level": "warning",
"msg": 'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
},
),
(
'time="2024-02-02T08:14:10+01:00" level=warning msg="a network with name influxNetwork exists but was not'
' created for project \\"influxdb\\".\\nSet `external: true` to use an existing network"',
{"logrus_mode": True},
{
"time": "2024-02-02T08:14:10+01:00",
"level": "warning",
"msg": 'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
},
),
(
'foo=bar a=14 baz="hello kitty" cool%story=bro f %^asdf',
{},
{
"foo": "bar",
"a": "14",
"baz": "hello kitty",
"cool%story": "bro",
"f": None,
"%^asdf": None,
},
),
(
'{"foo":"bar"}',
{},
{
"{": None,
"foo": None,
":": None,
"bar": None,
"}": None,
},
),
]
FAILURE_TEST_CASES: list[tuple[str, dict[str, t.Any], str]] = [
(
'foo=bar a=14 baz="hello kitty" cool%story=bro f %^asdf',
{"logrus_mode": True},
'Key must always be followed by "=" in logrus mode',
),
(
"{}",
{"logrus_mode": True},
'Key must always be followed by "=" in logrus mode',
),
(
"[]",
{"logrus_mode": True},
'Key must always be followed by "=" in logrus mode',
),
(
'{"foo=bar": "baz=bam"}',
{"logrus_mode": True},
'Key must always be followed by "=" in logrus mode',
),
]
@pytest.mark.parametrize("line, kwargs, result", SUCCESS_TEST_CASES)
def test_parse_line_success(
line: str, kwargs: dict[str, t.Any], result: dict[str, t.Any]
) -> None:
res = parse_line(line, **kwargs)
print(repr(res))
assert res == result
@pytest.mark.parametrize("line, kwargs, message", FAILURE_TEST_CASES)
def test_parse_line_failure(line: str, kwargs: dict[str, t.Any], message: str) -> None:
with pytest.raises(InvalidLogFmt) as exc:
parse_line(line, **kwargs)
print(repr(exc.value.args[0]))
assert exc.value.args[0] == message