cli_test.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # encoding: utf-8
  2. import os
  3. import shutil
  4. import tempfile
  5. from io import StringIO
  6. import docker
  7. import py
  8. import pytest
  9. from docker.constants import DEFAULT_DOCKER_API_VERSION
  10. from .. import mock
  11. from .. import unittest
  12. from ..helpers import build_config
  13. from compose.cli.command import get_project
  14. from compose.cli.command import get_project_name
  15. from compose.cli.docopt_command import NoSuchCommand
  16. from compose.cli.errors import UserError
  17. from compose.cli.main import TopLevelCommand
  18. from compose.const import IS_WINDOWS_PLATFORM
  19. from compose.const import LABEL_SERVICE
  20. from compose.container import Container
  21. from compose.project import Project
  22. class CLITestCase(unittest.TestCase):
  23. def test_default_project_name(self):
  24. test_dir = py._path.local.LocalPath('tests/fixtures/simple-composefile')
  25. with test_dir.as_cwd():
  26. project_name = get_project_name('.')
  27. assert 'simple-composefile' == project_name
  28. def test_project_name_with_explicit_base_dir(self):
  29. base_dir = 'tests/fixtures/simple-composefile'
  30. project_name = get_project_name(base_dir)
  31. assert 'simple-composefile' == project_name
  32. def test_project_name_with_explicit_uppercase_base_dir(self):
  33. base_dir = 'tests/fixtures/UpperCaseDir'
  34. project_name = get_project_name(base_dir)
  35. assert 'uppercasedir' == project_name
  36. def test_project_name_with_explicit_project_name(self):
  37. name = 'explicit-project-name'
  38. project_name = get_project_name(None, project_name=name)
  39. assert 'explicit-project-name' == project_name
  40. @mock.patch.dict(os.environ)
  41. def test_project_name_from_environment_new_var(self):
  42. name = 'namefromenv'
  43. os.environ['COMPOSE_PROJECT_NAME'] = name
  44. project_name = get_project_name(None)
  45. assert project_name == name
  46. def test_project_name_with_empty_environment_var(self):
  47. base_dir = 'tests/fixtures/simple-composefile'
  48. with mock.patch.dict(os.environ):
  49. os.environ['COMPOSE_PROJECT_NAME'] = ''
  50. project_name = get_project_name(base_dir)
  51. assert 'simple-composefile' == project_name
  52. @mock.patch.dict(os.environ)
  53. def test_project_name_with_environment_file(self):
  54. base_dir = tempfile.mkdtemp()
  55. try:
  56. name = 'namefromenvfile'
  57. with open(os.path.join(base_dir, '.env'), 'w') as f:
  58. f.write('COMPOSE_PROJECT_NAME={}'.format(name))
  59. project_name = get_project_name(base_dir)
  60. assert project_name == name
  61. # Environment has priority over .env file
  62. os.environ['COMPOSE_PROJECT_NAME'] = 'namefromenv'
  63. assert get_project_name(base_dir) == os.environ['COMPOSE_PROJECT_NAME']
  64. finally:
  65. shutil.rmtree(base_dir)
  66. def test_get_project(self):
  67. base_dir = 'tests/fixtures/longer-filename-composefile'
  68. project = get_project(base_dir)
  69. assert project.name == 'longer-filename-composefile'
  70. assert project.client
  71. assert project.services
  72. def test_command_help(self):
  73. with mock.patch('sys.stdout', new=StringIO()) as fake_stdout:
  74. TopLevelCommand.help({'COMMAND': 'up'})
  75. assert "Usage: up" in fake_stdout.getvalue()
  76. def test_command_help_nonexistent(self):
  77. with pytest.raises(NoSuchCommand):
  78. TopLevelCommand.help({'COMMAND': 'nonexistent'})
  79. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason="requires dockerpty")
  80. @mock.patch('compose.cli.main.RunOperation', autospec=True)
  81. @mock.patch('compose.cli.main.PseudoTerminal', autospec=True)
  82. @mock.patch('compose.service.Container.create')
  83. @mock.patch.dict(os.environ)
  84. def test_run_interactive_passes_logs_false(
  85. self,
  86. mock_container_create,
  87. mock_pseudo_terminal,
  88. mock_run_operation,
  89. ):
  90. os.environ['COMPOSE_INTERACTIVE_NO_CLI'] = 'true'
  91. mock_client = mock.create_autospec(docker.APIClient)
  92. mock_client.api_version = DEFAULT_DOCKER_API_VERSION
  93. mock_client._general_configs = {}
  94. mock_container_create.return_value = Container(mock_client, {
  95. 'Id': '37b35e0ba80d91009d37e16f249b32b84f72bda269985578ed6c75a0a13fcaa8',
  96. 'Config': {
  97. 'Labels': {
  98. LABEL_SERVICE: 'service',
  99. }
  100. },
  101. }, has_been_inspected=True)
  102. project = Project.from_config(
  103. name='composetest',
  104. client=mock_client,
  105. config_data=build_config({
  106. 'service': {'image': 'busybox'}
  107. }),
  108. )
  109. command = TopLevelCommand(project)
  110. with pytest.raises(SystemExit):
  111. command.run({
  112. 'SERVICE': 'service',
  113. 'COMMAND': None,
  114. '-e': [],
  115. '--label': [],
  116. '--user': None,
  117. '--no-deps': None,
  118. '--detach': False,
  119. '-T': None,
  120. '--entrypoint': None,
  121. '--service-ports': None,
  122. '--use-aliases': None,
  123. '--publish': [],
  124. '--volume': [],
  125. '--rm': None,
  126. '--name': None,
  127. '--workdir': None,
  128. })
  129. _, _, call_kwargs = mock_run_operation.mock_calls[0]
  130. assert call_kwargs['logs'] is False
  131. @mock.patch('compose.service.Container.create')
  132. def test_run_service_with_restart_always(self, mock_container_create):
  133. mock_client = mock.create_autospec(docker.APIClient)
  134. mock_client.api_version = DEFAULT_DOCKER_API_VERSION
  135. mock_client._general_configs = {}
  136. mock_container_create.return_value = Container(mock_client, {
  137. 'Id': '37b35e0ba80d91009d37e16f249b32b84f72bda269985578ed6c75a0a13fcaa8',
  138. 'Name': 'composetest_service_37b35',
  139. 'Config': {
  140. 'Labels': {
  141. LABEL_SERVICE: 'service',
  142. }
  143. },
  144. }, has_been_inspected=True)
  145. project = Project.from_config(
  146. name='composetest',
  147. client=mock_client,
  148. config_data=build_config({
  149. 'service': {
  150. 'image': 'busybox',
  151. 'restart': 'always',
  152. }
  153. }),
  154. )
  155. command = TopLevelCommand(project)
  156. command.run({
  157. 'SERVICE': 'service',
  158. 'COMMAND': None,
  159. '-e': [],
  160. '--label': [],
  161. '--user': None,
  162. '--no-deps': None,
  163. '--detach': True,
  164. '-T': None,
  165. '--entrypoint': None,
  166. '--service-ports': None,
  167. '--use-aliases': None,
  168. '--publish': [],
  169. '--volume': [],
  170. '--rm': None,
  171. '--name': None,
  172. '--workdir': None,
  173. })
  174. # NOTE: The "run" command is supposed to be a one-off tool; therefore restart policy "no"
  175. # (the default) is enforced despite explicit wish for "always" in the project
  176. # configuration file
  177. assert not mock_client.create_host_config.call_args[1].get('restart_policy')
  178. command = TopLevelCommand(project)
  179. command.run({
  180. 'SERVICE': 'service',
  181. 'COMMAND': None,
  182. '-e': [],
  183. '--label': [],
  184. '--user': None,
  185. '--no-deps': None,
  186. '--detach': True,
  187. '-T': None,
  188. '--entrypoint': None,
  189. '--service-ports': None,
  190. '--use-aliases': None,
  191. '--publish': [],
  192. '--volume': [],
  193. '--rm': True,
  194. '--name': None,
  195. '--workdir': None,
  196. })
  197. assert not mock_client.create_host_config.call_args[1].get('restart_policy')
  198. def test_command_manual_and_service_ports_together(self):
  199. project = Project.from_config(
  200. name='composetest',
  201. client=None,
  202. config_data=build_config({
  203. 'service': {'image': 'busybox'},
  204. }),
  205. )
  206. command = TopLevelCommand(project)
  207. with pytest.raises(UserError):
  208. command.run({
  209. 'SERVICE': 'service',
  210. 'COMMAND': None,
  211. '-e': [],
  212. '--label': [],
  213. '--user': None,
  214. '--no-deps': None,
  215. '--detach': True,
  216. '-T': None,
  217. '--entrypoint': None,
  218. '--service-ports': True,
  219. '--use-aliases': None,
  220. '--publish': ['80:80'],
  221. '--rm': None,
  222. '--name': None,
  223. })