Finish labels implementation, add tests and docs

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2015-03-20 14:26:10 -07:00
parent 014dba2841
commit bd72bd13c7
3 changed files with 54 additions and 6 deletions

View File

@ -454,14 +454,13 @@ def create_container_config(
for k, v in six.iteritems(environment) for k, v in six.iteritems(environment)
] ]
if isinstance(labels, six.string_types): if labels is not None and compare_version('1.18', version) < 0:
labels = [labels, ] raise errors.DockerException(
'labels were only introduced in API version 1.18'
)
if isinstance(labels, list): if isinstance(labels, list):
labels_dict = {} labels = dict((lbl, six.text_type('')) for lbl in labels)
for lbl in labels:
labels_dict[lbl] = {}
labels = labels_dict
if isinstance(mem_limit, six.string_types): if isinstance(mem_limit, six.string_types):
mem_limit = parse_bytes(mem_limit) mem_limit = parse_bytes(mem_limit)

View File

@ -209,6 +209,7 @@ from. Optionally a single string joining container id's with commas
* memswap_limit (int): * memswap_limit (int):
* host_config (dict): A [HostConfig](hostconfig.md) dictionary * host_config (dict): A [HostConfig](hostconfig.md) dictionary
* mac_address (str): The Mac Address to assign the container * 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. **Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.

View File

@ -1305,6 +1305,54 @@ class DockerClientTest(Cleanup, unittest.TestCase):
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS 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): def test_resize_container(self):
try: try:
self.client.resize( self.client.resize(