2015-10-22 18:12:43 +02:00
|
|
|
# encoding: utf-8
|
2014-04-25 23:58:21 +02:00
|
|
|
from __future__ import absolute_import
|
2015-08-24 21:25:25 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-07-15 21:59:25 +02:00
|
|
|
import os
|
|
|
|
|
2015-02-14 20:09:55 +01:00
|
|
|
import docker
|
2015-09-12 01:51:50 +02:00
|
|
|
import py
|
2015-09-22 23:37:14 +02:00
|
|
|
import pytest
|
2014-07-30 07:42:58 +02:00
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
from .. import mock
|
2015-08-24 21:25:25 +02:00
|
|
|
from .. import unittest
|
2015-09-12 01:51:50 +02:00
|
|
|
from compose.cli.command import get_project
|
|
|
|
from compose.cli.command import get_project_name
|
2015-06-19 20:35:06 +02:00
|
|
|
from compose.cli.docopt_command import NoSuchCommand
|
2015-08-07 14:42:37 +02:00
|
|
|
from compose.cli.errors import UserError
|
2015-05-27 04:09:26 +02:00
|
|
|
from compose.cli.main import TopLevelCommand
|
2015-09-22 23:37:14 +02:00
|
|
|
from compose.const import IS_WINDOWS_PLATFORM
|
2015-02-14 20:09:55 +01:00
|
|
|
from compose.service import Service
|
2014-07-15 21:59:25 +02:00
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
class CLITestCase(unittest.TestCase):
|
2014-07-14 20:32:10 +02:00
|
|
|
|
2015-09-12 01:51:50 +02:00
|
|
|
def test_default_project_name(self):
|
|
|
|
test_dir = py._path.local.LocalPath('tests/fixtures/simple-composefile')
|
|
|
|
with test_dir.as_cwd():
|
|
|
|
project_name = get_project_name('.')
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEquals('simplecomposefile', project_name)
|
2014-07-14 20:32:10 +02:00
|
|
|
|
|
|
|
def test_project_name_with_explicit_base_dir(self):
|
2015-09-12 01:51:50 +02:00
|
|
|
base_dir = 'tests/fixtures/simple-composefile'
|
|
|
|
project_name = get_project_name(base_dir)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEquals('simplecomposefile', project_name)
|
2014-07-13 08:35:37 +02:00
|
|
|
|
2014-10-21 13:32:54 +02:00
|
|
|
def test_project_name_with_explicit_uppercase_base_dir(self):
|
2015-09-12 01:51:50 +02:00
|
|
|
base_dir = 'tests/fixtures/UpperCaseDir'
|
|
|
|
project_name = get_project_name(base_dir)
|
2015-01-28 23:19:27 +01:00
|
|
|
self.assertEquals('uppercasedir', project_name)
|
2014-10-21 13:32:54 +02:00
|
|
|
|
2014-07-14 20:32:10 +02:00
|
|
|
def test_project_name_with_explicit_project_name(self):
|
2014-07-30 07:42:58 +02:00
|
|
|
name = 'explicit-project-name'
|
2015-09-12 01:51:50 +02:00
|
|
|
project_name = get_project_name(None, project_name=name)
|
2014-07-30 07:42:58 +02:00
|
|
|
self.assertEquals('explicitprojectname', project_name)
|
2014-07-14 20:32:10 +02:00
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
def test_project_name_from_environment_new_var(self):
|
|
|
|
name = 'namefromenv'
|
|
|
|
with mock.patch.dict(os.environ):
|
|
|
|
os.environ['COMPOSE_PROJECT_NAME'] = name
|
2015-09-12 01:51:50 +02:00
|
|
|
project_name = get_project_name(None)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEquals(project_name, name)
|
|
|
|
|
2014-07-30 07:42:58 +02:00
|
|
|
def test_get_project(self):
|
2015-09-12 01:51:50 +02:00
|
|
|
base_dir = 'tests/fixtures/longer-filename-composefile'
|
|
|
|
project = get_project(base_dir)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(project.name, 'longerfilenamecomposefile')
|
2014-07-30 07:42:58 +02:00
|
|
|
self.assertTrue(project.client)
|
|
|
|
self.assertTrue(project.services)
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
command = TopLevelCommand()
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
command.dispatch(['-h'], None)
|
2014-07-15 21:59:25 +02:00
|
|
|
|
2015-06-19 20:35:06 +02:00
|
|
|
def test_command_help(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
TopLevelCommand().dispatch(['help', 'up'], None)
|
|
|
|
|
|
|
|
self.assertIn('Usage: up', str(ctx.exception))
|
|
|
|
|
|
|
|
def test_command_help_nonexistent(self):
|
|
|
|
with self.assertRaises(NoSuchCommand):
|
|
|
|
TopLevelCommand().dispatch(['help', 'nonexistent'], None)
|
|
|
|
|
2016-01-21 20:15:15 +01:00
|
|
|
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason="requires dockerpty")
|
|
|
|
@mock.patch('compose.cli.main.PseudoTerminal', autospec=True)
|
|
|
|
def test_run_interactive_passes_logs_false(self, mock_pseudo_terminal):
|
|
|
|
command = TopLevelCommand()
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
|
|
|
mock_project = mock.Mock(client=mock_client)
|
|
|
|
mock_project.get_service.return_value = Service(
|
|
|
|
'service',
|
|
|
|
client=mock_client,
|
|
|
|
environment=['FOO=ONE', 'BAR=TWO'],
|
|
|
|
image='someimage')
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
|
|
|
'-e': ['BAR=NEW', 'OTHER=bär'.encode('utf-8')],
|
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'-d': False,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': None,
|
|
|
|
'--publish': [],
|
|
|
|
'--rm': None,
|
|
|
|
'--name': None,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, _, call_kwargs = mock_pseudo_terminal.mock_calls[0]
|
|
|
|
assert call_kwargs['logs'] is False
|
|
|
|
|
2015-09-22 23:37:14 +02:00
|
|
|
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason="requires dockerpty")
|
2016-01-21 19:34:18 +01:00
|
|
|
@mock.patch('compose.cli.main.PseudoTerminal', autospec=True)
|
|
|
|
def test_run_with_environment_merged_with_options_list(self, mock_pseudo_terminal):
|
2015-02-14 20:09:55 +01:00
|
|
|
command = TopLevelCommand()
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
2015-07-03 14:35:54 +02:00
|
|
|
mock_project = mock.Mock(client=mock_client)
|
2015-02-14 20:09:55 +01:00
|
|
|
mock_project.get_service.return_value = Service(
|
|
|
|
'service',
|
|
|
|
client=mock_client,
|
|
|
|
environment=['FOO=ONE', 'BAR=TWO'],
|
|
|
|
image='someimage')
|
|
|
|
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
2015-10-22 18:12:43 +02:00
|
|
|
'-e': ['BAR=NEW', 'OTHER=bär'.encode('utf-8')],
|
2015-02-15 03:08:47 +01:00
|
|
|
'--user': None,
|
2015-02-14 20:09:55 +01:00
|
|
|
'--no-deps': None,
|
|
|
|
'-d': True,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': None,
|
2015-08-07 14:42:37 +02:00
|
|
|
'--publish': [],
|
2015-02-14 20:09:55 +01:00
|
|
|
'--rm': None,
|
2015-08-26 19:33:03 +02:00
|
|
|
'--name': None,
|
2015-02-14 20:09:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
_, _, call_kwargs = mock_client.create_container.mock_calls[0]
|
|
|
|
self.assertEqual(
|
|
|
|
call_kwargs['environment'],
|
2015-10-22 18:12:43 +02:00
|
|
|
{'FOO': 'ONE', 'BAR': 'NEW', 'OTHER': u'bär'})
|
2015-02-14 20:09:55 +01:00
|
|
|
|
2015-04-07 10:11:36 +02:00
|
|
|
def test_run_service_with_restart_always(self):
|
|
|
|
command = TopLevelCommand()
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
2015-07-03 14:35:54 +02:00
|
|
|
mock_project = mock.Mock(client=mock_client)
|
2015-04-07 10:11:36 +02:00
|
|
|
mock_project.get_service.return_value = Service(
|
|
|
|
'service',
|
|
|
|
client=mock_client,
|
2015-11-14 00:29:25 +01:00
|
|
|
restart={'Name': 'always', 'MaximumRetryCount': 0},
|
2015-04-07 10:11:36 +02:00
|
|
|
image='someimage')
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
|
|
|
'-e': [],
|
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'-d': True,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': None,
|
2015-08-07 14:42:37 +02:00
|
|
|
'--publish': [],
|
2015-04-07 10:11:36 +02:00
|
|
|
'--rm': None,
|
2015-08-26 19:33:03 +02:00
|
|
|
'--name': None,
|
2015-04-07 10:11:36 +02:00
|
|
|
})
|
2015-09-17 16:33:58 +02:00
|
|
|
|
|
|
|
self.assertEquals(
|
|
|
|
mock_client.create_host_config.call_args[1]['restart_policy']['Name'],
|
|
|
|
'always'
|
|
|
|
)
|
2015-04-07 10:11:36 +02:00
|
|
|
|
|
|
|
command = TopLevelCommand()
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
2015-07-03 14:35:54 +02:00
|
|
|
mock_project = mock.Mock(client=mock_client)
|
2015-04-07 10:11:36 +02:00
|
|
|
mock_project.get_service.return_value = Service(
|
|
|
|
'service',
|
|
|
|
client=mock_client,
|
|
|
|
restart='always',
|
|
|
|
image='someimage')
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
|
|
|
'-e': [],
|
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'-d': True,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': None,
|
2015-08-07 14:42:37 +02:00
|
|
|
'--publish': [],
|
2015-04-07 10:11:36 +02:00
|
|
|
'--rm': True,
|
2015-08-26 19:33:03 +02:00
|
|
|
'--name': None,
|
2015-04-07 10:11:36 +02:00
|
|
|
})
|
2015-09-17 16:33:58 +02:00
|
|
|
|
|
|
|
self.assertFalse(
|
|
|
|
mock_client.create_host_config.call_args[1].get('restart_policy')
|
|
|
|
)
|
2015-08-07 14:42:37 +02:00
|
|
|
|
|
|
|
def test_command_manula_and_service_ports_together(self):
|
|
|
|
command = TopLevelCommand()
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
|
|
|
mock_project = mock.Mock(client=mock_client)
|
|
|
|
mock_project.get_service.return_value = Service(
|
|
|
|
'service',
|
|
|
|
client=mock_client,
|
|
|
|
restart='always',
|
|
|
|
image='someimage',
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.assertRaises(UserError):
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
|
|
|
'-e': [],
|
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'-d': True,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': True,
|
|
|
|
'--publish': ['80:80'],
|
|
|
|
'--rm': None,
|
2015-08-26 19:33:03 +02:00
|
|
|
'--name': None,
|
2015-08-07 14:42:37 +02:00
|
|
|
})
|