Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/become/test_ksu.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_ksu_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 ksu_exe = "ksu"
24 ksu_flags = ""
25
26 success = "BECOME-SUCCESS-.+?"
27
28 task = {
29 "become_user": "foo",
30 "become_method": "community.general.ksu",
31 }
32 var_options = {}
33 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
34 print(cmd)
35 assert (
36 re.match(
37 f"""{ksu_exe} {task["become_user"]} {ksu_flags} -e {default_exe} -c 'echo {success}; {default_cmd}'""", cmd
38 )
39 is not None
40 )
41
42
43 def test_ksu(mocker, parser, reset_cli_args):
44 options = parser.parse_args([])
45 context._init_global_context(options)
46
47 default_cmd = "/bin/foo"
48 default_exe = "/bin/bash"
49 ksu_exe = "ksu"
50 ksu_flags = ""
51
52 success = "BECOME-SUCCESS-.+?"
53
54 task = {
55 "become_user": "foo",
56 "become_method": "community.general.ksu",
57 "become_flags": ksu_flags,
58 }
59 var_options = {}
60 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
61 print(cmd)
62 assert (
63 re.match(
64 f"""{ksu_exe} {task["become_user"]} {ksu_flags} -e {default_exe} -c 'echo {success}; {default_cmd}'""", cmd
65 )
66 is not None
67 )
68
69
70 def test_ksu_varoptions(mocker, parser, reset_cli_args):
71 options = parser.parse_args([])
72 context._init_global_context(options)
73
74 default_cmd = "/bin/foo"
75 default_exe = "/bin/bash"
76 ksu_exe = "ksu"
77 ksu_flags = ""
78
79 success = "BECOME-SUCCESS-.+?"
80
81 task = {
82 "become_user": "foo",
83 "become_method": "community.general.ksu",
84 "become_flags": "xxx",
85 }
86 var_options = {
87 "ansible_become_user": "bar",
88 "ansible_become_flags": ksu_flags,
89 }
90 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
91 print(cmd)
92 assert (
93 re.match(
94 f"""{ksu_exe} {var_options["ansible_become_user"]} {ksu_flags} -e {default_exe} -c 'echo {success}; {default_cmd}'""",
95 cmd,
96 )
97 is not None
98 )