فهرست منبع

Merge pull request #440 from dnephin/python3

Support python 3
Aanand Prasad 10 سال پیش
والد
کامیت
fc63454c99

+ 19 - 0
Dockerfile

@@ -3,6 +3,7 @@ FROM debian:wheezy
 RUN set -ex; \
     apt-get update -qq; \
     apt-get install -y \
+        locales \
         gcc \
         make \
         zlib1g \
@@ -30,6 +31,18 @@ RUN set -ex; \
     rm -rf /Python-2.7.9; \
     rm Python-2.7.9.tgz
 
+# Build python 3.4 from source
+RUN set -ex; \
+    curl -LO https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz; \
+    tar -xzf Python-3.4.3.tgz; \
+    cd Python-3.4.3; \
+    ./configure --enable-shared; \
+    make; \
+    make install; \
+    cd ..; \
+    rm -rf /Python-3.4.3; \
+    rm Python-3.4.3.tgz
+
 # Make libpython findable
 ENV LD_LIBRARY_PATH /usr/local/lib
 
@@ -49,6 +62,10 @@ RUN set -ex; \
     rm -rf pip-7.0.1; \
     rm pip-7.0.1.tar.gz
 
+# Python3 requires a valid locale
+RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen
+ENV LANG en_US.UTF-8
+
 ENV ALL_DOCKER_VERSIONS 1.7.1 1.8.1
 
 RUN set -ex; \
@@ -63,6 +80,8 @@ RUN ln -s /usr/local/bin/docker-1.7.1 /usr/local/bin/docker
 RUN useradd -d /home/user -m -s /bin/bash user
 WORKDIR /code/
 
+RUN pip install tox
+
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 

+ 5 - 2
compose/cli/log_printer.py

@@ -4,9 +4,12 @@ from __future__ import unicode_literals
 import sys
 from itertools import cycle
 
+from six import next
+
 from . import colors
 from .multiplexer import Multiplexer
 from .utils import split_buffer
+from compose import utils
 
 
 class LogPrinter(object):
@@ -15,7 +18,7 @@ class LogPrinter(object):
         self.attach_params = attach_params or {}
         self.prefix_width = self._calculate_prefix_width(containers)
         self.generators = self._make_log_generators(monochrome)
-        self.output = output
+        self.output = utils.get_output_stream(output)
 
     def run(self):
         mux = Multiplexer(self.generators)
@@ -52,7 +55,7 @@ class LogPrinter(object):
         return generators
 
     def _make_log_generator(self, container, color_fn):
-        prefix = color_fn(self._generate_prefix(container)).encode('utf-8')
+        prefix = color_fn(self._generate_prefix(container))
         # Attach to container before log printer starts running
         line_generator = split_buffer(self._attach(container), '\n')
 

+ 2 - 1
compose/cli/utils.py

@@ -9,6 +9,7 @@ import ssl
 import subprocess
 
 from docker import version as docker_py_version
+from six.moves import input
 
 from .. import __version__
 
