From 60f42f7ec2d584cd7cfc8eb2eb617fcdab6aabb8 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Sat, 28 Jan 2023 15:09:25 +0100 Subject: [PATCH] test-systemd-tmpfiles: Fix execution when user is not in /etc/passwd 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 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/test-systemd-tmpfiles.py b/test/test-systemd-tmpfiles.py index af9ff9bf931..a60a6909a4f 100755 --- a/test/test-systemd-tmpfiles.py +++ b/test/test-systemd-tmpfiles.py @@ -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? -- 2.47.3