diff --git a/tripleo_common/image/image_uploader.py b/tripleo_common/image/image_uploader.py index 144af7fb5..1827a8549 100644 --- a/tripleo_common/image/image_uploader.py +++ b/tripleo_common/image/image_uploader.py @@ -273,12 +273,23 @@ class DockerImageUploader(ImageUploader): 'No label specified. Available labels: %s' % label_keys ) - tag_label = labels.get(tag_from_label) - if tag_label is None: - raise ImageUploaderException( - 'Image %s has no label %s. Available labels: %s' % - (image, tag_from_label, label_keys) - ) + if "{" in tag_from_label: + try: + tag_label = tag_from_label.format(**labels) + except ValueError as e: + raise ImageUploaderException(e) + except KeyError as e: + raise ImageUploaderException( + 'Image %s %s. Available labels: %s' % + (image, e, label_keys) + ) + else: + tag_label = labels.get(tag_from_label) + if tag_label is None: + raise ImageUploaderException( + 'Image %s has no label %s. Available labels: %s' % + (image, tag_from_label, label_keys) + ) # confirm the tag exists by checking for an entry in RepoTags repo_tags = i.get('RepoTags', []) diff --git a/tripleo_common/tests/image/test_image_uploader.py b/tripleo_common/tests/image/test_image_uploader.py index 7b06fceef..9d525ff49 100644 --- a/tripleo_common/tests/image/test_image_uploader.py +++ b/tripleo_common/tests/image/test_image_uploader.py @@ -291,9 +291,11 @@ class TestDockerImageUploader(base.TestCase): result = { 'Labels': { 'rdo_version': 'a', - 'build_version': '4.0.0' + 'build_version': '4.0.0', + 'release': '1.0.0', + 'version': '20180125' }, - 'RepoTags': ['a'] + 'RepoTags': ['a', '1.0.0-20180125'] } mock_process = mock.Mock() mock_process.communicate.return_value = (json.dumps(result), '') @@ -301,12 +303,34 @@ class TestDockerImageUploader(base.TestCase): mock_popen.return_value = mock_process sr = image_uploader.SECURE_REGISTRIES + # simple label -> tag self.assertEqual( ('docker.io/t/foo', 'a'), image_uploader.discover_tag_from_inspect( ('docker.io/t/foo', 'rdo_version', sr)) ) + # templated labels -> tag + self.assertEqual( + ('docker.io/t/foo', '1.0.0-20180125'), + image_uploader.discover_tag_from_inspect( + ('docker.io/t/foo', '{release}-{version}', sr)) + ) + + # Invalid template + self.assertRaises( + ImageUploaderException, + image_uploader.discover_tag_from_inspect, + ('docker.io/t/foo', '{release}-{version', sr) + ) + + # Missing label in template + self.assertRaises( + ImageUploaderException, + image_uploader.discover_tag_from_inspect, + ('docker.io/t/foo', '{releases}-{version}', sr) + ) + # no tag_from_label specified self.assertRaises( ImageUploaderException,