Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/become/test_dzdo.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_dzdo_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 dzdo_exe = "dzdo"
24 dzdo_flags = "-H -S -n"
25
26 success = "BECOME-SUCCESS-.+?"
27
28 task = {
29 "become_method": "community.general.dzdo",
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"""{dzdo_exe} {dzdo_flags} {default_exe} -c 'echo {success}; {default_cmd}'""", cmd) is not None
35
36
37 def test_dzdo(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 dzdo_exe = "dzdo"
44 dzdo_flags = ""
45
46 success = "BECOME-SUCCESS-.+?"
47
48 task = {
49 "become_user": "foo",
50 "become_method": "community.general.dzdo",
51 "become_flags": dzdo_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(
58 f"""{dzdo_exe} {dzdo_flags} -u {task["become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
59 cmd,
60 )
61 is not None
62 )
63 task["become_pass"] = "testpass"
64 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
65 print(cmd)
66 password_pattern = r"\"\[dzdo via ansible, key=.+?\] password:\""
67 assert (
68 re.match(
69 f"""{dzdo_exe} {dzdo_flags} -p {password_pattern} -u {task["become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
70 cmd,
71 )
72 is not None
73 )
74
75
76 def test_dzdo_varoptions(mocker, parser, reset_cli_args):
77 options = parser.parse_args([])
78 context._init_global_context(options)
79
80 default_cmd = "/bin/foo"
81 default_exe = "/bin/bash"
82 dzdo_exe = "dzdo"
83 dzdo_flags = ""
84
85 success = "BECOME-SUCCESS-.+?"
86
87 task = {
88 "become_user": "foo",
89 "become_method": "community.general.dzdo",
90 "become_flags": "xxx",
91 }
92 var_options = {
93 "ansible_become_user": "bar",
94 "ansible_become_flags": dzdo_flags,
95 }
96 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
97 print(cmd)
98 assert (
99 re.match(
100 f"""{dzdo_exe} {dzdo_flags} -u {var_options["ansible_become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
101 cmd,
102 )
103 is not None
104 )
105 var_options["ansible_become_pass"] = "testpass"
106 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
107 print(cmd)
108 password_pattern = r"\"\[dzdo via ansible, key=.+?\] password:\""
109 assert (
110 re.match(
111 f"""{dzdo_exe} {dzdo_flags} -p {password_pattern} -u {var_options["ansible_become_user"]} {default_exe} -c 'echo {success}; {default_cmd}'""",
112 cmd,
113 )
114 is not None
115 )