Skip to content

Commit 0a68f8d

Browse files
committed
Add unittests for PluginRequirement
1 parent b6cefa4 commit 0a68f8d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

pulp-glue/tests/conftest.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import json
12
import os
23
import typing as t
34

45
import pytest
56

67
from pulp_glue.common.context import PulpContext
7-
from pulp_glue.common.openapi import BasicAuthProvider
8+
from pulp_glue.common.openapi import BasicAuthProvider, OpenAPI
9+
10+
FAKE_OPENAPI_SPEC = json.dumps(
11+
{
12+
"openapi": "3.0.3",
13+
"info": {"x-pulp-app-versions": {"core": "3.75.0"}},
14+
"paths": {},
15+
}
16+
)
817

918

1019
@pytest.fixture
@@ -22,6 +31,26 @@ def pulp_ctx(
2231
return PulpContext.from_config(settings)
2332

2433

34+
@pytest.fixture
35+
def mock_pulp_ctx(
36+
request: pytest.FixtureRequest,
37+
monkeypatch: pytest.MonkeyPatch,
38+
) -> PulpContext:
39+
monkeypatch.setattr(
40+
OpenAPI, "load_api", lambda self, refresh_cache: self._parse_api(FAKE_OPENAPI_SPEC)
41+
)
42+
monkeypatch.setattr(
43+
OpenAPI,
44+
"_send_request",
45+
lambda *args, **kwags: pytest.fail("Invalid use of pulp_ctx fixture in a non live test."),
46+
)
47+
48+
verbose = request.config.getoption("verbose")
49+
settings: t.Dict[str, t.Any] = {"base_url": "nowhere"}
50+
settings["debug_callback"] = lambda i, s: i <= verbose and print(s)
51+
return PulpContext.from_config(settings)
52+
53+
2554
@pytest.fixture
2655
def fake_pulp_ctx(
2756
request: pytest.FixtureRequest, pulp_cli_settings: t.Dict[str, t.Dict[str, t.Any]]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
from pulp_glue.common.context import PluginRequirement, PulpContext
4+
from pulp_glue.common.exceptions import PulpException
5+
6+
pytestmark = pytest.mark.glue
7+
8+
9+
def test_has_plugin(mock_pulp_ctx: PulpContext) -> None:
10+
assert mock_pulp_ctx.has_plugin(PluginRequirement("core"))
11+
assert mock_pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.50"))
12+
assert not mock_pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.50,<3.60"))
13+
assert not mock_pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.50,<3.60"))
14+
assert mock_pulp_ctx.has_plugin(
15+
PluginRequirement("core", specifier=">=3.50,<3.60", inverted=True)
16+
)
17+
18+
assert not mock_pulp_ctx.has_plugin(PluginRequirement("elephant"))
19+
assert not mock_pulp_ctx.has_plugin(PluginRequirement("elephant", specifier=">=0.0.0"))
20+
assert mock_pulp_ctx.has_plugin(
21+
PluginRequirement("elephant", specifier=">=0.0.0", inverted=True)
22+
)
23+
24+
25+
def test_needs_plugin(mock_pulp_ctx: PulpContext) -> None:
26+
mock_pulp_ctx.needs_plugin(PluginRequirement("core"))
27+
mock_pulp_ctx.needs_plugin(PluginRequirement("elephant", feature="zoo"))
28+
with pytest.raises(PulpException, match=r"elephant.*zoo"):
29+
mock_pulp_ctx.has_plugin(PluginRequirement("core"))

0 commit comments

Comments
 (0)