From 162d9497ba15c595b5d661f58b3f15a49ab27a41 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Sun, 16 Dec 2018 11:07:06 +0530 Subject: [PATCH] Ad-hoc command: fix 'any' call in play_ds (#49852) * Ad-hoc command: fix 'any' call in play_ds * Unit test for ad-hoc cli * Review comments Signed-off-by: Abhijeet Kasurde --- lib/ansible/cli/adhoc.py | 2 +- test/units/cli/test_adhoc.py | 92 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/units/cli/test_adhoc.py diff --git a/lib/ansible/cli/adhoc.py b/lib/ansible/cli/adhoc.py index 0042556d76c..9983e081ae0 100644 --- a/lib/ansible/cli/adhoc.py +++ b/lib/ansible/cli/adhoc.py @@ -80,7 +80,7 @@ class AdHocCLI(CLI): mytask = {'action': {'module': self.options.module_name, 'args': parse_kv(self.options.module_args, check_raw=check_raw)}} # avoid adding to tasks that don't support it, unless set, then give user an error - if self.options.module_name not in ('include_role', 'include_tasks') or any(async_val, poll): + if self.options.module_name not in ('include_role', 'include_tasks') or any(frozenset((async_val, poll))): mytask['async_val'] = async_val mytask['poll'] = poll diff --git a/test/units/cli/test_adhoc.py b/test/units/cli/test_adhoc.py new file mode 100644 index 00000000000..899c11b6542 --- /dev/null +++ b/test/units/cli/test_adhoc.py @@ -0,0 +1,92 @@ +# Copyright: (c) 2018, Abhijeet Kasurde +# 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.cli.adhoc import AdHocCLI, display +from ansible.errors import AnsibleOptionsError + + +def test_parse(): + """ Test adhoc parse""" + adhoc_cli = AdHocCLI([]) + with pytest.raises(AnsibleOptionsError) as exec_info: + adhoc_cli.parse() + assert "Missing target hosts" == str(exec_info.value) + + +def test_with_command(): + """ Test simple adhoc command""" + module_name = 'command' + adhoc_cli = AdHocCLI(args=['-m', module_name, '-vv']) + adhoc_cli.parse() + assert adhoc_cli.options.module_name == module_name + assert display.verbosity == 2 + + +def test_with_extra_parameters(): + """ Test extra parameters""" + adhoc_cli = AdHocCLI(args=['-m', 'command', 'extra_parameters']) + with pytest.raises(AnsibleOptionsError) as exec_info: + adhoc_cli.parse() + assert "Extraneous options or arguments" == str(exec_info.value) + + +def test_simple_command(): + """ Test valid command and its run""" + adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost']) + adhoc_cli.parse() + adhoc_cli.options.module_args = "echo 'hi'" + ret = adhoc_cli.run() + assert ret == 0 + + +def test_no_argument(): + """ Test no argument command""" + adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost']) + adhoc_cli.parse() + with pytest.raises(AnsibleOptionsError) as exec_info: + adhoc_cli.run() + assert 'No argument passed to command module' == str(exec_info.value) + + +def test_did_you_mean_playbook(): + """ Test adhoc with yml file as argument parameter""" + adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost.yml']) + adhoc_cli.parse() + with pytest.raises(AnsibleOptionsError) as exec_info: + adhoc_cli.run() + assert 'No argument passed to command module (did you mean to run ansible-playbook?)' == str(exec_info.value) + + +def test_play_ds_positive(): + """ Test _play_ds""" + adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost']) + adhoc_cli.parse() + adhoc_cli.options.module_name = 'command' + ret = adhoc_cli._play_ds('command', 10, 2) + assert ret['name'] == 'Ansible Ad-Hoc' + assert ret['tasks'] == [{'action': {'module': 'command', 'args': {}}, 'async_val': 10, 'poll': 2}] + + +def test_play_ds_with_include_role(): + """ Test include_role command with poll""" + adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost']) + adhoc_cli.parse() + adhoc_cli.options.module_name = 'include_role' + ret = adhoc_cli._play_ds('include_role', None, 2) + assert ret['name'] == 'Ansible Ad-Hoc' + assert ret['gather_facts'] == 'no' + + +def test_run_import_playbook(): + """ Test import_playbook which is not allowed with ad-hoc command""" + import_playbook = 'import_playbook' + adhoc_cli = AdHocCLI(args=['/bin/ansible', '-m', import_playbook, 'localhost']) + adhoc_cli.parse() + with pytest.raises(AnsibleOptionsError) as exec_info: + adhoc_cli.run() + assert adhoc_cli.options.module_name == import_playbook + assert "'%s' is not a valid action for ad-hoc commands" % import_playbook == str(exec_info.value)