Parse build events from stderr. (#779)

This commit is contained in:
Felix Fontein 2024-01-25 06:40:32 +01:00 committed by GitHub
parent b5391c7971
commit b5d085bb88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 103 additions and 2 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- "docker_compose_v2 - properly parse dry-run build events from ``stderr`` (https://github.com/ansible-collections/community.docker/issues/778, https://github.com/ansible-collections/community.docker/pull/779)."

View File

@ -47,6 +47,8 @@ DOCKER_STATUS_WORKING = frozenset((
'Recreate',
# Extras for pull events
'Pulling',
# Extras for build start events
'Building',
))
DOCKER_STATUS_PULL = frozenset((
'Pulled',
@ -166,6 +168,24 @@ _RE_SKIPPED_EVENT = re.compile(
r'$'
)
_RE_BUILD_START_EVENT = re.compile(
r'^'
r'\s*'
r'build service'
r'\s+'
r'(?P<resource_id>\S+)'
r'$'
)
_RE_BUILD_PROGRESS_EVENT = re.compile(
r'^'
r'\s*'
r'==>'
r'\s+'
r'(?P<msg>.*)'
r'$'
)
# The following needs to be kept in sync with the MINIMUM_VERSION compose_v2 docs fragment
MINIMUM_COMPOSE_VERSION = '2.18.0'
@ -215,6 +235,14 @@ def _extract_event(line):
'Skipped',
match.group('msg'),
)
match = _RE_BUILD_START_EVENT.match(line)
if match:
return Event(
ResourceType.SERVICE,
match.group('resource_id'),
'Building',
None,
)
return None
@ -281,6 +309,10 @@ def parse_events(stderr, dry_run=False, warn_function=None):
error_event = None
_warn_missing_dry_run_prefix(line, warn_missing_dry_run_prefix, warn_function)
continue
match = _RE_BUILD_PROGRESS_EVENT.match(line)
if match:
# Ignore this
continue
match = _RE_CONTINUE_EVENT.match(line)
if match:
# Continuing an existing event

View File

@ -379,6 +379,7 @@ actions:
- Removing
- Recreating
- Pulling
- Building
'''
import traceback

View File

@ -8,16 +8,82 @@ __metaclass__ = type
import pytest
from ansible_collections.community.docker.plugins.module_utils.compose_v2 import (
Event,
parse_events,
)
from .compose_v2_test_cases import EVENT_TEST_CASES
EXTRA_TEST_CASES = [
(
'2.24.2-manual-build-dry-run',
'2.24.2',
True,
' DRY-RUN MODE - build service foobar \n'
' DRY-RUN MODE - ==> ==> writing image dryRun-8843d7f92416211de9ebb963ff4ce28125932878 \n'
' DRY-RUN MODE - ==> ==> naming to my-python \n'
' DRY-RUN MODE - Network compose_default Creating\n'
' DRY-RUN MODE - Network compose_default Created\n'
' DRY-RUN MODE - Container compose-foobar-1 Creating\n'
' DRY-RUN MODE - Container compose-foobar-1 Created\n'
' DRY-RUN MODE - Container ompose-foobar-1 Starting\n'
' DRY-RUN MODE - Container ompose-foobar-1 Started\n',
[
Event(
'service',
'foobar',
'Building',
None,
),
Event(
'network',
'compose_default',
'Creating',
None,
),
Event(
'network',
'compose_default',
'Created',
None,
),
Event(
'container',
'compose-foobar-1',
'Creating',
None,
),
Event(
'container',
'compose-foobar-1',
'Created',
None,
),
Event(
'container',
'ompose-foobar-1',
'Starting',
None,
),
Event(
'container',
'ompose-foobar-1',
'Started',
None,
),
],
[],
),
]
_ALL_TEST_CASES = EVENT_TEST_CASES + EXTRA_TEST_CASES
@pytest.mark.parametrize(
'test_id, compose_version, dry_run, stderr, events, warnings',
EVENT_TEST_CASES,
ids=[tc[0] for tc in EVENT_TEST_CASES],
_ALL_TEST_CASES,
ids=[tc[0] for tc in _ALL_TEST_CASES],
)
def test_parse_events(test_id, compose_version, dry_run, stderr, events, warnings):
collected_warnings = []