Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/become/test_pfexec.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_pfexec_wrap(mocker, parser, reset_cli_args):
18 """Test pfexec with wrap_exe explicitly enabled."""
19 options = parser.parse_args([])
20 context._init_global_context(options)
21
22 default_cmd = "/bin/foo"
23 default_exe = "/bin/bash"
24 pfexec_exe = "pfexec"
25
26 success = "BECOME-SUCCESS-.+?"
27
28 task = {
29 "become_method": "community.general.pfexec",
30 }
31 var_options = {
32 "ansible_pfexec_wrap_execution": "true",
33 }
34 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
35 print(cmd)
36 assert re.match(f"""{pfexec_exe} {default_exe} -c 'echo {success}; {default_cmd}'""", cmd) is not None
37
38
39 def test_pfexec_no_wrap(mocker, parser, reset_cli_args):
40 """Test pfexec with wrap_exe explicitly disabled."""
41 options = parser.parse_args([])
42 context._init_global_context(options)
43
44 default_cmd = "/bin/foo"
45 default_exe = "/bin/bash"
46 pfexec_exe = "pfexec"
47
48 success = "BECOME-SUCCESS-.+?"
49
50 task = {
51 "become_method": "community.general.pfexec",
52 "become_flags": "",
53 }
54 var_options = {
55 "ansible_pfexec_wrap_execution": "false",
56 }
57 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
58 print(cmd)
59 assert re.match(f"""{pfexec_exe} 'echo {success}; {default_cmd}'""", cmd) is not None
60
61
62 def test_pfexec_custom_flags(mocker, parser, reset_cli_args):
63 """Test pfexec with custom flags and wrap_exe enabled."""
64 options = parser.parse_args([])
65 context._init_global_context(options)
66
67 default_cmd = "/bin/foo"
68 default_exe = "/bin/bash"
69 pfexec_exe = "pfexec"
70 pfexec_flags = "-P basic"
71
72 success = "BECOME-SUCCESS-.+?"
73
74 task = {
75 "become_method": "community.general.pfexec",
76 "become_flags": pfexec_flags,
77 }
78 var_options = {
79 "ansible_pfexec_wrap_execution": "true",
80 }
81 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
82 print(cmd)
83 assert (
84 re.match(f"""{pfexec_exe} {pfexec_flags} {default_exe} -c 'echo {success}; {default_cmd}'""", cmd) is not None
85 )
86
87
88 def test_pfexec_varoptions(mocker, parser, reset_cli_args):
89 """Test that var_options override task options."""
90 options = parser.parse_args([])
91 context._init_global_context(options)
92
93 default_cmd = "/bin/foo"
94 default_exe = "/bin/bash"
95 pfexec_exe = "pfexec"
96
97 success = "BECOME-SUCCESS-.+?"
98
99 task = {
100 "become_user": "foo",
101 "become_method": "community.general.pfexec",
102 "become_flags": "xxx",
103 }
104 var_options = {
105 "ansible_become_user": "bar",
106 "ansible_become_flags": "",
107 "ansible_pfexec_wrap_execution": "true",
108 }
109 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
110 print(cmd)
111 # var_options override task flags, so flags should be empty
112 assert re.match(f"""{pfexec_exe} {default_exe} -c 'echo {success}; {default_cmd}'""", cmd) is not None