@@ -23,7 +24,7 @@ def yesno(prompt, default=None):
     Unrecognised input (anything other than "y", "n", "yes",
     "no" or "") will return None.
     """
-    answer = raw_input(prompt).strip().lower()
+    answer = input(prompt).strip().lower()
 
     if answer == "y" or answer == "yes":
         return True

+ 1 - 1
compose/config/validation.py

@@ -150,7 +150,7 @@ def process_errors(errors):
                 config_key = error.path[0]
                 required.append("Service '{}' option '{}' is invalid, {}".format(service_name, config_key, _clean_error_message(error.message)))
             elif error.validator == 'dependencies':
-                dependency_key = error.validator_value.keys()[0]
+                dependency_key = list(error.validator_value.keys())[0]
                 required_keys = ",".join(error.validator_value[dependency_key])
                 required.append("Invalid '{}' configuration for '{}' service: when defining '{}' you must set '{}' as well".format(
                     dependency_key, service_name, dependency_key, required_keys))

+ 8 - 5
compose/progress_stream.py

@@ -1,6 +1,8 @@
-import codecs
 import json
-import os
+
+import six
+
+from compose import utils
 
 
 class StreamOutputError(Exception):
@@ -8,13 +10,15 @@ class StreamOutputError(Exception):
 
 
 def stream_output(output, stream):
-    is_terminal = hasattr(stream, 'fileno') and os.isatty(stream.fileno())
-    stream = codecs.getwriter('utf-8')(stream)
+    is_terminal = hasattr(stream, 'isatty') and stream.isatty()
+    stream = utils.get_output_stream(stream)
     all_events = []
     lines = {}
     diff = 0
 
     for chunk in output:
+        if six.PY3:
+            chunk = chunk.decode('utf-8')
         event = json.loads(chunk)
         all_events.append(event)
 
@@ -55,7 +59,6 @@ def print_output_event(event, stream, is_terminal):
         # erase current line
         stream.write("%c[2K\r" % 27)
         terminator = "\r"
-        pass
     elif 'progressDetail' in event:
         return
 

+ 4 - 3
compose/project.py

@@ -17,6 +17,7 @@ from .legacy import check_for_legacy_containers
 from .service import Service
 from .utils import parallel_execute
 
+
 log = logging.getLogger(__name__)
 
 
@@ -323,11 +324,11 @@ class Project(object):
         else:
             service_names = self.service_names
 
-        containers = filter(None, [
+        containers = list(filter(None, [
             Container.from_ps(self.client, container)
             for container in self.client.containers(
                 all=stopped,
-                filters={'label': self.labels(one_off=one_off)})])
+                filters={'label': self.labels(one_off=one_off)})]))
 
         def matches_service_names(container):
             return container.labels.get(LABEL_SERVICE) in service_names
@@ -339,7 +340,7 @@ class Project(object):
                 self.service_names,
             )
 
-        return filter(matches_service_names, containers)
+        return [c for c in containers if matches_service_names(c)]
 
     def _inject_deps(self, acc, service):
         dep_names = service.get_dependency_names()

+ 10 - 6
compose/service.py

@@ -103,11 +103,11 @@ class Service(object):
     def containers(self, stopped=False, one_off=False, filters={}):
         filters.update({'label': self.labels(one_off=one_off)})
 
-        containers = filter(None, [
+        containers = list(filter(None, [
             Container.from_ps(self.client, container)
             for container in self.client.containers(
                 all=stopped,
-                filters=filters)])
+                filters=filters)]))
 
         if not containers:
             check_for_legacy_containers(
@@ -709,7 +709,11 @@ class Service(object):
     def build(self, no_cache=False):
         log.info('Building %s...' % self.name)
 
-        path = six.binary_type(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:
+            path = path.encode('utf8')
 
         build_output = self.client.build(
             path=path,
@@ -724,7 +728,7 @@ class Service(object):
         try:
             all_events = stream_output(build_output, sys.stdout)
         except StreamOutputError as e:
-            raise BuildError(self, unicode(e))
+            raise BuildError(self, six.text_type(e))
 
         # Ensure the HTTP connection is not reused for another
         # streaming command, as the Docker daemon can sometimes
@@ -840,7 +844,7 @@ def merge_volume_bindings(volumes_option, previous_container):
         volume_bindings.update(
             get_container_data_volumes(previous_container, volumes_option))
 
-    return volume_bindings.values()
+    return list(volume_bindings.values())
 
 
 def get_container_data_volumes(container, volumes_option):
@@ -853,7 +857,7 @@ def get_container_data_volumes(container, volumes_option):
     container_volumes = container.get('Volumes') or {}
     image_volumes = container.image_config['ContainerConfig'].get('Volumes') or {}
 
-    for volume in set(volumes_option + image_volumes.keys()):
+    for volume in set(volumes_option + list(image_volumes)):
         volume = parse_volume_spec(volume)
         # No need to preserve host volumes
         if volume.external:

+ 11 - 4
compose/utils.py

@@ -3,11 +3,12 @@ import hashlib
 import json
 import logging
 import sys
-from Queue import Empty
-from Queue import Queue
 from threading import Thread
 
+import six
 from docker.errors import APIError
+from six.moves.queue import Empty
+from six.moves.queue import Queue
 
 
 log = logging.getLogger(__name__)
@@ -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
     object we give it.
     """
-    stream = codecs.getwriter('utf-8')(sys.stdout)
+    stream = get_output_stream(sys.stdout)
     lines = []
     errors = {}
 
@@ -70,6 +71,12 @@ def parallel_execute(objects, obj_callable, msg_index, msg):
             stream.write("ERROR: for {}  {} \n".format(error, errors[error]))
 
 
+def get_output_stream(stream):
+    if six.PY3:
+        return stream
+    return codecs.getwriter('utf-8')(stream)
+
+
 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
@@ -97,5 +104,5 @@ def write_out_msg(stream, lines, msg_index, msg, status="done"):
 def json_hash(obj):
     dump = json.dumps(obj, sort_keys=True, separators=(',', ':'))
     h = hashlib.sha256()
-    h.update(dump)
+    h.update(dump.encode('utf8'))
     return h.hexdigest()

+ 0 - 1
requirements-dev.txt

