Skip to content

Commit

Permalink
Merge pull request docker#5341 from docker/5336-fix-label-parsing
Browse files Browse the repository at this point in the history
Fix service label parsing
  • Loading branch information
shin- authored Nov 3, 2017
2 parents 2780559 + 4563d8c commit 45d590e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
23 changes: 15 additions & 8 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,21 +709,17 @@ def process_service(service_config):
]

if 'build' in service_dict:
if isinstance(service_dict['build'], six.string_types):
service_dict['build'] = resolve_build_path(working_dir, service_dict['build'])
elif isinstance(service_dict['build'], dict):
if 'context' in service_dict['build']:
path = service_dict['build']['context']
service_dict['build']['context'] = resolve_build_path(working_dir, path)
if 'labels' in service_dict['build']:
service_dict['build']['labels'] = parse_labels(service_dict['build']['labels'])
process_build_section(service_dict, working_dir)

if 'volumes' in service_dict and service_dict.get('volume_driver') is None:
service_dict['volumes'] = resolve_volume_paths(working_dir, service_dict)

if 'sysctls' in service_dict:
service_dict['sysctls'] = build_string_dict(parse_sysctls(service_dict['sysctls']))

if 'labels' in service_dict:
service_dict['labels'] = parse_labels(service_dict['labels'])

service_dict = process_depends_on(service_dict)

for field in ['dns', 'dns_search', 'tmpfs']:
Expand All @@ -737,6 +733,17 @@ def process_service(service_config):
return service_dict


def process_build_section(service_dict, working_dir):
if isinstance(service_dict['build'], six.string_types):
service_dict['build'] = resolve_build_path(working_dir, service_dict['build'])
elif isinstance(service_dict['build'], dict):
if 'context' in service_dict['build']:
path = service_dict['build']['context']
service_dict['build']['context'] = resolve_build_path(working_dir, path)
if 'labels' in service_dict['build']:
service_dict['build']['labels'] = parse_labels(service_dict['build']['labels'])


def process_ports(service_dict):
if 'ports' not in service_dict:
return service_dict
Expand Down
55 changes: 37 additions & 18 deletions tests/unit/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,32 @@ def test_load_config_link_local_ips_network(self):
}
}

def test_load_config_service_labels(self):
base_file = config.ConfigFile(
'base.yaml',
{
'version': '2.1',
'services': {
'web': {
'image': 'example/web',
'labels': ['label_key=label_val']
},
'db': {
'image': 'example/db',
'labels': {
'label_key': 'label_val'
}
}
},
}
)
details = config.ConfigDetails('.', [base_file])
service_dicts = config.load(details).services
for service in service_dicts:
assert service['labels'] == {
'label_key': 'label_val'
}

def test_load_config_volume_and_network_labels(self):
base_file = config.ConfigFile(
'base.yaml',
Expand Down Expand Up @@ -434,30 +460,23 @@ def test_load_config_volume_and_network_labels(self):
)

details = config.ConfigDetails('.', [base_file])
network_dict = config.load(details).networks
volume_dict = config.load(details).volumes
loaded_config = config.load(details)

self.assertEqual(
network_dict,
{
'with_label': {
'labels': {
'label_key': 'label_val'
}
assert loaded_config.networks == {
'with_label': {
'labels': {
'label_key': 'label_val'
}
}
)
}

self.assertEqual(
volume_dict,
{
'with_label': {
'labels': {
'label_key': 'label_val'
}
assert loaded_config.volumes == {
'with_label': {
'labels': {
'label_key': 'label_val'
}
}
)
}

def test_load_config_invalid_service_names(self):
for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
Expand Down

0 comments on commit 45d590e

Please sign in to comment.