From 0defb98428ef66538cd5d788c4d3a8746cf136bf Mon Sep 17 00:00:00 2001 From: Tyler Levy Conde Date: Tue, 16 Jul 2024 11:52:37 -0600 Subject: [PATCH] Added test to catch Falsey value output issue in cmd.script --- .../integration/modules/test_cmdmod.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/pytests/integration/modules/test_cmdmod.py b/tests/pytests/integration/modules/test_cmdmod.py index 4e8ce5824ee..658cdbf8551 100644 --- a/tests/pytests/integration/modules/test_cmdmod.py +++ b/tests/pytests/integration/modules/test_cmdmod.py @@ -1,4 +1,6 @@ import pytest +import tempfile +import os @pytest.fixture(scope="module") @@ -22,3 +24,58 @@ def test_long_stdout(salt_cli, salt_minion): ) assert ret.returncode == 0 assert len(ret.data.strip()) == len(echo_str) + + +@pytest.fixture() +def test_script_path(): + """ + Create a temporary shell script that echoes its arguments. + + This fixture sets up a temporary shell script, makes it executable, + and yields the path to the script for use in tests. After the test + completes, the temporary file is automatically removed. + + Yields: + str: The path to the temporary shell script. + """ + script_content = "#!/bin/bash\necho $*" + + with tempfile.NamedTemporaryFile(mode='w', suffix='-salt_echo_num.sh') as temp_script: + temp_script.write(script_content) + temp_script_path = temp_script.name + + # Make the script executable + os.chmod(temp_script_path, 0o755) + + + yield temp_script_path + + +def test_script_with_falsey_args(subtests, salt_call_cli, test_script_path): + """ + Test `cmd.script` with various falsey arguments to ensure correct handling. + + This test runs the temporary shell script with a variety of arguments + that evaluate to false in Python. It uses subtests to individually test + each falsey argument and checks that the script outputs the argument correctly. + + Args: + subtests (SubTests): The subtests fixture for running parameterized tests. + salt_call_cli (SaltCallCLI): The salt CLI fixture for running salt commands. + test_script_path (str): The path to the temporary shell script. + """ + # List of values to test that evaluate to `False` when used in python conditionals + falsey_values = ["0", "", "''", "\"\"", "()", "[]", "{}", "False", "None"] + + for value in falsey_values: + expected_output = str(value).strip('"').strip("'") + with subtests.test(f"The script should print '{expected_output}' for input '{value}'", value=value): + # Run the script with the current falsey value as an argument + ret = salt_call_cli.run("--local", "cmd.script", f"file://{test_script_path}", str(value)) + + # Check that the script ran successfully and printed the expected output + assert ret.returncode == 0, f"The script failed to run with argument: {value}" + + # Verify that the script's output matches the expected output + assert expected_output in ret.json["stdout"] + \ No newline at end of file