@@ -4,4 +4,3 @@ git+https://github.com/pyinstaller/pyinstaller.git@12e40471c77f588ea5be352f7219c
 mock >= 1.0.1
 nose==1.3.4
 pep8==1.6.1
-unittest2==0.8.0

+ 1 - 1
script/test-versions

@@ -24,5 +24,5 @@ for version in $DOCKER_VERSIONS; do
     -e "DOCKER_DAEMON_ARGS" \
     --entrypoint="script/dind" \
     "$TAG" \
-    script/wrapdocker nosetests --with-coverage --cover-branches --cover-package=compose --cover-erase --cover-html-dir=coverage-html --cover-html "$@"
+    script/wrapdocker tox -e py27,py34 -- "$@"
 done

+ 2 - 2
setup.py

@@ -41,15 +41,15 @@ install_requires = [
 
 
 tests_require = [
-    'mock >= 1.0.1',
     'nose',
-    'pyinstaller',
     'flake8',
 ]
 
 
 if sys.version_info < (2, 7):
     tests_require.append('unittest2')
+if sys.version_info[:1] < (3,):
+    tests_require.append('mock >= 1.0.1')
 
 
 setup(

+ 5 - 0
tests/__init__.py

@@ -4,3 +4,8 @@ if sys.version_info >= (2, 7):
     import unittest  # NOQA
 else:
     import unittest2 as unittest  # NOQA
+
+try:
+    from unittest import mock
+except ImportError:
+    import mock  # NOQA

+ 24 - 24
tests/integration/cli_test.py

@@ -5,9 +5,9 @@ import shlex
 import sys
 from operator import attrgetter
 
-from mock import patch
 from six import StringIO
 
+from .. import mock
 from .testcases import DockerClientTestCase
 from compose.cli.errors import UserError
 from compose.cli.main import TopLevelCommand
@@ -51,13 +51,13 @@ class CLITestCase(DockerClientTestCase):
         self.command.base_dir = old_base_dir
 
     # TODO: address the "Inappropriate ioctl for device" warnings in test output
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_ps(self, mock_stdout):
         self.project.get_service('simple').create_container()
         self.command.dispatch(['ps'], None)
         self.assertIn('simplecomposefile_simple_1', mock_stdout.getvalue())
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_ps_default_composefile(self, mock_stdout):
         self.command.base_dir = 'tests/fixtures/multiple-composefiles'
         self.command.dispatch(['up', '-d'], None)
@@ -68,7 +68,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertIn('multiplecomposefiles_another_1', output)
         self.assertNotIn('multiplecomposefiles_yetanother_1', output)
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_ps_alternate_composefile(self, mock_stdout):
         config_path = os.path.abspath(
             'tests/fixtures/multiple-composefiles/compose2.yml')
@@ -83,19 +83,19 @@ class CLITestCase(DockerClientTestCase):
         self.assertNotIn('multiplecomposefiles_another_1', output)
         self.assertIn('multiplecomposefiles_yetanother_1', output)
 
-    @patch('compose.service.log')
+    @mock.patch('compose.service.log')
     def test_pull(self, mock_logging):
         self.command.dispatch(['pull'], None)
         mock_logging.info.assert_any_call('Pulling simple (busybox:latest)...')
         mock_logging.info.assert_any_call('Pulling another (busybox:latest)...')
 
-    @patch('compose.service.log')
+    @mock.patch('compose.service.log')
     def test_pull_with_digest(self, mock_logging):
         self.command.dispatch(['-f', 'digest.yml', 'pull'], None)
         mock_logging.info.assert_any_call('Pulling simple (busybox:latest)...')
         mock_logging.info.assert_any_call('Pulling digest (busybox@sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d)...')
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_build_no_cache(self, mock_stdout):
         self.command.base_dir = 'tests/fixtures/simple-dockerfile'
         self.command.dispatch(['build', 'simple'], None)
@@ -189,7 +189,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertFalse(config['AttachStdout'])
         self.assertFalse(config['AttachStdin'])
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_without_links(self, mock_stdout):
         self.command.base_dir = 'tests/fixtures/links-composefile'
         self.command.dispatch(['run', 'console', '/bin/true'], None)
@@ -202,7 +202,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertTrue(config['AttachStdout'])
         self.assertTrue(config['AttachStdin'])
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_links(self, __):
         self.command.base_dir = 'tests/fixtures/links-composefile'
         self.command.dispatch(['run', 'web', '/bin/true'], None)
@@ -211,14 +211,14 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(len(db.containers()), 1)
         self.assertEqual(len(console.containers()), 0)
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_with_no_deps(self, __):
         self.command.base_dir = 'tests/fixtures/links-composefile'
         self.command.dispatch(['run', '--no-deps', 'web', '/bin/true'], None)
         db = self.project.get_service('db')
         self.assertEqual(len(db.containers()), 0)
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_does_not_recreate_linked_containers(self, __):
         self.command.base_dir = 'tests/fixtures/links-composefile'
         self.command.dispatch(['up', '-d', 'db'], None)
@@ -234,7 +234,7 @@ class CLITestCase(DockerClientTestCase):
 
         self.assertEqual(old_ids, new_ids)
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_without_command(self, _):
         self.command.base_dir = 'tests/fixtures/commands-composefile'
         self.check_build('tests/fixtures/simple-dockerfile', tag='composetest_test')
@@ -255,7 +255,7 @@ class CLITestCase(DockerClientTestCase):
             [u'/bin/true'],
         )
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_entrypoint_overridden(self, _):
         self.command.base_dir = 'tests/fixtures/dockerfile_with_entrypoint'
         name = 'service'
@@ -270,18 +270,18 @@ class CLITestCase(DockerClientTestCase):
             [u'/bin/echo', u'helloworld'],
         )
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_user_overridden(self, _):
         self.command.base_dir = 'tests/fixtures/user-composefile'
         name = 'service'
         user = 'sshd'
