| 1 |
# Copyright (c) 2024, 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 os |
| 8 |
from unittest.mock import MagicMock, PropertyMock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import cmd_runner_fmt |
| 13 |
from ansible_collections.community.general.plugins.module_utils._python_runner import PythonRunner |
| 14 |
|
| 15 |
TC_RUNNER = dict( |
| 16 |
# SAMPLE: This shows all possible elements of a test case. It does not actually run. |
| 17 |
# |
| 18 |
# testcase_name=( |
| 19 |
# # input |
| 20 |
# dict( |
| 21 |
# args_bundle = dict( |
| 22 |
# param1=dict( |
| 23 |
# type="int", |
| 24 |
# value=11, |
| 25 |
# fmt_func=cmd_runner_fmt.as_opt_eq_val, |
| 26 |
# fmt_arg="--answer", |
| 27 |
# ), |
| 28 |
# param2=dict( |
| 29 |
# fmt_func=cmd_runner_fmt.as_bool, |
| 30 |
# fmt_arg="--bb-here", |
| 31 |
# ) |
| 32 |
# ), |
| 33 |
# runner_init_args = dict( |
| 34 |
# command="testing", |
| 35 |
# default_args_order=(), |
| 36 |
# check_rc=False, |
| 37 |
# force_lang="C", |
| 38 |
# path_prefix=None, |
| 39 |
# environ_update=None, |
| 40 |
# ), |
| 41 |
# runner_ctx_args = dict( |
| 42 |
# args_order=['aa', 'bb'], |
| 43 |
# output_process=None, |
| 44 |
# ignore_value_none=True, |
| 45 |
# ), |
| 46 |
# ), |
| 47 |
# # command execution |
| 48 |
# dict( |
| 49 |
# runner_ctx_run_args = dict(bb=True), |
| 50 |
# rc = 0, |
| 51 |
# out = "", |
| 52 |
# err = "", |
| 53 |
# ), |
| 54 |
# # expected |
| 55 |
# dict( |
| 56 |
# results=(), |
| 57 |
# run_info=dict( |
| 58 |
# cmd=['/mock/bin/testing', '--answer=11', '--bb-here'], |
| 59 |
# environ_update={'LANGUAGE': 'C', 'LC_ALL': 'C'}, |
| 60 |
# ), |
| 61 |
# exc=None, |
| 62 |
# ), |
| 63 |
# ), |
| 64 |
# |
| 65 |
aa_bb=( |
| 66 |
dict( |
| 67 |
args_bundle=dict( |
| 68 |
aa=dict(type="int", value=11, fmt_func=cmd_runner_fmt.as_opt_eq_val, fmt_arg="--answer"), |
| 69 |
bb=dict(fmt_func=cmd_runner_fmt.as_bool, fmt_arg="--bb-here"), |
| 70 |
), |
| 71 |
runner_init_args=dict(command="testing"), |
| 72 |
runner_ctx_args=dict(args_order=["aa", "bb"]), |
| 73 |
), |
| 74 |
dict(runner_ctx_run_args=dict(bb=True), rc=0, out="", err=""), |
| 75 |
dict( |
| 76 |
run_info=dict( |
| 77 |
cmd=["/mock/bin/python", "testing", "--answer=11", "--bb-here"], |
| 78 |
environ_update={"LANGUAGE": "C", "LC_ALL": "C"}, |
| 79 |
args_order=("aa", "bb"), |
| 80 |
), |
| 81 |
), |
| 82 |
), |
| 83 |
aa_bb_py3=( |
| 84 |
dict( |
| 85 |
args_bundle=dict( |
| 86 |
aa=dict(type="int", value=11, fmt_func=cmd_runner_fmt.as_opt_eq_val, fmt_arg="--answer"), |
| 87 |
bb=dict(fmt_func=cmd_runner_fmt.as_bool, fmt_arg="--bb-here"), |
| 88 |
), |
| 89 |
runner_init_args=dict(command="toasting", python="python3"), |
| 90 |
runner_ctx_args=dict(args_order=["aa", "bb"]), |
| 91 |
), |
| 92 |
dict(runner_ctx_run_args=dict(bb=True), rc=0, out="", err=""), |
| 93 |
dict( |
| 94 |
run_info=dict( |
| 95 |
cmd=["/mock/bin/python3", "toasting", "--answer=11", "--bb-here"], |
| 96 |
environ_update={"LANGUAGE": "C", "LC_ALL": "C"}, |
| 97 |
args_order=("aa", "bb"), |
| 98 |
), |
| 99 |
), |
| 100 |
), |
| 101 |
aa_bb_abspath=( |
| 102 |
dict( |
| 103 |
args_bundle=dict( |
| 104 |
aa=dict(type="int", value=11, fmt_func=cmd_runner_fmt.as_opt_eq_val, fmt_arg="--answer"), |
| 105 |
bb=dict(fmt_func=cmd_runner_fmt.as_bool, fmt_arg="--bb-here"), |
| 106 |
), |
| 107 |
runner_init_args=dict(command="toasting", python="/crazy/local/bin/python3"), |
| 108 |
runner_ctx_args=dict(args_order=["aa", "bb"]), |
| 109 |
), |
| 110 |
dict(runner_ctx_run_args=dict(bb=True), rc=0, out="", err=""), |
| 111 |
dict( |
| 112 |
run_info=dict( |
| 113 |
cmd=["/crazy/local/bin/python3", "toasting", "--answer=11", "--bb-here"], |
| 114 |
environ_update={"LANGUAGE": "C", "LC_ALL": "C"}, |
| 115 |
args_order=("aa", "bb"), |
| 116 |
), |
| 117 |
), |
| 118 |
), |
| 119 |
aa_bb_venv=( |
| 120 |
dict( |
| 121 |
args_bundle=dict( |
| 122 |
aa=dict(type="int", value=11, fmt_func=cmd_runner_fmt.as_opt_eq_val, fmt_arg="--answer"), |
| 123 |
bb=dict(fmt_func=cmd_runner_fmt.as_bool, fmt_arg="--bb-here"), |
| 124 |
), |
| 125 |
runner_init_args=dict(command="toasting", venv="/venv"), |
| 126 |
runner_ctx_args=dict(args_order=["aa", "bb"]), |
| 127 |
), |
| 128 |
dict(runner_ctx_run_args=dict(bb=True), rc=0, out="", err=""), |
| 129 |
dict( |
| 130 |
run_info=dict( |
| 131 |
cmd=["/venv/bin/python", "toasting", "--answer=11", "--bb-here"], |
| 132 |
environ_update={"LANGUAGE": "C", "LC_ALL": "C", "VIRTUAL_ENV": "/venv", "PATH": "/venv/bin"}, |
| 133 |
args_order=("aa", "bb"), |
| 134 |
), |
| 135 |
), |
| 136 |
), |
| 137 |
) |
| 138 |
TC_RUNNER_IDS = sorted(TC_RUNNER.keys()) |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
"runner_input, cmd_execution, expected", (TC_RUNNER[tc] for tc in TC_RUNNER_IDS), ids=TC_RUNNER_IDS |
| 143 |
) |
| 144 |
def test_runner_context(runner_input, cmd_execution, expected): |
| 145 |
arg_spec = {} |
| 146 |
params = {} |
| 147 |
arg_formats = {} |
| 148 |
for k, v in runner_input["args_bundle"].items(): |
| 149 |
try: |
| 150 |
arg_spec[k] = {"type": v["type"]} |
| 151 |
except KeyError: |
| 152 |
pass |
| 153 |
try: |
| 154 |
params[k] = v["value"] |
| 155 |
except KeyError: |
| 156 |
pass |
| 157 |
try: |
| 158 |
arg_formats[k] = v["fmt_func"](v["fmt_arg"]) |
| 159 |
except KeyError: |
| 160 |
pass |
| 161 |
|
| 162 |
orig_results = tuple(cmd_execution[x] for x in ("rc", "out", "err")) |
| 163 |
|
| 164 |
print(f"arg_spec={arg_spec}\nparams={params}\narg_formats={arg_formats}\n") |
| 165 |
|
| 166 |
module = MagicMock() |
| 167 |
type(module).argument_spec = PropertyMock(return_value=arg_spec) |
| 168 |
type(module).params = PropertyMock(return_value=params) |
| 169 |
module.get_bin_path.return_value = os.path.join( |
| 170 |
runner_input["runner_init_args"].get("venv", "/mock"), |
| 171 |
"bin", |
| 172 |
runner_input["runner_init_args"].get("python", "python"), |
| 173 |
) |
| 174 |
module.run_command.return_value = orig_results |
| 175 |
|
| 176 |
runner = PythonRunner(module=module, arg_formats=arg_formats, **runner_input["runner_init_args"]) |
| 177 |
|
| 178 |
def _extract_path(run_info): |
| 179 |
path = run_info.get("environ_update", {}).get("PATH") |
| 180 |
if path is not None: |
| 181 |
run_info["environ_update"] = {k: v for k, v in run_info["environ_update"].items() if k != "PATH"} |
| 182 |
return run_info, path |
| 183 |
|
| 184 |
def _assert_run_info_env_path(actual, expected): |
| 185 |
actual2 = set(actual.split(":")) |
| 186 |
assert expected in actual2, f"Missing expected path {expected} in output PATH: {actual}" |
| 187 |
|
| 188 |
def _assert_run_info(actual, expected): |
| 189 |
reduced = {k: actual[k] for k in expected.keys()} |
| 190 |
reduced, act_path = _extract_path(reduced) |
| 191 |
expected, exp_path = _extract_path(expected) |
| 192 |
if exp_path is not None: |
| 193 |
_assert_run_info_env_path(act_path, exp_path) |
| 194 |
assert reduced == expected, f"{reduced}" |
| 195 |
|
| 196 |
def _assert_run(expected, ctx, results): |
| 197 |
_assert_run_info(ctx.run_info, expected["run_info"]) |
| 198 |
assert results == expected.get("results", orig_results) |
| 199 |
|
| 200 |
exc = expected.get("exc") |
| 201 |
if exc: |
| 202 |
with pytest.raises(exc): |
| 203 |
with runner.context(**runner_input["runner_ctx_args"]) as ctx: |
| 204 |
results = ctx.run(**cmd_execution["runner_ctx_run_args"]) |
| 205 |
_assert_run(expected, ctx, results) |
| 206 |
|
| 207 |
else: |
| 208 |
with runner.context(**runner_input["runner_ctx_args"]) as ctx: |
| 209 |
results = ctx.run(**cmd_execution["runner_ctx_run_args"]) |
| 210 |
_assert_run(expected, ctx, results) |