cli_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. import logging
  4. import os
  5. import tempfile
  6. import shutil
  7. from .. import unittest
  8. import docker
  9. import mock
  10. from six import StringIO
  11. from compose.cli import main
  12. from compose.cli.main import TopLevelCommand
  13. from compose.cli.errors import ComposeFileNotFound
  14. from compose.service import Service
  15. class CLITestCase(unittest.TestCase):
  16. def test_default_project_name(self):
  17. cwd = os.getcwd()
  18. try:
  19. os.chdir('tests/fixtures/simple-composefile')
  20. command = TopLevelCommand()
  21. project_name = command.get_project_name(command.get_config_path())
  22. self.assertEquals('simplecomposefile', project_name)
  23. finally:
  24. os.chdir(cwd)
  25. def test_project_name_with_explicit_base_dir(self):
  26. command = TopLevelCommand()
  27. command.base_dir = 'tests/fixtures/simple-composefile'
  28. project_name = command.get_project_name(command.get_config_path())
  29. self.assertEquals('simplecomposefile', project_name)
  30. def test_project_name_with_explicit_uppercase_base_dir(self):
  31. command = TopLevelCommand()
  32. command.base_dir = 'tests/fixtures/UpperCaseDir'
  33. project_name = command.get_project_name(command.get_config_path())
  34. self.assertEquals('uppercasedir', project_name)
  35. def test_project_name_with_explicit_project_name(self):
  36. command = TopLevelCommand()
  37. name = 'explicit-project-name'
  38. project_name = command.get_project_name(None, project_name=name)
  39. self.assertEquals('explicitprojectname', project_name)
  40. def test_project_name_from_environment_old_var(self):
  41. command = TopLevelCommand()
  42. name = 'namefromenv'
  43. with mock.patch.dict(os.environ):
  44. os.environ['FIG_PROJECT_NAME'] = name
  45. project_name = command.get_project_name(None)
  46. self.assertEquals(project_name, name)
  47. def test_project_name_from_environment_new_var(self):
  48. command = TopLevelCommand()
  49. name = 'namefromenv'
  50. with mock.patch.dict(os.environ):
  51. os.environ['COMPOSE_PROJECT_NAME'] = name
  52. project_name = command.get_project_name(None)
  53. self.assertEquals(project_name, name)
  54. def test_filename_check(self):
  55. self.assertEqual('docker-compose.yml', get_config_filename_for_files([
  56. 'docker-compose.yml',
  57. 'docker-compose.yaml',
  58. 'fig.yml',
  59. 'fig.yaml',
  60. ]))
  61. self.assertEqual('docker-compose.yaml', get_config_filename_for_files([
  62. 'docker-compose.yaml',
  63. 'fig.yml',
  64. 'fig.yaml',
  65. ]))
  66. self.assertEqual('fig.yml', get_config_filename_for_files([
  67. 'fig.yml',
  68. 'fig.yaml',
  69. ]))
  70. self.assertEqual('fig.yaml', get_config_filename_for_files([
  71. 'fig.yaml',
  72. ]))
  73. self.assertRaises(ComposeFileNotFound, lambda: get_config_filename_for_files([]))
  74. def test_get_project(self):
  75. command = TopLevelCommand()
  76. command.base_dir = 'tests/fixtures/longer-filename-composefile'
  77. project = command.get_project(command.get_config_path())
  78. self.assertEqual(project.name, 'longerfilenamecomposefile')
  79. self.assertTrue(project.client)
  80. self.assertTrue(project.services)
  81. def test_help(self):
  82. command = TopLevelCommand()
  83. with self.assertRaises(SystemExit):
  84. command.dispatch(['-h'], None)
  85. def test_setup_logging(self):
  86. main.setup_logging()
  87. self.assertEqual(logging.getLogger().level, logging.DEBUG)
  88. self.assertEqual(logging.getLogger('requests').propagate, False)
  89. @mock.patch('compose.cli.main.dockerpty', autospec=True)
  90. def test_run_with_environment_merged_with_options_list(self, mock_dockerpty):
  91. command = TopLevelCommand()
  92. mock_client = mock.create_autospec(docker.Client)
  93. mock_project = mock.Mock()
  94. mock_project.get_service.return_value = Service(
  95. 'service',
  96. client=mock_client,
  97. environment=['FOO=ONE', 'BAR=TWO'],
  98. image='someimage')
  99. command.run(mock_project, {
  100. 'SERVICE': 'service',
  101. 'COMMAND': None,
  102. '-e': ['BAR=NEW', 'OTHER=THREE'],
  103. '--user': None,
  104. '--no-deps': None,
  105. '--allow-insecure-ssl': None,
  106. '-d': True,
  107. '-T': None,
  108. '--entrypoint': None,
  109. '--service-ports': None,
  110. '--rm': None,
  111. })
  112. _, _, call_kwargs = mock_client.create_container.mock_calls[0]
  113. self.assertEqual(
  114. call_kwargs['environment'],
  115. {'FOO': 'ONE', 'BAR': 'NEW', 'OTHER': 'THREE'})
  116. def get_config_filename_for_files(filenames):
  117. project_dir = tempfile.mkdtemp()
  118. try:
  119. make_files(project_dir, filenames)
  120. command = TopLevelCommand()
  121. command.base_dir = project_dir
  122. return os.path.basename(command.get_config_path())
  123. finally:
  124. shutil.rmtree(project_dir)
  125. def make_files(dirname, filenames):
  126. for fname in filenames:
  127. with open(os.path.join(dirname, fname), 'w') as f:
  128. f.write('')