|
1 | 1 | import hashlib |
2 | 2 | import uuid |
| 3 | +from datetime import timedelta |
| 4 | +from unittest import mock |
3 | 5 |
|
4 | 6 | import pytest |
5 | 7 |
|
|
9 | 11 | DistributedPublication, |
10 | 12 | PublishedArtifact, |
11 | 13 | ) |
| 14 | +from pulpcore.app.util import cache_key |
12 | 15 |
|
13 | 16 | from pulp_file.app.models import ( |
14 | 17 | FileContent, |
@@ -59,6 +62,34 @@ def update_dist(dist, repo=UNSET, repover=UNSET, pub=UNSET): |
59 | 62 | dist.save() |
60 | 63 |
|
61 | 64 |
|
| 65 | +def publish(repo_version, pass_through=True): |
| 66 | + """ |
| 67 | + Create and complete a publication through the Publication context manager. |
| 68 | +
|
| 69 | + `CreatedResource` is mocked out because it requires a current Task, which isn't set up in |
| 70 | + unit tests. This still runs the `__exit__` finalization that invalidates caches and records |
| 71 | + distributed publications. |
| 72 | + """ |
| 73 | + with mock.patch("pulpcore.app.models.publication.CreatedResource"): |
| 74 | + with FilePublication.create(repo_version, pass_through=pass_through) as pub: |
| 75 | + pass |
| 76 | + return pub |
| 77 | + |
| 78 | + |
| 79 | +def invalidated_base_paths(mock_cache): |
| 80 | + """Collect the set of base_paths passed to a mocked ``Cache().delete``.""" |
| 81 | + paths = set() |
| 82 | + for call in mock_cache.return_value.delete.call_args_list: |
| 83 | + base_key = call.kwargs.get("base_key") |
| 84 | + if base_key is None and call.args: |
| 85 | + base_key = call.args[0] |
| 86 | + if isinstance(base_key, str): |
| 87 | + paths.add(base_key) |
| 88 | + elif base_key is not None: |
| 89 | + paths.update(base_key) |
| 90 | + return paths |
| 91 | + |
| 92 | + |
62 | 93 | def create_version(repo, add=None, remove=None): |
63 | 94 | """ |
64 | 95 | Create a RepositoryVersion adding and/or removing content by path. |
@@ -137,6 +168,25 @@ def test_unaffected_when_older_repository_version_deleted(self, db): |
137 | 168 | v1.delete() |
138 | 169 | assert DistributedPublication.objects.filter(distribution=dist).count() == 1 |
139 | 170 |
|
| 171 | + def test_created_when_new_publication_for_distributed_repository(self, db): |
| 172 | + # A distribution serving a repository directly indirectly distributes the latest |
| 173 | + # publication. Creating a new publication should record it as distributed. |
| 174 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 175 | + version = create_version(repo, add=["some-file.txt"]) |
| 176 | + dist = dist_factory(repo=repo) |
| 177 | + pub = publish(version) |
| 178 | + assert DistributedPublication.objects.filter(distribution=dist, publication=pub).exists() |
| 179 | + |
| 180 | + def test_created_when_new_publication_for_distributed_repository_version(self, db): |
| 181 | + # A distribution serving a repository_version (with SERVE_FROM_PUBLICATION) indirectly |
| 182 | + # distributes the latest publication of that version. Creating a new publication should |
| 183 | + # record it as distributed, just like the repository case above. |
| 184 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 185 | + version = create_version(repo, add=["some-file.txt"]) |
| 186 | + dist = dist_factory(repover=version) |
| 187 | + pub = publish(version) |
| 188 | + assert DistributedPublication.objects.filter(distribution=dist, publication=pub).exists() |
| 189 | + |
140 | 190 |
|
141 | 191 | @pytest.mark.django_db |
142 | 192 | class TestGetFallbackCa: |
@@ -210,3 +260,89 @@ def version_without_content(self, version_with_content): |
210 | 260 | @pytest.fixture |
211 | 261 | def expected_ca(self, version_with_content): |
212 | 262 | return ContentArtifact.objects.get(relative_path=self.content_path) |
| 263 | + |
| 264 | + |
| 265 | +@pytest.mark.django_db |
| 266 | +class TestCacheInvalidationOnPublicationCreate: |
| 267 | + """ |
| 268 | + Creating a new publication changes the content served by every distribution that |
| 269 | + indirectly distributes that publication's repository/repository_version. The cache |
| 270 | + must be invalidated for all of them, not just the ones with a direct ``repository`` FK. |
| 271 | + """ |
| 272 | + |
| 273 | + def test_invalidates_repository_distribution(self, settings): |
| 274 | + settings.CACHE_ENABLED = True |
| 275 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 276 | + version = create_version(repo, add=["some-file.txt"]) |
| 277 | + dist = dist_factory(repo=repo) |
| 278 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 279 | + publish(version) |
| 280 | + assert cache_key(dist.base_path) in invalidated_base_paths(mock_cache) |
| 281 | + |
| 282 | + def test_invalidates_repository_version_distribution(self, settings): |
| 283 | + settings.CACHE_ENABLED = True |
| 284 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 285 | + version = create_version(repo, add=["some-file.txt"]) |
| 286 | + dist = dist_factory(repover=version) |
| 287 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 288 | + publish(version) |
| 289 | + assert cache_key(dist.base_path) in invalidated_base_paths(mock_cache) |
| 290 | + |
| 291 | + |
| 292 | +@pytest.mark.django_db |
| 293 | +class TestCacheInvalidationOnPublicationDelete: |
| 294 | + """ |
| 295 | + Deleting the publication currently served by a distribution changes what that distribution |
| 296 | + serves, so its cache must be invalidated -- including distributions that serve the |
| 297 | + publication indirectly through their repository_version. |
| 298 | + """ |
| 299 | + |
| 300 | + def test_invalidates_repository_distribution(self, settings): |
| 301 | + settings.CACHE_ENABLED = True |
| 302 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 303 | + version = create_version(repo, add=["some-file.txt"]) |
| 304 | + pub = pub_factory(version, pass_through=True) |
| 305 | + dist = dist_factory(repo=repo) |
| 306 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 307 | + pub.delete() |
| 308 | + assert cache_key(dist.base_path) in invalidated_base_paths(mock_cache) |
| 309 | + |
| 310 | + def test_invalidates_repository_version_distribution(self, settings): |
| 311 | + settings.CACHE_ENABLED = True |
| 312 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 313 | + version = create_version(repo, add=["some-file.txt"]) |
| 314 | + pub = pub_factory(version, pass_through=True) |
| 315 | + dist = dist_factory(repover=version) |
| 316 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 317 | + pub.delete() |
| 318 | + assert cache_key(dist.base_path) in invalidated_base_paths(mock_cache) |
| 319 | + |
| 320 | + def test_does_not_invalidate_repository_version_distribution_for_other_version(self, settings): |
| 321 | + # Deleting a publication of a different version must not invalidate a distribution that |
| 322 | + # serves an unrelated repository_version. |
| 323 | + settings.CACHE_ENABLED = True |
| 324 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 325 | + version1 = create_version(repo, add=["v1.txt"]) |
| 326 | + version2 = create_version(repo, add=["v2.txt"]) |
| 327 | + pub2 = pub_factory(version2, pass_through=True) |
| 328 | + dist = dist_factory(repover=version1) |
| 329 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 330 | + pub2.delete() |
| 331 | + assert cache_key(dist.base_path) not in invalidated_base_paths(mock_cache) |
| 332 | + |
| 333 | + def test_does_not_invalidate_when_deleting_superseded_publication(self, settings): |
| 334 | + # Deleting an older (non-latest) publication of the served version must not invalidate the |
| 335 | + # repository_version distribution, which still serves the newer publication. |
| 336 | + settings.CACHE_ENABLED = True |
| 337 | + repo = FileRepository.objects.create(name=f"repo-{uuid.uuid4().hex[:8]}") |
| 338 | + version = create_version(repo, add=["some-file.txt"]) |
| 339 | + old_pub = pub_factory(version, pass_through=True) |
| 340 | + new_pub = pub_factory(version, pass_through=True) |
| 341 | + # Force deterministic ordering (pulp_created is auto_now_add). |
| 342 | + FilePublication.objects.filter(pk=old_pub.pk).update( |
| 343 | + pulp_created=new_pub.pulp_created - timedelta(seconds=1) |
| 344 | + ) |
| 345 | + dist = dist_factory(repover=version) |
| 346 | + with mock.patch("pulpcore.app.models.publication.Cache") as mock_cache: |
| 347 | + old_pub.delete() |
| 348 | + assert cache_key(dist.base_path) not in invalidated_base_paths(mock_cache) |
0 commit comments