Update unit tests for stream_output to match the behaviour of a docker-py response.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-08-24 13:16:13 -04:00
parent 7e4c3142d7
commit 71ff872e8e
8 changed files with 29 additions and 21 deletions

View File

@ -4,13 +4,12 @@ from __future__ import unicode_literals
import sys import sys
from itertools import cycle from itertools import cycle
import six
from six import next from six import next
from compose import utils
from . import colors from . import colors
from .multiplexer import Multiplexer from .multiplexer import Multiplexer
from .utils import split_buffer from .utils import split_buffer
from compose import utils
class LogPrinter(object): class LogPrinter(object):

View File

@ -1,8 +1,9 @@
import codecs
import json import json
import six import six
from compose import utils
class StreamOutputError(Exception): class StreamOutputError(Exception):
pass pass
@ -10,14 +11,13 @@ class StreamOutputError(Exception):
def stream_output(output, stream): def stream_output(output, stream):
is_terminal = hasattr(stream, 'isatty') and stream.isatty() is_terminal = hasattr(stream, 'isatty') and stream.isatty()
if not six.PY3: stream = utils.get_output_stream(stream)
stream = codecs.getwriter('utf-8')(stream)
all_events = [] all_events = []
lines = {} lines = {}
diff = 0 diff = 0
for chunk in output: for chunk in output:
if six.PY3 and not isinstance(chunk, str): if six.PY3:
chunk = chunk.decode('utf-8') chunk = chunk.decode('utf-8')
event = json.loads(chunk) event = json.loads(chunk)
all_events.append(event) all_events.append(event)

View File

@ -324,11 +324,11 @@ class Project(object):
else: else:
service_names = self.service_names service_names = self.service_names
containers = filter(None, [ containers = list(filter(None, [
Container.from_ps(self.client, container) Container.from_ps(self.client, container)
for container in self.client.containers( for container in self.client.containers(
all=stopped, all=stopped,
filters={'label': self.labels(one_off=one_off)})]) filters={'label': self.labels(one_off=one_off)})]))
def matches_service_names(container): def matches_service_names(container):
return container.labels.get(LABEL_SERVICE) in service_names return container.labels.get(LABEL_SERVICE) in service_names

View File

@ -710,6 +710,8 @@ class Service(object):
log.info('Building %s...' % self.name) log.info('Building %s...' % self.name)
path = self.options['build'] path = self.options['build']
# python2 os.path() doesn't support unicode, so we need to encode it to
# a byte string
if not six.PY3: if not six.PY3:
path = path.encode('utf8') path = path.encode('utf8')

View File

@ -5,6 +5,7 @@ import logging
import sys import sys
from threading import Thread from threading import Thread
import six
from docker.errors import APIError from docker.errors import APIError
from six.moves.queue import Empty from six.moves.queue import Empty
from six.moves.queue import Queue from six.moves.queue import Queue
@ -18,7 +19,7 @@ def parallel_execute(objects, obj_callable, msg_index, msg):
For a given list of objects, call the callable passing in the first For a given list of objects, call the callable passing in the first
object we give it. object we give it.
""" """
stream = codecs.getwriter('utf-8')(sys.stdout) stream = get_output_stream()
lines = [] lines = []
errors = {} errors = {}
@ -70,6 +71,12 @@ def parallel_execute(objects, obj_callable, msg_index, msg):
stream.write("ERROR: for {} {} \n".format(error, errors[error])) stream.write("ERROR: for {} {} \n".format(error, errors[error]))
def get_output_stream(stream=sys.stdout):
if six.PY3:
return stream
return codecs.getwriter('utf-8')(stream)
def write_out_msg(stream, lines, msg_index, msg, status="done"): def write_out_msg(stream, lines, msg_index, msg, status="done"):
""" """
Using special ANSI code characters we can write out the msg over the top of Using special ANSI code characters we can write out the msg over the top of

View File

@ -1,8 +1,8 @@
import unittest import unittest
from docker.errors import APIError from docker.errors import APIError
from mock import Mock
from .. import mock
from .testcases import DockerClientTestCase from .testcases import DockerClientTestCase
from compose import legacy from compose import legacy
from compose.project import Project from compose.project import Project
@ -66,7 +66,7 @@ class UtilitiesTestCase(unittest.TestCase):
) )
def test_get_legacy_containers(self): def test_get_legacy_containers(self):
client = Mock() client = mock.Mock()
client.containers.return_value = [ client.containers.return_value = [
{ {
"Id": "abc123", "Id": "abc123",

View File

@ -10,27 +10,27 @@ from tests import unittest
class ProgressStreamTestCase(unittest.TestCase): class ProgressStreamTestCase(unittest.TestCase):
def test_stream_output(self): def test_stream_output(self):
output = [ output = [
'{"status": "Downloading", "progressDetail": {"current": ' b'{"status": "Downloading", "progressDetail": {"current": '
'31019763, "start": 1413653874, "total": 62763875}, ' b'31019763, "start": 1413653874, "total": 62763875}, '
'"progress": "..."}', b'"progress": "..."}',
] ]
events = progress_stream.stream_output(output, StringIO()) events = progress_stream.stream_output(output, StringIO())
self.assertEqual(len(events), 1) self.assertEqual(len(events), 1)
def test_stream_output_div_zero(self): def test_stream_output_div_zero(self):
output = [ output = [
'{"status": "Downloading", "progressDetail": {"current": ' b'{"status": "Downloading", "progressDetail": {"current": '
'0, "start": 1413653874, "total": 0}, ' b'0, "start": 1413653874, "total": 0}, '
'"progress": "..."}', b'"progress": "..."}',
] ]
events = progress_stream.stream_output(output, StringIO()) events = progress_stream.stream_output(output, StringIO())
self.assertEqual(len(events), 1) self.assertEqual(len(events), 1)
def test_stream_output_null_total(self): def test_stream_output_null_total(self):
output = [ output = [
'{"status": "Downloading", "progressDetail": {"current": ' b'{"status": "Downloading", "progressDetail": {"current": '
'0, "start": 1413653874, "total": null}, ' b'0, "start": 1413653874, "total": null}, '
'"progress": "..."}', b'"progress": "..."}',
] ]
events = progress_stream.stream_output(output, StringIO()) events = progress_stream.stream_output(output, StringIO())
self.assertEqual(len(events), 1) self.assertEqual(len(events), 1)

View File

@ -280,7 +280,7 @@ class ServiceTest(unittest.TestCase):
def test_build_does_not_pull(self): def test_build_does_not_pull(self):
self.mock_client.build.return_value = [ self.mock_client.build.return_value = [
'{"stream": "Successfully built 12345"}', b'{"stream": "Successfully built 12345"}',
] ]
service = Service('foo', client=self.mock_client, build='.') service = Service('foo', client=self.mock_client, build='.')