Debian Codegraph @

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

raw
1 # Copyright (c) 2026 Ansible Project
2 #
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 import re
9
10 from ansible import context
11
12 from .helper import call_become_plugin
13
14
15 def test_machinectl_basic(mocker, parser, reset_cli_args):
16 options = parser.parse_args([])
17 context._init_global_context(options)
18
19 default_cmd = "/bin/foo"
20 default_exe = "/bin/sh"
21 machinectl_exe = "machinectl"
22
23 success = "BECOME-SUCCESS-.+?"
24
25 task = {
26 "become_method": "community.general.machinectl",
27 "become_user": "root",
28 }
29 var_options = {}
30 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
31 assert (
32 re.match(
33 f"SYSTEMD_COLORS=0 {machinectl_exe} -q shell root@ {default_exe} -c 'echo {success}; {default_cmd}'",
34 cmd,
35 )
36 is not None
37 )
38
39
40 def test_machinectl_flags(mocker, parser, reset_cli_args):
41 options = parser.parse_args([])
42 context._init_global_context(options)
43
44 default_cmd = "/bin/foo"
45 default_exe = "/bin/sh"
46 machinectl_exe = "machinectl"
47 machinectl_flags = "--setenv=FOO=bar"
48
49 success = "BECOME-SUCCESS-.+?"
50
51 task = {
52 "become_method": "community.general.machinectl",
53 "become_user": "root",
54 "become_flags": machinectl_flags,
55 }
56 var_options = {}
57 cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
58 assert (
59 re.match(
60 f"SYSTEMD_COLORS=0 {machinectl_exe} -q shell --setenv=FOO=bar root@ {default_exe} -c 'echo {success}; {default_cmd}'",
61 cmd,
62 )
63 is not None
64 )