diff --git a/landscape/client/manager/tests/test_scriptexecution.py b/landscape/client/manager/tests/test_scriptexecution.py index 0065f8670..c65e8fd64 100644 --- a/landscape/client/manager/tests/test_scriptexecution.py +++ b/landscape/client/manager/tests/test_scriptexecution.py @@ -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 @@ -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) @@ -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() + else: + mock_chown.assert_called_with() + self.assertEqual(result, "foobar") def cleanup(result): patch_chown.stop() + patch_issnap.stop() return result return result.addErrback(check).addBoth(cleanup) @@ -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 + if uid == 0: + uid = 5678 + + return self._run_script(username, uid, gid, path, from_snap=True) def test_user_no_home(self): """