From 22b44a82752354202145b283132fa9abb14e8813 Mon Sep 17 00:00:00 2001 From: Barney Sowood Date: Fri, 18 Aug 2023 13:39:44 +0100 Subject: [PATCH] Add support for show_jid to salt-run Adds support for show_jid to the salt-run cli command to match the behaviour of the salt cli command. --- salt/cli/run.py | 3 +- salt/runner.py | 5 ++- .../functional/cli/test_salt_run_show_jid.py | 42 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/pytests/functional/cli/test_salt_run_show_jid.py diff --git a/salt/cli/run.py b/salt/cli/run.py index fba70fffbec..eac0cd54031 100644 --- a/salt/cli/run.py +++ b/salt/cli/run.py @@ -30,8 +30,9 @@ class SaltRun(salt.utils.parsers.SaltRunOptionParser): try: if check_user(self.config["user"]): pr = salt.utils.profile.activate_profile(profiling_enabled) + show_jid = self.config.get("show_jid", False) try: - ret = runner.run(full_return=True) + ret = runner.run(full_return=True, show_jid=show_jid) # In older versions ret['data']['retcode'] was used # for signaling the return code. This has been # changed for the orchestrate runner, but external diff --git a/salt/runner.py b/salt/runner.py index d3501b8f919..2ab0c075fd9 100644 --- a/salt/runner.py +++ b/salt/runner.py @@ -207,7 +207,7 @@ class Runner(RunnerClient): print(docs[fun]) # TODO: move to mixin whenever we want a salt-wheel cli - def run(self, full_return=False): + def run(self, full_return=False, show_jid=False): """ Execute the runner sequence """ @@ -288,6 +288,9 @@ class Runner(RunnerClient): return async_pub["jid"] # return the jid # otherwise run it in the main process + if show_jid: + print(f"jid: {self.jid}") + if self.opts.get("eauth"): ret = self.cmd_sync(low) if isinstance(ret, dict) and set(ret) == {"data", "outputter"}: diff --git a/tests/pytests/functional/cli/test_salt_run_show_jid.py b/tests/pytests/functional/cli/test_salt_run_show_jid.py new file mode 100644 index 00000000000..3fdfe9fb91a --- /dev/null +++ b/tests/pytests/functional/cli/test_salt_run_show_jid.py @@ -0,0 +1,42 @@ +""" +Tests for salt-run with show_jid +""" + +import logging +import re + +import pytest + +log = logging.getLogger(__name__) + + +@pytest.fixture(scope="module") +def salt_master(salt_factories): + """ + Salt master with `show_jid: True` + """ + config_defaults = { + "show_jid": True, + } + salt_master = salt_factories.salt_master_daemon( + "salt-run-show-jid-master", defaults=config_defaults + ) + with salt_master.started(): + yield salt_master + + +@pytest.fixture(scope="module") +def salt_run_cli(salt_master): + """ + The ``salt-run`` CLI as a fixture against the running master + """ + assert salt_master.is_running() + return salt_master.salt_run_cli(timeout=30) + + +def test_salt_run_show_jid(salt_run_cli): + """ + Test that jid is output + """ + ret = salt_run_cli.run("test.stdout_print") + assert re.match(r"jid: \d+", ret.stdout)