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

Refactor older tests #857

Merged
merged 2 commits into from
Aug 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 44 additions & 92 deletions pulp_deb/tests/functional/api/test_crud_remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,38 @@

from pulpcore.client.pulp_deb.exceptions import ApiException

from pulp_deb.tests.functional.constants import DOWNLOAD_POLICIES, DEB_SIGNING_KEY
from pulp_deb.tests.functional.utils import gen_local_deb_remote
from pulp_deb.tests.functional.utils import gen_deb_remote_verbose
from pulp_deb.tests.functional.constants import DOWNLOAD_POLICIES


@pytest.fixture
def deb_init_verbose_remote(deb_get_fixture_server_url, deb_remote_custom_data_factory):
"""A fixture that initializes are deb remote with verbose data."""

def _deb_init_verbose_remote(remove_policy=False, extra_data=False):
"""Generates a deb remote with verbose data.

:param remove_policy: (Optional) If set will remove the policy field from the dict
:param extra_data: (Optional) If set will return an extra set of data for testing
:returns: A tuple containing the created remote and the verbose data used to create it.
Optionally it will also return a second set of verbose data for testing purposes.
"""
url = deb_get_fixture_server_url()
data = gen_deb_remote_verbose(url, remove_policy)
remote = deb_remote_custom_data_factory(data)
return (
(remote, data)
if not extra_data
else (remote, data, gen_deb_remote_verbose(url, remove_policy))
)

return _deb_init_verbose_remote


@pytest.mark.parallel
def test_create_remote_repository(deb_remote_custom_data_factory, deb_get_fixture_server_url):
def test_create_remote_repository(deb_init_verbose_remote):
"""Test creation of the remote."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
remote, data = deb_init_verbose_remote()

for key, val in data.items():
if key != "username" and key != "password":
Expand All @@ -23,12 +45,10 @@ def test_create_remote_repository(deb_remote_custom_data_factory, deb_get_fixtur

@pytest.mark.parallel
def test_create_remote_repository_with_same_name(
deb_remote_custom_data_factory, deb_get_fixture_server_url
deb_remote_custom_data_factory, deb_init_verbose_remote
):
"""Verify whether it is possible to create a remote with the same name."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
deb_remote_custom_data_factory(data)
_, data = deb_init_verbose_remote()

