diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-01-28 15:09:25 +0100 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2023-01-29 17:49:38 +0000 |
commit | 60f42f7ec2d584cd7cfc8eb2eb617fcdab6aabb8 (patch) | |
tree | c4a9d383322f6f12a89503e5a20ed4bb3872097e /test | |
parent | 98a6d8505dad62e0aa8deb2185249825bce176ef (diff) | |
download | systemd-60f42f7ec2d584cd7cfc8eb2eb617fcdab6aabb8.tar.gz |
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.
Diffstat (limited to 'test')
-rwxr-xr-x | test/test-systemd-tmpfiles.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/test/test-systemd-tmpfiles.py b/test/test-systemd-tmpfiles.py index af9ff9bf93..a60a6909a4 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? |