mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
snap: using yaml-specified unit tests (#7191)
* snap: using yaml-specified unit tests * remove extraneous code
This commit is contained in:
parent
ce6b2bc362
commit
c2d3302fc4
1 changed files with 19 additions and 57 deletions
|
@ -7,25 +7,10 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from collections import namedtuple
|
|
||||||
from ansible_collections.community.general.plugins.modules import snap
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from .cmd_runner_test_utils import CmdRunnerTestHelper, ModuleTestCase, RunCmdCall
|
||||||
ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_command_calls"])
|
from ansible_collections.community.general.plugins.modules import snap as module
|
||||||
RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"])
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def patch_get_bin_path(mocker):
|
|
||||||
"""
|
|
||||||
Function used for mocking AnsibleModule.get_bin_path
|
|
||||||
"""
|
|
||||||
def mockie(self, path, *args, **kwargs):
|
|
||||||
return "/testbin/{0}".format(path)
|
|
||||||
mocker.patch("ansible.module_utils.basic.AnsibleModule.get_bin_path", mockie)
|
|
||||||
|
|
||||||
|
|
||||||
issue_6803_status_out = """Name Version Rev Tracking Publisher Notes
|
issue_6803_status_out = """Name Version Rev Tracking Publisher Notes
|
||||||
|
@ -457,50 +442,27 @@ TEST_CASES = [
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
TEST_CASES_IDS = [item.id for item in TEST_CASES]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("patch_ansible_module, testcase",
|
helper = CmdRunnerTestHelper(test_cases=TEST_CASES)
|
||||||
[[x.input, x] for x in TEST_CASES],
|
patch_bin = helper.cmd_fixture
|
||||||
ids=TEST_CASES_IDS,
|
|
||||||
indirect=["patch_ansible_module"])
|
|
||||||
@pytest.mark.usefixtures("patch_ansible_module")
|
@pytest.mark.parametrize('patch_ansible_module, testcase',
|
||||||
def test_snap(mocker, capfd, patch_get_bin_path, testcase):
|
helper.testcases_params, ids=helper.testcases_ids,
|
||||||
|
indirect=['patch_ansible_module'])
|
||||||
|
@pytest.mark.usefixtures('patch_ansible_module')
|
||||||
|
def test_module(mocker, capfd, patch_bin, testcase):
|
||||||
"""
|
"""
|
||||||
Run unit tests for test cases listen in TEST_CASES
|
Run unit tests for test cases listed in TEST_CASES
|
||||||
"""
|
"""
|
||||||
|
|
||||||
run_cmd_calls = testcase.run_command_calls
|
with helper(testcase, mocker) as ctx:
|
||||||
|
# Try to run test case
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
module.main()
|
||||||
|
|
||||||
# Mock function used for running commands first
|
out, err = capfd.readouterr()
|
||||||
call_results = [(x.rc, x.out, x.err) for x in run_cmd_calls]
|
results = json.loads(out)
|
||||||
mock_run_command = mocker.patch(
|
|
||||||
"ansible.module_utils.basic.AnsibleModule.run_command",
|
|
||||||
side_effect=call_results)
|
|
||||||
|
|
||||||
# Try to run test case
|
ctx.check_results(results)
|
||||||
with pytest.raises(SystemExit):
|
|
||||||
snap.main()
|
|
||||||
|
|
||||||
out, err = capfd.readouterr()
|
|
||||||
results = json.loads(out)
|
|
||||||
print("testcase =\n%s" % str(testcase))
|
|
||||||
print("results =\n%s" % results)
|
|
||||||
|
|
||||||
assert mock_run_command.call_count == len(run_cmd_calls)
|
|
||||||
if mock_run_command.call_count:
|
|
||||||
call_args_list = [(item[0][0], item[1]) for item in mock_run_command.call_args_list]
|
|
||||||
expected_call_args_list = [(item.command, item.environ) for item in run_cmd_calls]
|
|
||||||
print("call args list =\n%s" % call_args_list)
|
|
||||||
print("expected args list =\n%s" % expected_call_args_list)
|
|
||||||
try:
|
|
||||||
assert call_args_list == expected_call_args_list
|
|
||||||
except AssertionError:
|
|
||||||
for test_call_run, expected_call_run in zip(call_args_list, expected_call_args_list):
|
|
||||||
assert test_call_run == expected_call_run
|
|
||||||
|
|
||||||
assert results.get("changed", False) == testcase.output["changed"]
|
|
||||||
if "failed" in testcase:
|
|
||||||
assert results.get("failed", False) == testcase.output["failed"]
|
|
||||||
if "msg" in testcase:
|
|
||||||
assert results.get("msg", "") == testcase.output["msg"]
|
|
||||||
|
|
Loading…
Reference in a new issue