From 5ac73f8a39ef5792934a39b7200c942f17aaff05 Mon Sep 17 00:00:00 2001 From: git-hyagi <45576767+git-hyagi@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:22:29 -0300 Subject: [PATCH] update --- pulp_container/app/utils.py | 1 - .../functional/api/test_remote_filter2.py | 140 ++++++++++++++++++ .../functional/api/test_remote_filter3.py | 72 +++++++++ 3 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 pulp_container/tests/functional/api/test_remote_filter2.py create mode 100644 pulp_container/tests/functional/api/test_remote_filter3.py diff --git a/pulp_container/app/utils.py b/pulp_container/app/utils.py index 9177b6ded..ea91a4ba6 100644 --- a/pulp_container/app/utils.py +++ b/pulp_container/app/utils.py @@ -337,5 +337,4 @@ def filter(remote, element_list, tags=False): if any(fnmatch.fnmatch(item, pattern) for pattern in include) ] - return element_list diff --git a/pulp_container/tests/functional/api/test_remote_filter2.py b/pulp_container/tests/functional/api/test_remote_filter2.py new file mode 100644 index 000000000..4cf77496b --- /dev/null +++ b/pulp_container/tests/functional/api/test_remote_filter2.py @@ -0,0 +1,140 @@ +import fnmatch +import time +import subprocess +import pytest +import re + +from uuid import uuid4 + +from pulp_container.app.exceptions import InvalidRequest +from pulp_container.tests.functional.constants import ( + REGISTRY_V2, + REGISTRY_V2_FEED_URL, + PULP_HELLO_WORLD_REPO, + PULP_FIXTURE_1, +) + +# try: +# import debugpy +# debugpy.listen(('0.0.0.0',5678)) +# debugpy.wait_for_client() +# except: +# pass + + +@pytest.fixture +def pull_through_distribution( + gen_object_with_cleanup, + container_pull_through_remote_api, + container_pull_through_distribution_api, +): + def _pull_through_distribution(includes, excludes): + remote = gen_object_with_cleanup( + container_pull_through_remote_api, + { + "name": str(uuid4()), + "url": REGISTRY_V2_FEED_URL, + "includes": includes, + "excludes": excludes, + }, + ) + distribution = gen_object_with_cleanup( + container_pull_through_distribution_api, + {"name": str(uuid4()), "base_path": str(uuid4()), "remote": remote.pulp_href}, + ) + return distribution + + return _pull_through_distribution + + +@pytest.fixture +def pull_and_verify( + add_to_cleanup, + capfd, + container_pull_through_distribution_api, + container_distribution_api, + container_repository_api, + container_remote_api, + container_tag_api, + registry_client, + local_registry, +): + def _pull_and_verify(images, pull_through_distribution, includes, excludes): + tags_to_verify = [] + distr = pull_through_distribution(includes, excludes) + for _, image_path in enumerate(images, start=1): + local_image_path = f"{distr.base_path}/{image_path}" + + # 1. pull remote content through the pull-through distribution + if any(re.match(".*pulp.*", pattern) for pattern in excludes): + with pytest.raises(subprocess.CalledProcessError) as e: + local_registry.pull(local_image_path) + assert ( + re.search( + ".*No repository found for the defined remote filters.*", + capfd.readouterr().err, + ) + is not None + ) + continue + local_registry.pull(local_image_path) + + # when the client pulls the image, a repository, distribution, and remote is created in + # the background; therefore, scheduling the cleanup for these entities is necessary + path, tag = local_image_path.split(":") + tags_to_verify.append(tag) + repository = container_repository_api.list(name=path).results[0] + add_to_cleanup(container_repository_api, repository.pulp_href) + remote = container_remote_api.list(name=path).results[0] + add_to_cleanup(container_remote_api, remote.pulp_href) + distribution = container_distribution_api.list(name=path).results[0] + add_to_cleanup(container_distribution_api, distribution.pulp_href) + + pull_through_distribution = container_pull_through_distribution_api.list( + name=distr.name + ).results[0] + + assert [distribution.pulp_href] == pull_through_distribution.distributions + + return _pull_and_verify + + +@pytest.mark.skip() +def test_manifest_list_pull(delete_orphans_pre, pull_through_distribution, pull_and_verify): + images = [f"{PULP_HELLO_WORLD_REPO}:latest", f"{PULP_HELLO_WORLD_REPO}:linux"] + includes = [] + excludes = [] + pull_and_verify(images, pull_through_distribution, includes, excludes) + excludes = [] + pull_and_verify(images, pull_through_distribution, includes, excludes) + + +@pytest.mark.skip() +def test_no_filter(delete_orphans_pre, pull_through_distribution, pull_and_verify): + images = [f"{PULP_FIXTURE_1}:manifest_a", f"{PULP_FIXTURE_1}:manifest_b"] + includes = [] + excludes = [] + pull_and_verify(images, pull_through_distribution, includes, excludes) + + +@pytest.mark.skip() +def test_filter_exclude_with_regex(delete_orphans_pre, pull_through_distribution, pull_and_verify): + images = [f"{PULP_FIXTURE_1}:manifest_a", f"{PULP_FIXTURE_1}:manifest_b"] + includes = [] + excludes = ["pulp*"] + pull_and_verify(images, pull_through_distribution, includes, excludes) + + +@pytest.mark.skip() +def test_filter_exclude(delete_orphans_pre, pull_through_distribution, pull_and_verify): + images = [f"{PULP_FIXTURE_1}:manifest_a", f"{PULP_FIXTURE_1}:manifest_b"] + includes = [] + excludes = ["pulp/test-fixture-1"] + pull_and_verify(images, pull_through_distribution, includes, excludes) + + +def test_filter_include_and_exclude(delete_orphans_pre, pull_through_distribution, pull_and_verify): + images = [f"{PULP_FIXTURE_1}:manifest_a", f"{PULP_FIXTURE_1}:manifest_b"] + includes = ["pulp*"] + excludes = ["pulp/test-fixture-1"] + pull_and_verify(images, pull_through_distribution, includes, excludes) diff --git a/pulp_container/tests/functional/api/test_remote_filter3.py b/pulp_container/tests/functional/api/test_remote_filter3.py new file mode 100644 index 000000000..27031ce51 --- /dev/null +++ b/pulp_container/tests/functional/api/test_remote_filter3.py @@ -0,0 +1,72 @@ +import pytest +import re + +from pulpcore.client.pulp_container import ContainerRepositorySyncURL +from pulpcore.tests.functional import PulpTaskError +from pulp_container.tests.functional.constants import ( + REGISTRY_V2_FEED_URL, + PULP_HELLO_WORLD_REPO, +) + + +@pytest.fixture +def pull_and_verify( + add_to_cleanup, + capsys, + container_repository_api, + container_remote_api, + container_tag_api, + delete_orphans_pre, + monitor_task, +): + def _pull_and_verify(excludes, includes): + repository = container_repository_api.create({"name": "foo"}) + add_to_cleanup(container_repository_api, repository.pulp_href) + remote = container_remote_api.create( + { + "name": "foo", + "url": REGISTRY_V2_FEED_URL, + "upstream_name": PULP_HELLO_WORLD_REPO, + "excludes": excludes, + "includes": includes, + } + ) + add_to_cleanup(container_remote_api, remote.pulp_href) + sync_data = ContainerRepositorySyncURL(remote=remote.pulp_href) + sync_response = container_repository_api.sync(repository.pulp_href, sync_data) + if any(re.match(".*pulp.*", pattern) for pattern in excludes): + with pytest.raises(PulpTaskError) as e: + monitor_task(sync_response.task) + if ( + re.search( + ".*No repository found for the defined remote filters.*", + str(e.value), + ) + is not None + ): + return 0 + monitor_task(sync_response.task) + tags = container_tag_api.list().results + return len(tags) + + return _pull_and_verify + + +def test_filter_with_regex(pull_and_verify): + test = pull_and_verify(includes=[], excludes={"*pulp*": ""}) + assert test == 0 + + +def test_filter(pull_and_verify): + test = pull_and_verify(includes=[], excludes={"pulp/hello-world": ""}) + assert test == 0 + + +def test_filter_multiple_excludes(pull_and_verify): + test = pull_and_verify(includes=[], excludes={"pulp": "", "*hello*": ""}) + assert test == 0 + + +def test_empty_filters(pull_and_verify): + test = pull_and_verify(includes=[], excludes=[]) + assert test == 2