Skip to content

Commit

Permalink
Parse build events from stderr. (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored Jan 25, 2024
1 parent b5391c7 commit b5d085b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/779-compose-v2-build.yml
Original file line number Diff line number Diff line change
@@ -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)."
32 changes: 32 additions & 0 deletions plugins/module_utils/compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
'Recreate',
# Extras for pull events
'Pulling',
# Extras for build start events
'Building',
))
DOCKER_STATUS_PULL = frozenset((
'Pulled',
Expand Down Expand Up @@ -166,6 +168,24 @@ def from_docker_compose_event(cls, resource_type):
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'

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/docker_compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@
- Removing
- Recreating
- Pulling
- Building
'''

import traceback
Expand Down
70 changes: 68 additions & 2 deletions tests/unit/plugins/module_utils/test_compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,82 @@
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 = []
Expand Down

0 comments on commit b5d085b

Please sign in to comment.