diff options
Diffstat (limited to '')
-rw-r--r-- | test/units/plugins/become/__init__.py | 0 | ||||
-rw-r--r-- | test/units/plugins/become/conftest.py | 37 | ||||
-rw-r--r-- | test/units/plugins/become/test_su.py | 30 | ||||
-rw-r--r-- | test/units/plugins/become/test_sudo.py | 67 |
4 files changed, 134 insertions, 0 deletions
diff --git a/test/units/plugins/become/__init__.py b/test/units/plugins/become/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/units/plugins/become/__init__.py diff --git a/test/units/plugins/become/conftest.py b/test/units/plugins/become/conftest.py new file mode 100644 index 0000000..a04a5e2 --- /dev/null +++ b/test/units/plugins/become/conftest.py @@ -0,0 +1,37 @@ +# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> +# (c) 2017 Ansible Project +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import pytest + +from ansible.cli.arguments import option_helpers as opt_help +from ansible.utils import context_objects as co + + +@pytest.fixture +def parser(): + parser = opt_help.create_base_parser('testparser') + + opt_help.add_runas_options(parser) + opt_help.add_meta_options(parser) + opt_help.add_runtask_options(parser) + opt_help.add_vault_options(parser) + opt_help.add_async_options(parser) + opt_help.add_connect_options(parser) + opt_help.add_subset_options(parser) + opt_help.add_check_options(parser) + opt_help.add_inventory_options(parser) + + return parser + + +@pytest.fixture +def reset_cli_args(): + co.GlobalCLIArgs._Singleton__instance = None + yield + co.GlobalCLIArgs._Singleton__instance = None diff --git a/test/units/plugins/become/test_su.py b/test/units/plugins/become/test_su.py new file mode 100644 index 0000000..bf74a4c --- /dev/null +++ b/test/units/plugins/become/test_su.py @@ -0,0 +1,30 @@ +# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> +# (c) 2020 Ansible Project +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import re + +from ansible import context +from ansible.plugins.loader import become_loader, shell_loader + + +def test_su(mocker, parser, reset_cli_args): + options = parser.parse_args([]) + context._init_global_context(options) + + su = become_loader.get('su') + sh = shell_loader.get('sh') + sh.executable = "/bin/bash" + + su.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '', + }) + + cmd = su.build_become_command('/bin/foo', sh) + assert re.match(r"""su\s+foo -c '/bin/bash -c '"'"'echo BECOME-SUCCESS-.+?; /bin/foo'"'"''""", cmd) diff --git a/test/units/plugins/become/test_sudo.py b/test/units/plugins/become/test_sudo.py new file mode 100644 index 0000000..67eb9a4 --- /dev/null +++ b/test/units/plugins/become/test_sudo.py @@ -0,0 +1,67 @@ +# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> +# (c) 2020 Ansible Project +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import re + +from ansible import context +from ansible.plugins.loader import become_loader, shell_loader + + +def test_sudo(mocker, parser, reset_cli_args): + options = parser.parse_args([]) + context._init_global_context(options) + + sudo = become_loader.get('sudo') + sh = shell_loader.get('sh') + sh.executable = "/bin/bash" + + sudo.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '-n -s -H', + }) + + cmd = sudo.build_become_command('/bin/foo', sh) + + assert re.match(r"""sudo\s+-n -s -H\s+-u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd + + sudo.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '-n -s -H', + 'become_pass': 'testpass', + }) + + cmd = sudo.build_become_command('/bin/foo', sh) + assert re.match(r"""sudo\s+-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd + + sudo.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '-snH', + 'become_pass': 'testpass', + }) + + cmd = sudo.build_become_command('/bin/foo', sh) + assert re.match(r"""sudo\s+-sH\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd + + sudo.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '--non-interactive -s -H', + 'become_pass': 'testpass', + }) + + cmd = sudo.build_become_command('/bin/foo', sh) + assert re.match(r"""sudo\s+-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd + + sudo.set_options(direct={ + 'become_user': 'foo', + 'become_flags': '--non-interactive -nC5 -s -H', + 'become_pass': 'testpass', + }) + + cmd = sudo.build_become_command('/bin/foo', sh) + assert re.match(r"""sudo\s+-C5\s-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd |