Skip to content

Commit

Permalink
Merge pull request #2938 from shin-/2927-buildargs-types
Browse files Browse the repository at this point in the history
Constraint build argument types. Numbers are cast into strings.
  • Loading branch information
shin- committed Feb 18, 2016
2 parents 85ba08b + 93a02e4 commit d3a95c2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
16 changes: 13 additions & 3 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from ..const import COMPOSEFILE_V1 as V1
from ..const import COMPOSEFILE_V2_0 as V2_0
from ..utils import build_string_dict
from .errors import CircularReference
from .errors import ComposeFileNotFound
from .errors import ConfigurationError
Expand Down Expand Up @@ -292,8 +293,12 @@ def load(config_details):
config_details = config_details._replace(config_files=processed_files)

main_file = config_details.config_files[0]
volumes = load_mapping(config_details.config_files, 'get_volumes', 'Volume')
networks = load_mapping(config_details.config_files, 'get_networks', 'Network')
volumes = load_mapping(
config_details.config_files, 'get_volumes', 'Volume'
)
networks = load_mapping(
config_details.config_files, 'get_networks', 'Network'
)
service_dicts = load_services(
config_details.working_dir,
main_file,
Expand Down Expand Up @@ -333,6 +338,11 @@ def load_mapping(config_files, get_func, entity_type):

mapping[name] = config

if 'driver_opts' in config:
config['driver_opts'] = build_string_dict(
config['driver_opts']
)

return mapping


Expand Down Expand Up @@ -851,7 +861,7 @@ def normalize_build(service_dict, working_dir):
else:
build.update(service_dict['build'])
if 'args' in build:
build['args'] = resolve_build_args(build)
build['args'] = build_string_dict(resolve_build_args(build))

service_dict['build'] = build

Expand Down
2 changes: 1 addition & 1 deletion compose/config/fields_schema_v2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"driver_opts": {
"type": "object",
"patternProperties": {
"^.+$": {"type": "string"}
"^.+$": {"type": ["string", "number"]}
}
},
"external": {
Expand Down
15 changes: 14 additions & 1 deletion compose/config/service_schema_v2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@
"properties": {
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"}
"args": {
"oneOf": [
{"$ref": "#/definitions/list_of_strings"},
{
"type": "object",
"patternProperties": {
"^.+$": {
"type": ["string", "number"]
}
},
"additionalProperties": false
}
]
}
},
"additionalProperties": false
}
Expand Down
4 changes: 4 additions & 0 deletions compose/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ def json_hash(obj):

def microseconds_from_time_nano(time_nano):
return int(time_nano % 1000000000 / 1000)


def build_string_dict(source_dict):
return dict((k, str(v)) for k, v in source_dict.items())
43 changes: 42 additions & 1 deletion tests/unit/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_named_volume_config_empty(self):
assert volumes['simple'] == {}
assert volumes['other'] == {}

def test_volume_invalid_driver_opt(self):
def test_volume_numeric_driver_opt(self):
config_details = build_config_details({
'version': '2',
'services': {
Expand All @@ -241,6 +241,19 @@ def test_volume_invalid_driver_opt(self):
'simple': {'driver_opts': {'size': 42}},
}
})
cfg = config.load(config_details)
assert cfg.volumes['simple']['driver_opts']['size'] == '42'

def test_volume_invalid_driver_opt(self):
config_details = build_config_details({
'version': '2',
'services': {
'simple': {'image': 'busybox'}
},
'volumes': {
'simple': {'driver_opts': {'size': True}},
}
})
with pytest.raises(ConfigurationError) as exc:
config.load(config_details)
assert 'driver_opts.size contains an invalid type' in exc.exconly()
Expand Down Expand Up @@ -608,6 +621,34 @@ def test_config_build_configuration_v2(self):
self.assertTrue('context' in service[0]['build'])
self.assertEqual(service[0]['build']['dockerfile'], 'Dockerfile-alt')

def test_load_with_buildargs(self):
service = config.load(
build_config_details(
{
'version': '2',
'services': {
'web': {
'build': {
'context': '.',
'dockerfile': 'Dockerfile-alt',
'args': {
'opt1': 42,
'opt2': 'foobar'
}
}
}
}
},
'tests/fixtures/extends',
'filename.yml'
)
).services[0]
assert 'args' in service['build']
assert 'opt1' in service['build']['args']
assert isinstance(service['build']['args']['opt1'], str)
assert service['build']['args']['opt1'] == '42'
assert service['build']['args']['opt2'] == 'foobar'

def test_load_with_multiple_files_v2(self):
base_file = config.ConfigFile(
'base.yaml',
Expand Down

0 comments on commit d3a95c2

Please # to comment.