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
|
2016-02-05 18:09:07 +01:00
|
|
|
from ..helpers import build_config
|
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
|
2016-02-05 18:09:07 +01:00
|
|
|
from compose.project import Project
|
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)
|
|
|
|
|
2016-01-29 13:15:38 +01:00
|
|
|
def test_project_name_with_empty_environment_var(self):
|
|
|
|
base_dir = 'tests/fixtures/simple-composefile'
|
|
|
|
with mock.patch.dict(os.environ):
|
|
|
|
os.environ['COMPOSE_PROJECT_NAME'] = ''
|
|
|
|
project_name = get_project_name(base_dir)
|
|
|
|
self.assertEquals('simplecomposefile', project_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
|
|
|
|
2015-06-19 20:35:06 +02:00
|
|
|
def test_command_help(self):
|
2016-02-29 23:35:23 +01:00
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
|
|
TopLevelCommand.help({'COMMAND': 'up'})
|
2015-06-19 20:35:06 +02:00
|
|
|
|
2016-02-29 23:35:23 +01:00
|
|
|
assert 'Usage: up' in exc.exconly()
|
2015-06-19 20:35:06 +02:00
|
|
|
|
|
|
|
def test_command_help_nonexistent(self):
|
2016-02-29 23:35:23 +01:00
|
|
|
with pytest.raises(NoSuchCommand):
|
|
|
|
TopLevelCommand.help({'COMMAND': 'nonexistent'})
|
2015-06-19 20:35:06 +02:00
|
|
|
|
2016-01-21 20:15:15 +01:00
|
|
|
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason="requires dockerpty")
|
2016-02-04 17:01:11 +01:00
|
|
|
@mock.patch('compose.cli.main.RunOperation', autospec=True)
|
2016-01-21 20:15:15 +01:00
|
|
|
@mock.patch('compose.cli.main.PseudoTerminal', autospec=True)
|
2016-02-04 17:01:11 +01:00
|
|
|
def test_run_interactive_passes_logs_false(self, mock_pseudo_terminal, mock_run_operation):
|
2016-01-21 20:15:15 +01:00
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
2016-02-05 18:09:07 +01:00
|
|
|
project = Project.from_config(
|
|
|
|
name='composetest',
|
2016-01-21 20:15:15 +01:00
|
|
|
client=mock_client,
|
2016-02-05 18:09:07 +01:00
|
|
|
config_data=build_config({
|
|
|
|
'service': {'image': 'busybox'}
|
|
|
|
}),
|
|
|
|
)
|
2016-02-29 23:35:23 +01:00
|
|
|
command = TopLevelCommand(project)
|
2016-01-21 20:15:15 +01:00
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
2016-03-08 20:35:54 +01:00
|
|
|
command.run({
|
2016-01-21 20:15:15 +01:00
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
2016-02-05 18:09:07 +01:00
|
|
|
'-e': [],
|
2016-01-21 20:15:15 +01:00
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'-d': False,
|
|
|
|
'-T': None,
|
|
|
|
'--entrypoint': None,
|
|
|
|
'--service-ports': None,
|
|
|
|
'--publish': [],
|
|
|
|
'--rm': None,
|
|
|
|
'--name': None,
|
|
|
|
})
|
|
|
|
|
2016-02-04 17:01:11 +01:00
|
|
|
_, _, call_kwargs = mock_run_operation.mock_calls[0]
|
2016-01-21 20:15:15 +01:00
|
|
|
assert call_kwargs['logs'] is False
|
|
|
|
|
2015-04-07 10:11:36 +02:00
|
|
|
def test_run_service_with_restart_always(self):
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
2016-02-05 18:09:07 +01:00
|
|
|
|
|
|
|
project = Project.from_config(
|
|
|
|
name='composetest',
|
2015-04-07 10:11:36 +02:00
|
|
|
client=mock_client,
|
2016-02-05 18:09:07 +01:00
|
|
|
config_data=build_config({
|
|
|
|
'service': {
|
|
|
|
'image': 'busybox',
|
|
|
|
'restart': 'always',
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2016-02-29 23:35:23 +01:00
|
|
|
command = TopLevelCommand(project)
|
2016-03-08 20:35:54 +01:00
|
|
|
command.run({
|
2015-04-07 10:11:36 +02:00
|
|
|
'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
|
|
|
|
2016-02-29 23:35:23 +01:00
|
|
|
command = TopLevelCommand(project)
|
2016-03-08 20:35:54 +01:00
|
|
|
command.run({
|
2015-04-07 10:11:36 +02:00
|
|
|
'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):
|
2016-02-05 18:09:07 +01:00
|
|
|
project = Project.from_config(
|
|
|
|
name='composetest',
|
|
|
|
client=None,
|
|
|
|
config_data=build_config({
|
|
|
|
'service': {'image': 'busybox'},
|
|
|
|
}),
|
2015-08-07 14:42:37 +02:00
|
|
|
)
|
2016-02-29 23:35:23 +01:00
|
|
|
command = TopLevelCommand(project)
|
2015-08-07 14:42:37 +02:00
|
|
|
|
|
|
|
with self.assertRaises(UserError):
|
2016-03-08 20:35:54 +01:00
|
|
|
command.run({
|
2015-08-07 14:42:37 +02:00
|
|
|
'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
|
|
|
})
|