Skip to content

Commit

Permalink
Report error when importing inconsistent data
Browse files Browse the repository at this point in the history
When images or rpms are imported and compose id in composeinfo and
rpm/image manifest is not the same, report an error. This could happen
when user picks manifest for different compose by mistake.

It does not do any sophisticated validations nor does it compare any
other fields.

JIRA: PDC-952
  • Loading branch information
lubomir committed Sep 7, 2015
1 parent 3b0fa70 commit 7fa8345
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
14 changes: 14 additions & 0 deletions pdc/apps/compose/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from django.db import transaction, connection
from django.db.models import Q
from rest_framework import serializers

from pdc.apps.package.models import RPM
from pdc.apps.common import hacks as common_hacks
Expand All @@ -25,6 +26,15 @@
from pdc.apps.component.models import ReleaseComponent


def _maybe_raise_inconsistency_error(composeinfo, manifest, name):
"""Raise ValidationError if compose id is not the same in both files.
The name should describe the kind of manifest.
"""
if composeinfo.compose.id != manifest.compose.id:
raise serializers.ValidationError(
{'detail': ['Inconsistent data: different compose id in composeinfo and {0} file.'.format(name)]})


def get_or_insert_rpm(rpms_in_db, cursor, rpm_nevra, srpm_nevra, filename):
rpm_id = rpms_in_db.get(rpm_nevra, None)
if not rpm_id:
Expand Down Expand Up @@ -82,6 +92,8 @@ def compose__import_rpms(request, release_id, composeinfo, rpm_manifest):
ci = common_hacks.composeinfo_from_str(composeinfo)
rm = common_hacks.rpms_from_str(rpm_manifest)

_maybe_raise_inconsistency_error(ci, rm, 'rpms')

compose_date = "%s-%s-%s" % (ci.compose.date[:4], ci.compose.date[4:6], ci.compose.date[6:])
compose_type = models.ComposeType.objects.get(name=ci.compose.type)
acceptance_status = models.ComposeAcceptanceTestingState.objects.get(name='untested')
Expand Down Expand Up @@ -165,6 +177,8 @@ def compose__import_images(request, release_id, composeinfo, image_manifest):
ci = common_hacks.composeinfo_from_str(composeinfo)
im = common_hacks.images_from_str(image_manifest)

_maybe_raise_inconsistency_error(ci, im, 'images')

compose_date = "%s-%s-%s" % (ci.compose.date[:4], ci.compose.date[4:6], ci.compose.date[6:])
compose_type = models.ComposeType.objects.get(name=ci.compose.type)
compose_obj, created = lib._logged_get_or_create(
Expand Down
23 changes: 20 additions & 3 deletions pdc/apps/compose/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,14 +810,22 @@ def setUp(self):
self.compose_info = json.loads(f.read())
with open('pdc/apps/compose/fixtures/tests/rpm-manifest.json', 'r') as f:
self.manifest = json.loads(f.read())
self.client.post(reverse('releaseimportcomposeinfo-list'),
self.compose_info, format='json')
# Caching ids makes it faster, but the cache needs to be cleared for each test.
models.Path.CACHE = {}
common_models.SigKey.CACHE = {}

def test_import_inconsistent_data(self):
self.manifest['payload']['compose']['id'] = 'TP-1.0-20150315.0'
response = self.client.post(reverse('composerpm-list'),
{'rpm_manifest': self.manifest,
'release_id': 'tp-1.0',
'composeinfo': self.compose_info},
format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_import_and_retrieve_manifest(self):
response = self.client.post(reverse('releaseimportcomposeinfo-list'),
self.compose_info, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = self.client.post(reverse('composerpm-list'),
{'rpm_manifest': self.manifest,
'release_id': 'tp-1.0',
Expand Down Expand Up @@ -870,6 +878,15 @@ def test_import_images(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get('count'), 4)

def test_import_inconsistent_data(self):
self.manifest['payload']['compose']['id'] = 'TP-1.0-20150315.0'
response = self.client.post(reverse('composeimage-list'),
{'image_manifest': self.manifest,
'release_id': 'tp-1.0',
'composeinfo': self.compose_info},
format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_import_and_retrieve_images(self):
response = self.client.post(reverse('composeimage-list'),
{'image_manifest': self.manifest,
Expand Down

0 comments on commit 7fa8345

Please sign in to comment.