Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support setting a default username for command execution #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion device_test_core/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
should_cleanup: bool = None,
use_sudo: bool = True,
config: Dict[str, Any] = None,
user: str = "",
):
self._name = name
self._device_id = (
Expand All @@ -36,6 +37,7 @@ def __init__(
self._use_sudo = use_sudo
self._config = config
self._should_cleanup = should_cleanup
self._user = user

@property
def test_start_time(self) -> datetime:
Expand Down Expand Up @@ -83,16 +85,25 @@ def use_sudo(self) -> bool:
"""
return self._use_sudo

def user(self) -> str:
"""Shell User

Returns:
str: user to execute the commands under
"""
return self._user

@abstractmethod
def execute_command(
self, cmd: str, log_output: bool = True, shell: bool = True, **kwargs
self, cmd: str, log_output: bool = True, shell: bool = True, user: str = "", **kwargs
) -> CmdOutput:
"""Execute a command inside the docker container

Args:
cmd (str): Command to execute
log_output (bool, optional): Log the stdout after the command has executed
shell (bool, optional): Execute the command in a shell
user (bool, optional): User that the shell is executed under
**kwargs (Any, optional): Additional keyword arguments

Returns:
Expand Down
6 changes: 4 additions & 2 deletions device_test_core/docker/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ def get_device_stats(self) -> Any:
return self.container.stats(stream=False)

def execute_command(
self, cmd: str, log_output: bool = True, shell: bool = True, **kwargs
self, cmd: str, log_output: bool = True, shell: bool = True, user: str = "", **kwargs
) -> CmdOutput:
"""Execute a command inside the docker container

Args:
cmd (str): Command to execute
log_output (bool, optional): Log the stdout after the command has executed
shell (bool, optional): Execute the command in a shell
user (bool, optional): User that the shell is executed under
**kwargs (Any, optional): Additional keyword arguments

Raises:
Expand All @@ -162,6 +163,7 @@ def execute_command(
CmdOutput: Command output details, e.g. stdout, stderr and return_code
"""
run_cmd = []
user = user or self.user()

use_sudo = kwargs.pop("sudo", self.use_sudo())
if use_sudo:
Expand All @@ -176,7 +178,7 @@ def execute_command(
else:
run_cmd.append(cmd)

exit_code, output = self.container.exec_run(run_cmd, demux=True)
exit_code, output = self.container.exec_run(run_cmd, user=user or self.user(), demux=True)
stdout, stderr = output
if log_output:
log.info(
Expand Down
9 changes: 8 additions & 1 deletion device_test_core/local/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ def get_device_stats(self) -> Any:
)

def execute_command(
self, cmd: str, log_output: bool = True, shell: bool = True, **kwargs
self, cmd: str, log_output: bool = True, shell: bool = True, user: str = "", **kwargs
) -> CmdOutput:
"""Execute a command inside the docker container

Args:
cmd (str): Command to execute
log_output (bool, optional): Log the stdout after the command has executed
shell (bool, optional): Execute the command in a shell
user (bool, optional): User that the shell is executed under
**kwargs (Any, optional): Additional keyword arguments

Raises:
Expand All @@ -125,6 +126,12 @@ def execute_command(
"""
run_cmd = []

# Note: the local adapter has limited options to change which user is used to run, so use
# sudo to run as a different user
user = user or self.user()
if user:
run_cmd.extend(["sudo", "-E", "-u", user])

use_sudo = kwargs.pop("sudo", self.use_sudo())
if use_sudo:
run_cmd.extend(["sudo", "-E"])
Expand Down
8 changes: 7 additions & 1 deletion device_test_core/ssh/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ def _connect(self):
self._client.open()

def execute_command(
self, cmd: str, log_output: bool = True, shell: bool = True, **kwargs
self, cmd: str, log_output: bool = True, shell: bool = True, user: str = "", **kwargs
) -> CmdOutput:
"""Execute a command

Args:
cmd (str): Command to execute
log_output (bool, optional): Log the stdout after the command has executed
shell (bool, optional): Execute the command in a shell
user (bool, optional): User that the shell is executed under
**kwargs (Any, optional): Additional keyword arguments

Raises:
Expand All @@ -200,6 +201,11 @@ def execute_command(
"""
run_cmd = []

# Note: the ssh user is
user = user or self.user()
if user:
run_cmd.extend(["sudo", "-E", "-u", user])

use_sudo = kwargs.pop("sudo", self.use_sudo())
if use_sudo:
run_cmd.extend(["sudo", "-E"])
Expand Down