environment_test.py 2.5 KB

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