-        args = ['run', '--user={}'.format(user), name]
+        args = ['run', '--user={user}'.format(user=user), name]
         self.command.dispatch(args, None)
         service = self.project.get_service(name)
         container = service.containers(stopped=True, one_off=True)[0]
         self.assertEqual(user, container.get('Config.User'))
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_user_overridden_short_form(self, _):
         self.command.base_dir = 'tests/fixtures/user-composefile'
         name = 'service'
@@ -292,7 +292,7 @@ class CLITestCase(DockerClientTestCase):
         container = service.containers(stopped=True, one_off=True)[0]
         self.assertEqual(user, container.get('Config.User'))
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_environement_overridden(self, _):
         name = 'service'
         self.command.base_dir = 'tests/fixtures/environment-composefile'
@@ -312,7 +312,7 @@ class CLITestCase(DockerClientTestCase):
         # make sure a value with a = don't crash out
         self.assertEqual('moto=bobo', container.environment['allo'])
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_without_map_ports(self, __):
         # create one off container
         self.command.base_dir = 'tests/fixtures/ports-composefile'
@@ -330,7 +330,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(port_random, None)
         self.assertEqual(port_assigned, None)
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_map_ports(self, __):
 
         # create one off container
@@ -353,7 +353,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(port_range[0], "0.0.0.0:49153")
         self.assertEqual(port_range[1], "0.0.0.0:49154")
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_explicitly_maped_ports(self, __):
 
         # create one off container
@@ -372,7 +372,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(port_short, "0.0.0.0:30000")
         self.assertEqual(port_full, "0.0.0.0:30001")
 
-    @patch('dockerpty.start')
+    @mock.patch('dockerpty.start')
     def test_run_service_with_explicitly_maped_ip_ports(self, __):
 
         # create one off container
@@ -508,7 +508,7 @@ class CLITestCase(DockerClientTestCase):
         self.command.dispatch(['up', '-d'], None)
         container = self.project.get_service('simple').get_container()
 
-        @patch('sys.stdout', new_callable=StringIO)
+        @mock.patch('sys.stdout', new_callable=StringIO)
         def get_port(number, mock_stdout):
             self.command.dispatch(['port', 'simple', str(number)], None)
             return mock_stdout.getvalue().rstrip()
@@ -525,7 +525,7 @@ class CLITestCase(DockerClientTestCase):
             self.project.containers(service_names=['simple']),
             key=attrgetter('name'))
 
-        @patch('sys.stdout', new_callable=StringIO)
+        @mock.patch('sys.stdout', new_callable=StringIO)
         def get_port(number, mock_stdout, index=None):
             if index is None:
                 self.command.dispatch(['port', 'simple', str(number)], None)
@@ -547,7 +547,7 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(len(containers), 1)
         self.assertIn("FOO=1", containers[0].get('Config.Env'))
 
-    @patch.dict(os.environ)
+    @mock.patch.dict(os.environ)
     def test_home_and_env_var_in_volume_path(self):
         os.environ['VOLUME_NAME'] = 'my-volume'
         os.environ['HOME'] = '/tmp/home-dir'

+ 2 - 2
tests/integration/legacy_test.py

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

+ 1 - 2
tests/integration/resilience_test.py

@@ -1,8 +1,7 @@
 from __future__ import absolute_import
 from __future__ import unicode_literals
 
-import mock
-
+from .. import mock
 from .testcases import DockerClientTestCase
 from compose.project import Project
 

+ 19 - 25
tests/integration/service_test.py

