Issue #14698: Make test_posix more robust when the current UID doesn't have an

associated pwd entry.
This commit is contained in:
Charles-François Natali 2012-05-02 20:04:40 +02:00
commit d59240de83
1 changed files with 9 additions and 3 deletions

View File

@ -108,7 +108,11 @@ class PosixTester(unittest.TestCase):
# If a non-privileged user invokes it, it should fail with OSError # If a non-privileged user invokes it, it should fail with OSError
# EPERM. # EPERM.
if os.getuid() != 0: if os.getuid() != 0:
try:
name = pwd.getpwuid(posix.getuid()).pw_name name = pwd.getpwuid(posix.getuid()).pw_name
except KeyError:
# the current UID may not have a pwd entry
raise unittest.SkipTest("need a pwd entry")
try: try:
posix.initgroups(name, 13) posix.initgroups(name, 13)
except OSError as e: except OSError as e:
@ -624,8 +628,9 @@ class PosixTester(unittest.TestCase):
def test_getgrouplist(self): def test_getgrouplist(self):
with os.popen('id -G') as idg: with os.popen('id -G') as idg:
groups = idg.read().strip() groups = idg.read().strip()
ret = idg.close()
if not groups: if ret != 0 or not groups:
raise unittest.SkipTest("need working 'id -G'") raise unittest.SkipTest("need working 'id -G'")
self.assertEqual( self.assertEqual(
@ -637,8 +642,9 @@ class PosixTester(unittest.TestCase):
def test_getgroups(self): def test_getgroups(self):
with os.popen('id -G') as idg: with os.popen('id -G') as idg:
groups = idg.read().strip() groups = idg.read().strip()
ret = idg.close()
if not groups: if ret != 0 or not groups:
raise unittest.SkipTest("need working 'id -G'") raise unittest.SkipTest("need working 'id -G'")
# 'id -G' and 'os.getgroups()' should return the same # 'id -G' and 'os.getgroups()' should return the same