Skip to content

Commit bd72bd1

Browse files
committed
Finish labels implementation, add tests and docs
Signed-off-by: Aanand Prasad <[email protected]>
1 parent 014dba2 commit bd72bd1

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

docker/utils/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,13 @@ def create_container_config(
454454
for k, v in six.iteritems(environment)
455455
]
456456

457-
if isinstance(labels, six.string_types):
458-
labels = [labels, ]
457+
if labels is not None and compare_version('1.18', version) < 0:
458+
raise errors.DockerException(
459+
'labels were only introduced in API version 1.18'
460+
)
459461

460462
if isinstance(labels, list):
461-
labels_dict = {}
462-
for lbl in labels:
463-
labels_dict[lbl] = {}
464-
labels = labels_dict
463+
labels = dict((lbl, six.text_type('')) for lbl in labels)
465464

466465
if isinstance(mem_limit, six.string_types):
467466
mem_limit = parse_bytes(mem_limit)

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ from. Optionally a single string joining container id's with commas
209209
* memswap_limit (int):
210210
* host_config (dict): A [HostConfig](hostconfig.md) dictionary
211211
* mac_address (str): The Mac Address to assign the container
212+
* 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"]`)
212213

213214
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
214215

tests/test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,54 @@ def test_create_container_with_devices(self):
13051305
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
13061306
)
13071307

1308+
def test_create_container_with_labels_dict(self):
1309+
labels_dict = {
1310+
six.text_type('foo'): six.text_type('1'),
1311+
six.text_type('bar'): six.text_type('2'),
1312+
}
1313+
try:
1314+
self.client.create_container(
1315+
'busybox', 'true',
1316+
labels=labels_dict,
1317+
)
1318+
except Exception as e:
1319+
self.fail('Command should not raise exception: {0}'.format(e))
1320+
args = fake_request.call_args
1321+
self.assertEqual(args[0][0], url_prefix + 'containers/create')
1322+
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
1323+
self.assertEqual(
1324+
args[1]['headers'], {'Content-Type': 'application/json'}
1325+
)
1326+
self.assertEqual(
1327+
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
1328+
)
1329+
1330+
def test_create_container_with_labels_list(self):
1331+
labels_list = [
1332+
six.text_type('foo'),
1333+
six.text_type('bar'),
1334+
]
1335+
labels_dict = {
1336+
six.text_type('foo'): six.text_type(),
1337+
six.text_type('bar'): six.text_type(),
1338+
}
1339+
try:
1340+
self.client.create_container(
1341+
'busybox', 'true',
1342+
labels=labels_list,
1343+
)
1344+
except Exception as e:
1345+
self.fail('Command should not raise exception: {0}'.format(e))
1346+
args = fake_request.call_args
1347+
self.assertEqual(args[0][0], url_prefix + 'containers/create')
1348+
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
1349+
self.assertEqual(
1350+
args[1]['headers'], {'Content-Type': 'application/json'}
1351+
)
1352+
self.assertEqual(
1353+
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
1354+
)
1355+
13081356
def test_resize_container(self):
13091357
try:
13101358
self.client.resize(

0 commit comments

Comments
 (0)