Skip to content

Commit

Permalink
Dev: unittest: update unit tests for previous changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasyang2022 committed Mar 13, 2024
1 parent 8b6308b commit b901b4e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions test/unittests/test_sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def setUp(self) -> None:
self.local_shell.geteuid = mock.Mock(self.local_shell.geteuid)
self.local_shell.hostname = mock.Mock(self.local_shell.hostname)

@mock.patch('os.environ')
@mock.patch('subprocess.run')
def test_su_subprocess_run(self, mock_run: mock.MagicMock):
def test_su_subprocess_run(self, mock_run: mock.MagicMock, mock_environ: mock.MagicMock):
self.local_shell.get_effective_user_name.return_value = 'root'
self.local_shell.geteuid.return_value = 0
self.local_shell.su_subprocess_run(
Expand All @@ -24,10 +25,12 @@ def test_su_subprocess_run(self, mock_run: mock.MagicMock):
mock_run.assert_called_once_with(
['su', 'alice', '--login', '-s', '/bin/sh', '-c', 'foo'],
input=b'bar',
env=mock_environ,
)

@mock.patch('os.environ')
@mock.patch('subprocess.run')
def test_su_subprocess_run_as_root(self, mock_run: mock.MagicMock):
def test_su_subprocess_run_as_root(self, mock_run: mock.MagicMock, mock_environ: mock.MagicMock):
self.local_shell.get_effective_user_name.return_value = 'root'
self.local_shell.geteuid.return_value = 0
self.local_shell.su_subprocess_run(
Expand All @@ -37,6 +40,7 @@ def test_su_subprocess_run_as_root(self, mock_run: mock.MagicMock):
mock_run.assert_called_once_with(
['/bin/sh', '-c', 'foo'],
input=b'bar',
env=mock_environ,
)

@mock.patch('subprocess.run')
Expand Down Expand Up @@ -124,8 +128,9 @@ def test_subprocess_run_without_input_with_input_kwargs(self):
)
self.ssh_shell.local_shell.su_subprocess_run.assert_not_called()

@mock.patch('os.environ')
@mock.patch('subprocess.run')
def test_subprocess_run_without_input_local(self, mock_run):
def test_subprocess_run_without_input_local(self, mock_run, mock_environ: mock.MagicMock):
self.ssh_shell.subprocess_run_without_input(
'node1', 'bob',
'foo',
Expand All @@ -138,6 +143,7 @@ def test_subprocess_run_without_input_local(self, mock_run):
input=b'foo',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=mock_environ,
)


Expand Down

0 comments on commit b901b4e

Please sign in to comment.