Kaydet (Commit) bd72bd13 authored tarafından Aanand Prasad's avatar Aanand Prasad

Finish labels implementation, add tests and docs

Signed-off-by: 's avatarAanand Prasad <aanand.prasad@gmail.com>
üst 014dba28
......@@ -454,14 +454,13 @@ def create_container_config(
for k, v in six.iteritems(environment)
]
if isinstance(labels, six.string_types):
labels = [labels, ]
if labels is not None and compare_version('1.18', version) < 0:
raise errors.DockerException(
'labels were only introduced in API version 1.18'
)
if isinstance(labels, list):
labels_dict = {}
for lbl in labels:
labels_dict[lbl] = {}
labels = labels_dict
labels = dict((lbl, six.text_type('')) for lbl in labels)
if isinstance(mem_limit, six.string_types):
mem_limit = parse_bytes(mem_limit)
......
......@@ -209,6 +209,7 @@ from. Optionally a single string joining container id's with commas
* memswap_limit (int):
* host_config (dict): A [HostConfig](hostconfig.md) dictionary
* mac_address (str): The Mac Address to assign the container
* labels (dict or list): A dictionary of name-value labels (e.g. `{"label1": "value1", "label2": "value2"}`) or a list of names of labels to set with empty values (e.g. `["label1", "label2"]`)
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
......
......@@ -1305,6 +1305,54 @@ class DockerClientTest(Cleanup, unittest.TestCase):
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
)
def test_create_container_with_labels_dict(self):
labels_dict = {
six.text_type('foo'): six.text_type('1'),
six.text_type('bar'): six.text_type('2'),
}
try:
self.client.create_container(
'busybox', 'true',
labels=labels_dict,
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
args = fake_request.call_args
self.assertEqual(args[0][0], url_prefix + 'containers/create')
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
self.assertEqual(
args[1]['headers'], {'Content-Type': 'application/json'}
)
self.assertEqual(
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
)
def test_create_container_with_labels_list(self):
labels_list = [
six.text_type('foo'),
six.text_type('bar'),
]
labels_dict = {
six.text_type('foo'): six.text_type(),
six.text_type('bar'): six.text_type(),
}
try:
self.client.create_container(
'busybox', 'true',
labels=labels_list,
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
args = fake_request.call_args
self.assertEqual(args[0][0], url_prefix + 'containers/create')
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
self.assertEqual(
args[1]['headers'], {'Content-Type': 'application/json'}
)
self.assertEqual(
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
)
def test_resize_container(self):
try:
self.client.resize(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment