| 1 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import pytest |
| 7 |
import re |
| 8 |
|
| 9 |
from ansible import context |
| 10 |
from ansible.cli.adhoc import AdHocCLI, display |
| 11 |
from ansible.errors import AnsibleOptionsError |
| 12 |
|
| 13 |
|
| 14 |
def test_parse(): |
| 15 |
""" Test adhoc parse""" |
| 16 |
with pytest.raises(ValueError, match='A non-empty list for args is required'): |
| 17 |
adhoc_cli = AdHocCLI([]) |
| 18 |
|
| 19 |
adhoc_cli = AdHocCLI(['ansibletest']) |
| 20 |
with pytest.raises(SystemExit): |
| 21 |
adhoc_cli.parse() |
| 22 |
|
| 23 |
|
| 24 |
def test_with_command(): |
| 25 |
""" Test simple adhoc command""" |
| 26 |
module_name = 'command' |
| 27 |
adhoc_cli = AdHocCLI(args=['ansible', '-m', module_name, '-vv', 'localhost']) |
| 28 |
adhoc_cli.parse() |
| 29 |
assert context.CLIARGS['module_name'] == module_name |
| 30 |
assert display.verbosity == 2 |
| 31 |
|
| 32 |
|
| 33 |
def test_simple_command(): |
| 34 |
""" Test valid command and its run""" |
| 35 |
adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost', '-a', 'echo "hi"']) |
| 36 |
adhoc_cli.parse() |
| 37 |
ret = adhoc_cli.run() |
| 38 |
assert ret == 0 |
| 39 |
|
| 40 |
|
| 41 |
def test_no_argument(): |
| 42 |
""" Test no argument command""" |
| 43 |
adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost']) |
| 44 |
adhoc_cli.parse() |
| 45 |
with pytest.raises(AnsibleOptionsError) as exec_info: |
| 46 |
adhoc_cli.run() |
| 47 |
assert 'No argument passed to command module' == str(exec_info.value) |
| 48 |
|
| 49 |
|
| 50 |
def test_did_you_mean_playbook(): |
| 51 |
""" Test adhoc with yml file as argument parameter""" |
| 52 |
adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost.yml']) |
| 53 |
adhoc_cli.parse() |
| 54 |
with pytest.raises(AnsibleOptionsError) as exec_info: |
| 55 |
adhoc_cli.run() |
| 56 |
assert 'No argument passed to command module (did you mean to run ansible-playbook?)' == str(exec_info.value) |
| 57 |
|
| 58 |
|
| 59 |
def test_play_ds_positive(): |
| 60 |
""" Test _play_ds""" |
| 61 |
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost', '-m', 'command']) |
| 62 |
adhoc_cli.parse() |
| 63 |
ret = adhoc_cli._play_ds('command', 10, 2) |
| 64 |
assert ret['name'] == 'Ansible Ad-Hoc' |
| 65 |
assert ret['tasks'] == [{'action': 'command', 'args': {}, 'async_val': 10, 'poll': 2, 'timeout': 0}] |
| 66 |
|
| 67 |
|
| 68 |
def test_play_ds_with_include_role(): |
| 69 |
""" Test include_role command with poll""" |
| 70 |
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost', '-m', 'include_role']) |
| 71 |
adhoc_cli.parse() |
| 72 |
ret = adhoc_cli._play_ds('include_role', None, 2) |
| 73 |
assert ret['name'] == 'Ansible Ad-Hoc' |
| 74 |
assert ret['gather_facts'] == 'no' |
| 75 |
|
| 76 |
|
| 77 |
def test_run_import_playbook(): |
| 78 |
""" Test import_playbook which is not allowed with ad-hoc command""" |
| 79 |
import_playbook = 'import_playbook' |
| 80 |
adhoc_cli = AdHocCLI(args=['/bin/ansible', '-m', import_playbook, 'localhost']) |
| 81 |
adhoc_cli.parse() |
| 82 |
with pytest.raises(AnsibleOptionsError) as exec_info: |
| 83 |
adhoc_cli.run() |
| 84 |
assert context.CLIARGS['module_name'] == import_playbook |
| 85 |
assert "'%s' is not a valid action for ad-hoc commands" % import_playbook == str(exec_info.value) |
| 86 |
|
| 87 |
|
| 88 |
def test_run_no_extra_vars(): |
| 89 |
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost', '-e']) |
| 90 |
with pytest.raises(SystemExit) as exec_info: |
| 91 |
adhoc_cli.parse() |
| 92 |
assert exec_info.value.code == 2 |
| 93 |
|
| 94 |
|
| 95 |
def test_ansible_version(capsys): |
| 96 |
adhoc_cli = AdHocCLI(args=['/bin/ansible', '--version']) |
| 97 |
with pytest.raises(SystemExit): |
| 98 |
adhoc_cli.run() |
| 99 |
version = capsys.readouterr() |
| 100 |
version_lines = version.out.splitlines() |
| 101 |
|
| 102 |
assert len(version_lines) == 9, 'Incorrect number of lines in "ansible --version" output' |
| 103 |
assert re.match(r'ansible \[core [0-9.a-z]+\]', version_lines[0]), 'Incorrect ansible version line in "ansible --version" output' |
| 104 |
assert re.match(' config file = .*$', version_lines[1]), 'Incorrect config file line in "ansible --version" output' |
| 105 |
assert re.match(' configured module search path = .*$', version_lines[2]), 'Incorrect module search path in "ansible --version" output' |
| 106 |
assert re.match(' ansible python module location = .*$', version_lines[3]), 'Incorrect python module location in "ansible --version" output' |
| 107 |
assert re.match(' ansible collection location = .*$', version_lines[4]), 'Incorrect collection location in "ansible --version" output' |
| 108 |
assert re.match(' executable location = .*$', version_lines[5]), 'Incorrect executable locaction in "ansible --version" output' |
| 109 |
assert re.match(' python version = .*$', version_lines[6]), 'Incorrect python version in "ansible --version" output' |
| 110 |
assert re.match(' jinja version = .*$', version_lines[7]), 'Incorrect jinja version in "ansible --version" output' |
| 111 |
assert re.match(' pyyaml version = .*$', version_lines[8]), 'Missing pyyaml version in "ansible --version" output' |