environment_test.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import tempfile
  2. import pytest
  3. from ddt import data
  4. from ddt import ddt
  5. from .. import mock
  6. from ..acceptance.cli_test import dispatch
  7. from compose.cli.command import get_project
  8. from compose.cli.command import project_from_options
  9. from compose.config.environment import Environment
  10. from compose.config.errors import EnvFileNotFound
  11. from tests.integration.testcases import DockerClientTestCase
  12. @ddt
  13. class EnvironmentTest(DockerClientTestCase):
  14. @classmethod
  15. def setUpClass(cls):
  16. super().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().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. # '--env-file' are relative to the current working dir
  53. env = Environment.from_env_file(base_dir, base_dir+'/.env.override')
  54. dispatch(base_dir, ['--env-file', '.env.override', 'up'])
  55. project = get_project(project_dir=base_dir,
  56. config_path=['docker-compose.yml'],
  57. environment=env,
  58. override_dir=base_dir)
  59. containers = project.containers(stopped=True)
  60. assert len(containers) == 1
  61. assert "WHEREAMI=override" in containers[0].get('Config.Env')
  62. assert "DEFAULT_CONF_LOADED=true" in containers[0].get('Config.Env')
  63. dispatch(base_dir, ['--env-file', '.env.override', 'down'], None)
  64. def test_env_file_not_found_error(self):
  65. base_dir = 'tests/fixtures/env-file-override'
  66. with pytest.raises(EnvFileNotFound) as excinfo:
  67. Environment.from_env_file(base_dir, '.env.override')
  68. assert "Couldn't find env file" in excinfo.exconly()
  69. def test_dot_env_file(self):
  70. base_dir = 'tests/fixtures/env-file-override'
  71. # '.env' is relative to the project_dir (base_dir)
  72. env = Environment.from_env_file(base_dir, None)
  73. dispatch(base_dir, ['up'])
  74. project = get_project(project_dir=base_dir,
  75. config_path=['docker-compose.yml'],
  76. environment=env,
  77. override_dir=base_dir)
  78. containers = project.containers(stopped=True)
  79. assert len(containers) == 1
  80. assert "WHEREAMI=default" in containers[0].get('Config.Env')
  81. dispatch(base_dir, ['down'], None)