environment_test.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import tempfile
  2. from ddt import data
  3. from ddt import ddt
  4. from .. import mock
  5. from ..acceptance.cli_test import dispatch
  6. from compose.cli.command import get_project
  7. from compose.cli.command import project_from_options
  8. from compose.config.environment import Environment
  9. from tests.integration.testcases import DockerClientTestCase
  10. @ddt
  11. class EnvironmentTest(DockerClientTestCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. super(EnvironmentTest, cls).setUpClass()
  15. cls.compose_file = tempfile.NamedTemporaryFile(mode='w+b')
  16. cls.compose_file.write(bytes("""version: '3.2'
  17. services:
  18. svc:
  19. image: busybox:1.31.0-uclibc
  20. environment:
  21. TEST_VARIABLE: ${TEST_VARIABLE}""", encoding='utf-8'))
  22. cls.compose_file.flush()
  23. @classmethod
  24. def tearDownClass(cls):
  25. super(EnvironmentTest, cls).tearDownClass()
  26. cls.compose_file.close()
  27. @data('events',
  28. 'exec',
  29. 'kill',
  30. 'logs',
  31. 'pause',
  32. 'ps',
  33. 'restart',
  34. 'rm',
  35. 'start',
  36. 'stop',
  37. 'top',
  38. 'unpause')
  39. def _test_no_warning_on_missing_host_environment_var_on_silent_commands(self, cmd):
  40. options = {'COMMAND': cmd, '--file': [EnvironmentTest.compose_file.name]}
  41. with mock.patch('compose.config.environment.log') as fake_log:
  42. # Note that the warning silencing and the env variables check is
  43. # done in `project_from_options`
  44. # So no need to have a proper options map, the `COMMAND` key is enough
  45. project_from_options('.', options)
  46. assert fake_log.warn.call_count == 0
  47. class EnvironmentOverrideFileTest(DockerClientTestCase):
  48. def test_env_file_override(self):
  49. base_dir = 'tests/fixtures/env-file-override'
  50. dispatch(base_dir, ['--env-file', '.env.override', 'up'])
  51. project = get_project(project_dir=base_dir,
  52. config_path=['docker-compose.yml'],
  53. environment=Environment.from_env_file(base_dir, '.env.override'),
  54. override_dir=base_dir)
  55. containers = project.containers(stopped=True)
  56. assert len(containers) == 1
  57. assert "WHEREAMI=override" in containers[0].get('Config.Env')
  58. assert "DEFAULT_CONF_LOADED=true" in containers[0].get('Config.Env')
  59. dispatch(base_dir, ['--env-file', '.env.override', 'down'], None)