]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-systemd-tmpfiles: Fix execution when user is not in /etc/passwd
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 28 Jan 2023 14:09:25 +0000 (15:09 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sun, 29 Jan 2023 17:49:38 +0000 (17:49 +0000)
We might be running in a chroot as a uid that doesn't exist in /etc/passwd.
Let's make sure we don't fail in this scenario.

We pass $HOME when resetting the env so that we can find a home directory
and skip tests that depend on user name/group.

test/test-systemd-tmpfiles.py

index af9ff9bf9319086cb26f66dc373439d153c4c94e..a60a6909a4fc8729f8ca6d7ce0a820b8fe7e65cd 100755 (executable)
@@ -72,7 +72,7 @@ def test_uninitialized_t():
         return
 
     test_line('w /foo - - - - "specifier for --user %t"',
-              user=True, returncode=0, extra={'env':{}})
+              user=True, returncode=0, extra={'env':{'HOME': os.getenv('HOME')}})
 
 def test_content(line, expected, *, user, extra={}, subpath='/arg', path_cb=None):
     d = tempfile.TemporaryDirectory(prefix='test-systemd-tmpfiles.')
@@ -101,11 +101,21 @@ def test_valid_specifiers(*, user):
     test_content('f {} - - - - %U', '{}'.format(os.getuid() if user else 0), user=user)
     test_content('f {} - - - - %G', '{}'.format(os.getgid() if user else 0), user=user)
 
-    puser = pwd.getpwuid(os.getuid() if user else 0)
-    test_content('f {} - - - - %u', '{}'.format(puser.pw_name), user=user)
+    try:
+        puser = pwd.getpwuid(os.getuid() if user else 0)
+    except KeyError:
+        puser = None
 
-    pgroup = grp.getgrgid(os.getgid() if user else 0)
-    test_content('f {} - - - - %g', '{}'.format(pgroup.gr_name), user=user)
+    if puser:
+        test_content('f {} - - - - %u', '{}'.format(puser.pw_name), user=user)
+
+    try:
+        pgroup = grp.getgrgid(os.getgid() if user else 0)
+    except KeyError:
+        pgroup = None
+
+    if pgroup:
+        test_content('f {} - - - - %g', '{}'.format(pgroup.gr_name), user=user)
 
     # Note that %h is the only specifier in which we look the environment,
     # because we check $HOME. Should we even be doing that?