Skip to content

Fix/cli arg parsing and test logic 3007.x #68232

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

Open
wants to merge 3 commits into
base: 3007.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog/68121.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `test mode` causing unintended execution when non-boolean values are passed.
5 changes: 4 additions & 1 deletion salt/loader/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions tests/pytests/unit/loader/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading