Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/become/test_sudosu.py

raw
1 # Copyright (c) 2012-2014, Michael DeHaan <[email protected]>
2 # Copyright (c) 2021 Ansible Project
3 #
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 # Make coding more python3-ish
8 from __future__ import annotations
9
10 import re
11
12 from ansible import context
13
14 from .helper import call_become_plugin
15
16
17 def test_sudosu(mocker, parser, reset_cli_args):
18 options = parser.parse_args([])
19 context._init_global_context(options)
20
21 default_cmd = "/bin/foo"
22 default_exe = "/bin/bash"
23 sudo_exe = "sudo"
24 sudo_flags = "-H -s -n"
25
26 success = "BECOME-SUCCESS-.+?"
27
28 task = {
29 "become_user": "foo",
30 "become_method": "community.general.sudosu",
31 "become_flags": sudo_flags,
32 }
33 var_options = {}
34 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
35 print(cmd)
36 assert (
37 re.match(
38 f"""{sudo_exe} {sudo_flags} su -l {task["become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
39 cmd,
40 )
41 is not None
42 )
43
44 task = {
45 "become_user": "foo",
46 "become_method": "community.general.sudosu",
47 "become_flags": sudo_flags,
48 "become_pass": "testpass",
49 }
50 var_options = {}
51 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
52 print(cmd)
53 password_pattern = r"\[sudo via ansible, key=.+?\] password:"
54 assert (
55 re.match(
56 f"""{sudo_exe} {sudo_flags.replace("-n", "")} -p "{password_pattern}" """
57 f"""su -l {task["become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
58 cmd,
59 )
60 is not None
61 )