Skip to content

Commit

Permalink
Rewrite test_sync.py using pytest
Browse files Browse the repository at this point in the history
fixes: #327
  • Loading branch information
git-hyagi authored and lubosmj committed Jan 31, 2024
1 parent 3932076 commit 720412c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 157 deletions.
1 change: 1 addition & 0 deletions CHANGES/327.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rewritten test_sync.py using pytest.
8 changes: 4 additions & 4 deletions pulp_ostree/tests/functional/api/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
def test_filter_refs(
ostree_content_refs_api_client,
ostree_content_commits_api_client,
synced_repo_version,
sync_repo_version,
):
"""Check if refs can be filtered by their names and related commits' checksums."""
# check by name
_, _, repo = synced_repo_version
_, _, repo = sync_repo_version()
refs_stable = ostree_content_refs_api_client.list(
repository_version_added=repo.latest_version_href, name="stable"
).to_dict()
Expand All @@ -31,10 +31,10 @@ def test_filter_refs(
def test_filter_commits(
ostree_content_refs_api_client,
ostree_content_commits_api_client,
synced_repo_version,
sync_repo_version,
):
"""Check if commits can be filtered by their checksums."""
_, _, repo = synced_repo_version
_, _, repo = sync_repo_version()
refs_rawhide = ostree_content_refs_api_client.list(
repository_version_added=repo.latest_version_href, name="rawhide"
).to_dict()
Expand Down
20 changes: 10 additions & 10 deletions pulp_ostree/tests/functional/api/test_modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def test_add_ref_and_commit(
ostree_distributions_api_client,
ostree_repositories_api_client,
ostree_repository_factory,
synced_repo_version,
sync_repo_version,
tmp_path,
):
"""Copy one commit and one ref from the existing repository."""
repo_version1, remote, _ = synced_repo_version
repo_version1, remote, _ = sync_repo_version()
created_refs = ostree_content_refs_api_client.list(
repository_version_added=repo_version1.pulp_href
)
Expand Down Expand Up @@ -101,11 +101,11 @@ def test_add_refs_commits(
ostree_repositories_api_client,
ostree_repositories_versions_api_client,
ostree_repository_factory,
synced_repo_version,
sync_repo_version,
tmp_path,
):
"""Copy multiple refs and commits at once."""
repo_version1, remote, _ = synced_repo_version
repo_version1, remote, _ = sync_repo_version()
created_refs = ostree_content_refs_api_client.list(
repository_version_added=repo_version1.pulp_href
)
Expand Down Expand Up @@ -157,10 +157,10 @@ def test_copy_whole_repository(
normalize_content,
ostree_repositories_api_client,
ostree_repository_factory,
synced_repo_version,
sync_repo_version,
):
"""Initialize a new repository from the existing repository."""
repo_version1, _, repo1 = synced_repo_version
repo_version1, _, repo1 = sync_repo_version()
repo2 = ostree_repository_factory()
response = ostree_repositories_api_client.modify(
ostree_ostree_repository_href=repo2.pulp_href,
Expand All @@ -184,10 +184,10 @@ def test_remove_ref_and_commit(
ostree_content_refs_api_client,
ostree_repositories_api_client,
ostree_repositories_versions_api_client,
synced_repo_version,
sync_repo_version,
):
"""Remove one ref and one commit at once."""
repo_version1, _, repo1 = synced_repo_version
repo_version1, _, repo1 = sync_repo_version()
created_refs = ostree_content_refs_api_client.list(
repository_version_added=repo_version1.pulp_href
)
Expand Down Expand Up @@ -256,10 +256,10 @@ def test_add_remove_obj(
monitor_task,
ostree_content_objects_api_client,
ostree_repositories_api_client,
synced_repo_version,
sync_repo_version,
):
"""Try to modify an object (e.g., dirtree, dirmeta, ...) in the existing repository."""
repo_version1, _, repo1 = synced_repo_version
repo_version1, _, repo1 = sync_repo_version()
created_objs = ostree_content_objects_api_client.list(
repository_version_added=repo_version1.pulp_href
)
Expand Down
221 changes: 85 additions & 136 deletions pulp_ostree/tests/functional/api/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,154 +1,103 @@
import os
import shutil
import tempfile
import unittest

from pulp_smash.pulp3.bindings import delete_orphans, monitor_task
from pulp_smash.pulp3.utils import gen_repo, gen_distribution
import pytest

from pulp_ostree.tests.functional.utils import (
gen_ostree_client,
gen_ostree_remote,
init_local_repo_with_remote,
validate_repo_integrity,
)

from pulpcore.client.pulp_ostree import (
DistributionsOstreeApi,
OstreeOstreeDistribution,
RepositoriesOstreeApi,
RepositoriesOstreeVersionsApi,
RepositorySyncURL,
RemotesOstreeApi,
ContentRefsApi,
)


class BasicSyncTestCase(unittest.TestCase):
"""A test case that verifies the syncing scenario."""

@classmethod
def setUpClass(cls):
"""Initialize class-wide variables."""
client_api = gen_ostree_client()
cls.repositories_api = RepositoriesOstreeApi(client_api)
cls.versions_api = RepositoriesOstreeVersionsApi(client_api)
cls.remotes_api = RemotesOstreeApi(client_api)
cls.distributions_api = DistributionsOstreeApi(client_api)
cls.refs_api = ContentRefsApi(client_api)

cls.original_dir = os.getcwd()
cls.tmpdir = tempfile.TemporaryDirectory()
os.chdir(cls.tmpdir.name)

@classmethod
def tearDownClass(cls):
"""Clean orphaned content after finishing the tests."""
delete_orphans()
os.chdir(cls.original_dir)
cls.tmpdir.cleanup()

def setUp(self):
"""Clean orphaned content before each test."""
delete_orphans()

def test_on_demand_sync(self):
"""Test on_demand synchronization."""
self.sync("on_demand")

def test_immediate_sync(self):
"""Test immediate synchronization."""
self.sync("immediate")

def sync(self, policy):
"""Synchronize content from a remote repository and check validity of a Pulp repository."""
repo = self.repositories_api.create(gen_repo())
self.addCleanup(self.repositories_api.delete, repo.pulp_href)

body = gen_ostree_remote(depth=0, policy=policy)
remote = self.remotes_api.create(body)
self.addCleanup(self.remotes_api.delete, remote.pulp_href)
@pytest.fixture
def sync(
ostree_distribution_factory,
ostree_distributions_api_client,
sync_repo_version,
tmp_path,
):
"""Synchronize content from a remote repository and check validity of a Pulp repository."""

def _sync(
policy,
):
# 1. synchronize content from the remote
self.assertEqual(repo.latest_version_href, f"{repo.pulp_href}versions/0/")
repository_sync_data = RepositorySyncURL(remote=remote.pulp_href)
response = self.repositories_api.sync(repo.pulp_href, repository_sync_data)
repo_version = monitor_task(response.task).created_resources[0]

repository_version = self.versions_api.read(repo_version)
added_content = repository_version.content_summary.added
repo_version, remote, repo = sync_repo_version(policy=policy)
added_content = repo_version.content_summary.added

# 2. validate newly added content
self.assertEqual(added_content["ostree.config"]["count"], 1)
self.assertEqual(added_content["ostree.refs"]["count"], 2)
self.assertEqual(added_content["ostree.commit"]["count"], 2)
self.assertEqual(added_content["ostree.object"]["count"], 3)
self.assertEqual(added_content["ostree.summary"]["count"], 1)
assert added_content["ostree.config"]["count"] == 1
assert added_content["ostree.refs"]["count"] == 2
assert added_content["ostree.commit"]["count"] == 2
assert added_content["ostree.object"]["count"] == 3
assert added_content["ostree.summary"]["count"] == 1

# 3. synchronize from the same remote once again
previous_version_href = repo_version
repository_sync_data = RepositorySyncURL(remote=remote.pulp_href)
sync_response = self.repositories_api.sync(repo.pulp_href, repository_sync_data)
monitor_task(sync_response.task)
repo = self.repositories_api.read(repo.pulp_href)
previous_version_href = repo_version.pulp_href
_, remote, repo = sync_repo_version(remote=remote, repo=repo, policy=policy)

self.assertEqual(previous_version_href, repo.latest_version_href)
assert previous_version_href == repo.latest_version_href

# 4. publish the synced content
distribution_data = OstreeOstreeDistribution(**gen_distribution(repository=repo.pulp_href))
response = self.distributions_api.create(distribution_data)
distribution = monitor_task(response.task).created_resources[0]
self.addCleanup(self.distributions_api.delete, distribution)

ostree_repo_path = self.distributions_api.read(distribution).base_url
distribution = ostree_distribution_factory(repository=repo.pulp_href)
ostree_repo_path = ostree_distributions_api_client.read(distribution.pulp_href).base_url

# 5. initialize a local OSTree repository and pull the content from Pulp
remote_name = init_local_repo_with_remote(remote.name, ostree_repo_path)
self.addCleanup(shutil.rmtree, remote.name)
validate_repo_integrity(remote.name, f"{remote_name}:rawhide")
validate_repo_integrity(remote.name, f"{remote_name}:stable")

def test_filter_rawhide_ref_sync(self):
"""Synchronize content from a remote repository considering only a specific ref."""
repo = self.repositories_api.create(gen_repo())
self.addCleanup(self.repositories_api.delete, repo.pulp_href)

body = gen_ostree_remote(depth=0, include_refs=["rawhide"], exclude_refs=["stable"])
remote = self.remotes_api.create(body)
self.addCleanup(self.remotes_api.delete, remote.pulp_href)

self.assertEqual(remote.include_refs, ["rawhide"])
self.assertEqual(remote.exclude_refs, ["stable"])

repository_sync_data = RepositorySyncURL(remote=remote.pulp_href)
response = self.repositories_api.sync(repo.pulp_href, repository_sync_data)
repo_version = monitor_task(response.task).created_resources[0]

refs = self.refs_api.list(repository_version_added=repo_version).results
self.assertEqual(len(refs), 1)
self.assertEqual(refs[0].name, "rawhide")

def test_exclude_all_refs_sync(self):
"""Synchronize content from a remote repository when a user excludes all refs."""
repo = self.repositories_api.create(gen_repo())
self.addCleanup(self.repositories_api.delete, repo.pulp_href)

body = gen_ostree_remote(depth=0, exclude_refs=["*"])
remote = self.remotes_api.create(body)
self.addCleanup(self.remotes_api.delete, remote.pulp_href)

self.assertEqual(remote.include_refs, None)
self.assertEqual(remote.exclude_refs, ["*"])

repository_sync_data = RepositorySyncURL(remote=remote.pulp_href)
response = self.repositories_api.sync(repo.pulp_href, repository_sync_data)
repo_version = monitor_task(response.task).created_resources[0]

repository_version = self.versions_api.read(repo_version)
added_content = repository_version.content_summary.added

self.assertEqual(added_content["ostree.config"]["count"], 1)
self.assertEqual(added_content["ostree.summary"]["count"], 1)
self.assertRaises(KeyError, lambda: added_content["ostree.refs"])
self.assertRaises(KeyError, lambda: added_content["ostree.commit"])
self.assertRaises(KeyError, lambda: added_content["ostree.object"])
remote_name = init_local_repo_with_remote(tmp_path / remote.name, ostree_repo_path)
validate_repo_integrity(tmp_path / remote.name, f"{remote_name}:rawhide")
validate_repo_integrity(tmp_path / remote.name, f"{remote_name}:stable")

return _sync


@pytest.mark.parallel
def test_on_demand_sync(sync):
"""Test on_demand synchronization."""
sync("on_demand")


@pytest.mark.parallel
def test_immediate_sync(sync):
"""Test immediate synchronization."""
sync("immediate")


@pytest.mark.parallel
def test_filter_rawhide_ref_sync(
ostree_content_refs_api_client,
ostree_remote_factory,
ostree_repository_factory,
sync_repo_version,
):
"""Synchronize content from a remote repository considering only a specific ref."""
repo = ostree_repository_factory()
remote = ostree_remote_factory(depth=0, include_refs=["rawhide"], exclude_refs=["stable"])
assert remote.include_refs == ["rawhide"]
assert remote.exclude_refs == ["stable"]

repo_version, _, _ = sync_repo_version(repo=repo, remote=remote)
refs = ostree_content_refs_api_client.list(
repository_version_added=repo_version.pulp_href
).results
assert len(refs) == 1
assert refs[0].name == "rawhide"


@pytest.mark.parallel
def test_exclude_all_refs_sync(ostree_remote_factory, ostree_repository_factory, sync_repo_version):
"""Synchronize content from a remote repository when a user excludes all refs."""
repo = ostree_repository_factory()
remote = ostree_remote_factory(depth=0, exclude_refs=["*"])

assert remote.include_refs is None
assert remote.exclude_refs == ["*"]

repository_version, _, _ = sync_repo_version(repo=repo, remote=remote)
added_content = repository_version.content_summary.added

assert added_content["ostree.config"]["count"] == 1
assert added_content["ostree.summary"]["count"] == 1

with pytest.raises(KeyError):
added_content["ostree.refs"]
with pytest.raises(KeyError):
added_content["ostree.commit"]
with pytest.raises(KeyError):
added_content["ostree.object"]
24 changes: 17 additions & 7 deletions pulp_ostree/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _ostree_distribution_factory(**body):


@pytest.fixture(scope="class")
def synced_repo_version(
def sync_repo_version(
ostree_repositories_api_client,
ostree_repositories_versions_api_client,
ostree_repository_factory,
Expand All @@ -136,9 +136,19 @@ def synced_repo_version(
waits until the sync task completes and returns the
created repository version object.
"""
repo = ostree_repository_factory()
remote = ostree_remote_factory(url=OSTREE_FIXTURE_URL, policy="immediate")
result = ostree_repositories_api_client.sync(repo.pulp_href, {"remote": remote.pulp_href})
repo_version_href = monitor_task(result.task).created_resources[0]
repo = ostree_repositories_api_client.read(repo.pulp_href)
return ostree_repositories_versions_api_client.read(repo_version_href), remote, repo

def _sync_repo_version(repo=None, remote=None, policy="immediate"):
if repo is None:
repo = ostree_repository_factory()
if remote is None:
remote = ostree_remote_factory(url=OSTREE_FIXTURE_URL, policy=policy, depth=0)
result = ostree_repositories_api_client.sync(repo.pulp_href, {"remote": remote.pulp_href})
monitor_task_result = monitor_task(result.task)
repo = ostree_repositories_api_client.read(repo.pulp_href)
if len(monitor_task_result.created_resources) > 0:
repo_version_href = monitor_task_result.created_resources[0]
else:
repo_version_href = repo.latest_version_href
return ostree_repositories_versions_api_client.read(repo_version_href), remote, repo

return _sync_repo_version

0 comments on commit 720412c

Please sign in to comment.