Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/run/user/$UID as fallback if XDG_RUNTIME_DIR is not set #475

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion podman/api/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@


def get_runtime_dir() -> str:
"""Returns the runtime directory for the current user"""
"""Returns the runtime directory for the current user
The value in XDG_RUNTIME_DIR is preferred, but that is not always set, for
example, on headless servers. /run/user/$UID is defined in the XDG documentation.
"""
try:
return os.environ['XDG_RUNTIME_DIR']
except KeyError:
user = getpass.getuser()
run_user = f'/run/user/{user}'
if os.path.isdir(run_user):
return run_user
fallback = f'/tmp/podmanpy-runtime-dir-fallback-{user}'

try:
Expand Down
28 changes: 28 additions & 0 deletions podman/tests/unit/test_path_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import datetime
import os
import unittest
import tempfile
from unittest import mock

from podman import api


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):
self.assertNotEqual(self.xdg_runtime_dir, api.path_utils.get_runtime_dir())


if __name__ == '__main__':
unittest.main()