1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/plugins/module_utils/test_module_helper.py

108 lines
4.4 KiB
Python
Raw Normal View History

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
2020-11-20 11:27:53 +01:00
# -*- 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(
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
simple_boolean_true=("--superflag", ArgFormat.BOOLEAN, 0,
True, ["--superflag"]),
simple_boolean_false=("--superflag", ArgFormat.BOOLEAN, 0,
False, []),
simple_boolean_none=("--superflag", ArgFormat.BOOLEAN, 0,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
single_printf=("--param=%s", ArgFormat.PRINTF, 0,
"potatoes", ["--param=potatoes"]),
single_printf_no_substitution=("--param", ArgFormat.PRINTF, 0,
"potatoes", ["--param"]),
single_printf_none=("--param=%s", ArgFormat.PRINTF, 0,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
multiple_printf=(["--param", "free-%s"], ArgFormat.PRINTF, 0,
"potatoes", ["--param", "free-potatoes"]),
single_format=("--param={0}", ArgFormat.FORMAT, 0,
"potatoes", ["--param=potatoes"]),
single_format_none=("--param={0}", ArgFormat.FORMAT, 0,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
single_format_no_substitution=("--param", ArgFormat.FORMAT, 0,
"potatoes", ["--param"]),
multiple_format=(["--param", "free-{0}"], ArgFormat.FORMAT, 0,
"potatoes", ["--param", "free-potatoes"]),
multiple_format_none=(["--param", "free-{0}"], ArgFormat.FORMAT, 0,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
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_0star_none=((lambda v: ["piggies=[{0},{1},{2}]".format(v[0], v[1], v[2])]), None, 0,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
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_1star_none=((lambda a, b, c: ["piggies=[{0},{1},{2}]".format(a, b, c)]), None, 1,
None, []),
Modhelper improvements (#1480) * Improvements in module_helper - added `ModuleHelperException` to handle problems specific to ModuleHelper - updated `module_fails_on_exception` for `ModuleHelperException` - `StateMixin`: composed names of state methods are now calculated instead of fixed. - `CmdMixin`: added `run_command_fixed_options` to pass some parameters on every call * Improvements in module_helper - Named deprecations: ability to declare a `dict` of deprecations indexed by names, allowing module maintainer to trigger them by those names, and also allowing the module user to acknowledge them in a similar way. - Adding `ack_named_deprecations` to module's `argument_spec` when they exist. - Providing doc fragment for `ack_named_deprecations`. - Added method `__quit_module__` providing a hook for code that needs to be run when quitting the module. - Created convenience classes combining `ModuleHelper`, `StateMixin`, `CmdMixin`. * fixed validation * fixed validation * changelog fragment * Apply suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * Improvement on Named Deprecations Per the comments in PR, we want to expose a call to a ``deprecate`` method on the module code, so that pylint can properly perform its static analysis on deprecations. This prompted a revamp of the named deprecation feature. * Use .get() instead of [] for the param to ack named deprecations. * Changes from suggestions in the PR * removed named deprecations * Update changelogs/fragments/1480-module-helper-improvements.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/module_helper.py Co-authored-by: Felix Fontein <felix@fontein.de>
2021-01-15 21:29:23 +01:00
single_lambda_2star=(single_lambda_2star, None, 2,
dict(z='c', x='a', y='b'), ["piggies=[a,b,c]"]),
single_lambda_2star_none=(single_lambda_2star, None, 2,
None, []),
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
2020-11-20 11:27:53 +01:00
)
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