Improve parsing of warnings and errors (#811)

* Add logfmt message parser.

* Parse logfmt formatted warnings.

* Follow-up for #810.

* Fix handling of warning and error messages.

* Make Python 2 compatible.

* Linting. Improving tests.
This commit is contained in:
Felix Fontein
2024-03-03 14:38:55 +01:00
committed by GitHub
parent 37e28b62d3
commit 6aea7efed9
6 changed files with 421 additions and 16 deletions
@@ -0,0 +1,100 @@
# 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 (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from ansible_collections.community.docker.plugins.module_utils._logfmt import (
InvalidLogFmt,
parse_line,
)
SUCCESS_TEST_CASES = [
(
'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 = [
(
'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, kwargs, result):
res = parse_line(line, **kwargs)
print(repr(res))
assert res == result
@pytest.mark.parametrize('line, kwargs, message', FAILURE_TEST_CASES)
def test_parse_line_success(line, kwargs, message):
with pytest.raises(InvalidLogFmt) as exc:
parse_line(line, **kwargs)
print(repr(exc.value.args[0]))
assert exc.value.args[0] == message
@@ -175,6 +175,48 @@ EXTRA_TEST_CASES = [
],
[],
),
(
# https://github.com/ansible-collections/community.docker/issues/787
'2.20.3-logrus-warn',
'2.20.3',
False,
'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"\n',
[],
[
'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
],
),
(
# https://github.com/ansible-collections/community.docker/issues/807
'2.20.3-image-warning-error',
'2.20.3',
False,
" dummy3 Warning \n"
" dummy2 Warning \n"
" dummy Error \n"
" dummy4 Warning Foo bar \n"
" dummy5 Error Bar baz bam \n",
[
Event(
'unknown',
'dummy',
'Error',
None,
),
Event(
'unknown',
'dummy5',
'Error',
'Bar baz bam',
),
],
[
'Unspecified warning for dummy3',
'Unspecified warning for dummy2',
'dummy4: Foo bar',
],
),
]
_ALL_TEST_CASES = EVENT_TEST_CASES + EXTRA_TEST_CASES
@@ -193,5 +235,8 @@ def test_parse_events(test_id, compose_version, dry_run, stderr, events, warning
collected_events = parse_events(stderr, dry_run=dry_run, warn_function=collect_warning)
assert events == collected_events
assert warnings == collected_warnings
print(collected_events)
print(collected_warnings)
assert collected_events == events
assert collected_warnings == warnings