Skip to content

Commit

Permalink
Update event field names to match the new API fields.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <dnephin@docker.com>
  • Loading branch information
dnephin committed Jan 11, 2016
1 parent d1d3969 commit d3cd038
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
7 changes: 5 additions & 2 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ def events(self, project, options):
--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()
Expand All @@ -266,6 +268,7 @@ def json_format_event(event):
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):
"""
Expand Down
12 changes: 8 additions & 4 deletions compose/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,15 @@ def build_container_event(event, container):
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)
Expand Down
22 changes: 21 additions & 1 deletion tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import datetime
import json
import os
import shlex
Expand Down Expand Up @@ -864,7 +865,26 @@ def test_events_json(self):
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')
Expand Down
43 changes: 31 additions & 12 deletions tests/unit/project_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,19 @@ def dt_with_microseconds(dt, us):

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

Expand All @@ -254,24 +261,36 @@ def get_container(cid):
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),
},
]
Expand Down

0 comments on commit d3cd038

Please sign in to comment.