Support python 3

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2014-08-19 17:36:46 -04:00
commit 809443d6d0
23 changed files with 128 additions and 85 deletions

View file

@ -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,7 +270,7 @@ 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'
@ -281,7 +281,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_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'

View file

@ -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
@ -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])
@ -581,7 +581,7 @@ class ServiceTest(DockerClientTestCase):
service.scale(0)
self.assertEqual(len(service.containers()), 0)
@patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_scale_with_stopped_containers(self, mock_stdout):
"""
Given there are some stopped containers and scale is called with a
@ -608,7 +608,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 +632,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 +642,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 +652,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 +662,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 +671,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,7 +694,7 @@ 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
@ -815,7 +815,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'