From 13aa943855c0ca00185c8e18106d3994ec43f038 Mon Sep 17 00:00:00 2001 From: Jeremy Audet Date: Tue, 24 Apr 2018 14:12:08 -0400 Subject: [PATCH] Rewrite test case for artifact creation Clean up all created artifacts. Let test methods be more independent. Use iterators to churn through many combinations of both valid and invalid attributes. --- .../pulpcore/api_v3/test_crd_artifacts.py | 134 ++++++++++-------- 1 file changed, 77 insertions(+), 57 deletions(-) diff --git a/pulp_smash/tests/pulp3/pulpcore/api_v3/test_crd_artifacts.py b/pulp_smash/tests/pulp3/pulpcore/api_v3/test_crd_artifacts.py index 92d1c4639..4e49b1a55 100644 --- a/pulp_smash/tests/pulp3/pulpcore/api_v3/test_crd_artifacts.py +++ b/pulp_smash/tests/pulp3/pulpcore/api_v3/test_crd_artifacts.py @@ -1,13 +1,13 @@ # 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 @@ -15,76 +15,96 @@ 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 `_ + * `Pulp Smash #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 `_ - * `Pulp Smash #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):