Injecting os.environ in Environment instance happens outside of init method

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-03-22 15:42:30 -07:00
parent 1506f997de
commit 12ad3ff301
5 changed files with 29 additions and 19 deletions

View File

@ -449,12 +449,12 @@ def process_config_file(config_file, environment, service_name=None):
class ServiceExtendsResolver(object): class ServiceExtendsResolver(object):
def __init__(self, service_config, config_file, environment=None, already_seen=None): def __init__(self, service_config, config_file, environment, already_seen=None):
self.service_config = service_config self.service_config = service_config
self.working_dir = service_config.working_dir self.working_dir = service_config.working_dir
self.already_seen = already_seen or [] self.already_seen = already_seen or []
self.config_file = config_file self.config_file = config_file
self.environment = environment or Environment() self.environment = environment
@property @property
def signature(self): def signature(self):

View File

@ -41,19 +41,22 @@ class Environment(dict):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Environment, self).__init__(*args, **kwargs) super(Environment, self).__init__(*args, **kwargs)
self.missing_keys = [] self.missing_keys = []
self.update(os.environ)
@classmethod @classmethod
def from_env_file(cls, base_dir): def from_env_file(cls, base_dir):
result = cls() def _initialize():
if base_dir is None: result = cls()
if base_dir is None:
return result
env_file_path = os.path.join(base_dir, '.env')
try:
return cls(env_vars_from_file(env_file_path))
except ConfigurationError:
pass
return result return result
env_file_path = os.path.join(base_dir, '.env') instance = _initialize()
try: instance.update(os.environ)
return cls(env_vars_from_file(env_file_path)) return instance
except ConfigurationError:
pass
return result
def __getitem__(self, key): def __getitem__(self, key):
try: try:

View File

@ -90,7 +90,9 @@ class DockerClientTestCase(unittest.TestCase):
if 'command' not in kwargs: if 'command' not in kwargs:
kwargs['command'] = ["top"] kwargs['command'] = ["top"]
kwargs['environment'] = resolve_environment(kwargs, Environment()) kwargs['environment'] = resolve_environment(
kwargs, Environment.from_env_file(None)
)
labels = dict(kwargs.setdefault('labels', {})) labels = dict(kwargs.setdefault('labels', {}))
labels['com.docker.compose.test-name'] = self.id() labels['com.docker.compose.test-name'] = self.id()

View File

@ -37,7 +37,9 @@ def make_service_dict(name, service_dict, working_dir, filename=None):
filename=filename, filename=filename,
name=name, name=name,
config=service_dict), config=service_dict),
config.ConfigFile(filename=filename, config={})) config.ConfigFile(filename=filename, config={}),
environment=Environment.from_env_file(working_dir)
)
return config.process_service(resolver.run()) return config.process_service(resolver.run())
@ -2061,7 +2063,9 @@ class EnvTest(unittest.TestCase):
}, },
} }
self.assertEqual( self.assertEqual(
resolve_environment(service_dict, Environment()), resolve_environment(
service_dict, Environment.from_env_file(None)
),
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None}, {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None},
) )
@ -2099,7 +2103,8 @@ class EnvTest(unittest.TestCase):
os.environ['ENV_DEF'] = 'E3' os.environ['ENV_DEF'] = 'E3'
self.assertEqual( self.assertEqual(
resolve_environment( resolve_environment(
{'env_file': ['tests/fixtures/env/resolve.env']}, Environment() {'env_file': ['tests/fixtures/env/resolve.env']},
Environment.from_env_file(None)
), ),
{ {
'FILE_DEF': u'bär', 'FILE_DEF': u'bär',

View File

@ -20,7 +20,7 @@ def mock_env():
def test_interpolate_environment_variables_in_services(mock_env): def test_interpolate_environment_variables_in_services(mock_env):
services = { services = {
'servivea': { 'servicea': {
'image': 'example:${USER}', 'image': 'example:${USER}',
'volumes': ['$FOO:/target'], 'volumes': ['$FOO:/target'],
'logging': { 'logging': {
@ -32,7 +32,7 @@ def test_interpolate_environment_variables_in_services(mock_env):
} }
} }
expected = { expected = {
'servivea': { 'servicea': {
'image': 'example:jenny', 'image': 'example:jenny',
'volumes': ['bar:/target'], 'volumes': ['bar:/target'],
'logging': { 'logging': {
@ -44,7 +44,7 @@ def test_interpolate_environment_variables_in_services(mock_env):
} }
} }
assert interpolate_environment_variables( assert interpolate_environment_variables(
services, 'service', Environment() services, 'service', Environment.from_env_file(None)
) == expected ) == expected
@ -70,5 +70,5 @@ def test_interpolate_environment_variables_in_volumes(mock_env):
'other': {}, 'other': {},
} }
assert interpolate_environment_variables( assert interpolate_environment_variables(
volumes, 'volume', Environment() volumes, 'volume', Environment.from_env_file(None)
) == expected ) == expected