mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Adding module_utils/module_helper.py + big revamp in xfconf.py to use it (#1322)
* Big revamp in xfconf.py - added plugin/module_utils/module_helper.py - scaffold class for writing modules, beyond standard AnsibleModule - automatic capture of exceptions - easier dependency testing - StateMixin to easily handle different behaviours for 'state' param - CmdMixin to easily run external commands - adapted test_xfconf.py - the args for run_command are now lists instead of a string - value and previous_value were not being tested before (because xfconf wasn't filling results - see below) - added more tests: setting value to previous_value, getting non-existent property - rewritten xfconf module, keeping the same results - original module posted results as ansible_facts, this version still does it for compatibility, but also adds to the module result * Added suggestions from the PR * Added russoz as maintainer for the module_utils/module_helper.py file * Formatting using printf-style requires special treatment Strings not containing substitution tokens must work as well. * Tidied up variables in module definition * Tests with ArgFormat and DependencyCtxMgr * pytest parameters must be in the same order, it seems * improved testing for the DependencyCtxMgr * fixed test for older pythons * Moved changed property to improve readability * Added testcase for state: absent and adjusted xfconf after it * Fixed param name environ_update in run_command() * added changelog fragment * fixed tests after run_command param change
This commit is contained in:
parent
2ebf2861b6
commit
e3fcc7de2a
6 changed files with 693 additions and 188 deletions
84
tests/unit/plugins/module_utils/test_module_helper.py
Normal file
84
tests/unit/plugins/module_utils/test_module_helper.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2020, Alexei Znamensky <russoz@gmail.com>
|
||||
# Copyright (c) 2020 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.module_helper import (
|
||||
ArgFormat, DependencyCtxMgr, ModuleHelper
|
||||
)
|
||||
|
||||
|
||||
def single_lambda_2star(x, y, z):
|
||||
return ["piggies=[{0},{1},{2}]".format(x, y, z)]
|
||||
|
||||
|
||||
ARG_FORMATS = dict(
|
||||
simple_boolean_true=("--superflag", ArgFormat.BOOLEAN, 0, True, ["--superflag"]),
|
||||
simple_boolean_false=("--superflag", ArgFormat.BOOLEAN, 0, False, []),
|
||||
single_printf=("--param=%s", ArgFormat.PRINTF, 0, "potatoes", ["--param=potatoes"]),
|
||||
single_printf_no_substitution=("--param", ArgFormat.PRINTF, 0, "potatoes", ["--param"]),
|
||||
multiple_printf=(["--param", "free-%s"], ArgFormat.PRINTF, 0, "potatoes", ["--param", "free-potatoes"]),
|
||||
single_format=("--param={0}", ArgFormat.FORMAT, 0, "potatoes", ["--param=potatoes"]),
|
||||
single_format_no_substitution=("--param", ArgFormat.FORMAT, 0, "potatoes", ["--param"]),
|
||||
multiple_format=(["--param", "free-{0}"], ArgFormat.FORMAT, 0, "potatoes", ["--param", "free-potatoes"]),
|
||||
single_lambda_0star=((lambda v: ["piggies=[{0},{1},{2}]".format(v[0], v[1], v[2])]),
|
||||
None, 0, ['a', 'b', 'c'], ["piggies=[a,b,c]"]),
|
||||
single_lambda_1star=((lambda a, b, c: ["piggies=[{0},{1},{2}]".format(a, b, c)]),
|
||||
None, 1, ['a', 'b', 'c'], ["piggies=[a,b,c]"]),
|
||||
single_lambda_2star=(single_lambda_2star, None, 2, dict(z='c', x='a', y='b'), ["piggies=[a,b,c]"])
|
||||
)
|
||||
ARG_FORMATS_IDS = sorted(ARG_FORMATS.keys())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fmt, style, stars, value, expected',
|
||||
(ARG_FORMATS[tc] for tc in ARG_FORMATS_IDS),
|
||||
ids=ARG_FORMATS_IDS)
|
||||
def test_arg_format(fmt, style, stars, value, expected):
|
||||
af = ArgFormat('name', fmt, style, stars)
|
||||
actual = af.to_text(value)
|
||||
print("formatted string = {0}".format(actual))
|
||||
assert actual == expected
|
||||
|
||||
|
||||
ARG_FORMATS_FAIL = dict(
|
||||
int_fmt=(3, None, 0, "", [""]),
|
||||
bool_fmt=(True, None, 0, "", [""]),
|
||||
)
|
||||
ARG_FORMATS_FAIL_IDS = sorted(ARG_FORMATS_FAIL.keys())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fmt, style, stars, value, expected',
|
||||
(ARG_FORMATS_FAIL[tc] for tc in ARG_FORMATS_FAIL_IDS),
|
||||
ids=ARG_FORMATS_FAIL_IDS)
|
||||
def test_arg_format_fail(fmt, style, stars, value, expected):
|
||||
with pytest.raises(TypeError):
|
||||
af = ArgFormat('name', fmt, style, stars)
|
||||
actual = af.to_text(value)
|
||||
print("formatted string = {0}".format(actual))
|
||||
|
||||
|
||||
def test_dependency_ctxmgr():
|
||||
ctx = DependencyCtxMgr("POTATOES", "Potatoes must be installed")
|
||||
with ctx:
|
||||
import potatoes_that_will_never_be_there
|
||||
print("POTATOES: ctx.text={0}".format(ctx.text))
|
||||
assert ctx.text == "Potatoes must be installed"
|
||||
assert not ctx.has_it
|
||||
|
||||
ctx = DependencyCtxMgr("POTATOES2")
|
||||
with ctx:
|
||||
import potatoes_that_will_never_be_there_again
|
||||
assert not ctx.has_it
|
||||
print("POTATOES2: ctx.text={0}".format(ctx.text))
|
||||
assert ctx.text.startswith("No module named")
|
||||
assert "potatoes_that_will_never_be_there_again" in ctx.text
|
||||
|
||||
ctx = DependencyCtxMgr("TYPING")
|
||||
with ctx:
|
||||
import sys
|
||||
assert ctx.has_it
|
|
@ -9,7 +9,6 @@ __metaclass__ = type
|
|||
|
||||
import json
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible_collections.community.general.plugins.modules.system import xfconf
|
||||
|
||||
import pytest
|
||||
|
@ -22,7 +21,7 @@ def patch_xfconf(mocker):
|
|||
"""
|
||||
Function used for mocking some parts of redhat_subscribtion module
|
||||
"""
|
||||
mocker.patch('ansible_collections.community.general.plugins.modules.system.xfconf.AnsibleModule.get_bin_path',
|
||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.module_helper.AnsibleModule.get_bin_path',
|
||||
return_value='/testbin/xfconf-query')
|
||||
|
||||
|
||||
|
@ -41,7 +40,6 @@ def test_without_required_parameters(capfd, patch_xfconf):
|
|||
|
||||
|
||||
TEST_CASES = [
|
||||
# Test the case, when the system is already registered
|
||||
[
|
||||
{'channel': 'xfwm4', 'property': '/general/inactive_opacity', 'state': 'get'},
|
||||
{
|
||||
|
@ -49,15 +47,35 @@ TEST_CASES = [
|
|||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
'/testbin/xfconf-query --channel xfwm4 --property /general/inactive_opacity',
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/inactive_opacity'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '100/n', '',),
|
||||
(0, '100\n', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'value': '100'
|
||||
'previous_value': '100',
|
||||
'value': '100',
|
||||
}
|
||||
],
|
||||
[
|
||||
{'channel': 'xfwm4', 'property': '/general/i_dont_exist', 'state': 'get'},
|
||||
{
|
||||
'id': 'test_simple_property_get_nonexistent',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/i_dont_exist'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(1, '', 'Property "/general/i_dont_exist" does not exist on channel "xfwm4".\n',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'previous_value': None,
|
||||
'value': None,
|
||||
}
|
||||
],
|
||||
[
|
||||
|
@ -67,15 +85,16 @@ TEST_CASES = [
|
|||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
'/testbin/xfconf-query --channel xfwm4 --property /general/workspace_names',
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Value is an array with 3 items:\n\nMain\nWork\nTmp\n', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'value': ['Main', 'Work', 'Tmp']
|
||||
'previous_value': ['Main', 'Work', 'Tmp'],
|
||||
'value': ['Main', 'Work', 'Tmp'],
|
||||
},
|
||||
],
|
||||
[
|
||||
|
@ -85,15 +104,16 @@ TEST_CASES = [
|
|||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
'/testbin/xfconf-query --channel xfwm4 --property /general/use_compositing',
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/use_compositing'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'true', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'value': True
|
||||
'previous_value': 'true',
|
||||
'value': 'true',
|
||||
},
|
||||
],
|
||||
[
|
||||
|
@ -103,15 +123,84 @@ TEST_CASES = [
|
|||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
'/testbin/xfconf-query --channel xfwm4 --property /general/use_compositing',
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/use_compositing'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'false', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'value': False
|
||||
'previous_value': 'false',
|
||||
'value': 'false',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfwm4',
|
||||
'property': '/general/inactive_opacity',
|
||||
'state': 'present',
|
||||
'value_type': 'int',
|
||||
'value': 90,
|
||||
},
|
||||
{
|
||||
'id': 'test_property_set_property',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/inactive_opacity'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '100\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/inactive_opacity',
|
||||
'--create', '--type', 'int', '--set', '90'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': '100',
|
||||
'value': '90',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfwm4',
|
||||
'property': '/general/inactive_opacity',
|
||||
'state': 'present',
|
||||
'value_type': 'int',
|
||||
'value': 90,
|
||||
},
|
||||
{
|
||||
'id': 'test_property_set_property_same_value',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/inactive_opacity'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '90\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/inactive_opacity',
|
||||
'--create', '--type', 'int', '--set', '90'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'previous_value': '90',
|
||||
'value': '90',
|
||||
},
|
||||
],
|
||||
[
|
||||
|
@ -127,18 +216,19 @@ TEST_CASES = [
|
|||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
'/testbin/xfconf-query --channel xfwm4 --property /general/workspace_names',
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Value is an array with 3 items:\n\nMain\nWork\nTmp\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
"/testbin/xfconf-query --channel xfwm4 --property /general/workspace_names --create "
|
||||
"--force-array --type 'string' --set 'A' --type 'string' --set 'B' --type 'string' --set 'C'",
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names',
|
||||
'--create', '--force-array', '--type', 'string', '--set', 'A', '--type', 'string', '--set', 'B',
|
||||
'--type', 'string', '--set', 'C'],
|
||||
# Was return code checked?
|
||||
{'check_rc': False},
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
|
@ -148,6 +238,73 @@ TEST_CASES = [
|
|||
'value': ['A', 'B', 'C'],
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfwm4',
|
||||
'property': '/general/workspace_names',
|
||||
'state': 'present',
|
||||
'value_type': 'string',
|
||||
'value': ['A', 'B', 'C'],
|
||||
},
|
||||
{
|
||||
'id': 'test_property_set_array_to_same_value',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Value is an array with 3 items:\n\nA\nB\nC\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names',
|
||||
'--create', '--force-array', '--type', 'string', '--set', 'A', '--type', 'string', '--set', 'B',
|
||||
'--type', 'string', '--set', 'C'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'previous_value': ['A', 'B', 'C'],
|
||||
'value': ['A', 'B', 'C'],
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfwm4',
|
||||
'property': '/general/workspace_names',
|
||||
'state': 'absent',
|
||||
},
|
||||
{
|
||||
'id': 'test_property_reset_value',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Value is an array with 3 items:\n\nA\nB\nC\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfwm4', '--property', '/general/workspace_names',
|
||||
'--reset'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': ['A', 'B', 'C'],
|
||||
'value': None,
|
||||
},
|
||||
],
|
||||
]
|
||||
TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES]
|
||||
|
||||
|
@ -165,7 +322,7 @@ def test_xfconf(mocker, capfd, patch_xfconf, testcase):
|
|||
# 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.modules.system.xfconf.AnsibleModule.run_command',
|
||||
'ansible_collections.community.general.plugins.module_utils.module_helper.AnsibleModule.run_command',
|
||||
side_effect=call_results)
|
||||
|
||||
# Try to run test case
|
||||
|
@ -174,7 +331,8 @@ def test_xfconf(mocker, capfd, patch_xfconf, testcase):
|
|||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
print("results = %s" % results)
|
||||
print("testcase =\n%s" % testcase)
|
||||
print("results =\n%s" % results)
|
||||
assert 'changed' in results
|
||||
assert results['changed'] == testcase['changed']
|
||||
if 'msg' in results:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue