Skip to content

Commit

Permalink
fix: /run/user/ is based on UID not username
Browse files Browse the repository at this point in the history
Signed-off-by: Hans-Christoph Steiner <[email protected]>
  • Loading branch information
eighthave committed Dec 2, 2024
1 parent 367bce6 commit 61cb204
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion podman/api/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_runtime_dir() -> str:
return os.environ['XDG_RUNTIME_DIR']
except KeyError:
user = getpass.getuser()
run_user = f'/run/user/{user}'
run_user = f'/run/user/{os.getuid()}'
if os.path.isdir(run_user):
return run_user
fallback = f'/tmp/podmanpy-runtime-dir-fallback-{user}'
Expand Down
14 changes: 12 additions & 2 deletions podman/tests/unit/test_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@
class PathUtilsTestCase(unittest.TestCase):
def setUp(self):
self.xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR')
print('XDG_RUNTIME_DIR', self.xdg_runtime_dir)

@mock.patch.dict(os.environ, clear=True)
def test_get_runtime_dir_env_var_set(self):
with tempfile.TemporaryDirectory() as tmpdir:
os.environ['XDG_RUNTIME_DIR'] = str(tmpdir)
self.assertEqual(str(tmpdir), api.path_utils.get_runtime_dir())

@unittest.skipUnless(os.getenv('XDG_RUNTIME_DIR'), 'XDG_RUNTIME_DIR must be set')
@mock.patch.dict(os.environ, clear=True)
def test_get_runtime_dir_env_var_not_set(self):
if not self.xdg_runtime_dir:
self.skipTest('XDG_RUNTIME_DIR must be set for this test.')
if self.xdg_runtime_dir.startswith('/run/user/'):
self.skipTest("XDG_RUNTIME_DIR in /run/user/, can't check")
self.assertNotEqual(self.xdg_runtime_dir, api.path_utils.get_runtime_dir())

@mock.patch('os.path.isdir', lambda d: False)
@mock.patch.dict(os.environ, clear=True)
def test_get_runtime_dir_env_var_not_set_and_no_run(self):
"""Fake that XDG_RUNTIME_DIR is not set and /run/user/ does not exist."""
if not self.xdg_runtime_dir:
self.skipTest('XDG_RUNTIME_DIR must be set to fetch a working dir.')
self.assertNotEqual(self.xdg_runtime_dir, api.path_utils.get_runtime_dir())


Expand Down

0 comments on commit 61cb204

Please sign in to comment.