| 1 |
# Copyright (c) 2026, Alexei Znamensky <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import typing as t |
| 8 |
from io import StringIO |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible.playbook.play_context import PlayContext |
| 12 |
from ansible.plugins.loader import connection_loader |
| 13 |
|
| 14 |
BUILD_CMD_TEST_CASES: list[dict[str, t.Any]] = [ |
| 15 |
dict( |
| 16 |
id="sh simple", |
| 17 |
input=dict( |
| 18 |
cmd="""echo 123""", |
| 19 |
shell="sh", |
| 20 |
), |
| 21 |
output=["/bin/sh", "-c", "echo 123"], |
| 22 |
), |
| 23 |
dict( |
| 24 |
id="powershell simple", |
| 25 |
input=dict( |
| 26 |
cmd="""powershell -c My-Command1 -Param 'param' """, |
| 27 |
shell="powershell", |
| 28 |
), |
| 29 |
output=["powershell", "-c", "My-Command1 -Param 'param'"], |
| 30 |
), |
| 31 |
dict( |
| 32 |
id=r"D:\my path\powershell.exe simple", |
| 33 |
input=dict( |
| 34 |
cmd=r"""D:\my path\powershell.exe -c My-Command1 -Param 'param' """, |
| 35 |
shell="powershell", |
| 36 |
), |
| 37 |
output=[r"D:\my path\powershell.exe", "-c", "My-Command1 -Param 'param'"], |
| 38 |
), |
| 39 |
dict( |
| 40 |
id=r"\\127.0.0.1\share\powershell simple", |
| 41 |
input=dict( |
| 42 |
cmd=r"""\\127.0.0.1\share\powershell -c My-Command1 -Param 'param' """, |
| 43 |
shell="powershell", |
| 44 |
), |
| 45 |
output=[r"\\127.0.0.1\share\powershell", "-c", "My-Command1 -Param 'param'"], |
| 46 |
), |
| 47 |
dict( |
| 48 |
id=r"C:\powershell simple", |
| 49 |
input=dict( |
| 50 |
cmd=r"""C:\powershell -c My-Command1 -Param 'param' """, |
| 51 |
shell="powershell", |
| 52 |
), |
| 53 |
output=[r"C:\powershell", "-c", "My-Command1 -Param 'param'"], |
| 54 |
), |
| 55 |
dict( |
| 56 |
id=r'"C:\powershell" simple', |
| 57 |
input=dict( |
| 58 |
cmd=r""""C:\powershell" -c My-Command1 -Param 'param' """, |
| 59 |
shell="cmd", # the plugins does not care the shell type, as long as it is windows |
| 60 |
), |
| 61 |
output=[r"C:\powershell", "-c", "My-Command1 -Param 'param'"], |
| 62 |
), |
| 63 |
dict( |
| 64 |
id="powershell multiline", |
| 65 |
input=dict( |
| 66 |
cmd=( |
| 67 |
"powershell -ExecutionPolicy bypass -Command\n" |
| 68 |
"My-Command1 \\\n" |
| 69 |
" -Param 'param';\n" |
| 70 |
"My-Command2 \\\n" |
| 71 |
" -Param 'param'" |
| 72 |
), |
| 73 |
shell="powershell", |
| 74 |
), |
| 75 |
output=[ |
| 76 |
"powershell", |
| 77 |
"-ExecutionPolicy", |
| 78 |
"bypass", |
| 79 |
"-Command", |
| 80 |
"My-Command1 \\\n -Param 'param';\nMy-Command2 \\\n -Param 'param'", |
| 81 |
], |
| 82 |
), |
| 83 |
dict( |
| 84 |
id="cmd simple", |
| 85 |
input=dict( |
| 86 |
cmd="""CMD.EXE /C some-command /flag1 /flag2""", |
| 87 |
shell="powershell", |
| 88 |
), |
| 89 |
output=["CMD.EXE", "/C", "some-command /flag1 /flag2"], |
| 90 |
), |
| 91 |
dict( |
| 92 |
id=r"C:\cmd simple", |
| 93 |
input=dict( |
| 94 |
cmd=r"""C:\CMD.EXE /C some-command /flag1 /flag2""", |
| 95 |
shell="powershell", |
| 96 |
), |
| 97 |
output=[r"C:\CMD.EXE", "/C", "some-command /flag1 /flag2"], |
| 98 |
), |
| 99 |
dict( |
| 100 |
id=r'"C:\cmd" simple', |
| 101 |
input=dict( |
| 102 |
cmd=r""""C:\CMD" /c some-command /flag1 /flag2""", |
| 103 |
shell="powershell", |
| 104 |
), |
| 105 |
output=[r"C:\CMD", "/c", "some-command /flag1 /flag2"], |
| 106 |
), |
| 107 |
] |
| 108 |
|
| 109 |
|
| 110 |
BUILD_CMD_TEST_CASE_IDS: list[str] = [tc["id"] for tc in BUILD_CMD_TEST_CASES] |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.parametrize("testcase", BUILD_CMD_TEST_CASES, ids=BUILD_CMD_TEST_CASE_IDS) |
| 114 |
def test_build_command(mocker, testcase): |
| 115 |
mocker.patch("ansible.module_utils.common.process.get_bin_path").return_value = "/test/bin/incus" |
| 116 |
|
| 117 |
play_context = PlayContext() |
| 118 |
play_context.shell = testcase["input"].get("shell", "sh") |
| 119 |
in_stream = StringIO() |
| 120 |
conn = connection_loader.get("community.general.incus", play_context, in_stream) |
| 121 |
conn.set_option("remote_addr", "server1") |
| 122 |
conn.set_option("remote_user", "root") |
| 123 |
|
| 124 |
cli_preamble = [ |
| 125 |
"/test/bin/incus", |
| 126 |
"--project", |
| 127 |
"default", |
| 128 |
"exec", |
| 129 |
*(["-T"] if play_context.shell in ["cmd", "powershell"] else []), |
| 130 |
"local:server1", |
| 131 |
"--", |
| 132 |
] |
| 133 |
|
| 134 |
built = conn._build_command(testcase["input"]["cmd"]) |
| 135 |
tc_cmd = cli_preamble + testcase["output"] |
| 136 |
assert built == tc_cmd, f"\n built = {built}\ntestcase = {tc_cmd}" |