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
|
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-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-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
|
|
|
def test_default_project_name(self):
|
|
|
|
cwd = os.getcwd()
|
|
|
|
|
|
|
|
try:
|
2015-01-12 15:59:05 +01:00
|
|
|
os.chdir('tests/fixtures/simple-composefile')
|
2014-07-14 20:32:10 +02:00
|
|
|
command = TopLevelCommand()
|
2015-05-27 04:09:26 +02:00
|
|
|
project_name = command.get_project_name('.')
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEquals('simplecomposefile', project_name)
|
2014-07-14 20:32:10 +02:00
|
|
|
finally:
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
|
|
|
def test_project_name_with_explicit_base_dir(self):
|
2014-07-13 08:35:37 +02:00
|
|
|
command = TopLevelCommand()
|
2015-01-12 15:59:05 +01:00
|
|
|
command.base_dir = 'tests/fixtures/simple-composefile'
|
2015-05-27 04:09:26 +02:00
|
|
|
project_name = command.get_project_name(command.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):
|
|
|
|
command = TopLevelCommand()
|
2015-01-28 23:19:27 +01:00
|
|
|
command.base_dir = 'tests/fixtures/UpperCaseDir'
|
2015-05-27 04:09:26 +02:00
|
|
|
project_name = command.get_project_name(command.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):
|
|
|
|
command = TopLevelCommand()
|
2014-07-30 07:42:58 +02:00
|
|
|
name = 'explicit-project-name'
|
|
|
|
project_name = command.get_project_name(None, project_name=name)
|
|
|
|
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_old_var(self):
|
2014-09-23 18:28:08 +02:00
|
|
|
command = TopLevelCommand()
|
|
|
|
name = 'namefromenv'
|
|
|
|
with mock.patch.dict(os.environ):
|
|
|
|
os.environ['FIG_PROJECT_NAME'] = name
|
|
|
|
project_name = command.get_project_name(None)
|
|
|
|
self.assertEquals(project_name, name)
|
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
def test_project_name_from_environment_new_var(self):
|
|
|
|
command = TopLevelCommand()
|
|
|
|
name = 'namefromenv'
|
|
|
|
with mock.patch.dict(os.environ):
|
|
|
|
os.environ['COMPOSE_PROJECT_NAME'] = name
|
|
|
|
project_name = command.get_project_name(None)
|
|
|
|
self.assertEquals(project_name, name)
|
|
|
|
|
2014-07-30 07:42:58 +02:00
|
|
|
def test_get_project(self):
|
|
|
|
command = TopLevelCommand()
|
2015-01-12 15:59:05 +01:00
|
|
|
command.base_dir = 'tests/fixtures/longer-filename-composefile'
|
2015-05-27 04:09:26 +02:00
|
|
|
project = command.get_project()
|
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_dashes(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
TopLevelCommand().dispatch(['help', 'migrate-to-labels'], None)
|
|
|
|
|
|
|
|
self.assertIn('Usage: migrate-to-labels', str(ctx.exception))
|
|
|
|
|
|
|
|
def test_command_help_nonexistent(self):
|
|
|
|
with self.assertRaises(NoSuchCommand):
|
|
|
|
TopLevelCommand().dispatch(['help', 'nonexistent'], None)
|
|
|
|
|
2015-02-14 20:09:55 +01:00
|
|
|
@mock.patch('compose.cli.main.dockerpty', autospec=True)
|
|
|
|
def test_run_with_environment_merged_with_options_list(self, mock_dockerpty):
|
|
|
|
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,
|
|
|
|
'-e': ['BAR=NEW', 'OTHER=THREE'],
|
2015-02-15 03:08:47 +01:00
|
|
|
'--user': None,
|
2015-02-14 20:09:55 +01:00
|
|
|
'--no-deps': None,
|
|
|
|
'--allow-insecure-ssl': 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'],
|
|
|
|
{'FOO': 'ONE', 'BAR': 'NEW', 'OTHER': 'THREE'})
|
|
|
|
|
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,
|
|
|
|
restart='always',
|
|
|
|
image='someimage')
|
|
|
|
command.run(mock_project, {
|
|
|
|
'SERVICE': 'service',
|
|
|
|
'COMMAND': None,
|
|
|
|
'-e': [],
|
|
|
|
'--user': None,
|
|
|
|
'--no-deps': None,
|
|
|
|
'--allow-insecure-ssl': 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
|
|
|
})
|
|
|
|
_, _, call_kwargs = mock_client.create_container.mock_calls[0]
|
|
|
|
self.assertEquals(call_kwargs['host_config']['RestartPolicy']['Name'], 'always')
|
|
|
|
|
|
|
|
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,
|
|
|
|
'--allow-insecure-ssl': 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
|
|
|
})
|
|
|
|
_, _, call_kwargs = mock_client.create_container.mock_calls[0]
|
|
|
|
self.assertFalse('RestartPolicy' in call_kwargs['host_config'])
|
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,
|
|
|
|
'--allow-insecure-ssl': 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
|
|
|
})
|