diff --git a/changelog/68121.fixed.md b/changelog/68121.fixed.md new file mode 100644 index 000000000000..7b87e7ebe79d --- /dev/null +++ b/changelog/68121.fixed.md @@ -0,0 +1 @@ +Fix `test mode` causing unintended execution when non-boolean values are passed. diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py index 28cc93130259..9790190e1d8e 100644 --- a/salt/loader/lazy.py +++ b/salt/loader/lazy.py @@ -193,7 +193,10 @@ async def __call__( if hasattr(mod, "__opts__"): if not isinstance(mod.__opts__, salt.loader.context.NamedLoaderContext): if "test" in self.loader.opts: - mod.__opts__["test"] = self.loader.opts["test"] + if self.loader.opts["test"] is False: + mod.__opts__["test"] = False + else: + mod.__opts__["test"] = True set_test = True if self.loader.inject_globals: run_func = global_injector_decorator(self.loader.inject_globals)(run_func) diff --git a/tests/pytests/unit/loader/test_lazy.py b/tests/pytests/unit/loader/test_lazy.py index 841f015d2c59..6eeaa011dd5d 100644 --- a/tests/pytests/unit/loader/test_lazy.py +++ b/tests/pytests/unit/loader/test_lazy.py @@ -33,6 +33,9 @@ async def myasync(foo): def get_opts(key): return __opts__.get(key, None) + + async def get_opts_async(key): + return __opts__.get(key, None) """ with pytest.helpers.temp_file( "mod_a.py", directory=tmp_path, contents=mod_contents @@ -174,3 +177,23 @@ def test_loaded_func_ensures_test_boolean(loader_dir, test_value, expected): loaded_fun = loader["mod_a.get_opts"] ret = loaded_fun("test") assert ret is expected + + +@pytest.mark.parametrize( + "test_value, expected", + [ + (True, True), + (False, False), + ("abc", True), + (123, True), + ], +) +async def test_loaded_coro_ensures_test_boolean(loader_dir, test_value, expected): + """ + Coroutines loaded from LazyLoader's item lookups are LoadedCoro objects + """ + opts = {"optimization_order": [0, 1, 2], "test": test_value} + loader = salt.loader.lazy.LazyLoader([loader_dir], opts) + loaded_coro = loader["mod_a.get_opts_async"] + ret = await loaded_coro("test") + assert ret is expected