|
| 1 | +import json |
| 2 | +import typing as t |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pulp_glue.common import context as context_module |
| 7 | +from pulp_glue.common.context import PulpContext, PulpDistributionContext |
| 8 | +from pulp_glue.common.exceptions import PulpException |
| 9 | +from pulp_glue.common.openapi import OpenAPI |
| 10 | +from pulp_glue.file.context import PulpFileDistributionContext |
| 11 | +from pulp_glue.rpm.context import PulpRpmDistributionContext |
| 12 | + |
| 13 | +pytestmark = pytest.mark.glue |
| 14 | + |
| 15 | +REPO_HREF = "/pulp/api/v3/repositories/rpm/rpm/01234567-0123-0123-0123-0123456789ab/" |
| 16 | +REPO_VERSION_HREF = REPO_HREF + "versions/5/" |
| 17 | +PUBLICATION_HREF = "/pulp/api/v3/publications/rpm/rpm/01234567-0123-0123-0123-0123456789ab/" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def modern_pulp_ctx(monkeypatch: pytest.MonkeyPatch) -> PulpContext: |
| 22 | + spec = json.dumps( |
| 23 | + { |
| 24 | + "openapi": "3.0.3", |
| 25 | + "info": { |
| 26 | + "title": "test", |
| 27 | + "version": "0.0.0", |
| 28 | + "x-pulp-app-versions": {"core": "3.106.0", "rpm": "3.30.0", "file": "3.75.0"}, |
| 29 | + }, |
| 30 | + "paths": {}, |
| 31 | + } |
| 32 | + ) |
| 33 | + monkeypatch.setattr(context_module, "_patch_api_hook", lambda spec: spec) |
| 34 | + monkeypatch.setattr(OpenAPI, "load_api", lambda self, refresh_cache: self._parse_api(spec)) |
| 35 | + monkeypatch.setattr( |
| 36 | + OpenAPI, |
| 37 | + "_send_request", |
| 38 | + lambda *args, **kwargs: pytest.fail("No API calls allowed in unit tests."), |
| 39 | + ) |
| 40 | + settings: dict[str, t.Any] = {"base_url": "nowhere"} |
| 41 | + return PulpContext.from_config(settings) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + "ctx_cls", |
| 46 | + [PulpRpmDistributionContext, PulpFileDistributionContext], |
| 47 | + ids=["rpm", "file"], |
| 48 | +) |
| 49 | +class TestDistributionPreprocess: |
| 50 | + def test_version_with_repository( |
| 51 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 52 | + ) -> None: |
| 53 | + ctx = ctx_cls(modern_pulp_ctx) |
| 54 | + body = ctx.preprocess_entity({"version": 5, "repository": REPO_HREF}, partial=False) |
| 55 | + assert body["repository_version"] == REPO_VERSION_HREF |
| 56 | + assert body["repository"] is None |
| 57 | + assert body["publication"] is None |
| 58 | + assert "version" not in body |
| 59 | + |
| 60 | + def test_repository_without_version( |
| 61 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 62 | + ) -> None: |
| 63 | + ctx = ctx_cls(modern_pulp_ctx) |
| 64 | + body = ctx.preprocess_entity({"repository": REPO_HREF}, partial=False) |
| 65 | + assert body["repository"] == REPO_HREF |
| 66 | + assert body["repository_version"] is None |
| 67 | + assert body["publication"] is None |
| 68 | + |
| 69 | + def test_publication_without_repository( |
| 70 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 71 | + ) -> None: |
| 72 | + ctx = ctx_cls(modern_pulp_ctx) |
| 73 | + body = ctx.preprocess_entity({"publication": PUBLICATION_HREF}, partial=False) |
| 74 | + assert body["publication"] == PUBLICATION_HREF |
| 75 | + assert body["repository"] is None |
| 76 | + assert body["repository_version"] is None |
| 77 | + |
| 78 | + def test_version_without_repository_raises( |
| 79 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 80 | + ) -> None: |
| 81 | + ctx = ctx_cls(modern_pulp_ctx) |
| 82 | + with pytest.raises(PulpException, match="--repository"): |
| 83 | + ctx.preprocess_entity({"version": 5}, partial=False) |
| 84 | + |
| 85 | + def test_version_without_repository_partial_raises( |
| 86 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 87 | + ) -> None: |
| 88 | + ctx = ctx_cls(modern_pulp_ctx) |
| 89 | + ctx._entity = {"repository": None, "repository_version": None} |
| 90 | + with pytest.raises(PulpException, match="--repository"): |
| 91 | + ctx.preprocess_entity({"version": 5}, partial=True) |
| 92 | + |
| 93 | + def test_partial_version_infers_repository( |
| 94 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 95 | + ) -> None: |
| 96 | + ctx = ctx_cls(modern_pulp_ctx) |
| 97 | + ctx._entity = {"repository": REPO_HREF, "repository_version": None} |
| 98 | + body = ctx.preprocess_entity({"version": 3}, partial=True) |
| 99 | + assert body["repository_version"] == REPO_HREF + "versions/3/" |
| 100 | + assert body["repository"] is None |
| 101 | + assert body["publication"] is None |
| 102 | + |
| 103 | + def test_unrelated_field_no_nullification( |
| 104 | + self, modern_pulp_ctx: PulpContext, ctx_cls: type[PulpDistributionContext] |
| 105 | + ) -> None: |
| 106 | + ctx = ctx_cls(modern_pulp_ctx) |
| 107 | + body = ctx.preprocess_entity({"base_path": "/new/path"}, partial=True) |
| 108 | + assert body.get("base_path") == "/new/path" |
| 109 | + assert "repository" not in body |
| 110 | + assert "repository_version" not in body |
| 111 | + assert "publication" not in body |
0 commit comments