Merge user defined labels with one carried by the image

This commit is contained in:
Nicolas Delaby 2016-03-29 11:50:08 +02:00
parent 5877a19f59
commit c989ae5a7e
3 changed files with 60 additions and 0 deletions

View file

@ -443,6 +443,7 @@ VALID_CREATE_OPTS = {
},
'labels': {
'path': 'Config:Labels',
'image_path': 'Config:Labels',
'default': {},
},
'binds': {

View file

@ -411,6 +411,21 @@ def _compare(actual, create_kwargs, defaults_from_image):
# sometimes `[]`. We have to deal with it.
if bool(actual_data) != bool(data):
ret.update({item: {'old': actual_data, 'new': data}})
elif item == 'labels':
if actual_data is None:
actual_data = {}
if data is None:
data = {}
image_labels = _image_get(config['image_path'], default={})
if image_labels is not None:
image_labels = image_labels.copy()
if isinstance(data, list):
data = dict((k, '') for k in data)
image_labels.update(data)
data = image_labels
if actual_data != data:
ret.update({item: {'old': actual_data, 'new': data}})
continue
elif isinstance(data, list):
# Compare two sorted lists of items. Won't work for "command"

View file

@ -639,6 +639,50 @@ class DockerngTestCase(TestCase):
labels=['LABEL1', 'LABEL2'],
client_timeout=60)
def test_running_with_labels_from_image(self):
'''
Test dockerng.running with labels parameter supports also
labels carried by the image.
'''
dockerng_create = Mock()
image_id = 'a' * 128
dockerng_inspect_image = MagicMock(
return_value={
'Id': image_id,
'Config': {
'Hostname': 'saltstack-container',
'WorkingDir': '/',
'Cmd': ['bash'],
'Volumes': {'/path': {}},
'Entrypoint': None,
'ExposedPorts': {},
'Labels': {'IMAGE_LABEL': 'image_foo',
'LABEL1': 'label1'},
},
})
__salt__ = {'dockerng.list_containers': MagicMock(),
'dockerng.list_tags': MagicMock(),
'dockerng.pull': MagicMock(),
'dockerng.state': MagicMock(),
'dockerng.inspect_image': dockerng_inspect_image,
'dockerng.create': dockerng_create,
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
dockerng_state.running(
'cont',
image='image:latest',
labels=[{'LABEL1': 'foo1'}, {'LABEL2': 'foo2'}],
)
dockerng_create.assert_called_with(
'image:latest',
validate_input=False,
validate_ip_addrs=False,
name='cont',
labels={'LABEL1': 'foo1', 'LABEL2': 'foo2'},
client_timeout=60)
def test_network_present(self):
'''
Test dockerng.network_present