mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 13:57:43 +00:00
Merge branch 'feature/updater' into feature/store-i18n
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # plugins/lookup/deployment.py
This commit is contained in:
commit
2c04cce617
10 files changed
+245
-32
No files matched your search
@@ -29,6 +29,7 @@ class TestDeploymentLookup(unittest.TestCase):
|
||||
"inventory",
|
||||
"deployed",
|
||||
"runtime",
|
||||
"ephemeral",
|
||||
"all",
|
||||
},
|
||||
)
|
||||
@@ -225,6 +226,7 @@ class TestDeploymentLookup(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(result[0]["deployed"], ["only-this"])
|
||||
self.assertEqual(result[0]["runtime"], runtime)
|
||||
self.assertTrue(result[0]["ephemeral"])
|
||||
|
||||
@patch("plugins.lookup.deployment.list_invokable_app_ids")
|
||||
def test_deployed_uses_groups_in_persistent_runtime(self, mock_list):
|
||||
@@ -243,6 +245,7 @@ class TestDeploymentLookup(unittest.TestCase):
|
||||
result[0]["deployed"],
|
||||
["partial", "still-running", "also-running"],
|
||||
)
|
||||
self.assertFalse(result[0]["ephemeral"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
"""Unit tests for ``plugins/lookup/mcp_servers.py``.
|
||||
|
||||
Only the pure builder and the credential resolver are exercised; the lookup
|
||||
wrapper forwards ``roles_with_service``, ``applications`` and ``users`` into
|
||||
them.
|
||||
The pure builder, the credential resolver and the deploy closure are exercised;
|
||||
the lookup wrapper forwards ``roles_with_service``, ``applications`` and ``users``
|
||||
into them.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
import unittest
|
||||
from typing import ClassVar
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -89,6 +90,34 @@ class TestSelectDeployed(unittest.TestCase):
|
||||
self.assertEqual([], plugin_module.select_deployed(None, {"web-app-hermes"}))
|
||||
|
||||
|
||||
class TestDeployClosure(unittest.TestCase):
|
||||
def closure(self, deployment):
|
||||
def get(name, **kwargs):
|
||||
plugin = MagicMock()
|
||||
if name == "deployment":
|
||||
plugin.run.return_value = [deployment]
|
||||
else:
|
||||
plugin.run.side_effect = lambda terms, variables=None: [
|
||||
{*terms[0], "svc-db-postgres"}
|
||||
]
|
||||
return plugin
|
||||
|
||||
with patch.object(plugin_module.lookup_loader, "get", side_effect=get):
|
||||
return plugin_module.LookupModule()._deploy_closure(None, {})
|
||||
|
||||
def test_a_partial_run_on_a_persistent_host_narrows_nothing(self):
|
||||
self.assertEqual(
|
||||
set(),
|
||||
self.closure({"ephemeral": False, "whitelist": ["web-opt-rdr-domains"]}),
|
||||
)
|
||||
|
||||
def test_an_ephemeral_run_narrows_to_the_whitelist_closure(self):
|
||||
self.assertEqual(
|
||||
{"web-app-hermes", "svc-db-postgres"},
|
||||
self.closure({"ephemeral": True, "whitelist": ["web-app-hermes"]}),
|
||||
)
|
||||
|
||||
|
||||
class TestBuildMcpDiscovery(unittest.TestCase):
|
||||
def test_no_servers_yields_nothing(self):
|
||||
for servers in (None, []):
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
PROJECT_ROOT: Path = Path(__file__).resolve().parents[6]
|
||||
|
||||
__all__ = ["PROJECT_ROOT"]
|
||||
@@ -67,7 +67,9 @@ class FakeApi:
|
||||
permissive=False,
|
||||
refuse_on_create=False,
|
||||
create_error=None,
|
||||
flow_delete_status=200,
|
||||
):
|
||||
self.flow_delete_status = flow_delete_status
|
||||
self.permissive = permissive
|
||||
self.refuse_on_create = refuse_on_create
|
||||
self.create_error = create_error
|
||||
@@ -104,6 +106,8 @@ class FakeApi:
|
||||
self.flows_updated.append(payload["name"])
|
||||
return 200, payload
|
||||
if path.startswith("/api/v1/chatflows/") and method == "DELETE":
|
||||
if self.flow_delete_status != 200:
|
||||
return self.flow_delete_status, '{"message":"Forbidden"}'
|
||||
dead = path.rsplit("/", 1)[-1]
|
||||
self.flows_deleted.append(dead)
|
||||
self.flows = [f for f in self.flows if f["id"] != dead]
|
||||
@@ -352,6 +356,28 @@ class TestReconcileMcpServers(unittest.TestCase):
|
||||
module.main()
|
||||
self.assertEqual(["old"], api.flows_deleted)
|
||||
|
||||
def test_a_refused_delete_exits_with_the_no_retry_code(self) -> None:
|
||||
module = load_script([SUPPORTED_SERVER])
|
||||
api = FakeApi(
|
||||
flows=[{"id": "old", "name": "infinito:fixture:web-app-prometheus"}],
|
||||
flow_delete_status=403,
|
||||
)
|
||||
with patch.object(module, "call", api), self.assertRaises(SystemExit) as exit_:
|
||||
module.main()
|
||||
self.assertEqual(module.REFUSED_RC, exit_.exception.code)
|
||||
self.assertIn("FAILED deleting stale", str(exit_.exception))
|
||||
|
||||
def test_a_server_error_keeps_the_retryable_exit(self) -> None:
|
||||
module = load_script([SUPPORTED_SERVER])
|
||||
api = FakeApi(
|
||||
flows=[{"id": "old", "name": "infinito:fixture:web-app-prometheus"}],
|
||||
flow_delete_status=500,
|
||||
)
|
||||
with patch.object(module, "call", api), self.assertRaises(SystemExit) as exit_:
|
||||
module.main()
|
||||
self.assertNotEqual(module.REFUSED_RC, exit_.exception.code)
|
||||
self.assertIn("FAILED deleting stale", str(exit_.exception))
|
||||
|
||||
def test_loopback_and_metadata_are_probed_and_refused(self) -> None:
|
||||
module = load_script([SUPPORTED_SERVER])
|
||||
api = FakeApi()
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""Unit tests for ``roles/web-app-flowise/files/python/mint_mcp_api_key.py``."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from . import PROJECT_ROOT
|
||||
|
||||
SCRIPT_PATH = PROJECT_ROOT / "roles/web-app-flowise/files/python/mint_mcp_api_key.py"
|
||||
KEY_NAME = "infinito:mcp-registry"
|
||||
|
||||
|
||||
def load_script() -> object:
|
||||
spec = importlib.util.spec_from_file_location("mint_mcp_api_key", SCRIPT_PATH)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
with patch.dict(
|
||||
"os.environ",
|
||||
{"FLOWISE_BASE": "http://localhost:3000", "FLOWISE_API_KEY_NAME": KEY_NAME},
|
||||
):
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
class FakeApi:
|
||||
"""Stand-in for the Flowise ``/api/v1/apikey`` routes."""
|
||||
|
||||
def __init__(self, keys, delete_status=200):
|
||||
self.keys = list(keys)
|
||||
self.delete_status = delete_status
|
||||
self.calls = []
|
||||
|
||||
def __call__(self, path, method="GET", payload=None):
|
||||
self.calls.append((method, path))
|
||||
if method == "GET":
|
||||
return 200, list(self.keys)
|
||||
if method == "DELETE":
|
||||
if self.delete_status == 200:
|
||||
self.keys = [
|
||||
k for k in self.keys if path != f"/api/v1/apikey/{k['id']}"
|
||||
]
|
||||
return self.delete_status, "denied" if self.delete_status != 200 else {}
|
||||
created = {
|
||||
"id": "new",
|
||||
"keyName": payload["keyName"],
|
||||
"apiKey": "sk-new",
|
||||
"permissions": payload["permissions"],
|
||||
}
|
||||
self.keys.append(created)
|
||||
return 200, list(self.keys)
|
||||
|
||||
|
||||
class TestApiKey(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.module = load_script()
|
||||
|
||||
def key(self, permissions):
|
||||
return {
|
||||
"id": "old",
|
||||
"keyName": KEY_NAME,
|
||||
"apiKey": "sk-old",
|
||||
"permissions": permissions,
|
||||
}
|
||||
|
||||
def test_a_key_with_the_wanted_permissions_is_reused_in_any_order(self):
|
||||
api = FakeApi([self.key(list(reversed(self.module.KEY_PERMISSIONS)))])
|
||||
with patch.object(self.module, "call", api):
|
||||
self.assertEqual(self.module.api_key(), ("sk-old", False))
|
||||
self.assertEqual(api.calls, [("GET", "/api/v1/apikey")])
|
||||
|
||||
def test_a_key_missing_a_permission_is_replaced(self):
|
||||
stale = [p for p in self.module.KEY_PERMISSIONS if p != "chatflows:delete"]
|
||||
api = FakeApi([self.key(stale)])
|
||||
with patch.object(self.module, "call", api):
|
||||
self.assertEqual(self.module.api_key(), ("sk-new", True))
|
||||
self.assertEqual(
|
||||
api.calls,
|
||||
[
|
||||
("GET", "/api/v1/apikey"),
|
||||
("DELETE", "/api/v1/apikey/old"),
|
||||
("POST", "/api/v1/apikey"),
|
||||
],
|
||||
)
|
||||
self.assertEqual(
|
||||
set(api.keys[0]["permissions"]), set(self.module.KEY_PERMISSIONS)
|
||||
)
|
||||
|
||||
def test_a_missing_key_is_minted_without_a_delete(self):
|
||||
api = FakeApi([])
|
||||
with patch.object(self.module, "call", api):
|
||||
self.assertEqual(self.module.api_key(), ("sk-new", True))
|
||||
self.assertEqual(
|
||||
api.calls, [("GET", "/api/v1/apikey"), ("POST", "/api/v1/apikey")]
|
||||
)
|
||||
|
||||
def test_a_refused_delete_stops_the_run(self):
|
||||
api = FakeApi([self.key([])], delete_status=403)
|
||||
with patch.object(self.module, "call", api), self.assertRaises(SystemExit):
|
||||
self.module.api_key()
|
||||
|
||||
def test_the_fixture_prune_is_allowed_to_delete_chatflows(self):
|
||||
self.assertIn("chatflows:delete", self.module.KEY_PERMISSIONS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user