Skip to content

Commit

Permalink
Rewrite test case for artifact creation
Browse files Browse the repository at this point in the history
Clean up all created artifacts. Let test methods be more independent.
Use iterators to churn through many combinations of both valid and
invalid attributes.
  • Loading branch information
Ichimonji10 authored and Kersom committed Apr 24, 2018
1 parent bc55a91 commit 13aa943
Showing 1 changed file with 77 additions and 57 deletions.
134 changes: 77 additions & 57 deletions pulp_smash/tests/pulp3/pulpcore/api_v3/test_crd_artifacts.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,110 @@
# coding=utf-8
"""Tests that perform actions over artifacts."""
import hashlib
import itertools
import unittest
from random import randint

from requests.exceptions import HTTPError

from pulp_smash import api, cli, config, selectors, utils
from pulp_smash.constants import FILE_URL, FILE2_URL
from pulp_smash import api, cli, config, utils
from pulp_smash.constants import FILE_URL
from pulp_smash.exceptions import CalledProcessError
from pulp_smash.tests.pulp3.constants import ARTIFACTS_PATH
from pulp_smash.tests.pulp3.pulpcore.utils import set_up_module as setUpModule # pylint:disable=unused-import
from pulp_smash.tests.pulp3.utils import delete_orphans, get_auth


class ArtifactTestCase(unittest.TestCase, utils.SmokeTest):
"""Perform actions over an artifact."""
"""Create an artifact by uploading a file.
This test targets the following issues:
* `Pulp #2843 <https://pulp.plan.io/issues/2843>`_
* `Pulp Smash #726 <https://github.com/PulpQE/pulp-smash/issues/726>`_
"""

@classmethod
def setUpClass(cls):
"""Create class-wide variable."""
cls.artifact = {}
cls.cfg = config.get_config()
delete_orphans(cls.cfg)
cls.client = api.Client(cls.cfg, api.json_handler)
"""Delete orphans and create class-wide variables."""
cfg = config.get_config()
delete_orphans(cfg)
cls.client = api.Client(cfg, api.json_handler)
cls.client.request_kwargs['auth'] = get_auth()
cls.file = {'file': utils.http_get(FILE2_URL)}
cls.sha256 = hashlib.sha256(cls.file['file']).hexdigest()
cls.file = {'file': utils.http_get(FILE_URL)}
cls.file_sha256 = hashlib.sha256(cls.file['file']).hexdigest()
cls.file_size = len(cls.file['file'])

def test_01_create(self):
"""Create an artifact by uploading a file.
def test_upload_valid_attrs(self):
"""Upload a file, and provide valid attributes.
This test explores the design choice stated in `Pulp #2843`_ that an
artifact can be created using HTTP POST with a body of request that
contains a multipart form data.
For each possible combination of ``sha256`` and ``size`` (including
neither), do the following:
This test targets the following issues:
1. Upload a file with the chosen combination of attributes.
2. Verify that an artifact has been created, and that it has valid
attributes.
3. Delete the artifact, and verify that its attributes are
inaccessible.
"""
file_attrs = {'sha256': self.file_sha256, 'size': self.file_size}
for i in range(len(file_attrs) + 1):
for keys in itertools.combinations(file_attrs, i):
data = {key: file_attrs[key] for key in keys}
with self.subTest(data=data):
self._do_upload_valid_attrs(data)

def _do_upload_valid_attrs(self, data):
"""Upload a file with the given attributes."""
artifact = self.client.post(ARTIFACTS_PATH, data=data, files=self.file)
self.addCleanup(self.client.delete, artifact['_href'])
read_artifact = self.client.get(artifact['_href'])
for key, val in artifact.items():
with self.subTest(key=key):
self.assertEqual(read_artifact[key], val)
self.doCleanups()
with self.assertRaises(HTTPError):
self.client.get(artifact['_href'])

* `Pulp #2843 <https://pulp.plan.io/issues/2843>`_
* `Pulp Smash #726 <https://github.com/PulpQE/pulp-smash/issues/726>`_
def test_upload_invalid_attrs(self):
"""Upload a file, and provide invalid attributes.
Do the following:
For each possible combination of ``sha256`` and ``size`` (except for
neither), do the following:
1. Download a file in memory.
2. Upload the just downloaded file to pulp.
3. Verify that the checksum of the just created artifact is equal to
the checksum of the file that was uploaded.
1. Upload a file with the chosen combination of attributes. Verify that
an error is returned.
2. Verify that no artifacts exist in Pulp whose attributes match the
file that was unsuccessfully uploaded.
"""
files = {'file': utils.http_get(FILE_URL)}
type(self).artifact = self.client.post(ARTIFACTS_PATH, files=files)
self.assertEqual(
self.artifact['sha256'],
hashlib.sha256(files['file']).hexdigest()
)

def test_01_create_no_match_data(self):
"""Create an artifact providing the wrong digest and size."""
file_attrs = {'sha256': utils.uuid4(), 'size': self.file_size + 1}
for i in range(1, len(file_attrs) + 1):
for keys in itertools.combinations(file_attrs, i):
data = {key: file_attrs[key] for key in keys}
with self.subTest(data=data):
self._do_upload_invalid_attrs(data)

def _do_upload_invalid_attrs(self, data):
"""Upload a file with the given attributes."""
with self.assertRaises(HTTPError):
data = {'sha256': utils.uuid4(),
'size': randint(0, 100)}
self.client.post(ARTIFACTS_PATH, data=data, files=self.file)
for artifact in self.client.get(ARTIFACTS_PATH)['results']:
self.assertNotEqual(artifact['sha256'], self.sha256)
self.assertNotEqual(artifact['sha256'], self.file_sha256)

def test_01_create_with_match_data(self):
"""Create an artifact providing the right digest and size."""
data = {'sha256': self.sha256,
'size': len(self.file['file'])}
artifact = self.client.post(ARTIFACTS_PATH, data=data, files=self.file)
self.assertEqual(artifact['sha256'], data['sha256'])
self.assertEqual(artifact['size'], data['size'])

@selectors.skip_if(bool, 'artifact', False)
def test_02_read(self):
"""Read an artifact by its href."""
artifact = self.client.get(self.artifact['_href'])
for key, val in self.artifact.items():
with self.subTest(key=key):
self.assertEqual(artifact[key], val)
def test_upload_mixed_attrs(self):
"""Upload a file, and provide both valid and invalid attributes.
@selectors.skip_if(bool, 'artifact', False)
def test_03_delete(self):
"""Delete an artifact."""
self.client.delete(self.artifact['_href'])
with self.assertRaises(HTTPError):
self.client.get(self.artifact['_href'])
Do the following:
1. Upload a file and provide both an ``sha256`` and a ``size``. Let one
be valid, and the other be valid. Verify that an error is returned.
2. Verify that no artifacts exist in Pulp whose attributes match the
file that was unsuccessfully uploaded.
"""
for data in (
{'sha256': self.file_sha256, 'size': self.file_size + 1},
{'sha256': utils.uuid4(), 'size': self.file_size}):
with self.subTest(data=data):
self._do_upload_invalid_attrs(data)


class ArtifactsDeleteFileSystemTestCase(unittest.TestCase, utils.SmokeTest):
Expand Down

0 comments on commit 13aa943

Please sign in to comment.