mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-30 02:16:50 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
839336c21d | ||
|
|
2a5e4b8a46 | ||
|
|
fae1dbc198 | ||
|
|
dc7aba3bca | ||
|
|
03c66d0db4 | ||
|
|
5707fc1b33 | ||
|
|
830e3988aa | ||
|
|
f4ccef462e | ||
|
|
cd26dd6b0a | ||
|
|
009bfd786d | ||
|
|
9f08cc11a8 | ||
|
|
cae4bc80af | ||
|
|
058462973e | ||
|
|
23dc90812f | ||
|
|
97e17fcee2 | ||
|
|
d75d5f67f7 | ||
|
|
c597a75372 | ||
|
|
031a3b56bf |
@@ -231,8 +231,8 @@ stages:
|
||||
targets:
|
||||
- name: RHEL 9.0
|
||||
test: rhel/9.0
|
||||
- name: FreeBSD 12.3
|
||||
test: freebsd/12.3
|
||||
#- name: FreeBSD 12.4
|
||||
# test: freebsd/12.4
|
||||
groups:
|
||||
- 1
|
||||
- 2
|
||||
@@ -249,8 +249,8 @@ stages:
|
||||
test: macos/12.0
|
||||
- name: RHEL 8.5
|
||||
test: rhel/8.5
|
||||
- name: FreeBSD 13.0
|
||||
test: freebsd/13.0
|
||||
#- name: FreeBSD 13.1
|
||||
# test: freebsd/13.1
|
||||
groups:
|
||||
- 1
|
||||
- 2
|
||||
|
||||
@@ -6,6 +6,29 @@ Community General Release Notes
|
||||
|
||||
This changelog describes changes after version 5.0.0.
|
||||
|
||||
v6.6.5
|
||||
======
|
||||
|
||||
Release Summary
|
||||
---------------
|
||||
|
||||
Regular bugfix release.
|
||||
|
||||
Minor Changes
|
||||
-------------
|
||||
|
||||
- make - allows ``params`` to be used without value (https://github.com/ansible-collections/community.general/pull/7180).
|
||||
- pritunl module utils - ensure ``validate_certs`` parameter is honoured in all methods (https://github.com/ansible-collections/community.general/pull/7156).
|
||||
|
||||
Bugfixes
|
||||
--------
|
||||
|
||||
- CmdRunner module utils - does not attempt to resolve path if executable is a relative or absolute path (https://github.com/ansible-collections/community.general/pull/7200).
|
||||
- lxc connection plugin - now handles ``remote_addr`` defaulting to ``inventory_hostname`` correctly (https://github.com/ansible-collections/community.general/pull/7104).
|
||||
- nsupdate - fix a possible ``list index out of range`` exception (https://github.com/ansible-collections/community.general/issues/836).
|
||||
- oci_utils module util - fix inappropriate logical comparison expressions and makes them simpler. The previous checks had logical short circuits (https://github.com/ansible-collections/community.general/pull/7125).
|
||||
- pritunl module utils - fix incorrect URL parameter for orgnization add method (https://github.com/ansible-collections/community.general/pull/7161).
|
||||
|
||||
v6.6.4
|
||||
======
|
||||
|
||||
|
||||
@@ -1530,3 +1530,30 @@ releases:
|
||||
- 7081-redfish-utils-fix-for-storagecontrollers-deprecated-key.yaml
|
||||
- 7085-sanity.yml
|
||||
release_date: '2023-08-13'
|
||||
6.6.5:
|
||||
changes:
|
||||
bugfixes:
|
||||
- CmdRunner module utils - does not attempt to resolve path if executable is
|
||||
a relative or absolute path (https://github.com/ansible-collections/community.general/pull/7200).
|
||||
- lxc connection plugin - now handles ``remote_addr`` defaulting to ``inventory_hostname``
|
||||
correctly (https://github.com/ansible-collections/community.general/pull/7104).
|
||||
- nsupdate - fix a possible ``list index out of range`` exception (https://github.com/ansible-collections/community.general/issues/836).
|
||||
- oci_utils module util - fix inappropriate logical comparison expressions and
|
||||
makes them simpler. The previous checks had logical short circuits (https://github.com/ansible-collections/community.general/pull/7125).
|
||||
- pritunl module utils - fix incorrect URL parameter for orgnization add method
|
||||
(https://github.com/ansible-collections/community.general/pull/7161).
|
||||
minor_changes:
|
||||
- make - allows ``params`` to be used without value (https://github.com/ansible-collections/community.general/pull/7180).
|
||||
- pritunl module utils - ensure ``validate_certs`` parameter is honoured in
|
||||
all methods (https://github.com/ansible-collections/community.general/pull/7156).
|
||||
release_summary: Regular bugfix release.
|
||||
fragments:
|
||||
- 6.6.5.yml
|
||||
- 7104_fix_lxc_remoteaddr_default.yml
|
||||
- 7125-fix-inappropriate-comparison.yml
|
||||
- 7156-ensure-validate-certs-parameter-is-honoured.yml
|
||||
- 7161-fix-incorrect-post-parameter.yml
|
||||
- 7180-make_params_without_value.yml
|
||||
- 7200-cmd-runner-abs-path.yml
|
||||
- 7219-fix-nsupdate-cname.yaml
|
||||
release_date: '2023-09-11'
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
namespace: community
|
||||
name: general
|
||||
version: 6.6.4
|
||||
version: 6.6.5
|
||||
readme: README.md
|
||||
authors:
|
||||
- Ansible (https://github.com/ansible)
|
||||
|
||||
@@ -19,6 +19,7 @@ DOCUMENTATION = '''
|
||||
- Container identifier
|
||||
default: inventory_hostname
|
||||
vars:
|
||||
- name: inventory_hostname
|
||||
- name: ansible_host
|
||||
- name: ansible_lxc_host
|
||||
executable:
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
from functools import wraps
|
||||
|
||||
from ansible.module_utils.common.collections import is_sequence
|
||||
@@ -199,12 +200,17 @@ class CmdRunner(object):
|
||||
environ_update = {}
|
||||
self.environ_update = environ_update
|
||||
|
||||
self.command[0] = module.get_bin_path(self.command[0], opt_dirs=path_prefix, required=True)
|
||||
_cmd = self.command[0]
|
||||
self.command[0] = _cmd if (os.path.isabs(_cmd) or '/' in _cmd) else module.get_bin_path(_cmd, opt_dirs=path_prefix, required=True)
|
||||
|
||||
for mod_param_name, spec in iteritems(module.argument_spec):
|
||||
if mod_param_name not in self.arg_formats:
|
||||
self.arg_formats[mod_param_name] = _Format.as_default_type(spec.get('type', 'str'), mod_param_name)
|
||||
|
||||
@property
|
||||
def binary(self):
|
||||
return self.command[0]
|
||||
|
||||
def __call__(self, args_order=None, output_process=None, ignore_value_none=True, check_mode_skip=False, check_mode_return=None, **kwargs):
|
||||
if output_process is None:
|
||||
output_process = _process_as_is
|
||||
|
||||
@@ -79,7 +79,7 @@ def _post_pritunl_organization(
|
||||
api_secret=api_secret,
|
||||
base_url=base_url,
|
||||
method="POST",
|
||||
path="/organization/%s",
|
||||
path="/organization",
|
||||
headers={"Content-Type": "application/json"},
|
||||
data=json.dumps(organization_data),
|
||||
validate_certs=validate_certs,
|
||||
@@ -220,7 +220,7 @@ def post_pritunl_organization(
|
||||
api_secret=api_secret,
|
||||
base_url=base_url,
|
||||
organization_data={"name": organization_name},
|
||||
validate_certs=True,
|
||||
validate_certs=validate_certs,
|
||||
)
|
||||
|
||||
if response.getcode() != 200:
|
||||
@@ -248,7 +248,7 @@ def post_pritunl_user(
|
||||
base_url=base_url,
|
||||
organization_id=organization_id,
|
||||
user_data=user_data,
|
||||
validate_certs=True,
|
||||
validate_certs=validate_certs,
|
||||
)
|
||||
|
||||
if response.getcode() != 200:
|
||||
@@ -267,7 +267,7 @@ def post_pritunl_user(
|
||||
organization_id=organization_id,
|
||||
user_data=user_data,
|
||||
user_id=user_id,
|
||||
validate_certs=True,
|
||||
validate_certs=validate_certs,
|
||||
)
|
||||
|
||||
if response.getcode() != 200:
|
||||
@@ -287,7 +287,7 @@ def delete_pritunl_organization(
|
||||
api_secret=api_secret,
|
||||
base_url=base_url,
|
||||
organization_id=organization_id,
|
||||
validate_certs=True,
|
||||
validate_certs=validate_certs,
|
||||
)
|
||||
|
||||
if response.getcode() != 200:
|
||||
@@ -307,7 +307,7 @@ def delete_pritunl_user(
|
||||
base_url=base_url,
|
||||
organization_id=organization_id,
|
||||
user_id=user_id,
|
||||
validate_certs=True,
|
||||
validate_certs=validate_certs,
|
||||
)
|
||||
|
||||
if response.getcode() != 200:
|
||||
|
||||
@@ -561,7 +561,7 @@ def are_lists_equal(s, t):
|
||||
if s is None and t is None:
|
||||
return True
|
||||
|
||||
if (s is None and len(t) >= 0) or (t is None and len(s) >= 0) or (len(s) != len(t)):
|
||||
if s is None or t is None or (len(s) != len(t)):
|
||||
return False
|
||||
|
||||
if len(s) == 0:
|
||||
@@ -1026,10 +1026,7 @@ def check_if_user_value_matches_resources_attr(
|
||||
return
|
||||
|
||||
if (
|
||||
resources_value_for_attr is None
|
||||
and len(user_provided_value_for_attr) >= 0
|
||||
or user_provided_value_for_attr is None
|
||||
and len(resources_value_for_attr) >= 0
|
||||
resources_value_for_attr is None or user_provided_value_for_attr is None
|
||||
):
|
||||
res[0] = False
|
||||
return
|
||||
|
||||
@@ -154,7 +154,7 @@ def _get_ctl_binary(module):
|
||||
if ctl_binary is not None:
|
||||
return ctl_binary
|
||||
|
||||
module.fail_json(msg="Neither of apache2ctl nor apachctl found. At least one apache control binary is necessary.")
|
||||
module.fail_json(msg="Neither of apache2ctl nor apachectl found. At least one apache control binary is necessary.")
|
||||
|
||||
|
||||
def _module_is_enabled(module):
|
||||
|
||||
@@ -67,7 +67,7 @@ author:
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Set default client scopes on realm level
|
||||
community.general.keycloak_clientsecret_info:
|
||||
community.general.keycloak_clientscope_type:
|
||||
auth_client_id: admin-cli
|
||||
auth_keycloak_url: https://auth.example.com/auth
|
||||
auth_realm: master
|
||||
@@ -79,7 +79,7 @@ EXAMPLES = '''
|
||||
|
||||
|
||||
- name: Set default and optional client scopes on client level with token auth
|
||||
community.general.keycloak_clientsecret_info:
|
||||
community.general.keycloak_clientscope_type:
|
||||
auth_client_id: admin-cli
|
||||
auth_keycloak_url: https://auth.example.com/auth
|
||||
token: TOKEN
|
||||
|
||||
@@ -49,6 +49,7 @@ options:
|
||||
params:
|
||||
description:
|
||||
- Any extra parameters to pass to make.
|
||||
- If the value is empty, only the key will be used. For example, C(FOO:) will produce C(FOO), not C(FOO=).
|
||||
type: dict
|
||||
target:
|
||||
description:
|
||||
@@ -81,6 +82,18 @@ EXAMPLES = r'''
|
||||
chdir: /home/ubuntu/cool-project
|
||||
target: all
|
||||
file: /some-project/Makefile
|
||||
|
||||
- name: build arm64 kernel on FreeBSD, with 16 parallel jobs
|
||||
community.general.make:
|
||||
chdir: /usr/src
|
||||
jobs: 16
|
||||
target: buildkernel
|
||||
params:
|
||||
# This adds -DWITH_FDT to the command line:
|
||||
-DWITH_FDT:
|
||||
# The following adds TARGET=arm64 TARGET_ARCH=aarch64 to the command line:
|
||||
TARGET: arm64
|
||||
TARGET_ARCH: aarch64
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
@@ -174,7 +187,7 @@ def main():
|
||||
make_path = module.get_bin_path('make', required=True)
|
||||
make_target = module.params['target']
|
||||
if module.params['params'] is not None:
|
||||
make_parameters = [k + '=' + str(v) for k, v in iteritems(module.params['params'])]
|
||||
make_parameters = [k + (('=' + str(v)) if v is not None else '') for k, v in iteritems(module.params['params'])]
|
||||
else:
|
||||
make_parameters = []
|
||||
|
||||
|
||||
@@ -467,10 +467,8 @@ class RecordManager(object):
|
||||
if lookup.rcode() != dns.rcode.NOERROR:
|
||||
self.module.fail_json(msg='Failed to lookup TTL of existing matching record.')
|
||||
|
||||
if self.module.params['type'] == 'NS':
|
||||
current_ttl = lookup.answer[0].ttl if lookup.answer else lookup.authority[0].ttl
|
||||
else:
|
||||
current_ttl = lookup.answer[0].ttl
|
||||
current_ttl = lookup.answer[0].ttl if lookup.answer else lookup.authority[0].ttl
|
||||
|
||||
return current_ttl != self.module.params['ttl']
|
||||
|
||||
|
||||
|
||||
@@ -408,6 +408,14 @@ options:
|
||||
smbios:
|
||||
description:
|
||||
- Specifies SMBIOS type 1 fields.
|
||||
- "Comma separated, Base64 encoded (optional) SMBIOS properties:"
|
||||
- C([base64=<1|0>] [,family=<Base64 encoded string>])
|
||||
- C([,manufacturer=<Base64 encoded string>])
|
||||
- C([,product=<Base64 encoded string>])
|
||||
- C([,serial=<Base64 encoded string>])
|
||||
- C([,sku=<Base64 encoded string>])
|
||||
- C([,uuid=<UUID>])
|
||||
- C([,version=<Base64 encoded string>])
|
||||
type: str
|
||||
snapname:
|
||||
description:
|
||||
|
||||
@@ -21,6 +21,8 @@ from ansible_collections.community.general.plugins.module_utils.cmd_runner impor
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
cmd=dict(type="str", default="echo"),
|
||||
path_prefix=dict(type="str"),
|
||||
arg_formats=dict(type="dict", default={}),
|
||||
arg_order=dict(type="raw", required=True),
|
||||
arg_values=dict(type="dict", default={}),
|
||||
@@ -41,7 +43,7 @@ def main():
|
||||
|
||||
arg_formats[arg] = func(*args)
|
||||
|
||||
runner = CmdRunner(module, ['echo', '--'], arg_formats=arg_formats)
|
||||
runner = CmdRunner(module, [module.params["cmd"], '--'], arg_formats=arg_formats, path_prefix=module.params["path_prefix"])
|
||||
|
||||
with runner.context(p['arg_order'], check_mode_skip=p['check_mode_skip']) as ctx:
|
||||
result = ctx.run(**p['arg_values'])
|
||||
|
||||
7
tests/integration/targets/cmd_runner/meta/main.yml
Normal file
7
tests/integration/targets/cmd_runner/meta/main.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# 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
|
||||
|
||||
dependencies:
|
||||
- setup_remote_tmp_dir
|
||||
@@ -6,3 +6,4 @@
|
||||
ansible.builtin.include_tasks:
|
||||
file: test_cmd_echo.yml
|
||||
loop: "{{ cmd_echo_tests }}"
|
||||
when: item.condition | default(true) | bool
|
||||
|
||||
@@ -3,17 +3,26 @@
|
||||
# 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
|
||||
|
||||
- name: test cmd_echo [{{ item.name }}]
|
||||
cmd_echo:
|
||||
arg_formats: "{{ item.arg_formats|default(omit) }}"
|
||||
arg_order: "{{ item.arg_order }}"
|
||||
arg_values: "{{ item.arg_values|default(omit) }}"
|
||||
check_mode_skip: "{{ item.check_mode_skip|default(omit) }}"
|
||||
aa: "{{ item.aa|default(omit) }}"
|
||||
register: test_result
|
||||
check_mode: "{{ item.check_mode|default(omit) }}"
|
||||
ignore_errors: "{{ item.expect_error|default(omit) }}"
|
||||
- name: create copy of /bin/echo ({{ item.name }})
|
||||
ansible.builtin.copy:
|
||||
src: /bin/echo
|
||||
dest: "{{ item.copy_to }}/echo"
|
||||
mode: "755"
|
||||
when: item.copy_to is defined
|
||||
|
||||
- name: check results [{{ item.name }}]
|
||||
- name: test cmd_echo module ({{ item.name }})
|
||||
cmd_echo:
|
||||
cmd: "{{ item.cmd | default(omit) }}"
|
||||
path_prefix: "{{ item.path_prefix | default(omit) }}"
|
||||
arg_formats: "{{ item.arg_formats | default(omit) }}"
|
||||
arg_order: "{{ item.arg_order }}"
|
||||
arg_values: "{{ item.arg_values | default(omit) }}"
|
||||
check_mode_skip: "{{ item.check_mode_skip | default(omit) }}"
|
||||
aa: "{{ item.aa | default(omit) }}"
|
||||
register: test_result
|
||||
check_mode: "{{ item.check_mode | default(omit) }}"
|
||||
ignore_errors: "{{ item.expect_error | default(omit) }}"
|
||||
|
||||
- name: check results ({{ item.name }})
|
||||
assert:
|
||||
that: "{{ item.assertions }}"
|
||||
|
||||
@@ -138,3 +138,125 @@ cmd_echo_tests:
|
||||
- test_result.rc == 0
|
||||
- test_result.out == "-- --answer=11 --tt-arg potatoes\n"
|
||||
- test_result.err == ""
|
||||
|
||||
- name: use cmd echo
|
||||
cmd: echo
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
assertions:
|
||||
- test_result.rc == 0
|
||||
- test_result.out == "-- --answer=11 --tt-arg potatoes\n"
|
||||
- test_result.err == ""
|
||||
|
||||
- name: use cmd /bin/echo
|
||||
cmd: /bin/echo
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
assertions:
|
||||
- test_result.rc == 0
|
||||
- test_result.out == "-- --answer=11 --tt-arg potatoes\n"
|
||||
- test_result.err == ""
|
||||
|
||||
# this will not be in the regular set of paths get_bin_path() searches
|
||||
- name: use cmd {{ remote_tmp_dir }}/echo
|
||||
condition: >
|
||||
{{
|
||||
ansible_distribution != "MacOSX" and
|
||||
not (ansible_distribution == "CentOS" and ansible_distribution_major_version is version('7.0', '<'))
|
||||
}}
|
||||
copy_to: "{{ remote_tmp_dir }}"
|
||||
cmd: "{{ remote_tmp_dir }}/echo"
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
assertions:
|
||||
- test_result.rc == 0
|
||||
- test_result.out == "-- --answer=11 --tt-arg potatoes\n"
|
||||
- test_result.err == ""
|
||||
|
||||
- name: use cmd echo with path_prefix {{ remote_tmp_dir }}
|
||||
cmd: echo
|
||||
condition: >
|
||||
{{
|
||||
ansible_distribution != "MacOSX" and
|
||||
not (ansible_distribution == "CentOS" and ansible_distribution_major_version is version('7.0', '<'))
|
||||
}}
|
||||
copy_to: "{{ remote_tmp_dir }}"
|
||||
path_prefix: "{{ remote_tmp_dir }}"
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
assertions:
|
||||
- test_result.rc == 0
|
||||
- test_result.out == "-- --answer=11 --tt-arg potatoes\n"
|
||||
- test_result.err == ""
|
||||
|
||||
- name: use cmd never-existed
|
||||
cmd: never-existed
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
expect_error: true
|
||||
assertions:
|
||||
- >
|
||||
"Failed to find required executable" in test_result.msg
|
||||
|
||||
- name: use cmd /usr/bin/never-existed
|
||||
cmd: /usr/bin/never-existed
|
||||
arg_formats:
|
||||
aa:
|
||||
func: as_opt_eq_val
|
||||
args: [--answer]
|
||||
tt:
|
||||
func: as_opt_val
|
||||
args: [--tt-arg]
|
||||
arg_order: 'aa tt'
|
||||
arg_values:
|
||||
tt: potatoes
|
||||
aa: 11
|
||||
expect_error: true
|
||||
assertions:
|
||||
- >
|
||||
"No such file or directory" in test_result.msg
|
||||
|
||||
@@ -21,3 +21,8 @@
|
||||
update_cache: true
|
||||
upgrade: true
|
||||
when: archlinux_upgrade_tag is changed
|
||||
|
||||
- name: Remove EXTERNALLY-MANAGED file
|
||||
file:
|
||||
path: /usr/lib/python{{ ansible_python.version.major }}.{{ ansible_python.version.minor }}/EXTERNALLY-MANAGED
|
||||
state: absent
|
||||
|
||||
102
tests/unit/plugins/modules/cmd_runner_test_utils.py
Normal file
102
tests/unit/plugins/modules/cmd_runner_test_utils.py
Normal file
@@ -0,0 +1,102 @@
|
||||
# Copyright (c) Ansible project
|
||||
# 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
|
||||
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from collections import namedtuple
|
||||
from itertools import chain, repeat
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
|
||||
ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_command_calls"])
|
||||
RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"])
|
||||
|
||||
|
||||
class CmdRunnerTestHelper(object):
|
||||
def __init__(self, test_cases):
|
||||
self._test_cases = test_cases
|
||||
if isinstance(test_cases, (list, tuple)):
|
||||
self.testcases = test_cases
|
||||
else:
|
||||
self.testcases = self._make_test_cases()
|
||||
|
||||
@property
|
||||
def cmd_fixture(self):
|
||||
@pytest.fixture
|
||||
def patch_bin(mocker):
|
||||
def mockie(self, path, *args, **kwargs):
|
||||
return "/testbin/{0}".format(path)
|
||||
mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', mockie)
|
||||
|
||||
return patch_bin
|
||||
|
||||
def _make_test_cases(self):
|
||||
test_cases = yaml.safe_load(self._test_cases)
|
||||
|
||||
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 []
|
||||
|
||||
results.append(ModuleTestCase(**tc))
|
||||
|
||||
return results
|
||||
|
||||
@property
|
||||
def testcases_params(self):
|
||||
return [[x.input, x] for x in self.testcases]
|
||||
|
||||
@property
|
||||
def testcases_ids(self):
|
||||
return [item.id for item in self.testcases]
|
||||
|
||||
def __call__(self, testcase, mocker):
|
||||
return _Context(self, testcase, mocker)
|
||||
|
||||
|
||||
class _Context(object):
|
||||
def __init__(self, helper, testcase, mocker):
|
||||
self.helper = helper
|
||||
self.testcase = testcase
|
||||
self.mocker = mocker
|
||||
|
||||
self.run_cmd_calls = self.testcase.run_command_calls
|
||||
self.mock_run_cmd = self._make_mock_run_cmd()
|
||||
|
||||
def _make_mock_run_cmd(self):
|
||||
call_results = [(x.rc, x.out, x.err) for x in self.run_cmd_calls]
|
||||
error_call_results = (123,
|
||||
"OUT: testcase has not enough run_command calls",
|
||||
"ERR: testcase has not enough run_command calls")
|
||||
mock_run_command = self.mocker.patch('ansible.module_utils.basic.AnsibleModule.run_command',
|
||||
side_effect=chain(call_results, repeat(error_call_results)))
|
||||
return mock_run_command
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
return False
|
||||
|
||||
def check_results(self, results):
|
||||
print("testcase =\n%s" % str(self.testcase))
|
||||
print("results =\n%s" % results)
|
||||
if 'exception' in results:
|
||||
print("exception = \n%s" % results["exception"])
|
||||
|
||||
for test_result in self.testcase.output:
|
||||
assert results[test_result] == self.testcase.output[test_result], \
|
||||
"'{0}': '{1}' != '{2}'".format(test_result, results[test_result], self.testcase.output[test_result])
|
||||
|
||||
call_args_list = [(item[0][0], item[1]) for item in self.mock_run_cmd.call_args_list]
|
||||
expected_call_args_list = [(item.command, item.environ) for item in self.run_cmd_calls]
|
||||
print("call args list =\n%s" % call_args_list)
|
||||
print("expected args list =\n%s" % expected_call_args_list)
|
||||
|
||||
assert self.mock_run_cmd.call_count == len(self.run_cmd_calls)
|
||||
if self.mock_run_cmd.call_count:
|
||||
assert call_args_list == expected_call_args_list
|
||||
@@ -7,235 +7,34 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from collections import namedtuple
|
||||
from ansible_collections.community.general.plugins.modules import opkg
|
||||
from ansible_collections.community.general.plugins.modules import opkg as module
|
||||
|
||||
import pytest
|
||||
|
||||
TESTED_MODULE = opkg.__name__
|
||||
from .cmd_runner_test_utils import CmdRunnerTestHelper
|
||||
|
||||
|
||||
ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_command_calls"])
|
||||
RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_opkg(mocker):
|
||||
mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', return_value='/testbin/opkg')
|
||||
|
||||
|
||||
TEST_CASES = [
|
||||
ModuleTestCase(
|
||||
id="install_zlibdev",
|
||||
input={"name": "zlib-dev", "state": "present"},
|
||||
output={
|
||||
"msg": "installed 1 package(s)"
|
||||
},
|
||||
run_command_calls=[
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="",
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "install", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out=(
|
||||
"Installing zlib-dev (1.2.11-6) to root..."
|
||||
"Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk"
|
||||
"Installing zlib (1.2.11-6) to root..."
|
||||
"Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib_1.2.11-6_mips_24kc.ipk"
|
||||
"Configuring zlib."
|
||||
"Configuring zlib-dev."
|
||||
),
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="zlib-dev - 1.2.11-6\n",
|
||||
err="",
|
||||
),
|
||||
],
|
||||
),
|
||||
ModuleTestCase(
|
||||
id="install_zlibdev_present",
|
||||
input={"name": "zlib-dev", "state": "present"},
|
||||
output={
|
||||
"msg": "package(s) already present"
|
||||
},
|
||||
run_command_calls=[
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="zlib-dev - 1.2.11-6\n",
|
||||
err="",
|
||||
),
|
||||
],
|
||||
),
|
||||
ModuleTestCase(
|
||||
id="install_zlibdev_force_reinstall",
|
||||
input={"name": "zlib-dev", "state": "present", "force": "reinstall"},
|
||||
output={
|
||||
"msg": "installed 1 package(s)"
|
||||
},
|
||||
run_command_calls=[
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="zlib-dev - 1.2.11-6\n",
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "install", "--force-reinstall", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out=(
|
||||
"Installing zlib-dev (1.2.11-6) to root...\n"
|
||||
"Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk\n"
|
||||
"Configuring zlib-dev.\n"
|
||||
),
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="zlib-dev - 1.2.11-6\n",
|
||||
err="",
|
||||
),
|
||||
],
|
||||
),
|
||||
ModuleTestCase(
|
||||
id="install_zlibdev_with_version",
|
||||
input={"name": "zlib-dev=1.2.11-6", "state": "present"},
|
||||
output={
|
||||
"msg": "installed 1 package(s)"
|
||||
},
|
||||
run_command_calls=[
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="",
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "install", "zlib-dev=1.2.11-6"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out=(
|
||||
"Installing zlib-dev (1.2.11-6) to root..."
|
||||
"Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk"
|
||||
"Installing zlib (1.2.11-6) to root..."
|
||||
"Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib_1.2.11-6_mips_24kc.ipk"
|
||||
"Configuring zlib."
|
||||
"Configuring zlib-dev."
|
||||
),
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "zlib-dev"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="zlib-dev - 1.2.11-6 \n", # This output has the extra space at the end, to satisfy the behaviour of Yocto/OpenEmbedded's opkg
|
||||
err="",
|
||||
),
|
||||
],
|
||||
),
|
||||
ModuleTestCase(
|
||||
id="install_vim_updatecache",
|
||||
input={"name": "vim-fuller", "state": "present", "update_cache": True},
|
||||
output={
|
||||
"msg": "installed 1 package(s)"
|
||||
},
|
||||
run_command_calls=[
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "update"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="",
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "vim-fuller"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="",
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "install", "vim-fuller"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out=(
|
||||
"Multiple packages (libgcc1 and libgcc1) providing same name marked HOLD or PREFER. Using latest.\n"
|
||||
"Installing vim-fuller (9.0-1) to root...\n"
|
||||
"Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/packages/vim-fuller_9.0-1_x86_64.ipk\n"
|
||||
"Installing terminfo (6.4-2) to root...\n"
|
||||
"Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/base/terminfo_6.4-2_x86_64.ipk\n"
|
||||
"Installing libncurses6 (6.4-2) to root...\n"
|
||||
"Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/base/libncurses6_6.4-2_x86_64.ipk\n"
|
||||
"Configuring terminfo.\n"
|
||||
"Configuring libncurses6.\n"
|
||||
"Configuring vim-fuller.\n"
|
||||
),
|
||||
err="",
|
||||
),
|
||||
RunCmdCall(
|
||||
command=["/testbin/opkg", "list-installed", "vim-fuller"],
|
||||
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
rc=0,
|
||||
out="vim-fuller - 9.0-1 \n", # This output has the extra space at the end, to satisfy the behaviour of Yocto/OpenEmbedded's opkg
|
||||
err="",
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
TEST_CASES_IDS = [item.id for item in TEST_CASES]
|
||||
TESTED_MODULE = module.__name__
|
||||
with open("tests/unit/plugins/modules/test_opkg.yaml", "r") as TEST_CASES:
|
||||
helper = CmdRunnerTestHelper(test_cases=TEST_CASES)
|
||||
patch_bin = helper.cmd_fixture
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module, testcase',
|
||||
[[x.input, x] for x in 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_opkg(mocker, capfd, patch_opkg, 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
|
||||
"""
|
||||
|
||||
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
|
||||
call_results = [(x.rc, x.out, x.err) for x in run_cmd_calls]
|
||||
mock_run_command = mocker.patch('ansible.module_utils.basic.AnsibleModule.run_command', side_effect=call_results)
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
opkg.main()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
print("testcase =\n%s" % str(testcase))
|
||||
print("results =\n%s" % results)
|
||||
|
||||
for test_result in testcase.output:
|
||||
assert results[test_result] == testcase.output[test_result], \
|
||||
"'{0}': '{1}' != '{2}'".format(test_result, results[test_result], testcase.output[test_result])
|
||||
|
||||
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)
|
||||
|
||||
assert mock_run_command.call_count == len(run_cmd_calls)
|
||||
if mock_run_command.call_count:
|
||||
assert call_args_list == expected_call_args_list
|
||||
ctx.check_results(results)
|
||||
|
||||
142
tests/unit/plugins/modules/test_opkg.yaml
Normal file
142
tests/unit/plugins/modules/test_opkg.yaml
Normal file
@@ -0,0 +1,142 @@
|
||||
# -*- 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: install_zlibdev
|
||||
input:
|
||||
name: zlib-dev
|
||||
state: present
|
||||
output:
|
||||
msg: installed 1 package(s)
|
||||
run_command_calls:
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- command: [/testbin/opkg, install, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
Installing zlib-dev (1.2.11-6) to root...
|
||||
Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk
|
||||
Installing zlib (1.2.11-6) to root...
|
||||
Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib_1.2.11-6_mips_24kc.ipk
|
||||
Configuring zlib.
|
||||
Configuring zlib-dev.
|
||||
err: ""
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
zlib-dev - 1.2.11-6
|
||||
err: ""
|
||||
- id: install_zlibdev_present
|
||||
input:
|
||||
name: zlib-dev
|
||||
state: present
|
||||
output:
|
||||
msg: package(s) already present
|
||||
run_command_calls:
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
zlib-dev - 1.2.11-6
|
||||
err: ""
|
||||
- id: install_zlibdev_force_reinstall
|
||||
input:
|
||||
name: zlib-dev
|
||||
state: present
|
||||
force: reinstall
|
||||
output:
|
||||
msg: installed 1 package(s)
|
||||
run_command_calls:
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
zlib-dev - 1.2.11-6
|
||||
err: ""
|
||||
- command: [/testbin/opkg, install, --force-reinstall, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
Installing zlib-dev (1.2.11-6) to root...
|
||||
Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk
|
||||
Configuring zlib-dev.
|
||||
err: ""
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
zlib-dev - 1.2.11-6
|
||||
err: ""
|
||||
- id: install_zlibdev_with_version
|
||||
input:
|
||||
name: zlib-dev=1.2.11-6
|
||||
state: present
|
||||
output:
|
||||
msg: installed 1 package(s)
|
||||
run_command_calls:
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- command: [/testbin/opkg, install, zlib-dev=1.2.11-6]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
Installing zlib-dev (1.2.11-6) to root...
|
||||
Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib-dev_1.2.11-6_mips_24kc.ipk
|
||||
Installing zlib (1.2.11-6) to root...
|
||||
Downloading https://downloads.openwrt.org/releases/22.03.0/packages/mips_24kc/base/zlib_1.2.11-6_mips_24kc.ipk
|
||||
Configuring zlib.
|
||||
Configuring zlib-dev.
|
||||
err: ""
|
||||
- command: [/testbin/opkg, list-installed, zlib-dev]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "zlib-dev - 1.2.11-6 \n" # This output has the extra space at the end, to satisfy the behaviour of Yocto/OpenEmbedded's opkg
|
||||
err: ""
|
||||
- id: install_vim_updatecache
|
||||
input:
|
||||
name: vim-fuller
|
||||
state: present
|
||||
update_cache: true
|
||||
output:
|
||||
msg: installed 1 package(s)
|
||||
run_command_calls:
|
||||
- command: [/testbin/opkg, update]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- command: [/testbin/opkg, list-installed, vim-fuller]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- command: [/testbin/opkg, install, vim-fuller]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: |
|
||||
Multiple packages (libgcc1 and libgcc1) providing same name marked HOLD or PREFER. Using latest.
|
||||
Installing vim-fuller (9.0-1) to root...
|
||||
Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/packages/vim-fuller_9.0-1_x86_64.ipk
|
||||
Installing terminfo (6.4-2) to root...
|
||||
Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/base/terminfo_6.4-2_x86_64.ipk
|
||||
Installing libncurses6 (6.4-2) to root...
|
||||
Downloading https://downloads.openwrt.org/snapshots/packages/x86_64/base/libncurses6_6.4-2_x86_64.ipk
|
||||
Configuring terminfo.
|
||||
Configuring libncurses6.
|
||||
Configuring vim-fuller.
|
||||
err: ""
|
||||
- command: [/testbin/opkg, list-installed, vim-fuller]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "vim-fuller - 9.0-1 \n" # This output has the extra space at the end, to satisfy the behaviour of Yocto/OpenEmbedded's opkg
|
||||
err: ""
|
||||
@@ -13,300 +13,34 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible_collections.community.general.plugins.modules import xfconf
|
||||
from ansible_collections.community.general.plugins.modules import xfconf as module
|
||||
|
||||
import pytest
|
||||
|
||||
TESTED_MODULE = xfconf.__name__
|
||||
from .cmd_runner_test_utils import CmdRunnerTestHelper
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_xfconf(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/xfconf-query')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_without_required_parameters(capfd, patch_xfconf):
|
||||
"""
|
||||
Failure must occurs when all parameters are missing
|
||||
"""
|
||||
with pytest.raises(SystemExit):
|
||||
xfconf.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'missing required arguments' in results['msg']
|
||||
|
||||
|
||||
TEST_CASES = [
|
||||
[
|
||||
{
|
||||
'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', 'LC_ALL': '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', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': '100',
|
||||
'value_type': 'int',
|
||||
'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', 'LC_ALL': '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', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'previous_value': '90',
|
||||
'value_type': 'int',
|
||||
'value': '90',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfce4-session',
|
||||
'property': '/general/SaveOnExit',
|
||||
'state': 'present',
|
||||
'value_type': 'bool',
|
||||
'value': False,
|
||||
},
|
||||
{
|
||||
'id': 'test_property_set_property_bool_false',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfce4-session', '--property', '/general/SaveOnExit'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'true\n', '',),
|
||||
),
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--channel', 'xfce4-session', '--property', '/general/SaveOnExit',
|
||||
'--create', '--type', 'bool', '--set', 'false'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'false\n', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': 'true',
|
||||
'value_type': 'bool',
|
||||
'value': 'False',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
'channel': 'xfwm4',
|
||||
'property': '/general/workspace_names',
|
||||
'state': 'present',
|
||||
'value_type': 'string',
|
||||
'value': ['A', 'B', 'C'],
|
||||
},
|
||||
{
|
||||
'id': 'test_property_set_array',
|
||||
'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', 'LC_ALL': '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'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': ['Main', 'Work', 'Tmp'],
|
||||
'value_type': ['str', 'str', 'str'],
|
||||
'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', 'LC_ALL': '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', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': False,
|
||||
'previous_value': ['A', 'B', 'C'],
|
||||
'value_type': ['str', 'str', 'str'],
|
||||
'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', 'LC_ALL': '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', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '', '',),
|
||||
),
|
||||
],
|
||||
'changed': True,
|
||||
'previous_value': ['A', 'B', 'C'],
|
||||
'value_type': None,
|
||||
'value': None,
|
||||
},
|
||||
],
|
||||
]
|
||||
TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES]
|
||||
TESTED_MODULE = module.__name__
|
||||
with open("tests/unit/plugins/modules/test_xfconf.yaml", "r") as TEST_CASES:
|
||||
helper = CmdRunnerTestHelper(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_xfconf(mocker, capfd, patch_xfconf, 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.module_utils.basic.AnsibleModule.run_command',
|
||||
side_effect=call_results)
|
||||
with helper(testcase, mocker) as ctx:
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
module.main()
|
||||
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
xfconf.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
print("testcase =\n%s" % testcase)
|
||||
print("results =\n%s" % results)
|
||||
|
||||
assert 'changed' in results
|
||||
assert results['changed'] == testcase['changed']
|
||||
|
||||
for test_result in ('channel', 'property'):
|
||||
assert test_result in results, "'{0}' not found in {1}".format(test_result, results)
|
||||
assert results[test_result] == results['invocation']['module_args'][test_result], \
|
||||
"'{0}': '{1}' != '{2}'".format(test_result, results[test_result], results['invocation']['module_args'][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
|
||||
|
||||
expected_cmd, dummy, expected_res = testcase['run_command.calls'][-1]
|
||||
assert results['cmd'] == expected_cmd
|
||||
assert results['stdout'] == expected_res[1]
|
||||
assert results['stderr'] == expected_res[2]
|
||||
|
||||
for conditional_test_result in ('msg', 'value', 'previous_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])
|
||||
ctx.check_results(results)
|
||||
|
||||
185
tests/unit/plugins/modules/test_xfconf.yaml
Normal file
185
tests/unit/plugins/modules/test_xfconf.yaml
Normal file
@@ -0,0 +1,185 @@
|
||||
# -*- 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_missing_input
|
||||
input: {}
|
||||
output:
|
||||
failed: true
|
||||
msg: "missing required arguments: channel, property"
|
||||
- id: test_property_set_property
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/inactive_opacity
|
||||
state: present
|
||||
value_type: int
|
||||
value: 90
|
||||
output:
|
||||
changed: true
|
||||
previous_value: '100'
|
||||
type: int
|
||||
value: '90'
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/inactive_opacity]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "100\n"
|
||||
err: ""
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/inactive_opacity, --create, --type, int, --set, '90']
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- id: test_property_set_property_same_value
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/inactive_opacity
|
||||
state: present
|
||||
value_type: int
|
||||
value: 90
|
||||
output:
|
||||
changed: false
|
||||
previous_value: '90'
|
||||
type: int
|
||||
value: '90'
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/inactive_opacity]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "90\n"
|
||||
err: ""
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/inactive_opacity, --create, --type, int, --set, '90']
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- id: test_property_set_property_bool_false
|
||||
input:
|
||||
channel: xfce4-session
|
||||
property: /general/SaveOnExit
|
||||
state: present
|
||||
value_type: bool
|
||||
value: False
|
||||
output:
|
||||
changed: true
|
||||
previous_value: 'true'
|
||||
type: bool
|
||||
value: 'False'
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfce4-session, --property, /general/SaveOnExit]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "true\n"
|
||||
err: ""
|
||||
- command: [/testbin/xfconf-query, --channel, xfce4-session, --property, /general/SaveOnExit, --create, --type, bool, --set, 'false']
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "false\n"
|
||||
err: ""
|
||||
- id: test_property_set_array
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/workspace_names
|
||||
state: present
|
||||
value_type: string
|
||||
value: [A, B, C]
|
||||
output:
|
||||
changed: true
|
||||
previous_value: [Main, Work, Tmp]
|
||||
type: [string, string, string]
|
||||
value: [A, B, C]
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/workspace_names]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "Value is an array with 3 items:\n\nMain\nWork\nTmp\n"
|
||||
err: ""
|
||||
- command:
|
||||
- /testbin/xfconf-query
|
||||
- --channel
|
||||
- xfwm4
|
||||
- --property
|
||||
- /general/workspace_names
|
||||
- --create
|
||||
- --force-array
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- A
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- B
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- C
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- id: test_property_set_array_to_same_value
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/workspace_names
|
||||
state: present
|
||||
value_type: string
|
||||
value: [A, B, C]
|
||||
output:
|
||||
changed: false
|
||||
previous_value: [A, B, C]
|
||||
type: [string, string, string]
|
||||
value: [A, B, C]
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/workspace_names]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "Value is an array with 3 items:\n\nA\nB\nC\n"
|
||||
err: ""
|
||||
- command:
|
||||
- /testbin/xfconf-query
|
||||
- --channel
|
||||
- xfwm4
|
||||
- --property
|
||||
- /general/workspace_names
|
||||
- --create
|
||||
- --force-array
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- A
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- B
|
||||
- --type
|
||||
- string
|
||||
- --set
|
||||
- C
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
- id: test_property_reset_value
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/workspace_names
|
||||
state: absent
|
||||
output:
|
||||
changed: true
|
||||
previous_value: [A, B, C]
|
||||
type: null
|
||||
value: null
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/workspace_names]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: "Value is an array with 3 items:\n\nA\nB\nC\n"
|
||||
err: ""
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/workspace_names, --reset]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
|
||||
rc: 0
|
||||
out: ""
|
||||
err: ""
|
||||
@@ -6,167 +6,34 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible_collections.community.general.plugins.modules import xfconf_info
|
||||
from ansible_collections.community.general.plugins.modules import xfconf_info as module
|
||||
|
||||
import pytest
|
||||
|
||||
TESTED_MODULE = xfconf_info.__name__
|
||||
from .cmd_runner_test_utils import CmdRunnerTestHelper
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_xfconf_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/xfconf-query')
|
||||
|
||||
|
||||
TEST_CASES = [
|
||||
[
|
||||
{'channel': 'xfwm4', 'property': '/general/inactive_opacity'},
|
||||
{
|
||||
'id': 'test_simple_property_get',
|
||||
'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', 'LC_ALL': 'C'}, 'check_rc': True},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '100\n', '',),
|
||||
),
|
||||
],
|
||||
'is_array': False,
|
||||
'value': '100',
|
||||
}
|
||||
],
|
||||
[
|
||||
{'channel': 'xfwm4', 'property': '/general/i_dont_exist'},
|
||||
{
|
||||
'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', 'LC_ALL': 'C'}, 'check_rc': True},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(1, '', 'Property "/general/i_dont_exist" does not exist on channel "xfwm4".\n',),
|
||||
),
|
||||
],
|
||||
'is_array': False,
|
||||
}
|
||||
],
|
||||
[
|
||||
{'property': '/general/i_dont_exist'},
|
||||
{
|
||||
'id': 'test_property_no_channel',
|
||||
'run_command.calls': [],
|
||||
}
|
||||
],
|
||||
[
|
||||
{'channel': 'xfwm4', 'property': '/general/workspace_names'},
|
||||
{
|
||||
'id': 'test_property_get_array',
|
||||
'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', 'LC_ALL': 'C'}, 'check_rc': True},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Value is an array with 3 items:\n\nMain\nWork\nTmp\n', '',),
|
||||
),
|
||||
],
|
||||
'is_array': True,
|
||||
'value_array': ['Main', 'Work', 'Tmp'],
|
||||
},
|
||||
],
|
||||
[
|
||||
{},
|
||||
{
|
||||
'id': 'get_channels',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--list'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, 'Channels:\n a\n b\n c\n', '',),
|
||||
),
|
||||
],
|
||||
'is_array': False,
|
||||
'channels': ['a', 'b', 'c'],
|
||||
},
|
||||
],
|
||||
[
|
||||
{'channel': 'xfwm4'},
|
||||
{
|
||||
'id': 'get_properties',
|
||||
'run_command.calls': [
|
||||
(
|
||||
# Calling of following command will be asserted
|
||||
['/testbin/xfconf-query', '--list', '--channel', 'xfwm4'],
|
||||
# Was return code checked?
|
||||
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True},
|
||||
# Mock of returned code, stdout and stderr
|
||||
(0, '/general/wrap_cycle\n/general/wrap_layout\n/general/wrap_resistance\n/general/wrap_windows\n'
|
||||
'/general/wrap_workspaces\n/general/zoom_desktop\n', '',),
|
||||
),
|
||||
],
|
||||
'is_array': False,
|
||||
'properties': [
|
||||
'/general/wrap_cycle',
|
||||
'/general/wrap_layout',
|
||||
'/general/wrap_resistance',
|
||||
'/general/wrap_windows',
|
||||
'/general/wrap_workspaces',
|
||||
'/general/zoom_desktop',
|
||||
],
|
||||
},
|
||||
],
|
||||
]
|
||||
TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES]
|
||||
TESTED_MODULE = module.__name__
|
||||
with open("tests/unit/plugins/modules/test_xfconf_info.yaml", "r") as TEST_CASES:
|
||||
helper = CmdRunnerTestHelper(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_xfconf_info(mocker, capfd, patch_xfconf_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)
|
||||
with helper(testcase, mocker) as ctx:
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
module.main()
|
||||
|
||||
# Try to run test case
|
||||
with pytest.raises(SystemExit):
|
||||
xfconf_info.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
print("testcase =\n%s" % testcase)
|
||||
print("results =\n%s" % results)
|
||||
|
||||
for conditional_test_result in ('value_array', 'value', 'is_array', 'properties', 'channels'):
|
||||
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
|
||||
ctx.check_results(results)
|
||||
|
||||
83
tests/unit/plugins/modules/test_xfconf_info.yaml
Normal file
83
tests/unit/plugins/modules/test_xfconf_info.yaml
Normal file
@@ -0,0 +1,83 @@
|
||||
# -*- 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_property_get
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/inactive_opacity
|
||||
output:
|
||||
value: '100'
|
||||
is_array: false
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/inactive_opacity]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
|
||||
rc: 0
|
||||
out: "100\n"
|
||||
err: ""
|
||||
- id: test_simple_property_get_nonexistent
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/i_dont_exist
|
||||
output: {}
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/i_dont_exist]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
|
||||
rc: 1
|
||||
out: ""
|
||||
err: 'Property "/general/i_dont_exist" does not exist on channel "xfwm4".\n'
|
||||
- id: test_property_no_channel
|
||||
input:
|
||||
property: /general/i_dont_exist
|
||||
output:
|
||||
failed: true
|
||||
msg: "missing parameter(s) required by 'property': channel"
|
||||
run_command_calls: []
|
||||
- id: test_property_get_array
|
||||
input:
|
||||
channel: xfwm4
|
||||
property: /general/workspace_names
|
||||
output:
|
||||
is_array: true
|
||||
value_array: [Main, Work, Tmp]
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --channel, xfwm4, --property, /general/workspace_names]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
|
||||
rc: 0
|
||||
out: "Value is an array with 3 items:\n\nMain\nWork\nTmp\n"
|
||||
err: ""
|
||||
- id: get_channels
|
||||
input: {}
|
||||
output:
|
||||
channels: [a, b, c]
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --list]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
|
||||
rc: 0
|
||||
out: "Channels:\n a\n b\n c\n"
|
||||
err: ""
|
||||
- id: get_properties
|
||||
input:
|
||||
channel: xfwm4
|
||||
output:
|
||||
properties:
|
||||
- /general/wrap_cycle
|
||||
- /general/wrap_layout
|
||||
- /general/wrap_resistance
|
||||
- /general/wrap_windows
|
||||
- /general/wrap_workspaces
|
||||
- /general/zoom_desktop
|
||||
run_command_calls:
|
||||
- command: [/testbin/xfconf-query, --list, --channel, xfwm4]
|
||||
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
|
||||
rc: 0
|
||||
out: |
|
||||
/general/wrap_cycle
|
||||
/general/wrap_layout
|
||||
/general/wrap_resistance
|
||||
/general/wrap_windows
|
||||
/general/wrap_workspaces
|
||||
/general/zoom_desktop
|
||||
err: ""
|
||||
Reference in New Issue
Block a user