소스 검색

Update event field names to match the new API fields.

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 10 년 전
부모
커밋
d3cd038b84
4개의 변경된 파일65개의 추가작업 그리고 19개의 파일을 삭제
  1. 5 2
      compose/cli/main.py
  2. 8 4
      compose/project.py
  3. 21 1
      tests/acceptance/cli_test.py
  4. 31 12
      tests/unit/project_test.py

+ 5 - 2
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):
         """

+ 8 - 4
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)

+ 21 - 1
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')

+ 31 - 12
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),
             },
         ]