Add support for 'env_file' key
Signed-off-by: Ben Langfeld <ben@langfeld.me>
This commit is contained in:
parent
a12cf826cd
commit
98b6d7be78
7 changed files with 135 additions and 6 deletions
4
tests/fixtures/env/one.env
vendored
Normal file
4
tests/fixtures/env/one.env
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
ONE=2
|
||||
TWO=1
|
||||
THREE=3
|
||||
FOO=bar
|
||||
4
tests/fixtures/env/resolve.env
vendored
Normal file
4
tests/fixtures/env/resolve.env
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
FILE_DEF=F1
|
||||
FILE_DEF_EMPTY=
|
||||
ENV_DEF
|
||||
NO_DEF
|
||||
2
tests/fixtures/env/two.env
vendored
Normal file
2
tests/fixtures/env/two.env
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
FOO=baz
|
||||
DOO=dah
|
||||
|
|
@ -397,6 +397,12 @@ class ServiceTest(DockerClientTestCase):
|
|||
for k,v in {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''}.iteritems():
|
||||
self.assertEqual(env[k], v)
|
||||
|
||||
def test_env_from_file_combined_with_env(self):
|
||||
service = self.create_service('web', environment=['ONE=1', 'TWO=2', 'THREE=3'], env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'])
|
||||
env = service.start_container().environment
|
||||
for k,v in {'ONE': '1', 'TWO': '2', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}.iteritems():
|
||||
self.assertEqual(env[k], v)
|
||||
|
||||
def test_resolve_env(self):
|
||||
service = self.create_service('web', environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None})
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
|
|
|
|||
|
|
@ -247,3 +247,72 @@ class ServiceVolumesTest(unittest.TestCase):
|
|||
self.assertEqual(
|
||||
binding,
|
||||
('/home/user', dict(bind='/home/user', ro=False)))
|
||||
|
||||
class ServiceEnvironmentTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.mock_client = mock.create_autospec(docker.Client)
|
||||
self.mock_client.containers.return_value = []
|
||||
|
||||
def test_parse_environment(self):
|
||||
service = Service('foo',
|
||||
environment=['NORMAL=F1', 'CONTAINS_EQUALS=F=2', 'TRAILING_EQUALS='],
|
||||
client=self.mock_client,
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''}
|
||||
)
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
service = Service('foo',
|
||||
environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None},
|
||||
client=self.mock_client,
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
|
||||
)
|
||||
|
||||
def test_env_from_file(self):
|
||||
service = Service('foo',
|
||||
env_file='tests/fixtures/env/one.env',
|
||||
client=self.mock_client,
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'}
|
||||
)
|
||||
|
||||
def test_env_from_multiple_files(self):
|
||||
service = Service('foo',
|
||||
env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'],
|
||||
client=self.mock_client,
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}
|
||||
)
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment_from_file(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
service = Service('foo',
|
||||
env_file=['tests/fixtures/env/resolve.env'],
|
||||
client=self.mock_client,
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue