diff --git a/CHANGES/324.misc b/CHANGES/324.misc new file mode 100644 index 00000000..893646e6 --- /dev/null +++ b/CHANGES/324.misc @@ -0,0 +1 @@ +Rewritten test_filter.py using pytest. diff --git a/pulp_ostree/tests/functional/api/test_filter.py b/pulp_ostree/tests/functional/api/test_filter.py index d9b247d6..bae5698e 100644 --- a/pulp_ostree/tests/functional/api/test_filter.py +++ b/pulp_ostree/tests/functional/api/test_filter.py @@ -1,100 +1,67 @@ -import unittest - -from urllib.parse import urlparse - -from pulp_smash import config -from pulp_smash.pulp3.bindings import delete_orphans, monitor_task -from pulp_smash.pulp3.utils import gen_repo - -from pulp_ostree.tests.functional.utils import ( - gen_ostree_client, - gen_ostree_remote, -) -from pulp_ostree.tests.functional.utils import set_up_module as setUpModule # noqa:F401 - -from pulpcore.client.pulp_ostree import ( - ContentCommitsApi, - ContentRefsApi, - RepositoriesOstreeApi, - RepositoriesOstreeVersionsApi, - RepositorySyncURL, - RemotesOstreeApi, -) - - -class ContentFilterTestCase(unittest.TestCase): - """A test case that verifies the content filtering.""" - - @classmethod - def setUpClass(cls): - """Initialize class-wide variables.""" - cfg = config.get_config() - cls.registry_name = urlparse(cfg.get_base_url()).netloc - - client_api = gen_ostree_client() - cls.repositories_api = RepositoriesOstreeApi(client_api) - cls.versions_api = RepositoriesOstreeVersionsApi(client_api) - cls.remotes_api = RemotesOstreeApi(client_api) - - cls.commits_api = ContentCommitsApi(client_api) - cls.refs_api = ContentRefsApi(client_api) - - @classmethod - def tearDownClass(cls): - """Clean orphaned content after finishing the tests.""" - delete_orphans() - - def setUp(self): - """Clean orphaned content before each test.""" - delete_orphans() - - self.repo = self.repositories_api.create(gen_repo()) - self.addCleanup(self.repositories_api.delete, self.repo.pulp_href) - - body = gen_ostree_remote(depth=0, policy="immediate") - remote = self.remotes_api.create(body) - self.addCleanup(self.remotes_api.delete, remote.pulp_href) - - self.assertEqual(self.repo.latest_version_href, f"{self.repo.pulp_href}versions/0/") - repository_sync_data = RepositorySyncURL(remote=remote.pulp_href) - response = self.repositories_api.sync(self.repo.pulp_href, repository_sync_data) - repo_version = monitor_task(response.task).created_resources[0] - - self.repository_version = self.versions_api.read(repo_version) - - def test_filter_refs(self): - """Check if refs can be filtered by their names and related commits' checksums.""" - refs_stable = self.refs_api.list( - repository_version_added=self.repository_version.pulp_href, name="stable" - ).to_dict() - - self.assertEqual(refs_stable["count"], 1, refs_stable) - self.assertEqual(refs_stable["results"][0]["name"], "stable", refs_stable) - - commit = self.commits_api.read(refs_stable["results"][0]["commit"]) - - refs_commit_checksum = self.refs_api.list( - repository_version_added=self.repository_version.pulp_href, checksum=commit.checksum - ).to_dict() - - self.assertEqual(refs_commit_checksum["count"], 1, refs_commit_checksum) - self.assertEqual( - refs_commit_checksum["results"][0]["checksum"], commit.checksum, refs_commit_checksum - ) - - def test_filter_commits(self): - """Check if commits can be filtered by their checksums.""" - refs_rawhide = self.refs_api.list( - repository_version_added=self.repository_version.pulp_href, name="rawhide" - ).to_dict() - ref_rawhide = refs_rawhide["results"][0] - - commits_rawhide = self.commits_api.list( - repository_version_added=self.repository_version.pulp_href, - checksum=ref_rawhide["checksum"], - ).to_dict() - self.assertEqual(commits_rawhide["count"], 1, commits_rawhide) - - commit_rawhide = commits_rawhide["results"][0] - self.assertEqual(commit_rawhide["checksum"], ref_rawhide["checksum"], commit_rawhide) - self.assertEqual(commit_rawhide["pulp_href"], ref_rawhide["commit"], commit_rawhide) +import pytest + + +@pytest.fixture() +def setup( + repo, + remote, + ostree_repository_version, + ostree_repositories_api, + ostree_remotes_api, + delete_orphans_pre, +): + """Setup specific for ostree filter tests""" + yield ostree_repository_version + ostree_repositories_api.delete(repo.pulp_href) + ostree_remotes_api.delete(remote.pulp_href) + delete_orphans_pre + + +def test_filter_refs( + repo, + setup, + ostree_content_refs_api, + ostree_content_commits_api, +): + """Check if refs can be filtered by their names and related commits' checksums.""" + assert repo.latest_version_href == f"{repo.pulp_href}versions/0/" + repo_version = setup + refs_stable = ostree_content_refs_api.list( + repository_version_added=repo_version.pulp_href, name="stable" + ).to_dict() + + assert refs_stable["count"] == 1, refs_stable + assert refs_stable["results"][0]["name"] == "stable", refs_stable + + commit = ostree_content_commits_api.read(refs_stable["results"][0]["commit"]) + refs_commit_checksum = ostree_content_refs_api.list( + repository_version_added=repo_version.pulp_href, checksum=commit.checksum + ).to_dict() + + assert refs_commit_checksum["count"] == 1, refs_commit_checksum + assert refs_commit_checksum["results"][0]["checksum"] == commit.checksum, refs_commit_checksum + + +def test_filter_commits( + repo, + setup, + ostree_content_refs_api, + ostree_content_commits_api, +): + """Check if commits can be filtered by their checksums.""" + assert repo.latest_version_href == f"{repo.pulp_href}versions/0/" + repo_version = setup + refs_rawhide = ostree_content_refs_api.list( + repository_version_added=repo_version.pulp_href, name="rawhide" + ).to_dict() + ref_rawhide = refs_rawhide["results"][0] + + commits_rawhide = ostree_content_commits_api.list( + repository_version_added=repo_version.pulp_href, + checksum=ref_rawhide["checksum"], + ).to_dict() + assert commits_rawhide["count"] == 1, commits_rawhide + + commit_rawhide = commits_rawhide["results"][0] + assert commit_rawhide["checksum"] == ref_rawhide["checksum"], commit_rawhide + assert commit_rawhide["pulp_href"] == ref_rawhide["commit"], commit_rawhide diff --git a/pulp_ostree/tests/functional/conftest.py b/pulp_ostree/tests/functional/conftest.py new file mode 100644 index 00000000..1c8579eb --- /dev/null +++ b/pulp_ostree/tests/functional/conftest.py @@ -0,0 +1,105 @@ +import pytest +import uuid + +from pulp_ostree.tests.functional.utils import gen_ostree_remote +from pulpcore.client.pulp_ostree import ( + ApiClient, + ContentCommitsApi, + ContentRefsApi, + RepositoriesOstreeApi, + RepositoriesOstreeVersionsApi, + RepositorySyncURL, + RemotesOstreeApi, +) + + +@pytest.fixture() +def ostree_client(_api_client_set, bindings_cfg): + """Fixture to provide a client to Pulp API""" + api_client = ApiClient(bindings_cfg) + _api_client_set.add(api_client) + yield api_client + _api_client_set.remove(api_client) + + +@pytest.fixture() +def ostree_content_refs_api(ostree_client): + """Fixture that returns an instance of ContentRefsApi""" + return ContentRefsApi(ostree_client) + + +@pytest.fixture() +def ostree_repositories_api(ostree_client): + """Fixture that returns an instance of RepositoriesOstreeApi""" + return RepositoriesOstreeApi(ostree_client) + + +@pytest.fixture() +def ostree_remotes_api(ostree_client): + """Fixture that returns an instance of RepositoriesOstreeApi""" + return RemotesOstreeApi(ostree_client) + + +@pytest.fixture() +def ostree_repositories_versions_api(ostree_client): + """Fixture that returns an instance of RepositoriesOstreeVersionsApi""" + return RepositoriesOstreeVersionsApi(ostree_client) + + +@pytest.fixture() +def ostree_repository_sync_url(ostree_client): + """Fixture that returns an instance of RepositorySyncURL""" + return RepositorySyncURL(ostree_client) + + +@pytest.fixture() +def ostree_content_commits_api(ostree_client): + """Fixture that returns an instance of ContentCommitsApi""" + return ContentCommitsApi(ostree_client) + + +@pytest.fixture() +def remote(ostree_remotes_api): + """Fixture that creates an ostree Remote and returns the object created""" + body = gen_ostree_remote(depth=0, policy="immediate") + return ostree_remotes_api.create(body) + + +@pytest.fixture() +def random_name(): + """Fixture that returns a dictionary with a random uuid for the `name` key""" + return {"name": str(uuid.uuid4())} + + +@pytest.fixture() +def repo(ostree_repositories_api, random_name): + """Fixture that creates an ostree Repository""" + return ostree_repositories_api.create(random_name) + + +@pytest.fixture() +def ostree_repository_version( + monitor_task, remote, repo, ostree_repositories_api, ostree_repositories_versions_api +): + """ + Fixture that syncs a Remote ostree Repository, + waits until the sync task completes and returns the + created repository version object. + """ + repository_sync_data = RepositorySyncURL(remote=remote.pulp_href) + response = ostree_repositories_api.sync(repo.pulp_href, repository_sync_data) + repo_version = monitor_task(response.task).created_resources[0] + + return ostree_repositories_versions_api.read(repo_version) + + +@pytest.fixture() +def ostree_repositories_delete(ostree_repositories_api, repo): + """Fixture that deletes an ostree Repository""" + return ostree_repositories_api.delete(repo.pulp_href) + + +@pytest.fixture() +def ostree_remotes_delete(ostree_remotes_api, remote): + """Fixture that deletes an ostree Remote""" + return ostree_remotes_api.delete(remote.pulp_href)