Skip to content

Commit

Permalink
Add test for script execution from a snap
Browse files Browse the repository at this point in the history
  • Loading branch information
mcw-work committed Feb 5, 2024
1 parent 19fbfc1 commit eed41ab
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions landscape/client/manager/tests/test_scriptexecution.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,14 @@ def check(result):
result.addCallback(check)
return result

def _run_script(self, username, uid, gid, path):
expected_uid = uid if uid != os.getuid() else None
expected_gid = gid if gid != os.getgid() else None
def _run_script(self, username, uid, gid, path, from_snap=False):

if from_snap:
expected_gid = None
expected_uid = None
else:
expected_uid = uid if uid != os.getuid() else None
expected_gid = gid if gid != os.getgid() else None

factory = StubProcessFactory()
self.plugin.process_factory = factory
Expand All @@ -432,6 +437,10 @@ def _run_script(self, username, uid, gid, path):
patch_chown = mock.patch("os.chown")
mock_chown = patch_chown.start()

patch_issnap = mock.patch("landscape.client.IS_SNAP")
patch_issnap.return_value = from_snap
patch_issnap.start()

result = self.plugin.run_script("/bin/sh", "echo hi", user=username)

self.assertEqual(len(factory.spawns), 1)
Expand All @@ -447,11 +456,15 @@ def _run_script(self, username, uid, gid, path):
protocol.processEnded(Failure(ProcessDone(0)))

def check(result):
mock_chown.assert_called_with()
self.assertEqual(result, "foobar")
if from_snap:
mock.chown.assert_not_called()

Check warning on line 460 in landscape/client/manager/tests/test_scriptexecution.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_scriptexecution.py#L459-L460

Added lines #L459 - L460 were not covered by tests
else:
mock_chown.assert_called_with()
self.assertEqual(result, "foobar")

Check warning on line 463 in landscape/client/manager/tests/test_scriptexecution.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_scriptexecution.py#L462-L463

Added lines #L462 - L463 were not covered by tests

def cleanup(result):
patch_chown.stop()
patch_issnap.stop()
return result

return result.addErrback(check).addBoth(cleanup)
Expand All @@ -469,7 +482,27 @@ def test_user(self):
gid = info.pw_gid
path = info.pw_dir

return self._run_script(username, uid, gid, path)
return self._run_script(username, uid, gid, path, from_snap=False)

def test_user_from_snap(self):
"""
Running a script as a particular user calls
C{IReactorProcess.spawnProcess} with an appropriate C{uid} argument,
with the user's primary group as the C{gid} argument and with the user
home as C{path} argument.
"""
uid = os.getuid()
info = pwd.getpwuid(uid)
username = info.pw_name
gid = info.pw_gid
path = info.pw_dir

if gid == 0:
gid = 1234

Check warning on line 501 in landscape/client/manager/tests/test_scriptexecution.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_scriptexecution.py#L501

Added line #L501 was not covered by tests
if uid == 0:
uid = 5678

Check warning on line 503 in landscape/client/manager/tests/test_scriptexecution.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_scriptexecution.py#L503

Added line #L503 was not covered by tests

return self._run_script(username, uid, gid, path, from_snap=True)

def test_user_no_home(self):
"""
Expand Down

0 comments on commit eed41ab

Please sign in to comment.