Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up salt.matcher.confirm_top by using __context__ #647

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion salt/matchers/confirm_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/pytests/unit/matchers/test_confirm_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import salt.config
import salt.loader
from tests.support.mock import patch


@pytest.fixture
Expand All @@ -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()
Loading