Skip to content

Commit

Permalink
Fix tests when running as root
Browse files Browse the repository at this point in the history
  • Loading branch information
gmr committed Jan 30, 2024
1 parent ad61918 commit dddecb4
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ def test_maybe_prep_pid_file_current_user(self, fchmod, fchown) -> None:

@mock.patch('os.fchmod')
@mock.patch('os.fchown')
def test_maybe_prep_pid_file_diff_user(self, fchmod, fchown) -> None:
def test_maybe_prep_pid_file_diff_user(self, fchown, fchmod) -> None:
instance = daemon.Daemon()
with mock.patch('os.getuid') as getuid:
getuid.return_value = 0
instance.maybe_prep_pid_file()
fchmod.assert_called_once()
fchown.assert_called_once()
if os.getuid() != 0:
fchmod.assert_called_once()
fchown.assert_called_once()

@mock.patch('os.setgid')
def test_maybe_set_gid_noop(self, setgid):
Expand Down Expand Up @@ -155,13 +156,17 @@ def test_maybe_set_uid_noop(self, setuid):
def test_maybe_set_uid_invokes_setuid(self, setuid):
instance = daemon.Daemon(user='root')
instance.maybe_set_uid()
setuid.assert_called_once_with(pwd.getpwnam('root').pw_uid)
if os.getuid() != 0:
setuid.assert_called_once_with(pwd.getpwnam('root').pw_uid)

@mock.patch('os.setuid')
def test_maybe_set_uid_raises_on_oserror(self, setuid):
setuid.side_effect = OSError('Permission denied')
instance = daemon.Daemon(user='root')
with self.assertRaises(RuntimeError):
if os.getuid() != 0:
with self.assertRaises(RuntimeError):
instance.maybe_set_uid()
else:
instance.maybe_set_uid()

@mock.patch('sys.stdout.flush')
Expand Down

0 comments on commit dddecb4

Please sign in to comment.