Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for CRD artifacts. #885

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ developers, not a gospel.
api/pulp_smash.tests.pulp3.constants
api/pulp_smash.tests.pulp3.file
api/pulp_smash.tests.pulp3.file.api_v3
api/pulp_smash.tests.pulp3.file.api_v3.test_crd_artifacts
api/pulp_smash.tests.pulp3.file.api_v3.test_crd_publications
api/pulp_smash.tests.pulp3.file.api_v3.test_crud_importers
api/pulp_smash.tests.pulp3.file.api_v3.test_crud_publishers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`pulp_smash.tests.pulp3.file.api_v3.test_crd_artifacts`
=======================================================

Location: :doc:`/index` → :doc:`/api` → :doc:`/api/pulp_smash.tests.pulp3.file.api_v3.test_crd_artifacts`

.. automodule:: pulp_smash.tests.pulp3.file.api_v3.test_crd_artifacts
2 changes: 2 additions & 0 deletions pulp_smash/tests/pulp3/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

BASE_PATH = '/api/v3/'

ARTIFACTS_PATH = urljoin(BASE_PATH, 'artifacts/')

BASE_IMPORTER_PATH = urljoin(BASE_PATH, 'importers/')

BASE_PUBLISHER_PATH = urljoin(BASE_PATH, 'publishers/')
Expand Down
71 changes: 71 additions & 0 deletions pulp_smash/tests/pulp3/file/api_v3/test_crd_artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# coding=utf-8
"""Tests that perform actions over artifacts."""
import hashlib
import unittest

from requests.exceptions import HTTPError

from pulp_smash import api, config, selectors, utils
from pulp_smash.constants import FILE_URL
from pulp_smash.tests.pulp3.constants import ARTIFACTS_PATH
from pulp_smash.tests.pulp3.pulpcore.utils import set_up_module as setUpModule # noqa pylint:disable=unused-import
from pulp_smash.tests.pulp3.utils import clean_artifacts, get_auth


class ArtifactTestCase(unittest.TestCase, utils.SmokeTest):
"""Perform actions over an artifact."""

@classmethod
def setUpClass(cls):
"""Create class-wide variable."""
cls.artifact = {}
cls.cfg = config.get_config()
cls.client = api.Client(cls.cfg, api.json_handler)
cls.client.request_kwargs['auth'] = get_auth()

def test_01_create(self):
"""Create an artifact by uploading a file.

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.

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>`_

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.
"""
files = {'file': utils.http_get(FILE_URL)}

# To avoid upload of duplicates in pulp. This function call may be
# removed after pulp3 is able to handle duplicates, and how to delete
# artifacts that are content units as well.
clean_artifacts(self.cfg)

type(self).artifact = self.client.post(ARTIFACTS_PATH, files=files)
self.assertEqual(
self.artifact['sha256'],
hashlib.sha256(files['file']).hexdigest()
)

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

@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'])
29 changes: 29 additions & 0 deletions pulp_smash/tests/pulp3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from pulp_smash import api, config, selectors
from pulp_smash.tests.pulp3.constants import (
ARTIFACTS_PATH,
FILE_CONTENT_PATH,
FILE_IMPORTER_PATH,
JWT_PATH,
STATUS_PATH,
Expand Down Expand Up @@ -230,3 +232,30 @@ def get_content_unit_names(repo):
content_unit['path'] # A misnomer. Think "name," not "path."
for content_unit in read_repo_content(repo)['results']
]


def clean_artifacts(cfg=None):
"""Clean all artifacts present in pulp.

:param pulp_smash.config.PulpSmashConfig cfg: Information about the Pulp
host.
"""
if cfg is None:
cfg = config.get_config()
clean_content_units(cfg)
client = api.Client(cfg, api.json_handler)
for artifact in client.get(ARTIFACTS_PATH)['results']:
client.delete(artifact['_href'])


def clean_content_units(cfg=None):
"""Clean all content units present in pulp.

:param pulp_smash.config.PulpSmashConfig cfg: Information about the Pulp
host.
"""
if cfg is None:
cfg = config.get_config()
client = api.Client(cfg, api.json_handler)
for content_unit in client.get(FILE_CONTENT_PATH)['results']:
client.delete(content_unit['_href'])