mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-02 23:31:25 -07:00
Fix become plugins (#50)
* Fix become plugins. * Fix become unit tests to avoid play_context.make_become_cmd. * Remove hack. * Remove explicit defaults. Adjust tests to be more like Ansible itself. * Forgot two lines. * Rewrite tests (again). * Rename play_context -> task, add possibility to pass var_options. * Add var_options variants. * Properly test overwriting.
This commit is contained in:
parent
7d8ca8bdbb
commit
0026c9f5b2
14 changed files with 238 additions and 84 deletions
|
@ -10,30 +10,54 @@ __metaclass__ = type
|
|||
import re
|
||||
|
||||
from ansible import context
|
||||
from ansible.playbook.play_context import PlayContext
|
||||
from ansible.plugins.loader import become_loader
|
||||
|
||||
from .helper import call_become_plugin
|
||||
|
||||
|
||||
def test_doas(mocker, parser, reset_cli_args):
|
||||
options = parser.parse_args([])
|
||||
context._init_global_context(options)
|
||||
play_context = PlayContext()
|
||||
|
||||
default_cmd = "/bin/foo"
|
||||
default_exe = "/bin/bash"
|
||||
doas_exe = 'doas'
|
||||
doas_flags = '-n'
|
||||
|
||||
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
|
||||
assert cmd == default_cmd
|
||||
success = 'BECOME-SUCCESS-.+?'
|
||||
|
||||
task = {
|
||||
'become_user': 'foo',
|
||||
'become_method': 'community.general.doas',
|
||||
'become_flags': doas_flags,
|
||||
}
|
||||
var_options = {}
|
||||
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
|
||||
print(cmd)
|
||||
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, task['become_user'], default_exe, success,
|
||||
default_cmd), cmd) is not None)
|
||||
|
||||
|
||||
def test_doas_varoptions(mocker, parser, reset_cli_args):
|
||||
options = parser.parse_args([])
|
||||
context._init_global_context(options)
|
||||
|
||||
default_cmd = "/bin/foo"
|
||||
default_exe = "/bin/bash"
|
||||
doas_exe = 'doas'
|
||||
doas_flags = '-n'
|
||||
|
||||
success = 'BECOME-SUCCESS-.+?'
|
||||
|
||||
play_context.become = True
|
||||
play_context.become_user = 'foo'
|
||||
play_context.set_become_plugin(become_loader.get('doas'))
|
||||
play_context.become_method = 'doas'
|
||||
play_context.become_flags = doas_flags
|
||||
cmd = play_context.make_become_cmd(cmd=default_cmd, executable=default_exe)
|
||||
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, play_context.become_user, default_exe, success,
|
||||
task = {
|
||||
'become_user': 'foo',
|
||||
'become_method': 'community.general.doas',
|
||||
'become_flags': 'xxx',
|
||||
}
|
||||
var_options = {
|
||||
'ansible_become_user': 'bar',
|
||||
'ansible_become_flags': doas_flags,
|
||||
}
|
||||
cmd = call_become_plugin(task, var_options, cmd=default_cmd, executable=default_exe)
|
||||
print(cmd)
|
||||
assert (re.match("""%s %s -u %s %s -c 'echo %s; %s'""" % (doas_exe, doas_flags, var_options['ansible_become_user'], default_exe, success,
|
||||
default_cmd), cmd) is not None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue