Allow a combination of labels for tag discover

Some downstream build pipelines do not apply a label which exactly
matches the version tag. The only way to build the full versioned tag
is by combining multiple labels in a template pattern.

This change uses a python string.format syntax to allow multiple tags
to be combined.

Change-Id: Id0fb010ebcdd4d3f3650c94065fc42489475c42c
Closes-Bug: #1743883
This commit is contained in:
Steve Baker 2018-01-25 16:35:00 +13:00
parent f69ea9e8af
commit 7fb175465b
2 changed files with 43 additions and 8 deletions

View File

@ -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', [])

View File

@ -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,