@@ -7,10 +7,10 @@ import tempfile
 from os import path
 
 from docker.errors import APIError
-from mock import patch
 from six import StringIO
 from six import text_type
 
+from .. import mock
 from .testcases import DockerClientTestCase
 from compose import __version__
 from compose.const import LABEL_CONTAINER_NUMBER
@@ -358,12 +358,12 @@ class ServiceTest(DockerClientTestCase):
         )
 
         old_container = create_and_start_container(service)
-        self.assertEqual(old_container.get('Volumes').keys(), ['/data'])
+        self.assertEqual(list(old_container.get('Volumes').keys()), ['/data'])
         volume_path = old_container.get('Volumes')['/data']
 
         new_container, = service.execute_convergence_plan(
             ConvergencePlan('recreate', [old_container]))
-        self.assertEqual(new_container.get('Volumes').keys(), ['/data'])
+        self.assertEqual(list(new_container.get('Volumes')), ['/data'])
         self.assertEqual(new_container.get('Volumes')['/data'], volume_path)
 
     def test_start_container_passes_through_options(self):
@@ -460,7 +460,7 @@ class ServiceTest(DockerClientTestCase):
         )
         container = create_and_start_container(service)
         container.wait()
-        self.assertIn('success', container.logs())
+        self.assertIn(b'success', container.logs())
         self.assertEqual(len(self.client.images(name='composetest_test')), 1)
 
     def test_start_container_uses_tagged_image_if_it_exists(self):
@@ -473,7 +473,7 @@ class ServiceTest(DockerClientTestCase):
         )
         container = create_and_start_container(service)
         container.wait()
-        self.assertIn('success', container.logs())
+        self.assertIn(b'success', container.logs())
 
     def test_start_container_creates_ports(self):
         service = self.create_service('web', ports=[8000])
@@ -498,7 +498,7 @@ class ServiceTest(DockerClientTestCase):
         with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
             f.write("FROM busybox\n")
 
-        with open(os.path.join(base_dir, b'foo\xE2bar'), 'w') as f:
+        with open(os.path.join(base_dir.encode('utf8'), b'foo\xE2bar'), 'w') as f:
             f.write("hello world\n")
 
         self.create_service('web', build=text_type(base_dir)).build()
@@ -581,8 +581,7 @@ class ServiceTest(DockerClientTestCase):
         service.scale(0)
         self.assertEqual(len(service.containers()), 0)
 
-    @patch('sys.stdout', new_callable=StringIO)
-    def test_scale_with_stopped_containers(self, mock_stdout):
+    def test_scale_with_stopped_containers(self):
         """
         Given there are some stopped containers and scale is called with a
         desired number that is the same as the number of stopped containers,
@@ -591,15 +590,11 @@ class ServiceTest(DockerClientTestCase):
         service = self.create_service('web')
         next_number = service._next_container_number()
         valid_numbers = [next_number, next_number + 1]
-        service.create_container(number=next_number, quiet=True)
-        service.create_container(number=next_number + 1, quiet=True)
-
-        for container in service.containers():
-            self.assertFalse(container.is_running)
+        service.create_container(number=next_number)
+        service.create_container(number=next_number + 1)
 
-        service.scale(2)
-
-        self.assertEqual(len(service.containers()), 2)
+        with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
+            service.scale(2)
         for container in service.containers():
             self.assertTrue(container.is_running)
             self.assertTrue(container.number in valid_numbers)
@@ -608,7 +603,7 @@ class ServiceTest(DockerClientTestCase):
         self.assertNotIn('Creating', captured_output)
         self.assertIn('Starting', captured_output)
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_scale_with_stopped_containers_and_needing_creation(self, mock_stdout):
         """
         Given there are some stopped containers and scale is called with a
@@ -632,7 +627,7 @@ class ServiceTest(DockerClientTestCase):
         self.assertIn('Creating', captured_output)
         self.assertIn('Starting', captured_output)
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_scale_with_api_returns_errors(self, mock_stdout):
         """
         Test that when scaling if the API returns an error, that error is handled
@@ -642,7 +637,7 @@ class ServiceTest(DockerClientTestCase):
         next_number = service._next_container_number()
         service.create_container(number=next_number, quiet=True)
 
-        with patch(
+        with mock.patch(
             'compose.container.Container.create',
                 side_effect=APIError(message="testing", response={}, explanation="Boom")):
 
@@ -652,7 +647,7 @@ class ServiceTest(DockerClientTestCase):
         self.assertTrue(service.containers()[0].is_running)
         self.assertIn("ERROR: for 2  Boom", mock_stdout.getvalue())
 
-    @patch('sys.stdout', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
     def test_scale_with_api_returns_unexpected_exception(self, mock_stdout):
         """
         Test that when scaling if the API returns an error, that is not of type
