Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Prefer manifest_digest over digest, the actual field name on the model.
Browse files Browse the repository at this point in the history
Raise a validation exception if name or manifest_digest are missing.
Added tests.

closes #3250
https://pulp.plan.io/issues/3250

(cherry picked from commit f824973)
  • Loading branch information
mibanescu authored and pcreech committed Jan 22, 2018
1 parent 908f589 commit 0993057
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions common/pulp_docker/common/error_codes.py
Expand Up @@ -46,3 +46,5 @@
['checksum_type', 'checksum'])
DKR1018 = Error("DKR1018", _("Layer %(layer)s is not present in the image"),
['layer'])
DKR1019 = Error("DKR1019", _("Tag does not contain required field: %(field)s."),
['field'])
12 changes: 10 additions & 2 deletions plugins/pulp_docker/plugins/importers/upload.py
Expand Up @@ -331,8 +331,16 @@ def process_main(self, item=None):
:type item: None
"""

tag = self.parent.metadata['name']
digest = self.parent.metadata['digest']
md = self.parent.metadata
tag = md.get('name')
if tag is None:
raise PulpCodedValidationException(error_code=error_codes.DKR1019,
field='name')
# https://pulp.plan.io/issues/3250 - use manifest_digest if available
digest = md.get('manifest_digest', md.get('digest'))
if digest is None:
raise PulpCodedValidationException(error_code=error_codes.DKR1019,
field='manifest_digest')
repo_id = self.parent.repo.id
manifest_type_id = models.Manifest._content_type_id.default
repo_manifest_ids = repository.get_associated_unit_ids(repo_id, manifest_type_id)
Expand Down
31 changes: 31 additions & 0 deletions plugins/test/unit/plugins/importers/test_upload.py
Expand Up @@ -10,6 +10,7 @@
import tarfile
import tempfile
import unittest
from pulp.server.exceptions import PulpCodedValidationException
from pulp_docker.common import constants
from pulp_docker.plugins import models
from pulp_docker.plugins.importers import upload
Expand Down Expand Up @@ -167,6 +168,36 @@ def test_AddUnits_error_missing_layer(self, _repo_controller, _Manifest_save, _B
"Layer this-is-missing.tar is not present in the image",
str(ctx.exception))

def test_AddTags__error_no_name(self, _repo_controller, _Manifest_save, _Blob_save):
# This is where we will untar the image
step_work_dir = os.path.join(self.work_dir, "working_dir")
os.makedirs(step_work_dir)

parent = mock.MagicMock(metadata=dict(), parent=None)
step = upload.AddTags(step_type=constants.UPLOAD_STEP_SAVE,
working_dir=step_work_dir)
step.parent = parent
with self.assertRaises(PulpCodedValidationException) as ctx:
step.process_main()
self.assertEquals(
"Tag does not contain required field: name.",
str(ctx.exception))

def test_AddTags__error_no_manifest_digest(self, _repo_controller, _Manifest_save, _Blob_save):
# This is where we will untar the image
step_work_dir = os.path.join(self.work_dir, "working_dir")
os.makedirs(step_work_dir)

parent = mock.MagicMock(metadata=dict(name="aaa"), parent=None)
step = upload.AddTags(step_type=constants.UPLOAD_STEP_SAVE,
working_dir=step_work_dir)
step.parent = parent
with self.assertRaises(PulpCodedValidationException) as ctx:
step.process_main()
self.assertEquals(
"Tag does not contain required field: manifest_digest.",
str(ctx.exception))

def _create_layer(self, content):
sha = hashlib.sha256()
sha.update(content)
Expand Down

0 comments on commit 0993057

Please sign in to comment.