with pytest.raises(ApiException) as exc:
deb_remote_custom_data_factory(data)
Expand All @@ -37,13 +57,9 @@ def test_create_remote_repository_with_same_name(


@pytest.mark.parallel
def test_create_remote_repository_without_url(
deb_remote_custom_data_factory, deb_get_fixture_server_url
):
def test_create_remote_repository_without_url(deb_remote_custom_data_factory):
"""Verify whether it is possible to create a remote without an URL."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
del data["url"]
data = gen_deb_remote_verbose()

with pytest.raises(ApiException) as exc:
deb_remote_custom_data_factory(data)
Expand All @@ -52,31 +68,19 @@ def test_create_remote_repository_without_url(


@pytest.mark.parallel
def test_read_remote_by_href(
deb_get_fixture_server_url,
deb_get_remote_by_href,
deb_remote_custom_data_factory,
):
def test_read_remote_by_href(deb_init_verbose_remote, deb_get_remote_by_href):
"""Verify whether it is possible to read a remote repository by its href."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
remote, _ = deb_init_verbose_remote()
read_remote = deb_get_remote_by_href(remote.pulp_href)

for key, val in remote.to_dict().items():
assert read_remote.to_dict()[key] == val


@pytest.mark.parallel
def test_read_remote_by_name(
deb_get_fixture_server_url,
deb_get_remotes_by_name,
deb_remote_custom_data_factory,
):
def test_read_remote_by_name(deb_init_verbose_remote, deb_get_remotes_by_name):
"""Verify whether it is possible to read a remote repository by its name."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
remote, _ = deb_init_verbose_remote()
read_remote = deb_get_remotes_by_name(remote.name)

assert len(read_remote.results) == 1
Expand All @@ -86,17 +90,9 @@ def test_read_remote_by_name(


@pytest.mark.parallel
def test_patch_remote(
deb_get_fixture_server_url,
deb_get_remote_by_href,
deb_patch_remote,
deb_remote_custom_data_factory,
):
def test_patch_remote(deb_init_verbose_remote, deb_get_remote_by_href, deb_patch_remote):
"""Verify whether it is possible to update a remote with PATCH."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
patch_data = gen_verbose_remote_data(url)
remote, _, patch_data = deb_init_verbose_remote(extra_data=True)
deb_patch_remote(remote, patch_data)
patch_remote = deb_get_remote_by_href(remote.pulp_href)

Expand All @@ -108,17 +104,9 @@ def test_patch_remote(


@pytest.mark.parallel
def test_put_remote(
deb_get_fixture_server_url,
deb_get_remote_by_href,
deb_put_remote,
deb_remote_custom_data_factory,
):
def test_put_remote(deb_init_verbose_remote, deb_get_remote_by_href, deb_put_remote):
"""Verify whether it is possible to update a remote with PUT."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
put_data = gen_verbose_remote_data(url)
remote, _, put_data = deb_init_verbose_remote(extra_data=True)
deb_put_remote(remote, put_data)
put_remote = deb_get_remote_by_href(remote.pulp_href)

Expand All @@ -130,16 +118,9 @@ def test_put_remote(


@pytest.mark.parallel
def test_delete_remote(
deb_delete_remote,
deb_get_fixture_server_url,
deb_get_remote_by_href,
deb_remote_custom_data_factory,
):
def test_delete_remote(deb_init_verbose_remote, deb_delete_remote, deb_get_remote_by_href):
"""Verify whether it is possible to delete a remote."""
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
remote = deb_remote_custom_data_factory(data)
remote, _ = deb_init_verbose_remote()
deb_delete_remote(remote)

with pytest.raises(ApiException) as exc:
Expand All @@ -150,17 +131,12 @@ def test_delete_remote(

@pytest.mark.parallel
def test_remote_download_policies(
deb_init_verbose_remote,
deb_get_remote_by_href,
deb_get_fixture_server_url,
deb_patch_remote,
deb_remote_custom_data_factory,
):
"""Verify download policy behavior for valid and invalid values."""
# Create a remote without a download policy.
url = deb_get_fixture_server_url()
data = gen_verbose_remote_data(url)
del data["policy"]
remote = deb_remote_custom_data_factory(data)
remote, _ = deb_init_verbose_remote(remove_policy=True)

# Verify the creation is successful and the "immediate" policy is applied.
assert remote.policy == "immediate"
Expand All @@ -185,27 +161,3 @@ def test_remote_download_policies(
# Verify that the remote policy remains unchanged
remote = deb_get_remote_by_href(remote.pulp_href)
assert remote.policy == remote_snapshot.policy


def gen_verbose_remote_data(url):
"""Return a semi-random dict for use in defining a remote.

For most tests, it's desirable to create remotes with as few attributes
as possible, so that the tests can specifically target and attempt to break
specific features. This module specifically targets remotes, so it makes
sense to provide as many attributes as possible.
Note that 'username' and 'password' are write-only attributes.
"""
data = gen_local_deb_remote(url)
data.update(
{
"password": str(uuid4()),
"username": str(uuid4()),
"policy": choice(DOWNLOAD_POLICIES),
"distributions": f"{str(uuid4())} {str(uuid4())}",
"components": f"{str(uuid4())} {str(uuid4())}",
"architectures": f"{str(uuid4())} {str(uuid4())}",
"gpgkey": DEB_SIGNING_KEY,
}
)
return data
12 changes: 2 additions & 10 deletions pulp_deb/tests/functional/api/test_download_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,11 @@ def _deb_get_random_content_unit_path(repo, is_verbatim=False):
@pytest.mark.parallel
@pytest.mark.parametrize("is_verbatim", [False, True])
def test_download_content(
deb_init_and_sync,
deb_distribution_factory,
deb_get_fixture_server_url,
deb_publication_factory,
deb_remote_factory,
deb_repository_factory,
deb_verbatim_publication_factory,
deb_get_repository_by_href,
deb_get_random_content_unit_path,
deb_sync_repository,
deb_fixture_server,
download_content_unit,
http_get,
Expand All @@ -150,11 +146,7 @@ def test_download_content(
this case.
"""
# Create repository, remote and sync them
repo = deb_repository_factory()
url = deb_get_fixture_server_url()
remote = deb_remote_factory(url=url)
deb_sync_repository(remote, repo)
repo = deb_get_repository_by_href(repo.pulp_href)
repo, _ = deb_init_and_sync()

# Create a publication and a distribution
publication = (
Expand Down
45 changes: 11 additions & 34 deletions pulp_deb/tests/functional/api/test_download_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,18 @@
from pulp_deb.tests.functional.constants import DEB_FIXTURE_PACKAGE_COUNT, DEB_FIXTURE_SUMMARY


@pytest.mark.parametrize("policy", ["on_demand", "streamed"])
@pytest.mark.parametrize("remote_args", [{"policy": "on_demand"}, {"policy": "streamed"}])
def test_download_policy(
apt_package_api,
deb_init_and_sync,
deb_get_present_content_summary,
deb_get_added_content_summary,
deb_get_fixture_server_url,
deb_get_repository_by_href,
deb_publication_factory,
deb_remote_factory,
deb_repository_factory,
deb_sync_repository,
orphans_cleanup_api_client,
policy,
remote_args,
delete_orphans_pre,
):
"""Test whether lazy synced content can be accessed with different download policies."""
orphans_cleanup_api_client.cleanup({"orphan_protection_time": 0})
# Create repository and remote and verify latest `repository_version` is 0
repo = deb_repository_factory()
url = deb_get_fixture_server_url()
remote = deb_remote_factory(url=url, policy=policy)
assert repo.latest_version_href.endswith("/0/")

# Sync and verify latest `repository_version` is 1
deb_sync_repository(remote, repo)
repo = deb_get_repository_by_href(repo.pulp_href)
repo, remote = deb_init_and_sync(remote_args=remote_args)
assert repo.latest_version_href.endswith("/1/")

# Verify the correct amount of content units are available
Expand All @@ -37,8 +24,7 @@ def test_download_policy(

# Sync again and verify the same amount of content units are available
latest_version_href = repo.latest_version_href
deb_sync_repository(remote, repo)
repo = deb_get_repository_by_href(repo.pulp_href)
repo, _ = deb_init_and_sync(repository=repo, remote=remote)
assert repo.latest_version_href == latest_version_href
assert DEB_FIXTURE_SUMMARY == deb_get_present_content_summary(repo.to_dict())

Expand All @@ -51,29 +37,21 @@ def test_download_policy(
assert content.count == DEB_FIXTURE_PACKAGE_COUNT


@pytest.mark.parametrize("policy", ["on_demand", "streamed"])
@pytest.mark.parametrize("remote_args", [{"policy": "on_demand"}, {"policy": "streamed"}])
def test_lazy_sync_immediate_download_test(
artifacts_api_client,
deb_get_fixture_server_url,
deb_init_and_sync,
deb_get_remote_by_href,
deb_get_repository_by_href,
deb_patch_remote,
deb_remote_factory,
deb_repository_factory,
deb_sync_repository,
remote_args,
delete_orphans_pre,
policy,
):
"""Test whether a immediate sync after a lazy one has all artifacts available."""
# Cleanup artifacts
NON_LAZY_ARTIFACT_COUNT = 14

# Create repository and remote and sync them
repo = deb_repository_factory()
url = deb_get_fixture_server_url()
remote = deb_remote_factory(url=url, policy=policy)
deb_sync_repository(remote, repo)
repo = deb_get_repository_by_href(repo.pulp_href)
repo, remote = deb_init_and_sync(remote_args=remote_args)

# Verify amount of artifacts are equal to NON_LAZY_ARTIFACT_COUNT
artifacts = artifacts_api_client.list()
Expand All @@ -85,7 +63,6 @@ def test_lazy_sync_immediate_download_test(
assert remote.policy == "immediate"

# Sync with updated remote and verify the correct amount artifacts
deb_sync_repository(remote, repo)
repo = deb_get_repository_by_href(repo.pulp_href)
repo, _ = deb_init_and_sync(repository=repo, remote=remote)
artifacts = artifacts_api_client.list()
assert artifacts.count == NON_LAZY_ARTIFACT_COUNT + DEB_FIXTURE_PACKAGE_COUNT
Loading