From 40cad3e7a9125de5f2dba6c0123c2d1b2598f58b Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:30:15 +1200 Subject: [PATCH] gconftool2: using yaml-specified unit tests (#7196) * gconftool2: using yaml-specified unit tests * gconftool2_info: using yaml-specified unit tests * adjust code for skip and xfail --- .../plugins/modules/cmd_runner_test_utils.py | 30 ++- tests/unit/plugins/modules/test_gconftool2.py | 200 +----------------- .../unit/plugins/modules/test_gconftool2.yaml | 139 ++++++++++++ .../plugins/modules/test_gconftool2_info.py | 90 +------- .../plugins/modules/test_gconftool2_info.yaml | 28 +++ tests/unit/plugins/modules/test_snap.py | 2 + 6 files changed, 215 insertions(+), 274 deletions(-) create mode 100644 tests/unit/plugins/modules/test_gconftool2.yaml create mode 100644 tests/unit/plugins/modules/test_gconftool2_info.yaml diff --git a/tests/unit/plugins/modules/cmd_runner_test_utils.py b/tests/unit/plugins/modules/cmd_runner_test_utils.py index 83644a699f..62e3cb73cf 100644 --- a/tests/unit/plugins/modules/cmd_runner_test_utils.py +++ b/tests/unit/plugins/modules/cmd_runner_test_utils.py @@ -14,7 +14,7 @@ import pytest import yaml -ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_command_calls"]) +ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_command_calls", "flags"]) RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"]) @@ -42,8 +42,13 @@ class CmdRunnerTestHelper(object): results = [] for tc in test_cases: - tc["run_command_calls"] = [RunCmdCall(**r) for r in tc["run_command_calls"]] if tc.get("run_command_calls") else [] - + for tc_param in ["input", "output", "flags"]: + if not tc.get(tc_param): + tc[tc_param] = {} + if tc.get("run_command_calls"): + tc["run_command_calls"] = [RunCmdCall(**r) for r in tc["run_command_calls"]] + else: + tc["run_command_calls"] = [] results.append(ModuleTestCase(**tc)) return results @@ -85,7 +90,7 @@ class _Context(object): def __exit__(self, exc_type, exc_val, exc_tb): return False - def run(self): + def _run(self): with pytest.raises(SystemExit): self.helper.module_main() @@ -94,6 +99,23 @@ class _Context(object): self.check_results(results) + def test_flags(self, flag=None): + flags = self.testcase.flags + if flag: + flags = flags.get(flag) + return flags + + def run(self): + func = self._run + + test_flags = self.test_flags() + if test_flags.get("skip"): + pytest.skip(reason=test_flags["skip"]) + if test_flags.get("xfail"): + pytest.xfail(reason=test_flags["xfail"]) + + func() + def check_results(self, results): print("testcase =\n%s" % str(self.testcase)) print("results =\n%s" % results) diff --git a/tests/unit/plugins/modules/test_gconftool2.py b/tests/unit/plugins/modules/test_gconftool2.py index 32c71bcb3e..b63709a94e 100644 --- a/tests/unit/plugins/modules/test_gconftool2.py +++ b/tests/unit/plugins/modules/test_gconftool2.py @@ -6,206 +6,26 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - -from ansible_collections.community.general.plugins.modules import gconftool2 import pytest - -@pytest.fixture -def patch_gconftool2(mocker): - """ - Function used for mocking some parts of redhat_subscription module - """ - mocker.patch('ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.get_bin_path', - return_value='/testbin/gconftool-2') +from ansible_collections.community.general.plugins.modules import gconftool2 as module +from .cmd_runner_test_utils import CmdRunnerTestHelper -TEST_CASES = [ - [ - {'state': 'get', 'key': '/desktop/gnome/background/picture_filename'}, - { - 'id': 'test_simple_element_get', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '100\n', '',), - ), - ], - } - ], - [ - {'state': 'get', 'key': '/desktop/gnome/background/picture_filename'}, - { - 'id': 'test_simple_element_get_not_found', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', "No value set for `/desktop/gnome/background/picture_filename'\n",), - ), - ], - } - ], - [ - {'state': 'present', 'key': '/desktop/gnome/background/picture_filename', 'value': 200, 'value_type': 'int'}, - { - 'id': 'test_simple_element_set', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '100\n', '',), - ), - ( - ['/testbin/gconftool-2', '--type', 'int', '--set', '/desktop/gnome/background/picture_filename', '200'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '200\n', '',), - ), - ], - 'new_value': '200', - 'changed': True, - } - ], - [ - {'state': 'present', 'key': '/desktop/gnome/background/picture_filename', 'value': 200, 'value_type': 'int'}, - { - 'id': 'test_simple_element_set_idempotency_int', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '200\n', '',), - ), - ( - ['/testbin/gconftool-2', '--type', 'int', '--set', '/desktop/gnome/background/picture_filename', '200'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '200\n', '',), - ), - ], - 'new_value': '200', - 'changed': False, - } - ], - [ - {'state': 'present', 'key': '/apps/gnome_settings_daemon/screensaver/start_screensaver', 'value': 'false', 'value_type': 'bool'}, - { - 'id': 'test_simple_element_set_idempotency_bool', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/apps/gnome_settings_daemon/screensaver/start_screensaver'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, 'false\n', '',), - ), - ( - ['/testbin/gconftool-2', '--type', 'bool', '--set', '/apps/gnome_settings_daemon/screensaver/start_screensaver', 'false'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ( - ['/testbin/gconftool-2', '--get', '/apps/gnome_settings_daemon/screensaver/start_screensaver'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, 'false\n', '',), - ), - ], - 'new_value': 'false', - 'changed': False, - } - ], - [ - {'state': 'absent', 'key': '/desktop/gnome/background/picture_filename'}, - { - 'id': 'test_simple_element_unset', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '200\n', '',), - ), - ( - ['/testbin/gconftool-2', '--unset', '/desktop/gnome/background/picture_filename'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ], - 'changed': True, - } - ], - [ - {'state': 'absent', 'key': '/apps/gnome_settings_daemon/screensaver/start_screensaver'}, - { - 'id': 'test_simple_element_unset_idempotency', - 'run_command.calls': [ - ( - ['/testbin/gconftool-2', '--get', '/apps/gnome_settings_daemon/screensaver/start_screensaver'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ( - ['/testbin/gconftool-2', '--unset', '/apps/gnome_settings_daemon/screensaver/start_screensaver'], - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - (0, '', '',), - ), - ], - 'changed': False, - } - ], -] -TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES] +with open("tests/unit/plugins/modules/test_gconftool2.yaml", "r") as TEST_CASES: + helper = CmdRunnerTestHelper(module.main, test_cases=TEST_CASES) + patch_bin = helper.cmd_fixture @pytest.mark.parametrize('patch_ansible_module, testcase', - TEST_CASES, - ids=TEST_CASES_IDS, + helper.testcases_params, ids=helper.testcases_ids, indirect=['patch_ansible_module']) @pytest.mark.usefixtures('patch_ansible_module') -def test_gconftool2(mocker, capfd, patch_gconftool2, testcase): +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 """ - # Mock function used for running commands first - call_results = [item[2] for item in testcase['run_command.calls']] - mock_run_command = mocker.patch( - 'ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.run_command', - side_effect=call_results) - - # Try to run test case - with pytest.raises(SystemExit): - gconftool2.main() - - out, err = capfd.readouterr() - results = json.loads(out) - print("testcase =\n%s" % testcase) - print("results =\n%s" % results) - - if 'changed' in testcase: - assert results.get('changed', False) == testcase['changed'] - if 'new_value' in testcase: - assert results.get('new_value', None) == testcase['new_value'] - - for conditional_test_result in ('value',): - if conditional_test_result in testcase: - assert conditional_test_result in results, "'{0}' not found in {1}".format(conditional_test_result, results) - assert results[conditional_test_result] == testcase[conditional_test_result], \ - "'{0}': '{1}' != '{2}'".format(conditional_test_result, results[conditional_test_result], testcase[conditional_test_result]) - - assert mock_run_command.call_count == len(testcase['run_command.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[0], item[1]) for item in testcase['run_command.calls']] - print("call args list =\n%s" % call_args_list) - print("expected args list =\n%s" % expected_call_args_list) - assert call_args_list == expected_call_args_list + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run() diff --git a/tests/unit/plugins/modules/test_gconftool2.yaml b/tests/unit/plugins/modules/test_gconftool2.yaml new file mode 100644 index 0000000000..242daca6af --- /dev/null +++ b/tests/unit/plugins/modules/test_gconftool2.yaml @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Alexei Znamensky (russoz@gmail.com) +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +--- +- id: test_simple_element_get + input: + state: get + key: /desktop/gnome/background/picture_filename + output: {} + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "100\n" + err: "" +- id: test_simple_element_get_not_found + input: + state: get + key: /desktop/gnome/background/picture_filename + output: {} + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "No value set for `/desktop/gnome/background/picture_filename'\n" +- id: test_simple_element_set + input: + state: present + key: /desktop/gnome/background/picture_filename + value: 200 + value_type: int + output: + new_value: '200' + changed: true + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "100\n" + err: "" + - command: [/testbin/gconftool-2, --type, int, --set, /desktop/gnome/background/picture_filename, "200"] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "200\n" + err: "" +- id: test_simple_element_set_idempotency_int + input: + state: present + key: /desktop/gnome/background/picture_filename + value: 200 + value_type: int + output: + new_value: '200' + changed: false + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "200\n" + err: "" + - command: [/testbin/gconftool-2, --type, int, --set, /desktop/gnome/background/picture_filename, "200"] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "200\n" + err: "" +- id: test_simple_element_set_idempotency_bool + input: + state: present + key: /apps/gnome_settings_daemon/screensaver/start_screensaver + value: false + value_type: bool + output: + new_value: 'false' + changed: false + run_command_calls: + - command: [/testbin/gconftool-2, --get, /apps/gnome_settings_daemon/screensaver/start_screensaver] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "false\n" + err: "" + - command: [/testbin/gconftool-2, --type, bool, --set, /apps/gnome_settings_daemon/screensaver/start_screensaver, "False"] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" + - command: [/testbin/gconftool-2, --get, /apps/gnome_settings_daemon/screensaver/start_screensaver] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "false\n" + err: "" +- id: test_simple_element_unset + input: + state: absent + key: /desktop/gnome/background/picture_filename + output: + new_value: null + changed: true + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "200\n" + err: "" + - command: [/testbin/gconftool-2, --unset, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" +- id: test_simple_element_unset_idempotency + input: + state: absent + key: /apps/gnome_settings_daemon/screensaver/start_screensaver + output: + new_value: null + changed: false + run_command_calls: + - command: [/testbin/gconftool-2, --get, /apps/gnome_settings_daemon/screensaver/start_screensaver] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" + - command: [/testbin/gconftool-2, --unset, /apps/gnome_settings_daemon/screensaver/start_screensaver] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "" diff --git a/tests/unit/plugins/modules/test_gconftool2_info.py b/tests/unit/plugins/modules/test_gconftool2_info.py index 08508cadfb..e7b99f22a0 100644 --- a/tests/unit/plugins/modules/test_gconftool2_info.py +++ b/tests/unit/plugins/modules/test_gconftool2_info.py @@ -6,96 +6,26 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - -from ansible_collections.community.general.plugins.modules import gconftool2_info import pytest - -@pytest.fixture -def patch_gconftool2_info(mocker): - """ - Function used for mocking some parts of redhat_subscription module - """ - mocker.patch('ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.get_bin_path', - return_value='/testbin/gconftool-2') +from ansible_collections.community.general.plugins.modules import gconftool2_info +from .cmd_runner_test_utils import CmdRunnerTestHelper -TEST_CASES = [ - [ - {'key': '/desktop/gnome/background/picture_filename'}, - { - 'id': 'test_simple_element_get', - 'run_command.calls': [ - ( - # Calling of following command will be asserted - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - # Was return code checked? - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - # Mock of returned code, stdout and stderr - (0, '100\n', '',), - ), - ], - 'value': '100', - } - ], - [ - {'key': '/desktop/gnome/background/picture_filename'}, - { - 'id': 'test_simple_element_get_not_found', - 'run_command.calls': [ - ( - # Calling of following command will be asserted - ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], - # Was return code checked? - {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, - # Mock of returned code, stdout and stderr - (0, '', "No value set for `/desktop/gnome/background/picture_filename'\n",), - ), - ], - 'value': None, - } - ], -] -TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES] +with open("tests/unit/plugins/modules/test_gconftool2_info.yaml", "r") as TEST_CASES: + helper = CmdRunnerTestHelper(gconftool2_info.main, test_cases=TEST_CASES) + patch_bin = helper.cmd_fixture @pytest.mark.parametrize('patch_ansible_module, testcase', - TEST_CASES, - ids=TEST_CASES_IDS, + helper.testcases_params, ids=helper.testcases_ids, indirect=['patch_ansible_module']) @pytest.mark.usefixtures('patch_ansible_module') -def test_gconftool2_info(mocker, capfd, patch_gconftool2_info, testcase): +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 """ - # Mock function used for running commands first - call_results = [item[2] for item in testcase['run_command.calls']] - mock_run_command = mocker.patch( - 'ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.run_command', - side_effect=call_results) - - # Try to run test case - with pytest.raises(SystemExit): - gconftool2_info.main() - - out, err = capfd.readouterr() - results = json.loads(out) - print("testcase =\n%s" % testcase) - print("results =\n%s" % results) - - for conditional_test_result in ('value',): - if conditional_test_result in testcase: - assert conditional_test_result in results, "'{0}' not found in {1}".format(conditional_test_result, results) - assert results[conditional_test_result] == testcase[conditional_test_result], \ - "'{0}': '{1}' != '{2}'".format(conditional_test_result, results[conditional_test_result], testcase[conditional_test_result]) - - assert mock_run_command.call_count == len(testcase['run_command.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[0], item[1]) for item in testcase['run_command.calls']] - print("call args list =\n%s" % call_args_list) - print("expected args list =\n%s" % expected_call_args_list) - assert call_args_list == expected_call_args_list + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run() diff --git a/tests/unit/plugins/modules/test_gconftool2_info.yaml b/tests/unit/plugins/modules/test_gconftool2_info.yaml new file mode 100644 index 0000000000..52e2fb0844 --- /dev/null +++ b/tests/unit/plugins/modules/test_gconftool2_info.yaml @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Alexei Znamensky (russoz@gmail.com) +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +--- +- id: test_simple_element_get + input: + key: /desktop/gnome/background/picture_filename + output: + value: '100' + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "100\n" + err: "" +- id: test_simple_element_get_not_found + input: + key: /desktop/gnome/background/picture_filename + output: + value: null + run_command_calls: + - command: [/testbin/gconftool-2, --get, /desktop/gnome/background/picture_filename] + environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "" + err: "No value set for `/desktop/gnome/background/picture_filename'\n" diff --git a/tests/unit/plugins/modules/test_snap.py b/tests/unit/plugins/modules/test_snap.py index b32214b6a4..1286715c54 100644 --- a/tests/unit/plugins/modules/test_snap.py +++ b/tests/unit/plugins/modules/test_snap.py @@ -381,6 +381,7 @@ TEST_CASES = [ id="simple case", input={"name": ["hello-world"]}, output=dict(changed=True, snaps_installed=["hello-world"]), + flags={}, run_command_calls=[ RunCmdCall( command=['/testbin/snap', 'info', 'hello-world'], @@ -409,6 +410,7 @@ TEST_CASES = [ id="issue_6803", input={"name": ["microk8s", "kubectl"], "classic": True}, output=dict(changed=True, snaps_installed=["microk8s", "kubectl"]), + flags={}, run_command_calls=[ RunCmdCall( command=['/testbin/snap', 'info', 'microk8s', 'kubectl'],