Return minion_id on success

This makes checking the response downstream more reliable.
This commit is contained in:
jeanluc 2022-12-14 23:26:14 +01:00 committed by Megan Wilhite
parent 0873ece26c
commit b622d96a62
2 changed files with 16 additions and 12 deletions

View file

@ -15,16 +15,19 @@ log = logging.getLogger(__name__)
def compound_matches(expr, minion_id):
"""
Check whether a minion is matched by a given compound match expression.
Pillar values will be matched literally only since this function is intended
for remote calling. This also applies to node groups defined on the master.
On success, this function will return the minion ID, otherwise False.
.. note::
Pillar values will be matched literally only since this function is intended
for remote calling. This also applies to node groups defined on the master.
Custom matchers are not respected.
.. note::
If a module calls this runner from a minion, you will need to explicitly
allow the remote call. See :conf_master:`peer_run`.
.. note::
Custom matchers are not respected.
CLI Example:
.. code-block:: bash
@ -51,7 +54,7 @@ def compound_matches(expr, minion_id):
if not salt.utils.verify.valid_id(__opts__, minion_id):
log.warning("Got invalid minion ID.")
return False
log.debug(f"Evaluating if minion '{minion_id}' is matched by '{expr}'.")
log.debug("Evaluating if minion '%s' is matched by '%s'.", minion_id, expr)
ckminions = salt.utils.minions.CkMinions(__opts__)
# Compound expressions are usually evaluated in greedy mode since you
# want to make sure the executing user has privileges to run a command on
@ -63,7 +66,8 @@ def compound_matches(expr, minion_id):
minions = ckminions._check_compound_pillar_exact_minions(
expr, DEFAULT_TARGET_DELIM, greedy=False
)
return minion_id in minions["minions"]
if minion_id in minions["minions"]:
return minion_id
except Exception: # pylint: disable=broad-except
pass
return False

View file

@ -136,7 +136,7 @@ class TestMatchCompoundRunner:
ret = match_salt_run_cli.run("cache.clear_all", "match-minion-alice")
assert ret.returncode == 0
yield
ret = match_salt_minion_alice.salt_call_cli().run("pillar.items")
match_salt_minion_alice.salt_call_cli().run("pillar.items")
@pytest.fixture
def eve_cached(self, match_salt_minion_eve):
@ -174,7 +174,7 @@ class TestMatchCompoundRunner:
"match.compound_matches", expr, "match-minion-alice"
)
assert ret.returncode == 0
assert ret.data is expected
assert bool(ret.data) is expected
@pytest.mark.usefixtures("eve_cached")
def test_match_compound_matches_only_allows_exact_pillar_matching(
@ -211,7 +211,7 @@ class TestMatchCompoundRunner:
"match.compound_matches", expr, "match-minion-alice"
)
assert ret.returncode == 0
assert ret.data is expected
assert bool(ret.data) is expected
@pytest.mark.parametrize(
"expr,expected",
@ -232,7 +232,7 @@ class TestMatchCompoundRunner:
"match.compound_matches", expr, "match-minion-alice"
)
assert ret.returncode == 0
assert ret.data is expected
assert bool(ret.data) is expected
@pytest.mark.parametrize(
"minion_id",
@ -267,7 +267,7 @@ class TestMatchCompoundRunner:
"publish.runner", "match.compound_matches", [expr, "match-minion-alice"]
)
assert ret.returncode == 0
assert ret.data is expected
assert bool(ret.data) is expected
class TestMatchCompoundRunnerWithoutMinionDataCache: