summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/become.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib/ansible_test/_internal/become.py')
-rw-r--r--test/lib/ansible_test/_internal/become.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/lib/ansible_test/_internal/become.py b/test/lib/ansible_test/_internal/become.py
index dc0a208a62..5a5506a14e 100644
--- a/test/lib/ansible_test/_internal/become.py
+++ b/test/lib/ansible_test/_internal/become.py
@@ -5,9 +5,18 @@ import abc
import shlex
import typing as t
+from .util import (
+ get_subclasses,
+)
+
class Become(metaclass=abc.ABCMeta):
"""Base class for become implementations."""
+ @classmethod
+ def name(cls):
+ """The name of this plugin."""
+ return cls.__name__.lower()
+
@property
@abc.abstractmethod
def method(self): # type: () -> str
@@ -18,6 +27,38 @@ class Become(metaclass=abc.ABCMeta):
"""Return the given command, if any, with privilege escalation."""
+class Doas(Become):
+ """Become using 'doas'."""
+ @property
+ def method(self): # type: () -> str
+ """The name of the Ansible become plugin that is equivalent to this."""
+ raise NotImplementedError('Ansible has no built-in doas become plugin.')
+
+ def prepare_command(self, command): # type: (t.List[str]) -> t.List[str]
+ """Return the given command, if any, with privilege escalation."""
+ become = ['doas', '-n']
+
+ if command:
+ become.extend(['sh', '-c', ' '.join(shlex.quote(c) for c in command)])
+ else:
+ become.extend(['-s'])
+
+ return become
+
+
+class DoasSudo(Doas):
+ """Become using 'doas' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
+ @classmethod
+ def name(cls):
+ """The name of this plugin."""
+ return 'doas_sudo'
+
+ @property
+ def method(self): # type: () -> str
+ """The name of the Ansible become plugin that is equivalent to this."""
+ return 'sudo'
+
+
class Su(Become):
"""Become using 'su'."""
@property
@@ -35,6 +76,19 @@ class Su(Become):
return become
+class SuSudo(Su):
+ """Become using 'su' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
+ @classmethod
+ def name(cls):
+ """The name of this plugin."""
+ return 'su_sudo'
+
+ @property
+ def method(self): # type: () -> str
+ """The name of the Ansible become plugin that is equivalent to this."""
+ return 'sudo'
+
+
class Sudo(Become):
"""Become using 'sudo'."""
@property
@@ -50,3 +104,6 @@ class Sudo(Become):
become.extend(['sh', '-c', ' '.join(shlex.quote(c) for c in command)])
return become
+
+
+SUPPORTED_BECOME_METHODS = {cls.name(): cls for cls in get_subclasses(Become)}