tests: fix group membership check in sbang tests. (#33658)

Fixes an issue on the RHEL8 UBI container where this test would fail because `gr_mem`
was empty for every entry in the `grp` DB.

You have to check *both* the `pwd` database (which has primary groups) and `grp` (which
has other gorups) to do this correctly.

- [x] update `llnl.util.filesystem.group_ids()` to do this
- [x] use it in the `sbang` test
This commit is contained in:
Todd Gamblin 2022-11-02 03:00:16 -07:00 committed by GitHub
parent 4bad9f9b13
commit a4d978be59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -505,8 +505,15 @@ def group_ids(uid=None):
if uid is None: if uid is None:
uid = getuid() uid = getuid()
user = pwd.getpwuid(uid).pw_name
return [g.gr_gid for g in grp.getgrall() if user in g.gr_mem] pwd_entry = pwd.getpwuid(uid)
user = pwd_entry.pw_name
# user's primary group id may not be listed in grp (i.e. /etc/group)
# you have to check pwd for that, so start the list with that
gids = [pwd_entry.pw_gid]
return sorted(set(gids + [g.gr_gid for g in grp.getgrall() if user in g.gr_mem]))
@system_path_filter(arg_slice=slice(1)) @system_path_filter(arg_slice=slice(1))

View File

@ -7,7 +7,6 @@
Test that Spack's shebang filtering works correctly. Test that Spack's shebang filtering works correctly.
""" """
import filecmp import filecmp
import getpass
import os import os
import shutil import shutil
import stat import stat
@ -273,6 +272,9 @@ def configure_group_perms():
# and grp does not act on remote groups. # and grp does not act on remote groups.
# To ensure we find a group we can operate on, we get take the first group # To ensure we find a group we can operate on, we get take the first group
# listed which has the current user as a member. # listed which has the current user as a member.
gid = fs.group_ids(os.getuid())[0]
group_name = grp.getgrgid(gid).gr_name
conf = syaml.load_config( conf = syaml.load_config(
"""\ """\
all: all:
@ -281,7 +283,7 @@ def configure_group_perms():
write: group write: group
group: {0} group: {0}
""".format( """.format(
[g.gr_name for g in grp.getgrall() if getpass.getuser() in g.gr_mem][0] group_name
) )
) )
spack.config.set("packages", conf, scope="user") spack.config.set("packages", conf, scope="user")