Skip to content

Commit 68e0d44

Browse files
committed
Converts tests to use pytest fixtures
closes: #111
1 parent 6244f15 commit 68e0d44

File tree

6 files changed

+192
-302
lines changed

6 files changed

+192
-302
lines changed

Diff for: CHANGES/111.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Converted functional tests to use pytest fixtures.

Diff for: pulp_maven/tests/functional/api/test_crud_remotes.py

+68-121
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,69 @@
11
"""Tests that CRUD maven remotes."""
2-
import unittest
3-
4-
from requests.exceptions import HTTPError
5-
6-
from pulp_smash import api, config, utils
7-
8-
from pulp_maven.tests.functional.constants import MAVEN_REMOTE_PATH
9-
from pulp_maven.tests.functional.utils import skip_if, gen_maven_remote
10-
from pulp_maven.tests.functional.utils import set_up_module as setUpModule # noqa:F401
11-
12-
13-
class CRUDRemotesTestCase(unittest.TestCase):
14-
"""CRUD remotes."""
15-
16-
@classmethod
17-
def setUpClass(cls):
18-
"""Create class-wide variables."""
19-
cls.cfg = config.get_config()
20-
cls.client = api.Client(cls.cfg, api.json_handler)
21-
22-
def test_01_create_remote(self):
23-
"""Create a remote."""
24-
body = _gen_verbose_remote()
25-
type(self).remote = self.client.post(MAVEN_REMOTE_PATH, body)
26-
for key in ("username", "password"):
27-
del body[key]
28-
for key, val in body.items():
29-
with self.subTest(key=key):
30-
self.assertEqual(self.remote[key], val)
31-
32-
@skip_if(bool, "remote", False)
33-
def test_02_create_same_name(self):
34-
"""Try to create a second remote with an identical name.
35-
36-
See: `Pulp Smash #1055
37-
<https://github.com/PulpQE/pulp-smash/issues/1055>`_.
38-
"""
39-
body = gen_maven_remote()
40-
body["name"] = self.remote["name"]
41-
with self.assertRaises(HTTPError):
42-
self.client.post(MAVEN_REMOTE_PATH, body)
43-
44-
@skip_if(bool, "remote", False)
45-
def test_02_read_remote(self):
46-
"""Read a remote by its href."""
47-
remote = self.client.get(self.remote["pulp_href"])
48-
for key, val in self.remote.items():
49-
with self.subTest(key=key):
50-
self.assertEqual(remote[key], val)
51-
52-
@skip_if(bool, "remote", False)
53-
def test_02_read_remotes(self):
54-
"""Read a remote by its name."""
55-
page = self.client.get(MAVEN_REMOTE_PATH, params={"name": self.remote["name"]})
56-
self.assertEqual(len(page["results"]), 1)
57-
for key, val in self.remote.items():
58-
with self.subTest(key=key):
59-
self.assertEqual(page["results"][0][key], val)
60-
61-
@skip_if(bool, "remote", False)
62-
def test_03_partially_update(self):
63-
"""Update a remote using HTTP PATCH."""
64-
body = _gen_verbose_remote()
65-
self.client.patch(self.remote["pulp_href"], body)
66-
for key in ("username", "password"):
67-
del body[key]
68-
type(self).remote = self.client.get(self.remote["pulp_href"])
69-
for key, val in body.items():
70-
with self.subTest(key=key):
71-
self.assertEqual(self.remote[key], val)
72-
73-
@skip_if(bool, "remote", False)
74-
def test_04_fully_update(self):
75-
"""Update a remote using HTTP PUT."""
76-
body = _gen_verbose_remote()
77-
self.client.put(self.remote["pulp_href"], body)
78-
for key in ("username", "password"):
79-
del body[key]
80-
type(self).remote = self.client.get(self.remote["pulp_href"])
81-
for key, val in body.items():
82-
with self.subTest(key=key):
83-
self.assertEqual(self.remote[key], val)
84-
85-
@skip_if(bool, "remote", False)
86-
def test_05_delete(self):
87-
"""Delete a remote."""
88-
self.client.delete(self.remote["pulp_href"])
89-
with self.assertRaises(HTTPError):
90-
self.client.get(self.remote["pulp_href"])
91-
92-
93-
class CreateRemoteNoURLTestCase(unittest.TestCase):
94-
"""Verify whether is possible to create a remote without a URL."""
95-
96-
def test_all(self):
97-
"""Verify whether is possible to create a remote without a URL.
98-
99-
This test targets the following issues:
100-
101-
* `Pulp #3395 <https://pulp.plan.io/issues/3395>`_
102-
* `Pulp Smash #984 <https://github.com/PulpQE/pulp-smash/issues/984>`_
103-
"""
104-
body = gen_maven_remote()
105-
del body["url"]
106-
with self.assertRaises(HTTPError):
107-
api.Client(config.get_config()).post(MAVEN_REMOTE_PATH, body)
108-
109-
110-
def _gen_verbose_remote():
111-
"""Return a semi-random dict for use in defining a remote.
112-
113-
For most tests, it's desirable to create remotes with as few attributes
114-
as possible, so that the tests can specifically target and attempt to break
115-
specific features. This module specifically targets remotes, so it makes
116-
sense to provide as many attributes as possible.
117-
118-
Note that 'username' and 'password' are write-only attributes.
119-
"""
120-
attrs = gen_maven_remote()
121-
attrs.update({"password": utils.uuid4(), "username": utils.uuid4()})
122-
return attrs
2+
import json
3+
import uuid
4+
5+
import pytest
6+
7+
from pulpcore.client.pulp_maven.exceptions import ApiException
8+
9+
10+
@pytest.mark.parallel
11+
def test_remote_crud_workflow(maven_remote_api_client, gen_object_with_cleanup, monitor_task):
12+
remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"}
13+
remote = gen_object_with_cleanup(maven_remote_api_client, remote_data)
14+
assert remote.url == remote_data["url"]
15+
assert remote.name == remote_data["name"]
16+
17+
with pytest.raises(ApiException) as exc:
18+
gen_object_with_cleanup(maven_remote_api_client, remote_data)
19+
assert exc.value.status == 400
20+
assert json.loads(exc.value.body) == {"name": ["This field must be unique."]}
21+
22+
update_response = maven_remote_api_client.partial_update(
23+
remote.pulp_href, {"url": "https://example.com"}
24+
)
25+
task = monitor_task(update_response.task)
26+
assert task.created_resources == []
27+
28+
remote = maven_remote_api_client.read(remote.pulp_href)
29+
assert remote.url == "https://example.com"
30+
31+
all_new_remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"}
32+
update_response = maven_remote_api_client.update(remote.pulp_href, all_new_remote_data)
33+
task = monitor_task(update_response.task)
34+
assert task.created_resources == []
35+
36+
remote = maven_remote_api_client.read(remote.pulp_href)
37+
assert remote.name == all_new_remote_data["name"]
38+
assert remote.url == all_new_remote_data["url"]
39+
40+
41+
@pytest.mark.parallel
42+
def test_create_maven_remote_with_invalid_parameter(
43+
maven_remote_api_client, gen_object_with_cleanup
44+
):
45+
unexpected_field_remote_data = {
46+
"name": str(uuid.uuid4()),
47+
"url": "http://example.com",
48+
"foo": "bar",
49+
}
50+
51+
with pytest.raises(ApiException) as exc:
52+
gen_object_with_cleanup(maven_remote_api_client, unexpected_field_remote_data)
53+
assert exc.value.status == 400
54+
assert json.loads(exc.value.body) == {"foo": ["Unexpected field"]}
55+
56+
57+
@pytest.mark.parallel
58+
def test_create_maven_remote_without_url(maven_remote_api_client, gen_object_with_cleanup):
59+
with pytest.raises(ApiException) as exc:
60+
gen_object_with_cleanup(maven_remote_api_client, {"name": str(uuid.uuid4())})
61+
assert exc.value.status == 400
62+
assert json.loads(exc.value.body) == {"url": ["This field is required."]}
63+
64+
65+
@pytest.mark.parallel
66+
def test_default_remote_policy_immediate(maven_remote_api_client, gen_object_with_cleanup):
67+
remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"}
68+
remote = gen_object_with_cleanup(maven_remote_api_client, remote_data)
69+
assert remote.policy == "immediate"
+29-68
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,43 @@
11
"""Tests that verify download of content served by Pulp."""
22
import hashlib
3-
import unittest
4-
from random import choice
53
from urllib.parse import urljoin
64

