Skip to content

Commit 43494fd

Browse files
author
javierfh03
committed
Fix the use of PrivilegesError
1 parent ddd28c8 commit 43494fd

5 files changed

Lines changed: 25 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6-
## [0.2.0] - 2024-02-29
6+
## [0.2.0] - 2024-01-01
77
### Changed
88
- @javierfh03 - Add Authentication and Privileges errors and changes in the command managers structure.
99
### Fixed
1010
- @javierfh03 - Fix error when execute complex commands.
1111
- @javierfh03 - Fix error when return additional line in list in the output from command.
1212
- @javierfh03 - Fix wait to execute command.
13+
- @javierfh03 - Fix the use of PrivilegesError.
1314

1415
## [0.1.1] - 2024-02-14
1516
### Changed

src/shell_executor_lib/manager/command_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async def execute_command(self, command: str, sudo: bool = False, *stdin: str) -
5858
try:
5959
return await executor(f"su - {self.user} -c \"sudo -S {command}\"", self.__password,
6060
self.__password, *stdin)
61-
except CommandError:
62-
raise PrivilegesError(self.user)
61+
except CommandError as command_error:
62+
if "is not in the sudoers file" in command_error.response:
63+
raise PrivilegesError(self.user)
64+
raise command_error
6365

6466
return await executor(f"su - {self.user} -c \"{command}\"", self.__password, *stdin)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Exposed mock classes and methods."""
22
from mock_shell_executor_lib.mock_command_executor import (mock_subprocess_error_exit_code, mock_subprocess_correct,
33
mock_subprocess_invalid_exit_code,
4-
mock_subprocess_invalid_outs, mock_output_data)
4+
mock_subprocess_invalid_outs, mock_output_data,
5+
mock_subprocess_sudo_error_exit_code)
56

67
mock_asyncio_subprocess = "asyncio.create_subprocess_shell"

tests/mock_shell_executor_lib/mock_command_executor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
mock_subprocess_error_exit_code.stderr.read.return_value = b""
1212
mock_subprocess_error_exit_code.returncode = 1
1313

14+
mock_subprocess_sudo_error_exit_code = AsyncMock()
15+
mock_subprocess_sudo_error_exit_code.stdout.read.return_value = b""
16+
mock_subprocess_sudo_error_exit_code.stderr.read.return_value = b"pepe is not in the sudoers file."
17+
mock_subprocess_sudo_error_exit_code.returncode = 1
18+
1419
mock_subprocess_invalid_exit_code = AsyncMock()
1520
mock_subprocess_invalid_exit_code.stdout.read.return_value = b"Output data\nOutput data\nOutput data"
1621
mock_subprocess_invalid_exit_code.stderr.read.return_value = b""

tests/test_shell_executor_lib/test_command_manager.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from unittest import mock
44

55
from mock_shell_executor_lib import mock_asyncio_subprocess, mock_subprocess_correct, mock_output_data, \
6-
mock_subprocess_error_exit_code
7-
from shell_executor_lib import CommandManager, PrivilegesError, AuthenticationError
6+
mock_subprocess_error_exit_code, mock_subprocess_sudo_error_exit_code
7+
from shell_executor_lib import CommandManager, PrivilegesError, AuthenticationError, CommandError
88

99

1010
class TestCommandManager(unittest.IsolatedAsyncioTestCase):
@@ -28,9 +28,16 @@ async def test_execute_command_with_privileges(self) -> None:
2828
self.assertEqual(await (await CommandManager.init("augusto", "augusto"))
2929
.execute_command("ls", True), mock_output_data)
3030

31-
async def test_execute_command_with_privileges_error(self) -> None:
32-
"""Test error if the user does not have privileges."""
31+
async def test_execute_command_with_privileges_using_no_sudo_user(self) -> None:
32+
"""Test error if the command return an error."""
3333
with mock.patch(mock_asyncio_subprocess, side_effect=(mock_subprocess_correct,
34-
mock_subprocess_error_exit_code)):
34+
mock_subprocess_sudo_error_exit_code)):
3535
with self.assertRaises(PrivilegesError):
3636
await (await CommandManager.init("augusto", "augusto")).execute_command("ls", True)
37+
38+
async def test_error_when_execute_command_with_privileges(self) -> None:
39+
"""Test error if the command return an error."""
40+
with mock.patch(mock_asyncio_subprocess, side_effect=(mock_subprocess_correct,
41+
mock_subprocess_error_exit_code)):
42+
with self.assertRaises(CommandError):
43+
await (await CommandManager.init("augusto", "augusto")).execute_command("ls", True)

0 commit comments

Comments
 (0)