Allow setting compatibility options from environment

Signed-off-by: Kevin Roy <kiniou@gmail.com>
This commit is contained in:
Kevin Roy 2019-11-18 15:30:35 +01:00
parent f1cfd93c8f
commit 093cc2c089
No known key found for this signature in database
GPG Key ID: 395AC071480EB00B
2 changed files with 47 additions and 20 deletions

View File

@ -59,7 +59,7 @@ def project_from_options(project_dir, options, additional_options={}):
tls_config=tls_config_from_options(options, environment), tls_config=tls_config_from_options(options, environment),
environment=environment, environment=environment,
override_dir=override_dir, override_dir=override_dir,
compatibility=options.get('--compatibility'), compatibility=compatibility_from_options(project_dir, options, environment),
interpolate=(not additional_options.get('--no-interpolate')), interpolate=(not additional_options.get('--no-interpolate')),
environment_file=environment_file environment_file=environment_file
) )
@ -90,7 +90,7 @@ def get_config_from_options(base_dir, options, additional_options={}):
) )
return config.load( return config.load(
config.find(base_dir, config_path, environment, override_dir), config.find(base_dir, config_path, environment, override_dir),
options.get('--compatibility'), compatibility_from_options(config_path, options, environment),
not additional_options.get('--no-interpolate') not additional_options.get('--no-interpolate')
) )
@ -198,3 +198,13 @@ def get_project_name(working_dir, project_name=None, environment=None):
return normalize_name(project) return normalize_name(project)
return 'default' return 'default'
def compatibility_from_options(working_dir, options=None, environment=None):
"""Get compose v3 compatibility from --compatibility option
or from COMPOSE_COMPATIBILITY environment variable."""
compatibility_option = options.get('--compatibility')
compatibility_environment = environment.get_boolean('COMPOSE_COMPATIBILITY')
return compatibility_option or compatibility_environment

View File

@ -43,6 +43,24 @@ ProcessResult = namedtuple('ProcessResult', 'stdout stderr')
BUILD_CACHE_TEXT = 'Using cache' BUILD_CACHE_TEXT = 'Using cache'
BUILD_PULL_TEXT = 'Status: Image is up to date for busybox:1.27.2' BUILD_PULL_TEXT = 'Status: Image is up to date for busybox:1.27.2'
COMPOSE_COMPATIBILITY_DICT = {
'version': '2.3',
'volumes': {'foo': {'driver': 'default'}},
'networks': {'bar': {}},
'services': {
'foo': {
'command': '/bin/true',
'image': 'alpine:3.10.1',
'scale': 3,
'restart': 'always:7',
'mem_limit': '300M',
'mem_reservation': '100M',
'cpus': 0.7,
'volumes': ['foo:/bar:rw'],
'networks': {'bar': None},
}
},
}
def start_process(base_dir, options): def start_process(base_dir, options):
@ -564,24 +582,23 @@ services:
self.base_dir = 'tests/fixtures/compatibility-mode' self.base_dir = 'tests/fixtures/compatibility-mode'
result = self.dispatch(['--compatibility', 'config']) result = self.dispatch(['--compatibility', 'config'])
assert yaml.safe_load(result.stdout) == { assert yaml.load(result.stdout) == COMPOSE_COMPATIBILITY_DICT
'version': '2.3',
'volumes': {'foo': {'driver': 'default'}}, @mock.patch.dict(os.environ)
'networks': {'bar': {}}, def test_config_compatibility_mode_from_env(self):
'services': { self.base_dir = 'tests/fixtures/compatibility-mode'
'foo': { os.environ['COMPOSE_COMPATIBILITY'] = 'true'
'command': '/bin/true', result = self.dispatch(['config'])
'image': 'alpine:3.10.1',
'scale': 3, assert yaml.load(result.stdout) == COMPOSE_COMPATIBILITY_DICT
'restart': 'always:7',
'mem_limit': '300M', @mock.patch.dict(os.environ)
'mem_reservation': '100M', def test_config_compatibility_mode_from_env_and_option_precedence(self):
'cpus': 0.7, self.base_dir = 'tests/fixtures/compatibility-mode'
'volumes': ['foo:/bar:rw'], os.environ['COMPOSE_COMPATIBILITY'] = 'false'
'networks': {'bar': None}, result = self.dispatch(['--compatibility', 'config'])
}
}, assert yaml.load(result.stdout) == COMPOSE_COMPATIBILITY_DICT
}
def test_ps(self): def test_ps(self):
self.project.get_service('simple').create_container() self.project.get_service('simple').create_container()