7-
from pulp_smash import api, config, utils
8-
from pulp_smash.pulp3.utils import gen_distribution, gen_repo
5+
from pulp_maven.tests.functional.utils import download_file
96

10-
from pulp_maven.tests.functional.utils import gen_maven_remote, get_maven_content_paths
11-
from pulp_maven.tests.functional.constants import (
12-
MAVEN_CONTENT_PATH,
13-
MAVEN_DISTRIBUTION_PATH,
14-
MAVEN_FIXTURE_URL,
15-
MAVEN_REMOTE_PATH,
16-
MAVEN_REPO_PATH,
17-
)
18-
from pulp_maven.tests.functional.utils import set_up_module as setUpModule # noqa:F401
197

8+
def test_download_content(
9+
maven_distribution_factory, maven_remote_factory, maven_artifact_api_client
10+
):
11+
"""Verify whether content served by pulp can be downloaded.
2012
21-
class DownloadContentTestCase(unittest.TestCase):
22-
"""Verify whether content served by pulp can be downloaded."""
13+
The process of creating a Maven mirror is:
2314
24-
def test_all(self):
25-
"""Verify whether content served by pulp can be downloaded.
15+
1. Create a Maven Remote with a URL pointing to the root of a Maven repository.
16+
2. Create a distribution with the remote set HREF from 1.
2617
27-
The process of creating a Maven mirror is simple:
18+
Do the following:
2819
29-
1. Create a Maven Remote with a URL pointing to the root of a Maven repository.
30-
2. Create a distribution with the remote set HREF from 1.
20+
1. Create a Maven Remote and a Distribution.
21+
2. Select a random content unit in the distribution. Download that
22+
content unit from Pulp, and verify that the content unit has the
23+
same checksum when fetched directly from Maven Central.
24+
"""
25+
remote = maven_remote_factory(url="https://repo1.maven.org/maven2/")
26+
distribution = maven_distribution_factory(remote=remote.pulp_href)
3127

32-
Do the following:
28+
# Pick a content unit, and download it from the remote repository
29+
unit_path = "academy/alex/custommatcher/1.0/custommatcher-1.0-javadoc.jar.sha1"
30+
remote_unit_url = urljoin(remote.url, unit_path)
31+
downloaded_file = download_file(remote_unit_url)
32+
remote_unit_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
3333

34-
1. Create a Maven Remote and a Distribution.
35-
2. Select a random content unit in the distribution. Download that
36-
content unit from Pulp, and verify that the content unit has the
37-
same checksum when fetched directly from Maven Central.
34+
# And from Pulp
35+
pulp_unit_url = urljoin(distribution.base_url, unit_path)
36+
downloaded_file = download_file(pulp_unit_url)
37+
pulp_unit_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
3838

39-
This test targets the following issues:
39+
assert remote_unit_checksum == pulp_unit_checksum
4040

41-
* `Pulp #2895 <https://pulp.plan.io/issues/2895>`_
42-
* `Pulp Smash #872 <https://github.com/PulpQE/pulp-smash/issues/872>`_
43-
"""
44-
cfg = config.get_config()
45-
client = api.Client(cfg, api.json_handler)
46-
47-
repo = client.post(MAVEN_REPO_PATH, gen_repo())
48-
self.addCleanup(client.delete, repo["pulp_href"])
49-
50-
body = gen_maven_remote()
51-
remote = client.post(MAVEN_REMOTE_PATH, body)
52-
self.addCleanup(client.delete, remote["pulp_href"])
53-
54-
repo = client.get(repo["pulp_href"])
55-
56-
# Create a distribution.
57-
body = gen_distribution()
58-
body["remote"] = remote["pulp_href"]
59-
response_dict = client.post(MAVEN_DISTRIBUTION_PATH, body)
60-
dist_task = client.get(response_dict["task"])
61-
distribution_href = dist_task["created_resources"][0]
62-
distribution = client.get(distribution_href)
63-
self.addCleanup(client.delete, distribution["pulp_href"])
64-
65-
# Pick a content unit, and download it from both Pulp Fixtures…
66-
unit_path = choice(get_maven_content_paths(repo))
67-
fixtures_hash = hashlib.sha256(
68-
utils.http_get(urljoin(MAVEN_FIXTURE_URL, unit_path))
69-
).hexdigest()
70-
71-
# …and Pulp.
72-
client.response_handler = api.safe_handler
73-
74-
unit_url = urljoin(distribution["base_url"] + "/", unit_path)
75-
76-
pulp_hash = hashlib.sha256(client.get(unit_url).content).hexdigest()
77-
self.assertEqual(fixtures_hash, pulp_hash)
78-
79-
# Check that Pulp created a MavenArtifact
80-
content_filter_url = MAVEN_CONTENT_PATH + "?filename=custommatcher-1.0-javadoc.jar.sha1"
81-
content_unit = client.get(content_filter_url)
82-
self.assertEqual(1, content_unit.json()["count"])
41+
# Check that Pulp created a MavenArtifact
42+
content_response = maven_artifact_api_client.list(filename="custommatcher-1.0-javadoc.jar.sha1")
43+
assert content_response.count == 1

