From d3cd038b845d9708c42b9281253be342e2fe97d0 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 8 Jan 2016 16:54:35 -0500 Subject: [PATCH] Update event field names to match the new API fields. Signed-off-by: Daniel Nephin --- compose/cli/main.py | 7 ++++-- compose/project.py | 12 ++++++---- tests/acceptance/cli_test.py | 22 +++++++++++++++++- tests/unit/project_test.py | 43 ++++++++++++++++++++++++++---------- 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index d99816cfa..d10b95823 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -256,8 +256,10 @@ class TopLevelCommand(DocoptCommand): --json Output events as a stream of json objects """ def format_event(event): - return ("{time}: service={service} event={event} " - "container={container} image={image}").format(**event) + attributes = ["%s=%s" % item for item in event['attributes'].items()] + return ("{time} {type} {action} {id} ({attrs})").format( + attrs=", ".join(sorted(attributes)), + **event) def json_format_event(event): event['time'] = event['time'].isoformat() @@ -266,6 +268,7 @@ class TopLevelCommand(DocoptCommand): for event in project.events(): formatter = json_format_event if options['--json'] else format_event print(formatter(event)) + sys.stdout.flush() def help(self, project, options): """ diff --git a/compose/project.py b/compose/project.py index b4eed7c8d..50f991be7 100644 --- a/compose/project.py +++ b/compose/project.py @@ -282,11 +282,15 @@ class Project(object): time = time.replace( microsecond=microseconds_from_time_nano(event['timeNano'])) return { - 'service': container.service, - 'event': event['status'], - 'container': container.id, - 'image': event['from'], 'time': time, + 'type': 'container', + 'action': event['status'], + 'id': container.id, + 'service': container.service, + 'attributes': { + 'name': container.name, + 'image': event['from'], + } } service_names = set(self.service_names) diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 322d9b5a8..db3e1b438 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from __future__ import unicode_literals +import datetime import json import os import shlex @@ -864,7 +865,26 @@ class CLITestCase(DockerClientTestCase): os.kill(events_proc.pid, signal.SIGINT) result = wait_on_process(events_proc, returncode=1) lines = [json.loads(line) for line in result.stdout.rstrip().split('\n')] - assert [e['event'] for e in lines] == ['create', 'start', 'create', 'start'] + assert [e['action'] for e in lines] == ['create', 'start', 'create', 'start'] + + def test_events_human_readable(self): + events_proc = start_process(self.base_dir, ['events']) + self.dispatch(['up', '-d', 'simple']) + wait_on_condition(ContainerCountCondition(self.project, 1)) + + os.kill(events_proc.pid, signal.SIGINT) + result = wait_on_process(events_proc, returncode=1) + lines = result.stdout.rstrip().split('\n') + assert len(lines) == 2 + + container, = self.project.containers() + expected_template = ( + ' container {} {} (image=busybox:latest, ' + 'name=simplecomposefile_simple_1)') + + assert expected_template.format('create', container.id) in lines[0] + assert expected_template.format('start', container.id) in lines[1] + assert lines[0].startswith(datetime.date.today().isoformat()) def test_env_file_relative_to_compose_file(self): config_path = os.path.abspath('tests/fixtures/env-file/docker-compose.yml') diff --git a/tests/unit/project_test.py b/tests/unit/project_test.py index a4b61b64d..c8590a1f9 100644 --- a/tests/unit/project_test.py +++ b/tests/unit/project_test.py @@ -238,12 +238,19 @@ class ProjectTest(unittest.TestCase): def get_container(cid): if cid == 'abcde': - labels = {LABEL_SERVICE: 'web'} + name = 'web' + labels = {LABEL_SERVICE: name} elif cid == 'ababa': - labels = {LABEL_SERVICE: 'db'} + name = 'db' + labels = {LABEL_SERVICE: name} else: labels = {} - return {'Id': cid, 'Config': {'Labels': labels}} + name = '' + return { + 'Id': cid, + 'Config': {'Labels': labels}, + 'Name': '/project_%s_1' % name, + } self.mock_client.inspect_container.side_effect = get_container @@ -254,24 +261,36 @@ class ProjectTest(unittest.TestCase): assert not list(events) assert events_list == [ { + 'type': 'container', 'service': 'web', - 'event': 'create', - 'container': 'abcde', - 'image': 'example/image', + 'action': 'create', + 'id': 'abcde', + 'attributes': { + 'name': 'project_web_1', + 'image': 'example/image', + }, 'time': dt_with_microseconds(1420092061, 2), }, { + 'type': 'container', 'service': 'web', - 'event': 'attach', - 'container': 'abcde', - 'image': 'example/image', + 'action': 'attach', + 'id': 'abcde', + 'attributes': { + 'name': 'project_web_1', + 'image': 'example/image', + }, 'time': dt_with_microseconds(1420092061, 3), }, { + 'type': 'container', 'service': 'db', - 'event': 'create', - 'container': 'ababa', - 'image': 'example/db', + 'action': 'create', + 'id': 'ababa', + 'attributes': { + 'name': 'project_db_1', + 'image': 'example/db', + }, 'time': dt_with_microseconds(1420092061, 4), }, ]