From 46c8c461e370d45929d2b4b1b3456fa6f6f1bfee Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Wed, 8 May 2024 12:57:28 +0200 Subject: [PATCH 1/2] Speed up salt.matcher.confirm_top by using __context__ --- salt/matchers/confirm_top.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/salt/matchers/confirm_top.py b/salt/matchers/confirm_top.py index 7435f4ae94d..d2edc99d8f8 100644 --- a/salt/matchers/confirm_top.py +++ b/salt/matchers/confirm_top.py @@ -21,7 +21,11 @@ def confirm_top(match, data, nodegroups=None): if "match" in item: matcher = item["match"] - matchers = salt.loader.matchers(__opts__) + if "matchers" in __context__: + matchers = __context__["matchers"] + else: + matchers = salt.loader.matchers(__opts__) + __context__["matchers"] = matchers funcname = matcher + "_match.match" if matcher == "nodegroup": return matchers[funcname](match, nodegroups) From b527909e1d11514fcf282dcabe781c4e6a19c3d6 Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Thu, 9 May 2024 09:56:34 +0200 Subject: [PATCH 2/2] Add test for getting matchers from __context__ in matchers.confirm_top --- tests/pytests/unit/matchers/test_confirm_top.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/pytests/unit/matchers/test_confirm_top.py b/tests/pytests/unit/matchers/test_confirm_top.py index 514df323b69..f439fcf94ad 100644 --- a/tests/pytests/unit/matchers/test_confirm_top.py +++ b/tests/pytests/unit/matchers/test_confirm_top.py @@ -2,6 +2,7 @@ import salt.config import salt.loader +from tests.support.mock import patch @pytest.fixture @@ -12,3 +13,17 @@ def matchers(minion_opts): def test_sanity(matchers): match = matchers["confirm_top.confirm_top"] assert match("*", []) is True + + +@pytest.mark.parametrize("in_context", [False, True]) +def test_matchers_from_context(matchers, in_context): + match = matchers["confirm_top.confirm_top"] + with patch.dict( + matchers.pack["__context__"], {"matchers": matchers} if in_context else {} + ), patch("salt.loader.matchers", return_value=matchers) as loader_matchers: + assert match("*", []) is True + assert id(matchers.pack["__context__"]["matchers"]) == id(matchers) + if in_context: + loader_matchers.assert_not_called() + else: + loader_matchers.assert_called_once()