@@ -662,7 +657,7 @@ class ServiceTest(DockerClientTestCase):
         next_number = service._next_container_number()
         service.create_container(number=next_number, quiet=True)
 
-        with patch(
+        with mock.patch(
             'compose.container.Container.create',
                 side_effect=ValueError("BOOM")):
             with self.assertRaises(ValueError):
@@ -671,7 +666,7 @@ class ServiceTest(DockerClientTestCase):
         self.assertEqual(len(service.containers()), 1)
         self.assertTrue(service.containers()[0].is_running)
 
-    @patch('compose.service.log')
+    @mock.patch('compose.service.log')
     def test_scale_with_desired_number_already_achieved(self, mock_log):
         """
         Test that calling scale with a desired number that is equal to the
@@ -694,14 +689,13 @@ class ServiceTest(DockerClientTestCase):
         captured_output = mock_log.info.call_args[0]
         self.assertIn('Desired container number already achieved', captured_output)
 
-    @patch('compose.service.log')
+    @mock.patch('compose.service.log')
     def test_scale_with_custom_container_name_outputs_warning(self, mock_log):
         """
         Test that calling scale on a service that has a custom container name
         results in warning output.
         """
         service = self.create_service('web', container_name='custom-container')
-
         self.assertEqual(service.custom_container_name(), 'custom-container')
 
         service.scale(3)
@@ -815,7 +809,7 @@ class ServiceTest(DockerClientTestCase):
         for k, v in {'ONE': '1', 'TWO': '2', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}.items():
             self.assertEqual(env[k], v)
 
-    @patch.dict(os.environ)
+    @mock.patch.dict(os.environ)
     def test_resolve_env(self):
         os.environ['FILE_DEF'] = 'E1'
         os.environ['FILE_DEF_EMPTY'] = 'E2'

+ 1 - 2
tests/unit/cli/docker_client_test.py

@@ -3,9 +3,8 @@ from __future__ import unicode_literals
 
 import os
 
-import mock
-
 from compose.cli import docker_client
+from tests import mock
 from tests import unittest
 
 

+ 5 - 2
tests/unit/cli/verbose_proxy_test.py

@@ -1,6 +1,8 @@
 from __future__ import absolute_import
 from __future__ import unicode_literals
 
+import six
+
 from compose.cli import verbose_proxy
 from tests import unittest
 
@@ -8,7 +10,8 @@ from tests import unittest
 class VerboseProxyTestCase(unittest.TestCase):
 
     def test_format_call(self):
-        expected = "(u'arg1', True, key=u'value')"
+        prefix = '' if six.PY3 else 'u'
+        expected = "(%(p)s'arg1', True, key=%(p)s'value')" % dict(p=prefix)
         actual = verbose_proxy.format_call(
             ("arg1", True),
             {'key': 'value'})
@@ -21,7 +24,7 @@ class VerboseProxyTestCase(unittest.TestCase):
         self.assertEqual(expected, actual)
 
     def test_format_return(self):
-        expected = "{u'Id': u'ok'}"
+        expected = repr({'Id': 'ok'})
         actual = verbose_proxy.format_return({'Id': 'ok'}, 2)
         self.assertEqual(expected, actual)
 

+ 1 - 1
tests/unit/cli_test.py

@@ -4,8 +4,8 @@ from __future__ import unicode_literals
 import os
 
 import docker
-import mock
 
+from .. import mock
 from .. import unittest
 from compose.cli.docopt_command import NoSuchCommand
 from compose.cli.errors import UserError

+ 21 - 20
tests/unit/config_test.py

@@ -1,9 +1,11 @@
+from __future__ import print_function
+
 import os
 import shutil
 import tempfile
+from operator import itemgetter
 
-import mock
-
+from .. import mock
 from .. import unittest
 from compose.config import config
 from compose.config.errors import ConfigurationError
@@ -16,6 +18,10 @@ def make_service_dict(name, service_dict, working_dir):
     return config.ServiceLoader(working_dir=working_dir).make_service_dict(name, service_dict)
 
 
+def service_sort(services):
+    return sorted(services, key=itemgetter('name'))
+
+
 class ConfigTest(unittest.TestCase):
     def test_load(self):
         service_dicts = config.load(
@@ -30,8 +36,8 @@ class ConfigTest(unittest.TestCase):
         )
 
         self.assertEqual(
-            sorted(service_dicts, key=lambda d: d['name']),
-            sorted([
+            service_sort(service_dicts),
+            service_sort([
                 {
                     'name': 'bar',
                     'image': 'busybox',
@@ -41,7 +47,7 @@ class ConfigTest(unittest.TestCase):
                     'name': 'foo',
                     'image': 'busybox',
                 }
-            ], key=lambda d: d['name'])
+            ])
         )
 
     def test_load_throws_error_when_not_dict(self):
@@ -682,12 +688,7 @@ class ExtendsTest(unittest.TestCase):
     def test_extends(self):
         service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
 
-        service_dicts = sorted(
-            service_dicts,
-            key=lambda sd: sd['name'],
-        )
-
-        self.assertEqual(service_dicts, [
+        self.assertEqual(service_sort(service_dicts), service_sort([
             {
                 'name': 'mydb',
                 'image': 'busybox',
@@ -704,7 +705,7 @@ class ExtendsTest(unittest.TestCase):
                     "BAZ": "2",
                 },
             }
-        ])
+        ]))
 
     def test_nested(self):
         service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
@@ -726,7 +727,7 @@ class ExtendsTest(unittest.TestCase):
         We specify a 'file' key that is the filename we're already in.
         """
         service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
-        self.assertEqual(service_dicts, [
+        self.assertEqual(service_sort(service_dicts), service_sort([
             {
                 'environment':
                 {
@@ -747,7 +748,7 @@ class ExtendsTest(unittest.TestCase):
                 'image': 'busybox',
                 'name': 'web'
             }
-        ])
+        ]))
 
     def test_circular(self):
         try:
@@ -854,7 +855,7 @@ class ExtendsTest(unittest.TestCase):
         config is valid and correctly extends from itself.
         """
         service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
-        self.assertEqual(service_dicts, [
+        self.assertEqual(service_sort(service_dicts), service_sort([
             {
                 'name': 'myweb',
                 'image': 'busybox',
@@ -870,7 +871,7 @@ class ExtendsTest(unittest.TestCase):
                     "BAZ": "3",
                 }
             }
-        ])
+        ]))
 
     def test_blacklisted_options(self):
         def load_config():
@@ -885,24 +886,24 @@ class ExtendsTest(unittest.TestCase):
             other_config = {'web': {'links': ['db']}}
 
             with mock.patch.object(config, 'load_yaml', return_value=other_config):
-                print load_config()
+                print(load_config())
 
         with self.assertRaisesRegexp(ConfigurationError, 'volumes_from'):
             other_config = {'web': {'volumes_from': ['db']}}
 
             with mock.patch.object(config, 'load_yaml', return_value=other_config):
-                print load_config()
+                print(load_config())
 
         with self.assertRaisesRegexp(ConfigurationError, 'net'):
             other_config = {'web': {'net': 'container:db'}}
 
             with mock.patch.object(config, 'load_yaml', return_value=other_config):
-                print load_config()
+                print(load_config())
 
         other_config = {'web': {'net': 'host'}}
 
         with mock.patch.object(config, 'load_yaml', return_value=other_config):
-            print load_config()
+            print(load_config())
 
     def test_volume_path(self):
         dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')

+ 1 - 1
tests/unit/container_test.py

@@ -1,8 +1,8 @@
 from __future__ import unicode_literals
 
 import docker
-import mock
 
+from .. import mock
 from .. import unittest
 from compose.container import Container
 from compose.container import get_container_name

+ 7 - 2
tests/unit/log_printer_test.py

@@ -3,6 +3,8 @@ from __future__ import unicode_literals
 
 import os
 
+import six
+
 from .. import unittest
 from compose.cli.log_printer import LogPrinter
 
@@ -31,14 +33,17 @@ class LogPrinterTest(unittest.TestCase):
         self.assertIn('\033[', output)
 
     def test_unicode(self):
-        glyph = u'\u2022'.encode('utf-8')
+        glyph = u'\u2022'
 
         def reader(*args, **kwargs):
-            yield glyph + b'\n'
+            yield glyph + '\n'
 
         container = MockContainer(reader)
         output = run_log_printer([container])
 
+        if six.PY2:
+            output = output.decode('utf-8')
+
         self.assertIn(glyph, output)
 
 

+ 9 - 10
tests/unit/progress_stream_test.py

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

+ 1 - 1
tests/unit/project_test.py

@@ -1,8 +1,8 @@
 from __future__ import unicode_literals
 
 import docker
-import mock
 
+from .. import mock
 from .. import unittest
 from compose.const import LABEL_SERVICE
 from compose.container import Container

+ 4 - 4
tests/unit/service_test.py

@@ -2,9 +2,9 @@ from __future__ import absolute_import
 from __future__ import unicode_literals
 
 import docker
-import mock
 from docker.utils import LogConfig
 
+from .. import mock
 from .. import unittest
 from compose.const import LABEL_ONE_OFF
 from compose.const import LABEL_PROJECT
@@ -34,14 +34,14 @@ class ServiceTest(unittest.TestCase):
     def test_containers(self):
         service = Service('db', self.mock_client, 'myproject', image='foo')
         self.mock_client.containers.return_value = []
-        self.assertEqual(service.containers(), [])
+        self.assertEqual(list(service.containers()), [])
 
     def test_containers_with_containers(self):
         self.mock_client.containers.return_value = [
             dict(Name=str(i), Image='foo', Id=i) for i in range(3)
         ]
         service = Service('db', self.mock_client, 'myproject', image='foo')
-        self.assertEqual([c.id for c in service.containers()], range(3))
+        self.assertEqual([c.id for c in service.containers()], list(range(3)))
 
         expected_labels = [
             '{0}=myproject'.format(LABEL_PROJECT),
@@ -280,7 +280,7 @@ class ServiceTest(unittest.TestCase):
 
     def test_build_does_not_pull(self):
         self.mock_client.build.return_value = [
-            '{"stream": "Successfully built 12345"}',
+            b'{"stream": "Successfully built 12345"}',
         ]
 
         service = Service('foo', client=self.mock_client, build='.')

+ 18 - 18
tests/unit/split_buffer_test.py

@@ -8,38 +8,38 @@ from compose.cli.utils import split_buffer
 class SplitBufferTest(unittest.TestCase):
     def test_single_line_chunks(self):
         def reader():
-            yield b'abc\n'
-            yield b'def\n'
-            yield b'ghi\n'
+            yield 'abc\n'
+            yield 'def\n'
+            yield 'ghi\n'
 
-        self.assert_produces(reader, [b'abc\n', b'def\n', b'ghi\n'])
+        self.assert_produces(reader, ['abc\n', 'def\n', 'ghi\n'])
 
     def test_no_end_separator(self):
         def reader():
-            yield b'abc\n'
-            yield b'def\n'
-            yield b'ghi'
+            yield 'abc\n'
+            yield 'def\n'
+            yield 'ghi'
 
-        self.assert_produces(reader, [b'abc\n', b'def\n', b'ghi'])
+        self.assert_produces(reader, ['abc\n', 'def\n', 'ghi'])
 
     def test_multiple_line_chunk(self):
         def reader():
-            yield b'abc\ndef\nghi'
+            yield 'abc\ndef\nghi'
 
-        self.assert_produces(reader, [b'abc\n', b'def\n', b'ghi'])
+        self.assert_produces(reader, ['abc\n', 'def\n', 'ghi'])
 
     def test_chunked_line(self):
         def reader():
-            yield b'a'
-            yield b'b'
-            yield b'c'
-            yield b'\n'
-            yield b'd'
+            yield 'a'
+            yield 'b'
+            yield 'c'
+            yield '\n'
+            yield 'd'
 
-        self.assert_produces(reader, [b'abc\n', b'd'])
+        self.assert_produces(reader, ['abc\n', 'd'])
 
     def test_preserves_unicode_sequences_within_lines(self):
-        string = u"a\u2022c\n".encode('utf-8')
+        string = u"a\u2022c\n"
 
         def reader():
             yield string
@@ -47,7 +47,7 @@ class SplitBufferTest(unittest.TestCase):
         self.assert_produces(reader, [string])
 
     def assert_produces(self, reader, expectations):
-        split = split_buffer(reader(), b'\n')
+        split = split_buffer(reader(), '\n')
 
         for (actual, expected) in zip(split, expectations):
             self.assertEqual(type(actual), type(expected))

+ 15 - 3
tox.ini

@@ -1,15 +1,16 @@
 [tox]
-envlist = py27,pre-commit
+envlist = py27,py34,pre-commit
 
 [testenv]
 usedevelop=True
 passenv =
     LD_LIBRARY_PATH
+setenv =
+    HOME=/tmp
 deps =
     -rrequirements.txt
-    -rrequirements-dev.txt
 commands =
-    nosetests -v {posargs}
+    nosetests -v --with-coverage --cover-branches --cover-package=compose --cover-erase --cover-html-dir=coverage-html --cover-html {posargs}
     flake8 compose tests setup.py
 
 [testenv:pre-commit]
@@ -20,6 +21,17 @@ commands =
     pre-commit install
     pre-commit run --all-files
 
+[testenv:py27]
+deps =
+    {[testenv]deps}
+    -rrequirements-dev.txt
+
+[testenv:py34]
+deps =
+    {[testenv]deps}
+    flake8
+    nose
+
 [flake8]
 # ignore line-length for now
 ignore = E501,E203