Diff for: pulp_maven/tests/functional/conftest.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import uuid
2+
3+
import pytest
4+
5+
from pulpcore.client.pulp_maven import (
6+
ApiClient,
7+
ContentArtifactApi,
8+
DistributionsMavenApi,
9+
RemotesMavenApi,
10+
RepositoriesMavenApi,
11+
)
12+
13+
14+
@pytest.fixture(scope="session")
15+
def maven_client(_api_client_set, bindings_cfg):
16+
api_client = ApiClient(bindings_cfg)
17+
_api_client_set.add(api_client)
18+
yield api_client
19+
_api_client_set.remove(api_client)
20+
21+
22+
@pytest.fixture(scope="session")
23+
def maven_artifact_api_client(maven_client):
24+
return ContentArtifactApi(maven_client)
25+
26+
27+
@pytest.fixture(scope="session")
28+
def maven_distro_api_client(maven_client):
29+
return DistributionsMavenApi(maven_client)
30+
31+
32+
@pytest.fixture(scope="session")
33+
def maven_repo_api_client(maven_client):
34+
return RepositoriesMavenApi(maven_client)
35+
36+
37+
@pytest.fixture(scope="session")
38+
def maven_remote_api_client(maven_client):
39+
return RemotesMavenApi(maven_client)
40+
41+
42+
@pytest.fixture
43+
def maven_distribution_factory(maven_distro_api_client, gen_object_with_cleanup):
44+
def _maven_distribution_factory(**kwargs):
45+
data = {"base_path": str(uuid.uuid4()), "name": str(uuid.uuid4())}
46+
data.update(kwargs)
47+
return gen_object_with_cleanup(maven_distro_api_client, data)
48+
49+
return _maven_distribution_factory
50+
51+
52+
@pytest.fixture
53+
def maven_repo_factory(maven_repo_api_client, gen_object_with_cleanup):
54+
"""A factory to generate a Maven Repository with auto-deletion after the test run."""
55+
56+
def _maven_repo_factory(**kwargs):
57+
kwargs.setdefault("name", str(uuid.uuid4()))
58+
return gen_object_with_cleanup(maven_repo_api_client, kwargs)
59+
60+
yield _maven_repo_factory
61+
62+
63+
@pytest.fixture
64+
def maven_remote_factory(maven_remote_api_client, gen_object_with_cleanup):
65+
"""A factory to generate a Maven Remote with auto-deletion after the test run."""
66+
67+
def _maven_remote_factory(**kwargs):
68+
kwargs.setdefault("name", str(uuid.uuid4()))
69+
return gen_object_with_cleanup(maven_remote_api_client, kwargs)
70+
71+
yield _maven_remote_factory

0 commit comments

Comments
 (0)