summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2019-02-28 22:43:19 +0100
committerGitHub <noreply@github.com>2019-02-28 22:43:19 +0100
commit2a98faee2b3be7a0e06b3a2959ba91b20b272355 (patch)
tree210dd6c95e5e96010468ce56c0dd3419ffd88300 /test
parentReboot - add parameter for paths to search for shutdown command (#51194) (diff)
downloadansible-2a98faee2b3be7a0e06b3a2959ba91b20b272355.tar.xz
ansible-2a98faee2b3be7a0e06b3a2959ba91b20b272355.zip
Move _handle_aliases() out of basic.py (#48578)
Refinements: - return legal_inputs and update class properties - remove redundant arguments from method and handle in caller - add better exception types to method * Add unit tests for handle_aliases
Diffstat (limited to 'test')
-rw-r--r--test/units/executor/module_common/test_recursive_finder.py9
-rw-r--r--test/units/module_utils/basic/test_atomic_move.py1
-rw-r--r--test/units/module_utils/common/parameters/test_handle_aliases.py102
3 files changed, 111 insertions, 1 deletions
diff --git a/test/units/executor/module_common/test_recursive_finder.py b/test/units/executor/module_common/test_recursive_finder.py
index 0b919f6987..e61ad1efc8 100644
--- a/test/units/executor/module_common/test_recursive_finder.py
+++ b/test/units/executor/module_common/test_recursive_finder.py
@@ -44,7 +44,9 @@ MODULE_UTILS_BASIC_IMPORTS = frozenset((('_text',),
('basic',),
('common', '__init__'),
('common', '_collections_compat'),
+ ('common', 'collections'),
('common', 'file'),
+ ('common', 'parameters'),
('common', 'process'),
('common', 'sys_info'),
('common', '_utils'),
@@ -58,8 +60,13 @@ MODULE_UTILS_BASIC_IMPORTS = frozenset((('_text',),
MODULE_UTILS_BASIC_FILES = frozenset(('ansible/module_utils/_text.py',
'ansible/module_utils/basic.py',
- 'ansible/module_utils/common/__init__.py',
+ 'ansible/module_utils/six/__init__.py',
+ 'ansible/module_utils/_text.py',
'ansible/module_utils/common/_collections_compat.py',
+ 'ansible/module_utils/common/collections.py',
+ 'ansible/module_utils/common/parameters.py',
+ 'ansible/module_utils/parsing/convert_bool.py',
+ 'ansible/module_utils/common/__init__.py',
'ansible/module_utils/common/file.py',
'ansible/module_utils/common/process.py',
'ansible/module_utils/common/sys_info.py',
diff --git a/test/units/module_utils/basic/test_atomic_move.py b/test/units/module_utils/basic/test_atomic_move.py
index 1dea91702c..dce3941906 100644
--- a/test/units/module_utils/basic/test_atomic_move.py
+++ b/test/units/module_utils/basic/test_atomic_move.py
@@ -62,6 +62,7 @@ def fake_stat(mocker):
stat1.st_mode = 0o0644
stat1.st_uid = 0
stat1.st_gid = 0
+ stat1.st_flags = 0
yield stat1
diff --git a/test/units/module_utils/common/parameters/test_handle_aliases.py b/test/units/module_utils/common/parameters/test_handle_aliases.py
new file mode 100644
index 0000000000..bc88437fba
--- /dev/null
+++ b/test/units/module_utils/common/parameters/test_handle_aliases.py
@@ -0,0 +1,102 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2019 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.module_utils.common.parameters import handle_aliases
+from ansible.module_utils._text import to_native
+
+DEFAULT_LEGAL_INPUTS = [
+ '_ansible_check_mode',
+ '_ansible_debug',
+ '_ansible_diff',
+ '_ansible_keep_remote_files',
+ '_ansible_module_name',
+ '_ansible_no_log',
+ '_ansible_remote_tmp',
+ '_ansible_selinux_special_fs',
+ '_ansible_shell_executable',
+ '_ansible_socket',
+ '_ansible_string_conversion_action',
+ '_ansible_syslog_facility',
+ '_ansible_tmpdir',
+ '_ansible_verbosity',
+ '_ansible_version',
+]
+
+
+def test_handle_aliases_no_aliases():
+ argument_spec = {
+ 'name': {'type': 'str'},
+ }
+
+ params = {
+ 'name': 'foo',
+ 'path': 'bar'
+ }
+
+ expected = (
+ {},
+ DEFAULT_LEGAL_INPUTS + ['name'],
+ )
+ expected[1].sort()
+
+ result = handle_aliases(argument_spec, params)
+ result[1].sort()
+ assert expected == result
+
+
+def test_handle_aliases_basic():
+ argument_spec = {
+ 'name': {'type': 'str', 'aliases': ['surname', 'nick']},
+ }
+
+ params = {
+ 'name': 'foo',
+ 'path': 'bar',
+ 'surname': 'foo',
+ 'nick': 'foo',
+ }
+
+ expected = (
+ {'surname': 'name', 'nick': 'name'},
+ DEFAULT_LEGAL_INPUTS + ['name', 'surname', 'nick'],
+ )
+ expected[1].sort()
+
+ result = handle_aliases(argument_spec, params)
+ result[1].sort()
+ assert expected == result
+
+
+def test_handle_aliases_value_error():
+ argument_spec = {
+ 'name': {'type': 'str', 'aliases': ['surname', 'nick'], 'default': 'bob', 'required': True},
+ }
+
+ params = {
+ 'name': 'foo',
+ }
+
+ with pytest.raises(ValueError) as ve:
+ handle_aliases(argument_spec, params)
+ assert 'internal error: aliases must be a list or tuple' == to_native(ve.error)
+
+
+def test_handle_aliases_type_error():
+ argument_spec = {
+ 'name': {'type': 'str', 'aliases': 'surname'},
+ }
+
+ params = {
+ 'name': 'foo',
+ }
+
+ with pytest.raises(TypeError) as te:
+ handle_aliases(argument_spec, params)
+ assert 'internal error: required and default are mutually exclusive' in to_native(te.error)