Debian Codegraph @

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

raw
1 # Copyright (c) 2012-2014, Michael DeHaan <[email protected]>
2 # Copyright (c) 2020 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_pbrun_basic(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 pbrun_exe = "pbrun"
24 pbrun_flags = ""
25
26 success = "BECOME-SUCCESS-.+?"
27
28 task = {
29 "become_method": "community.general.pbrun",
30 }
31 var_options = {}
32 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
33 print(cmd)
34 assert re.match(f"""{pbrun_exe} {pbrun_flags} 'echo {success}; {default_cmd}'""", cmd) is not None
35
36
37 def test_pbrun(mocker, parser, reset_cli_args):
38 options = parser.parse_args([])
39 context._init_global_context(options)
40
41 default_cmd = "/bin/foo"
42 default_exe = "/bin/bash"
43 pbrun_exe = "pbrun"
44 pbrun_flags = ""
45
46 success = "BECOME-SUCCESS-.+?"
47
48 task = {
49 "become_user": "foo",
50 "become_method": "community.general.pbrun",
51 "become_flags": pbrun_flags,
52 }
53 var_options = {}
54 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
55 print(cmd)
56 assert (
57 re.match(f"""{pbrun_exe} {pbrun_flags} -u {task["become_user"]} 'echo {success}; {default_cmd}'""", cmd)
58 is not None
59 )
60
61
62 def test_pbrun_var_varoptions(mocker, parser, reset_cli_args):
63 options = parser.parse_args([])
64 context._init_global_context(options)
65
66 default_cmd = "/bin/foo"
67 default_exe = "/bin/bash"
68 pbrun_exe = "pbrun"
69 pbrun_flags = ""
70
71 success = "BECOME-SUCCESS-.+?"
72
73 task = {
74 "become_user": "foo",
75 "become_method": "community.general.pbrun",
76 "become_flags": "xxx",
77 }
78 var_options = {
79 "ansible_become_user": "bar",
80 "ansible_become_flags": pbrun_flags,
81 }
82 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
83 print(cmd)
84 assert (
85 re.match(
86 f"""{pbrun_exe} {pbrun_flags} -u {var_options["ansible_become_user"]} 'echo {success}; {default_cmd}'""",
87 cmd,
88 )
89 is not None
90 )