mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 22:37:29 +00:00
fix(codescan): clear the mechanical CodeQL notes and lint them away (WIP)
Work-in-progress commit on the collective code-scanning sidetree, so main can be merged in; the single quality-high gate runs on the integration branch before the pull request. Fixes the CodeQL notes py/import-and-import-from, py/unused-import, py/unused-global-variable, py/empty-except, py/mixed-tuple-returns and py/unused-local-variable across 194 files, and adds one lint per rule under tests/lint/repository/python/ so none of them can come back: test_import_and_import_from, test_unused_imports_hidden_from_ruff, test_unused_global_variables, test_empty_except, test_mixed_tuple_returns, test_unused_local_variables, plus test_modules_import. Accepted security finding: MEDUSA-MP-SCAN-2056 (low) on utils/i18n/client.py:21, an import line matched by a keyword heuristic; operator-confirmed false positive. 🎯 Fix: 85 % 💥 Break: 8 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 78 🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3fdfce419e
commit
326f063bd3
194 files changed
+1663
-590
No files matched your search
@@ -2,7 +2,6 @@ import unittest
|
||||
|
||||
import reserved_users
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from reserved_users import non_reserved_users, reserved_usernames
|
||||
|
||||
|
||||
class TestReservedUsersFilters(unittest.TestCase):
|
||||
@@ -41,10 +40,10 @@ class TestReservedUsersFilters(unittest.TestCase):
|
||||
|
||||
def test_reserved_usernames_requires_dict(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
reserved_usernames(["not", "a", "dict"])
|
||||
reserved_users.reserved_usernames(["not", "a", "dict"])
|
||||
|
||||
def test_reserved_usernames_returns_only_non_identity(self):
|
||||
result = reserved_usernames(self.users)
|
||||
result = reserved_users.reserved_usernames(self.users)
|
||||
self.assertIn("admin", result)
|
||||
self.assertIn("backup", result)
|
||||
self.assertIn("service\\.user", result)
|
||||
@@ -52,29 +51,31 @@ class TestReservedUsersFilters(unittest.TestCase):
|
||||
self.assertNotIn("kevin", result)
|
||||
|
||||
def test_missing_accounts_field_counts_as_reserved(self):
|
||||
self.assertIn("noaccounts", reserved_usernames(self.users))
|
||||
self.assertNotIn("no_accounts_field", non_reserved_users(self.users))
|
||||
self.assertIn("noaccounts", reserved_users.reserved_usernames(self.users))
|
||||
self.assertNotIn(
|
||||
"no_accounts_field", reserved_users.non_reserved_users(self.users)
|
||||
)
|
||||
|
||||
def test_reserved_usernames_ignores_entries_without_username(self):
|
||||
result = reserved_usernames(self.users)
|
||||
result = reserved_users.reserved_usernames(self.users)
|
||||
for item in result:
|
||||
self.assertNotIn("no_username_field", item)
|
||||
|
||||
def test_reserved_usernames_escapes_special_chars(self):
|
||||
result = reserved_usernames(self.users)
|
||||
result = reserved_users.reserved_usernames(self.users)
|
||||
self.assertIn("service\\.user", result)
|
||||
self.assertNotIn("service.user", result)
|
||||
|
||||
def test_reserved_usernames_empty_dict(self):
|
||||
result = reserved_usernames({})
|
||||
result = reserved_users.reserved_usernames({})
|
||||
self.assertEqual(result, [])
|
||||
|
||||
def test_non_reserved_users_requires_dict(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
non_reserved_users("not-a-dict")
|
||||
reserved_users.non_reserved_users("not-a-dict")
|
||||
|
||||
def test_non_reserved_users_returns_only_identity_provisioned(self):
|
||||
result = non_reserved_users(self.users)
|
||||
result = reserved_users.non_reserved_users(self.users)
|
||||
self.assertIsInstance(result, dict)
|
||||
|
||||
self.assertIn("kevin", result)
|
||||
@@ -83,11 +84,11 @@ class TestReservedUsersFilters(unittest.TestCase):
|
||||
self.assertNotIn("service.user", result)
|
||||
|
||||
def test_non_reserved_users_ignores_non_dict_entries(self):
|
||||
result = non_reserved_users(self.users)
|
||||
result = reserved_users.non_reserved_users(self.users)
|
||||
self.assertNotIn("not_a_dict", result)
|
||||
|
||||
def test_non_reserved_users_empty_dict(self):
|
||||
result = non_reserved_users({})
|
||||
result = reserved_users.non_reserved_users({})
|
||||
self.assertEqual(result, {})
|
||||
|
||||
def test_filtermodule_registers_filters(self):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -51,12 +50,12 @@ class TestAddonEnvFlagsLookup(unittest.TestCase):
|
||||
"no_required": {"enabled": True},
|
||||
}
|
||||
self._patchers = [
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags.lookup_loader",
|
||||
_applications_loader({}),
|
||||
),
|
||||
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
patch(
|
||||
mock.patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags._render_with_templar",
|
||||
side_effect=lambda v, **k: v,
|
||||
),
|
||||
@@ -108,12 +107,12 @@ class TestNoAddons(unittest.TestCase):
|
||||
lookup = LookupModule()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags.lookup_loader",
|
||||
_applications_loader({}),
|
||||
),
|
||||
patch("plugins.lookup.addon_env_flags.get", return_value={}),
|
||||
patch(
|
||||
mock.patch("plugins.lookup.addon_env_flags.get", return_value={}),
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags._render_with_templar",
|
||||
side_effect=lambda v, **k: v,
|
||||
),
|
||||
@@ -154,12 +153,12 @@ class TestBridgeDeploymentGating(unittest.TestCase):
|
||||
},
|
||||
}
|
||||
self._patchers = [
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags.lookup_loader",
|
||||
_applications_loader({}),
|
||||
),
|
||||
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
patch(
|
||||
mock.patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags._render_with_templar",
|
||||
side_effect=lambda v, **k: v,
|
||||
),
|
||||
@@ -231,12 +230,12 @@ class TestBridgeResolvesProviderKey(unittest.TestCase):
|
||||
"ca_trust": {"enabled": True, "required": True, "bridges": ["sso"]},
|
||||
}
|
||||
self._patchers = [
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags.lookup_loader",
|
||||
_applications_loader(self.applications),
|
||||
),
|
||||
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
patch(
|
||||
mock.patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
|
||||
mock.patch(
|
||||
"plugins.lookup.addon_env_flags._render_with_templar",
|
||||
side_effect=lambda v, **k: v,
|
||||
),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -17,19 +17,19 @@ def _context():
|
||||
roles=["sys-ctl-hlth-csp", "web-app-matomo"],
|
||||
applications={},
|
||||
config=lambda role, path, default: SUBNETS.get(role, default),
|
||||
lookup=mock.Mock(),
|
||||
lookup=unittest.mock.Mock(),
|
||||
)
|
||||
|
||||
|
||||
class TestAddressPoolOverlapsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with (
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
),
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
|
||||
@@ -5,7 +5,6 @@ import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -44,7 +43,7 @@ def _patched_loader(flavor="internal", base="https://cdn.example.org/"):
|
||||
|
||||
loader_mock = mock.MagicMock()
|
||||
loader_mock.get.side_effect = _get
|
||||
return patch.object(asset_mod, "lookup_loader", loader_mock)
|
||||
return mock.patch.object(asset_mod, "lookup_loader", loader_mock)
|
||||
|
||||
|
||||
def _write_lock(tmpdir: str, packages: dict) -> None:
|
||||
@@ -98,7 +97,7 @@ class TestLockedVersion(unittest.TestCase):
|
||||
self.tmpdir = tempfile.mkdtemp()
|
||||
|
||||
def _patched_role_path(self):
|
||||
return patch.object(
|
||||
return mock.patch.object(
|
||||
asset_mod, "abs_role_path_by_application_id", return_value=self.tmpdir
|
||||
)
|
||||
|
||||
@@ -139,7 +138,7 @@ class TestAssetRun(unittest.TestCase):
|
||||
|
||||
def _run(self, terms, variables):
|
||||
with (
|
||||
patch.object(
|
||||
mock.patch.object(
|
||||
asset_mod, "abs_role_path_by_application_id", return_value=self.tmpdir
|
||||
),
|
||||
_patched_loader(flavor="internal"),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import sys
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -66,7 +65,7 @@ class TestCertPlanLookup(unittest.TestCase):
|
||||
return mock.MagicMock(run=_applications_from_vars)
|
||||
return mock.MagicMock(run=lambda *_ra, **_rk: [{}])
|
||||
|
||||
loader_patcher = patch("plugins.lookup.cert.lookup_loader")
|
||||
loader_patcher = mock.patch("plugins.lookup.cert.lookup_loader")
|
||||
loader_mock = loader_patcher.start()
|
||||
loader_mock.get.side_effect = _get
|
||||
self.addCleanup(loader_patcher.stop)
|
||||
|
||||
@@ -12,7 +12,6 @@ from __future__ import annotations
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -37,7 +36,7 @@ def _run(
|
||||
applications: dict[str, Any] | None = None,
|
||||
) -> list[str]:
|
||||
apps = applications if applications is not None else _apps()
|
||||
with patch("plugins.lookup.container_address.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_address.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import annotations
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import Any, ClassVar
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -36,7 +35,7 @@ def _apps(*, db_enabled=False, db_shared=False, redis=False, oauth2=False):
|
||||
def _run(app_id, applications, *, variables=None, **kwargs):
|
||||
lookup = LookupModule()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.container_depends_on.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_depends_on.lookup_loader") as loader_mock:
|
||||
loader_mock.get.side_effect = _applications_loader(applications)
|
||||
return lookup.run([app_id], variables=variables or {}, **kwargs)
|
||||
|
||||
@@ -164,7 +163,9 @@ class TestContainerDependsOnLookup(unittest.TestCase):
|
||||
lookup = LookupModule()
|
||||
lookup._templar = _StubTemplar()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.container_depends_on.lookup_loader") as loader_mock:
|
||||
with mock.patch(
|
||||
"plugins.lookup.container_depends_on.lookup_loader"
|
||||
) as loader_mock:
|
||||
loader_mock.get.side_effect = _applications_loader(
|
||||
_apps(db_enabled=True, redis=True)
|
||||
)
|
||||
|
||||
@@ -9,7 +9,6 @@ from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -18,7 +17,7 @@ from plugins.lookup.container_image import LookupModule
|
||||
|
||||
def _run_with_ref(ref: str, terms, **kwargs) -> tuple[list[str], mock.MagicMock]:
|
||||
image_plugin = mock.MagicMock(run=mock.MagicMock(return_value=[ref]))
|
||||
with patch("plugins.lookup.container_image.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_image.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = image_plugin
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
@@ -60,7 +59,7 @@ class TestContainerImageLookup(unittest.TestCase):
|
||||
image_plugin = mock.MagicMock(
|
||||
run=mock.MagicMock(side_effect=AnsibleError("boom"))
|
||||
)
|
||||
with patch("plugins.lookup.container_image.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_image.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = image_plugin
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import ClassVar
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -28,7 +27,7 @@ def _run(terms, *, app_id="web-app-gitea", **kwargs):
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
apps = _apps()
|
||||
with patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
return lm.run(terms, variables={"application_id": app_id}, **kwargs)
|
||||
|
||||
@@ -90,7 +89,7 @@ class TestContainerPortsLookup(unittest.TestCase):
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
with (
|
||||
patch("plugins.lookup.container_ports.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
@@ -108,7 +107,7 @@ class TestContainerPortsLookup(unittest.TestCase):
|
||||
lookup = LookupModule()
|
||||
lookup._templar = _Templar()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [_apps()]
|
||||
)
|
||||
@@ -147,7 +146,7 @@ def _run_edge(terms):
|
||||
lookup = LookupModule()
|
||||
lookup._loader = mock.MagicMock()
|
||||
apps = _edge_apps()
|
||||
with patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
return lookup.run(terms, variables={"application_id": "web-svc-coturn"})[0]
|
||||
|
||||
@@ -213,7 +212,7 @@ class TestContainerPortsDictTerms(unittest.TestCase):
|
||||
def test_key_names_the_block(self):
|
||||
lookup = LookupModule()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [_edge_apps()]
|
||||
)
|
||||
@@ -228,7 +227,7 @@ class TestContainerPortsDictTerms(unittest.TestCase):
|
||||
lookup = LookupModule()
|
||||
lookup._loader = mock.MagicMock()
|
||||
with (
|
||||
patch("plugins.lookup.container_ports.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.container_ports.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
|
||||
@@ -9,7 +9,6 @@ from __future__ import annotations
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -36,7 +35,7 @@ def _run(
|
||||
apps = applications if applications is not None else _apps()
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.container_service.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.container_service.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
return lm.run([application_id, service_key], variables=variables or {})
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import importlib.util
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -88,7 +87,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -115,7 +114,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
self.assertEqual(out["version"], "")
|
||||
self.assertEqual(out["reach_host"], "127.0.0.1")
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -150,7 +149,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -183,7 +182,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
self.assertEqual(out["reach_host"], "127.0.0.1")
|
||||
self.assertEqual(out["instance"], "foo")
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -229,7 +228,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -266,7 +265,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -316,7 +315,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -361,7 +360,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -407,7 +406,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -433,7 +432,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
lookup = self._make_lookup(vars_)
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -445,7 +444,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
self.assertFalse(out["local"])
|
||||
self.assertEqual(out["host"], "")
|
||||
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
@@ -525,7 +524,7 @@ class DatabaseLookupTests(unittest.TestCase):
|
||||
|
||||
def _resolve(self, vars_):
|
||||
lookup = self._make_lookup(vars_)
|
||||
with patch.object(
|
||||
with mock.patch.object(
|
||||
self.db_lookup_mod,
|
||||
"entity_name",
|
||||
side_effect=self._fake_entity_name,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -17,14 +17,14 @@ def _context():
|
||||
return DeployClosureContext(
|
||||
roles=["svc-db-mariadb", "web-app-matomo", "web-app-nextcloud"],
|
||||
applications={},
|
||||
config=mock.Mock(),
|
||||
config=unittest.mock.Mock(),
|
||||
lookup=lambda name, role, default: DOMAINS.get(role, default),
|
||||
)
|
||||
|
||||
|
||||
class TestDeployDomainsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with mock.patch(
|
||||
with unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
|
||||
@@ -17,7 +17,6 @@ sys.path.insert(
|
||||
)
|
||||
|
||||
import docker_cards as docker_cards_module
|
||||
from docker_cards import LookupModule
|
||||
|
||||
from utils.roles.mapping import (
|
||||
ROLE_FILE_META_INFO,
|
||||
@@ -224,7 +223,7 @@ logo:
|
||||
return lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
|
||||
def test_lookup_when_group_includes_application_id(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["TLS_ENABLED"] = True
|
||||
@@ -246,7 +245,7 @@ logo:
|
||||
self.assertTrue(card["iframe"])
|
||||
|
||||
def test_iframe_flag_defaults_to_enabled(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
@@ -254,7 +253,7 @@ logo:
|
||||
self.assertTrue(result[0][0]["iframe"])
|
||||
|
||||
def test_iframe_flag_false_decouples_from_enabled(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["applications"]["portfolio"]["services"]["dashboard"][
|
||||
"iframe"
|
||||
@@ -267,7 +266,7 @@ logo:
|
||||
self.assertFalse(cards[0]["iframe"])
|
||||
|
||||
def test_lookup_url_uses_https_when_tls_enabled_true(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["TLS_ENABLED"] = True
|
||||
|
||||
@@ -276,7 +275,7 @@ logo:
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_uses_http_when_tls_enabled_false(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["TLS_ENABLED"] = False
|
||||
|
||||
@@ -285,7 +284,7 @@ logo:
|
||||
self.assertEqual(result[0][0]["url"], "http://myportfolio.com")
|
||||
|
||||
def test_lookup_when_group_excludes_application_id(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["group_names"] = []
|
||||
|
||||
@@ -295,7 +294,7 @@ logo:
|
||||
self.assertEqual(len(result[0]), 0)
|
||||
|
||||
def test_lookup_url_renders_domain_url_jinja(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["domains"] = {"portfolio": "{{ DOMAIN_PRIMARY }}"}
|
||||
@@ -307,7 +306,7 @@ logo:
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_https_when_tls_enabled_is_string(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["TLS_ENABLED"] = "true"
|
||||
@@ -317,7 +316,7 @@ logo:
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_http_when_tls_enabled_is_string(self):
|
||||
lookup_module = LookupModule()
|
||||
lookup_module = docker_cards_module.LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["TLS_ENABLED"] = "false"
|
||||
@@ -342,14 +341,14 @@ logo:
|
||||
{"enabled": True, "card": "client"}
|
||||
)
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "https://client.myportfolio.com")
|
||||
|
||||
def test_card_url_without_card_key_targets_the_primary_domain(self):
|
||||
fake_variables = self._card_target_variables({"enabled": True})
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
@@ -357,7 +356,7 @@ logo:
|
||||
fake_variables = self._card_target_variables({"enabled": True, "card": "nope"})
|
||||
|
||||
with self.assertRaisesRegex(AnsibleError, "nope"):
|
||||
self._run_lookup(LookupModule(), fake_variables)
|
||||
self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
def _add_role(self, role_name, application_id, info_yaml):
|
||||
role_dir = Path(self.test_roles_dir) / role_name
|
||||
@@ -388,7 +387,7 @@ logo:
|
||||
"enabled"
|
||||
] = False
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual(result, [[]])
|
||||
|
||||
@@ -396,14 +395,14 @@ logo:
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["applications"]["portfolio"]["services"] = {}
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual([c["title"] for c in result[0]], ["Portfolio Application"])
|
||||
|
||||
def test_enabled_true_shows_the_card(self):
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual([c["title"] for c in result[0]], ["Portfolio Application"])
|
||||
|
||||
@@ -413,14 +412,14 @@ logo:
|
||||
False
|
||||
)
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual([c["title"] for c in result[0]], ["Portfolio Application"])
|
||||
|
||||
def test_both_cards_render_when_both_are_enabled(self):
|
||||
fake_variables = self._two_role_variables()
|
||||
|
||||
result = self._run_lookup(LookupModule(), fake_variables)
|
||||
result = self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
self.assertEqual(
|
||||
[c["title"] for c in result[0]],
|
||||
@@ -434,7 +433,9 @@ logo:
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = self._run_lookup(LookupModule(), self._base_fake_variables())
|
||||
result = self._run_lookup(
|
||||
docker_cards_module.LookupModule(), self._base_fake_variables()
|
||||
)
|
||||
|
||||
self.assertEqual([c["title"] for c in result[0]], ["Portfolio Application"])
|
||||
|
||||
@@ -445,7 +446,7 @@ logo:
|
||||
] = "maybe"
|
||||
|
||||
with self.assertRaisesRegex(AnsibleError, "portfolio"):
|
||||
self._run_lookup(LookupModule(), fake_variables)
|
||||
self._run_lookup(docker_cards_module.LookupModule(), fake_variables)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -45,12 +45,12 @@ def _context(roles=("svc-db-mariadb", "web-app-matomo", "web-app-discourse")):
|
||||
class TestDockerOwnershipLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with (
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
) as context,
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import sys
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -32,7 +31,7 @@ class TestDomainLookup(unittest.TestCase):
|
||||
self.lookup._loader = mock.MagicMock()
|
||||
|
||||
def run_lookup(self, application_id, domains_map):
|
||||
with patch("plugins.lookup.domain.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.domain.lookup_loader") as loader_mock:
|
||||
|
||||
def _get(name, *a, **k):
|
||||
if name == "domains":
|
||||
@@ -90,7 +89,7 @@ class TestDomainLookup(unittest.TestCase):
|
||||
self.run_lookup_raw([123])
|
||||
|
||||
def run_lookup_raw(self, terms):
|
||||
with patch("plugins.lookup.domain.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.domain.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [{"app": "example.com"}]
|
||||
)
|
||||
|
||||
@@ -15,8 +15,8 @@ for every host, and per-host narrowing stays in the body's ``when``.
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
import unittest.mock
|
||||
from typing import ClassVar
|
||||
from unittest import mock
|
||||
|
||||
from plugins.lookup.group_roles import LookupModule
|
||||
|
||||
@@ -28,7 +28,7 @@ ENTRIES = [
|
||||
|
||||
|
||||
def run(**kwargs):
|
||||
with mock.patch(
|
||||
with unittest.mock.patch(
|
||||
"plugins.lookup.group_roles.ordered_roles", return_value=list(ENTRIES)
|
||||
):
|
||||
return LookupModule().run(["web-app"], **kwargs)[0]
|
||||
|
||||
@@ -13,7 +13,6 @@ from __future__ import annotations
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -47,7 +46,7 @@ def _run(
|
||||
**kwargs: Any,
|
||||
) -> list[str]:
|
||||
apps = applications if applications is not None else _apps()
|
||||
with patch("plugins.lookup.image.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.image.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
@@ -205,7 +204,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
|
||||
def test_missing_version_returns_bare_image_with_warning(self):
|
||||
apps = _apps(version=None)
|
||||
with patch("plugins.lookup.image._warn") as warn:
|
||||
with mock.patch("plugins.lookup.image._warn") as warn:
|
||||
out = _run(
|
||||
"web-app-mattermost",
|
||||
"mattermost",
|
||||
@@ -217,7 +216,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
|
||||
def test_image_override_with_missing_services_version_warns(self):
|
||||
apps = _apps(version=None)
|
||||
with patch("plugins.lookup.image._warn") as warn:
|
||||
with mock.patch("plugins.lookup.image._warn") as warn:
|
||||
out = _run(
|
||||
"web-app-mattermost",
|
||||
"mattermost",
|
||||
@@ -245,7 +244,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
self.assertEqual(out, ["mattermost/mattermost-team-edition:11.8.0"])
|
||||
|
||||
def test_custom_true_uses_entity_name_custom(self):
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -258,7 +257,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
self.assertEqual(out, ["mattermost_custom:11.8.0"])
|
||||
|
||||
def test_custom_true_with_version_override(self):
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -272,7 +271,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
self.assertEqual(out, ["mattermost_custom:local"])
|
||||
|
||||
def test_custom_true_with_image_override_image_wins(self):
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
) as get_name:
|
||||
@@ -287,7 +286,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
get_name.assert_not_called()
|
||||
|
||||
def test_custom_true_swarm_prefix_applied(self):
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -314,7 +313,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
|
||||
def test_custom_true_ignores_services_image(self):
|
||||
apps = _apps(image="mattermost/mattermost-team-edition", version="11.8.0")
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -328,7 +327,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
self.assertEqual(out, ["mattermost_custom:11.8.0"])
|
||||
|
||||
def test_custom_true_tag_only_returns_bare(self):
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -354,7 +353,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
def test_entry_custom_true_used_without_kwarg(self):
|
||||
apps = _apps()
|
||||
apps["web-app-mattermost"]["services"]["mattermost"]["custom"] = True
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
@@ -394,7 +393,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
apps["web-app-mattermost"]["services"]["wb"] = apps["web-app-mattermost"][
|
||||
"services"
|
||||
].pop("mattermost")
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="mattermost",
|
||||
) as get_name:
|
||||
@@ -429,7 +428,7 @@ class TestImageLookup(unittest.TestCase):
|
||||
|
||||
def test_custom_true_empty_entity_name_raises(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.image.entity_name",
|
||||
return_value="",
|
||||
),
|
||||
|
||||
@@ -4,7 +4,6 @@ import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from plugins.lookup.native_metrics_apps import LookupModule
|
||||
|
||||
@@ -26,8 +25,8 @@ def _run(
|
||||
lm._loader = mock.MagicMock()
|
||||
lm._templar = mock.MagicMock(template=lambda v: v)
|
||||
with (
|
||||
patch("plugins.lookup.native_metrics_apps.ROLES_DIR", roles_dir),
|
||||
patch("plugins.lookup.native_metrics_apps.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.native_metrics_apps.ROLES_DIR", roles_dir),
|
||||
mock.patch("plugins.lookup.native_metrics_apps.lookup_loader") as loader_mock,
|
||||
):
|
||||
|
||||
def _get(name, *a, **k):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -17,25 +17,27 @@ def _config_run(terms, variables=None):
|
||||
|
||||
class TestNetworkOwnershipLookup(unittest.TestCase):
|
||||
def _run(self, terms, closure=None, side_effect=None):
|
||||
applications = mock.Mock()
|
||||
applications = unittest.mock.Mock()
|
||||
applications.run.return_value = [{"web-app-matomo": {}}]
|
||||
config = mock.Mock()
|
||||
config = unittest.mock.Mock()
|
||||
config.run.side_effect = _config_run
|
||||
with (
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.lookup_loader.get",
|
||||
side_effect=lambda name, **kwargs: {
|
||||
"applications": applications,
|
||||
"config": config,
|
||||
}[name],
|
||||
),
|
||||
mock.patch(f"{MODULE}.types_from_group_names", return_value=["server"]),
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.types_from_group_names", return_value=["server"]
|
||||
),
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure",
|
||||
return_value=closure or {"web-app-matomo", "sys-ctl-hlth-csp"},
|
||||
side_effect=side_effect,
|
||||
) as closure_mock,
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
||||
),
|
||||
):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -58,15 +57,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def _run(self, terms, **kwargs):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
return self.plugin.run(terms, variables=self.variables, **kwargs)[0]
|
||||
@@ -124,17 +123,17 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
"nginx": {"type": "volume", "path": "/opt/canonical/nginx"},
|
||||
}
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=AssertionError(
|
||||
"get() must not be called when canonical paths are present"
|
||||
),
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value=canonical,
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
out = self.plugin.run(["files.configuration"], variables=self.variables)[0]
|
||||
@@ -144,15 +143,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
fake_tls = _FakeTlsResolveLookup("https")
|
||||
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get(tls=fake_tls)
|
||||
out = self.plugin.run(
|
||||
@@ -166,15 +165,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def test_domain_protocol_override_http(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
out = self.plugin.run(
|
||||
@@ -190,15 +189,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def test_invalid_protocol_override_raises(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
@@ -210,15 +209,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def test_files_lua_is_one_file_per_domain(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
out = self.plugin.run(
|
||||
@@ -231,15 +230,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def test_files_lua_without_domain_raises(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
@@ -247,15 +246,15 @@ class TestNginxPathsLookup(unittest.TestCase):
|
||||
|
||||
def test_invalid_usage_raises(self):
|
||||
with (
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get",
|
||||
side_effect=self._fake_get,
|
||||
),
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.nginx.get_canonical_volumes",
|
||||
return_value={},
|
||||
),
|
||||
patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.nginx.lookup_loader") as loader_mock,
|
||||
):
|
||||
loader_mock.get.side_effect = self._loader_get()
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import importlib.util
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import ClassVar
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -59,7 +58,9 @@ class ObjstoreLookupTests(unittest.TestCase):
|
||||
lookup = self._make_lookup(vars_)
|
||||
with (
|
||||
mock.patch.object(self.mod, "lookup_loader") as loader_mock,
|
||||
patch.object(self.mod, "entity_name", side_effect=self._fake_entity_name),
|
||||
mock.patch.object(
|
||||
self.mod, "entity_name", side_effect=self._fake_entity_name
|
||||
),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [applications]
|
||||
@@ -358,7 +359,7 @@ class ObjstoreSameOriginProxyTests(unittest.TestCase):
|
||||
|
||||
with (
|
||||
mock.patch.object(self.mod, "lookup_loader") as loader_mock,
|
||||
patch.object(
|
||||
mock.patch.object(
|
||||
self.mod,
|
||||
"entity_name",
|
||||
side_effect=lambda r: r.split("-", 2)[-1],
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -11,17 +11,23 @@ MODULE = "plugins.lookup.package_batch"
|
||||
|
||||
class TestPackageBatchLookup(unittest.TestCase):
|
||||
def _run(self, terms, group_names, closure=None, side_effect=None):
|
||||
applications = mock.Mock()
|
||||
applications = unittest.mock.Mock()
|
||||
applications.run.return_value = [{"web-app-x": {}}]
|
||||
with (
|
||||
mock.patch(f"{MODULE}.lookup_loader.get", return_value=applications),
|
||||
mock.patch(f"{MODULE}.types_from_group_names", return_value=["server"]),
|
||||
mock.patch(
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.lookup_loader.get", return_value=applications
|
||||
),
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.types_from_group_names", return_value=["server"]
|
||||
),
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure",
|
||||
return_value=closure or {"web-app-x"},
|
||||
side_effect=side_effect,
|
||||
) as closure_mock,
|
||||
mock.patch(f"{MODULE}.batch_ids", return_value=["git"]) as ids_mock,
|
||||
unittest.mock.patch(
|
||||
f"{MODULE}.batch_ids", return_value=["git"]
|
||||
) as ids_mock,
|
||||
):
|
||||
lookup = LookupModule()
|
||||
result = lookup.run(terms, variables={"group_names": group_names})
|
||||
|
||||
@@ -5,7 +5,6 @@ from __future__ import annotations
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -43,7 +42,7 @@ def _run(terms, *, variables, **kwargs):
|
||||
lm = LookupModule()
|
||||
lm._templar = _Templar(variables)
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [_apps()])
|
||||
return lm.run(terms, variables=variables, **kwargs)
|
||||
|
||||
@@ -98,7 +97,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
lm = LookupModule()
|
||||
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
out = lm.run(
|
||||
["web-app-matrix", "matrix", "http"],
|
||||
@@ -121,7 +120,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
lm = LookupModule()
|
||||
lm._templar = _ResolvingTemplar({"DEPLOYMENT_MODE": "swarm"})
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
out = lm.run(
|
||||
["web-app-matrix", "matrix", "http"],
|
||||
@@ -140,7 +139,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
||||
lm._loader = mock.MagicMock()
|
||||
with (
|
||||
patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
@@ -161,7 +160,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
{"DEPLOYMENT_MODE": "swarm", "compose_mode_force": "compose"}
|
||||
)
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
out = lm.run(
|
||||
["web-app-matrix", "matrix", "http"],
|
||||
@@ -186,7 +185,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
{"DEPLOYMENT_MODE": "compose", "compose_mode_force": "compose"}
|
||||
)
|
||||
lm._loader = mock.MagicMock()
|
||||
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
||||
out = lm.run(
|
||||
["web-app-matrix", "matrix", "http"],
|
||||
@@ -232,7 +231,7 @@ class TestProxyPassLookup(unittest.TestCase):
|
||||
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
||||
lm._loader = mock.MagicMock()
|
||||
with (
|
||||
patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import unittest.mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -44,13 +44,13 @@ def _context():
|
||||
roles=sorted(APPLICATIONS),
|
||||
applications=APPLICATIONS,
|
||||
config=_config,
|
||||
lookup=mock.Mock(),
|
||||
lookup=unittest.mock.Mock(),
|
||||
)
|
||||
|
||||
|
||||
class TestPublicPortsLookup(unittest.TestCase):
|
||||
def _run(self, terms, side_effect=None):
|
||||
with mock.patch(
|
||||
with unittest.mock.patch(
|
||||
f"{MODULE}.deploy_closure_context",
|
||||
return_value=_context(),
|
||||
side_effect=side_effect,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -22,7 +21,7 @@ class TestRbacGroupPathLookup(unittest.TestCase):
|
||||
def _applications_from_vars(_terms, variables=None, **_kwargs):
|
||||
return [(variables or {}).get("applications", {})]
|
||||
|
||||
patcher = patch("plugins.lookup.rbac_group_path.lookup_loader")
|
||||
patcher = mock.patch("plugins.lookup.rbac_group_path.lookup_loader")
|
||||
loader_mock = patcher.start()
|
||||
loader_mock.get.return_value = mock.MagicMock(run=_applications_from_vars)
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
@@ -8,7 +8,6 @@ tests stay hermetic — no filesystem access into ``roles/``.
|
||||
import importlib.util
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -128,7 +127,7 @@ class RolesWithServiceLookupTests(unittest.TestCase):
|
||||
def _dispatch(name, **_kwargs):
|
||||
return stubs[name]
|
||||
|
||||
with patch.object(self.mod.lookup_loader, "get", side_effect=_dispatch):
|
||||
with mock.patch.object(self.mod.lookup_loader, "get", side_effect=_dispatch):
|
||||
return lookup.run(terms, variables=vars_ or {}, **kwargs)
|
||||
|
||||
def test_zero_terms_raises(self):
|
||||
|
||||
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -25,17 +24,17 @@ def _run(
|
||||
static_registry=None,
|
||||
**kwargs,
|
||||
):
|
||||
loader_patch = patch("plugins.lookup.service.lookup_loader")
|
||||
loader_patch = mock.patch("plugins.lookup.service.lookup_loader")
|
||||
patches = [
|
||||
loader_patch,
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.service.build_service_registry_from_roles_dir",
|
||||
return_value=static_registry or {},
|
||||
),
|
||||
]
|
||||
if service_registry is not None:
|
||||
patches.append(
|
||||
patch(
|
||||
mock.patch(
|
||||
"plugins.lookup.service.build_service_registry_from_applications",
|
||||
return_value=service_registry,
|
||||
)
|
||||
@@ -308,7 +307,7 @@ class TestServiceCycleGuard(unittest.TestCase):
|
||||
class TestServiceErrors(unittest.TestCase):
|
||||
def test_raises_when_group_names_not_list(self):
|
||||
with (
|
||||
patch("plugins.lookup.service.lookup_loader") as loader_mock,
|
||||
mock.patch("plugins.lookup.service.lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
||||
|
||||
@@ -9,7 +9,6 @@ The lookup walks ``group_names`` and returns a
|
||||
import importlib.util
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -51,7 +50,7 @@ class ServiceExtensionsLookupTests(unittest.TestCase):
|
||||
group_names: list[str],
|
||||
):
|
||||
lookup = self._make_lookup({"group_names": group_names})
|
||||
with patch.object(self.mod, "lookup_loader") as loader_mock:
|
||||
with mock.patch.object(self.mod, "lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [applications]
|
||||
)
|
||||
@@ -72,7 +71,7 @@ class ServiceExtensionsLookupTests(unittest.TestCase):
|
||||
def test_non_list_group_names_raises(self):
|
||||
lookup = self._make_lookup({"group_names": "not-a-list"})
|
||||
with (
|
||||
patch.object(self.mod, "lookup_loader") as loader_mock,
|
||||
mock.patch.object(self.mod, "lookup_loader") as loader_mock,
|
||||
self.assertRaises(AnsibleError),
|
||||
):
|
||||
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
||||
|
||||
@@ -4,7 +4,6 @@ import tempfile
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from plugins.lookup.service_registry import LookupModule
|
||||
from utils.cache.yaml import dump_yaml_str
|
||||
@@ -82,7 +81,9 @@ class TestServiceRegistryOrdered(unittest.TestCase):
|
||||
result-arrival order (consumers deploy before their engines)."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
roles_dir = self._roles_dir(td)
|
||||
with patch("plugins.lookup.service_registry.lookup_loader") as loader_mock:
|
||||
with mock.patch(
|
||||
"plugins.lookup.service_registry.lookup_loader"
|
||||
) as loader_mock:
|
||||
lm = LookupModule()
|
||||
lm._loader = mock.MagicMock()
|
||||
result = lm.run(
|
||||
@@ -121,7 +122,7 @@ class TestServiceRegistryOrdered(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
with patch("plugins.lookup.service_registry.lookup_loader") as loader_mock:
|
||||
with mock.patch("plugins.lookup.service_registry.lookup_loader") as loader_mock:
|
||||
loader_mock.get.return_value = mock.MagicMock(
|
||||
run=lambda *_a, **_k: [applications]
|
||||
)
|
||||
|
||||
@@ -2,8 +2,8 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import unittest
|
||||
import unittest.mock
|
||||
from typing import ClassVar
|
||||
from unittest import mock
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -45,7 +45,7 @@ class TestSsoWhitelistDomains(unittest.TestCase):
|
||||
|
||||
def _run(self, variables, domains=None, node_onion=""):
|
||||
domains = {self.APP: f"pihole.{PRIMARY}"} if domains is None else domains
|
||||
with mock.patch(
|
||||
with unittest.mock.patch(
|
||||
"plugins.lookup.sso_whitelist_domains.lookup_loader.get",
|
||||
side_effect=_loader_for(domains, node_onion),
|
||||
):
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import sys
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
@@ -43,7 +42,7 @@ class TestTlsResolveLookup(unittest.TestCase):
|
||||
|
||||
return mock.MagicMock(run=_run)
|
||||
|
||||
loader_patcher = patch("plugins.lookup.tls.lookup_loader")
|
||||
loader_patcher = mock.patch("plugins.lookup.tls.lookup_loader")
|
||||
loader_mock = loader_patcher.start()
|
||||
loader_mock.get.side_effect = _get
|
||||
self.addCleanup(loader_patcher.stop)
|
||||
|
||||
Reference in new issue
Block a user