Enable gating tests for shadow-utils

Resolves: rhbz#1962811

Files from test directory are updated from the internal 8.5.0 repo
This commit is contained in:
Steeve Goveas 2021-05-28 18:37:25 +05:30
parent 3eb64bd5e9
commit d3db3c8cb5
5 changed files with 213 additions and 197 deletions

7
gating.yaml Normal file
View File

@ -0,0 +1,7 @@
--- !Policy
product_versions:
- rhel-9
decision_context: osci_compose_gate
rules:
- !PassingTestCaseRule {test_case_name: osci.brew-build.tier0.functional}
- !PassingTestCaseRule {test_case_name: baseos-ci.brew-build.tier1.functional}

View File

@ -72,6 +72,6 @@ $(METADATA): Makefile
@echo "TestTime: 5m" >> $(METADATA) @echo "TestTime: 5m" >> $(METADATA)
@echo "RunFor: $(PACKAGE_NAME)" >> $(METADATA) @echo "RunFor: $(PACKAGE_NAME)" >> $(METADATA)
@echo "Requires: $(PACKAGE_NAME)" >> $(METADATA) @echo "Requires: $(PACKAGE_NAME)" >> $(METADATA)
@echo "Requires: python" >> $(METADATA) @echo "Requires: python3-devel" >> $(METADATA)
rhts-lint $(METADATA) rhts-lint $(METADATA)

View File

@ -3,9 +3,13 @@
. /usr/share/beakerlib/beakerlib.sh || exit 1 . /usr/share/beakerlib/beakerlib.sh || exit 1
rlJournalStart rlJournalStart
rlFileBackup --clean /etc/default/useradd- /etc/default/useradd rlFileBackup --clean /etc/default/useradd- /etc/default/useradd /etc/nsswitch.conf
setenforce 0 setenforce 0
python sanity_test.py -v # We do not want sssd to interfere
for i in passwd group shadow ; do
sed -i "s/^$i:.*/$i: files/" /etc/nsswitch.conf
done
python3 sanity_test.py -v
setenforce 1 setenforce 1
rlFileRestore rlFileRestore

View File

@ -15,7 +15,7 @@ TODO:
import unittest import unittest
import pwd import pwd
import grp import grp
import commands import subprocess
import os import os
import os.path import os.path
import sys import sys
@ -24,14 +24,14 @@ import tempfile
import rpm import rpm
import shutil import shutil
from UserDict import UserDict from collections import UserDict
class RedHatVersion(object): class RedHatVersion(object):
def __init__(self, type=None, version=None, release=None): def __init__(self, type=None, version=None, release=None):
self.type = type self.type = type
self.version = version self.version = version
self.release = release self.release = release
self.rhel = False self.rhel = False
def __eq__( self, other): def __eq__( self, other):
""" """
@ -64,9 +64,12 @@ class RedHatVersion(object):
rhel_versions[ver_rpm] = 6 rhel_versions[ver_rpm] = 6
if ver_rpm in rhel_versions.keys(): if ver_rpm in rhel_versions.keys():
return (rhel_versions[ver_rpm], rel_rpm) return (rhel_versions[ver_rpm], rel_rpm)
elif b'.' in ver_rpm:
ver_rpm = ver_rpm.split(b'.')[0]
return (int(ver_rpm), rel_rpm)
def is_rhel(self): def is_rhel(self):
return self.rhel return self.rhel
def get_info(self): def get_info(self):
""" """
@ -80,11 +83,11 @@ class RedHatVersion(object):
mi.pattern('name', rpm.RPMMIRE_GLOB, 'redhat-release*') mi.pattern('name', rpm.RPMMIRE_GLOB, 'redhat-release*')
if mi: if mi:
self.rhel = True self.rhel = True
return ('RHEL',) + self.__get_rhel_info(mi) return ('RHEL',) + self.__get_rhel_info(mi)
else: else:
mi = ts.dbMatch('name','fedora-release') mi = ts.dbMatch('name','fedora-release')
self.rhel = False self.rhel = False
if mi.count() != 0: if mi.count() != 0:
return ('Fedora',) + self.__get_fedora_info(mi) return ('Fedora',) + self.__get_fedora_info(mi)
@ -189,17 +192,20 @@ class LoginDefsParser(UserDict):
def __init__(self, path="/etc/login.defs",split=None): def __init__(self, path="/etc/login.defs",split=None):
self.path = path self.path = path
self.data = []
UserDict.__init__(self) UserDict.__init__(self)
try: try:
defs = open(path) defs = open(path)
except IOError: except IOError:
print "Could not open the config file %s" % (path) print("Could not open the config file %s" % (path))
return
for line in defs: for line in defs:
if line.startswith('#'): continue if line.startswith('#'): continue
fields = line.split(split) fields = line.split(split)
if len(fields) != 2: continue # yeah, we're dirty if len(fields) != 2: continue # yeah, we're dirty
self.data[fields[0]] = fields[1] self.data[fields[0]] = fields[1]
defs.close()
def serialize(self): def serialize(self):
output = open(self.path, "w+") output = open(self.path, "w+")
@ -245,7 +251,7 @@ class ShadowUtilsTestBase:
""" Handy routines """ """ Handy routines """
def getDefaults(self): def getDefaults(self):
# get the default values for so we can compare against that # get the default values for so we can compare against that
(status, defaults_str) = commands.getstatusoutput('useradd -D') (status, defaults_str) = subprocess.getstatusoutput('useradd -D')
if status != 0: if status != 0:
raise RuntimeError("Could not get the default values for useradd") raise RuntimeError("Could not get the default values for useradd")
return dict([ rec.split("=") for rec in defaults_str.split("\n") ]) return dict([ rec.split("=") for rec in defaults_str.split("\n") ])
@ -265,15 +271,15 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
self.username = "test-shadow-utils-useradd" self.username = "test-shadow-utils-useradd"
def tearDown(self): def tearDown(self):
commands.getstatusoutput("userdel -r %s" % (self.username)) subprocess.getstatusoutput("userdel -r %s" % (self.username))
def testBasicAdd(self): def testBasicAdd(self):
""" useradd: Tests basic adding of a user """ """ useradd: Tests basic adding of a user """
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
runme = "useradd %s" % (self.username) runme = "useradd %s" % (self.username)
(status, output) = commands.getstatusoutput(runme) (status, output) = subprocess.getstatusoutput(runme)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -281,9 +287,9 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
def testExistingUser(self): def testExistingUser(self):
""" useradd: Test that user with an existing name cannot be added """ """ useradd: Test that user with an existing name cannot be added """
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.assertNotEqual(commands.getstatusoutput("useradd %s" % (self.username))[0], 0, "FAIL: User that already exists added") self.assertNotEqual(subprocess.getstatusoutput("useradd %s" % (self.username))[0], 0, "FAIL: User that already exists added")
def testCustomUID(self): def testCustomUID(self):
""" useradd: Adding an user with a specific UID """ """ useradd: Adding an user with a specific UID """
@ -293,8 +299,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
expected["pw_uid"] = UID expected["pw_uid"] = UID
runme = "useradd %s -u %d" % (self.username, UID) runme = "useradd %s -u %d" % (self.username, UID)
(status, output) = commands.getstatusoutput(runme) (status, output) = subprocess.getstatusoutput(runme)
self.failUnlessEqual(status, 0, "Issued command: %s\n" % (runme) + "Got from useradd: %s\n" % (output)) self.assertEqual(status, 0, "Issued command: %s\n" % (runme) + "Got from useradd: %s\n" % (output))
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -302,7 +308,7 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
def testNegativeUID(self): def testNegativeUID(self):
""" useradd: Tests that user cannot have a negative UID assigned """ """ useradd: Tests that user cannot have a negative UID assigned """
self.assertNotEqual(commands.getstatusoutput("useradd %s --uid -5" % (self.username))[0], 0, "FAIL: User with UID < 0 added") self.assertNotEqual(subprocess.getstatusoutput("useradd %s --uid -5" % (self.username))[0], 0, "FAIL: User with UID < 0 added")
def testCustomExistingUID(self): def testCustomExistingUID(self):
""" useradd: Adding a user with a specific existing UID """ """ useradd: Adding a user with a specific existing UID """
@ -311,20 +317,20 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
expected["pw_uid"] = UID expected["pw_uid"] = UID
(status_u, output_u) = commands.getstatusoutput("useradd %s -u %d" % (self.username, UID)) (status_u, output_u) = subprocess.getstatusoutput("useradd %s -u %d" % (self.username, UID))
# must fail without -o flag # must fail without -o flag
(status_u_no_o, output_u_no_o) = commands.getstatusoutput("useradd foo -u %d" % (UID)) (status_u_no_o, output_u_no_o) = subprocess.getstatusoutput("useradd foo -u %d" % (UID))
# must pass with -o flag # must pass with -o flag
(status_o, output_o) = commands.getstatusoutput("useradd foo -u %d -o" % (UID)) (status_o, output_o) = subprocess.getstatusoutput("useradd foo -u %d -o" % (UID))
# clean up # clean up
(status, output) = commands.getstatusoutput("userdel -r foo") (status, output) = subprocess.getstatusoutput("userdel -r foo")
self.failUnlessEqual(status_u, 0, "FAIL: cannot add an user with a specified UID\n"+output_u) self.assertEqual(status_u, 0, "FAIL: cannot add an user with a specified UID\n"+output_u)
self.assertEqual(status_o, 0, "FAIL: cannot add an user with an existing UID using the -o flag\n"+output_o) self.assertEqual(status_o, 0, "FAIL: cannot add an user with an existing UID using the -o flag\n"+output_o)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.assertNotEqual(status_u_no_o, 0, "FAIL: user with an existing UID added\n"+output_u_no_o) self.assertNotEqual(status_u_no_o, 0, "FAIL: user with an existing UID added\n"+output_u_no_o)
def testCustomGID(self): def testCustomGID(self):
@ -333,8 +339,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
expected["pw_gid"] = GID expected["pw_gid"] = GID
(status, output) = commands.getstatusoutput("useradd %s -g %d" % (self.username, GID)) (status, output) = subprocess.getstatusoutput("useradd %s -g %d" % (self.username, GID))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -346,8 +352,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
expected["pw_shell"] = shell expected["pw_shell"] = shell
(status, output) = commands.getstatusoutput("useradd %s -s %s" % (self.username, shell)) (status, output) = subprocess.getstatusoutput("useradd %s -s %s" % (self.username, shell))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -355,14 +361,14 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
def testCustomHome(self): def testCustomHome(self):
""" useradd: Adding an user with a specific home directory """ """ useradd: Adding an user with a specific home directory """
home = "/tmp/useradd-test" home = "/tmp/useradd-test"
os.mkdir(home) os.mkdir(home)
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
expected["pw_dir"] = home expected["pw_dir"] = home
(status, output) = commands.getstatusoutput("useradd %s -d %s" % (self.username, home)) (status, output) = subprocess.getstatusoutput("useradd %s -d %s" % (self.username, home))
shutil.rmtree(home) shutil.rmtree(home)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -375,19 +381,19 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
# system account with no home dir # system account with no home dir
expected = self.getDefaultUserInfo(self.username) expected = self.getDefaultUserInfo(self.username)
(status, output) = commands.getstatusoutput("useradd -r %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd -r %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
self.assertEqual(os.path.exists(created["pw_dir"]), False, "FAIL: System user has a home dir created") self.assertEqual(os.path.exists(created["pw_dir"]), False, "FAIL: System user has a home dir created")
self.assertEqual(created["pw_uid"] < defaults['UID_MIN'], True, "FAIL: System user has UID > UID_MIN") self.assertEqual(created["pw_uid"] < int(defaults['UID_MIN']), True, "FAIL: System user has UID > UID_MIN")
self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not add a system user") self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not add a system user")
def testAddToMoreGroups(self): def testAddToMoreGroups(self):
""" useradd: Creating an user that belongs to more than one group """ """ useradd: Creating an user that belongs to more than one group """
(status, output) = commands.getstatusoutput("useradd -G bin %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd -G bin %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
gr_bin = GroupInfo() gr_bin = GroupInfo()
gr_bin.get_info_name("bin") gr_bin.get_info_name("bin")
@ -397,8 +403,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
def testAddWithCommonName(self): def testAddWithCommonName(self):
""" useradd: Specifying a comment (user for account name) """ """ useradd: Specifying a comment (user for account name) """
comment = "zzzzzz" comment = "zzzzzz"
(status, output) = commands.getstatusoutput("useradd -c %s %s" % (comment, self.username)) (status, output) = subprocess.getstatusoutput("useradd -c %s %s" % (comment, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -408,32 +414,32 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
""" useradd: Check if permissions on newly created home dir match the umask """ """ useradd: Check if permissions on newly created home dir match the umask """
defaults = LoginDefsParser() defaults = LoginDefsParser()
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
import stat import stat
perm = os.stat(created["pw_dir"])[stat.ST_MODE] perm = os.stat(created["pw_dir"])[stat.ST_MODE]
mode = int(oct(perm & 0777)) mode = perm & 0o777
self.assertEqual(defaults["UMASK"], "077", "FAIL: umask setting is not sane - is %s, should be 077" % (defaults["UMASK"])) self.assertEqual(defaults["UMASK"], "077", "FAIL: umask setting is not sane - is %s, should be 077" % (defaults["UMASK"]))
self.assertEqual(int(defaults["UMASK"]) + mode , 777, "FAIL: newly-created home dir does not match the umask") self.assertEqual(int(defaults["UMASK"], 8) + mode , 0o777, "FAIL: newly-created home dir does not match the umask")
def testCreateMailSpool(self): def testCreateMailSpool(self):
""" useradd: Check whether the mail spool gets created when told to""" """ useradd: Check whether the mail spool gets created when told to"""
# set up creating of mail spool # set up creating of mail spool
defaults = LoginDefsParser("/etc/default/useradd", split="=") defaults = LoginDefsParser("/etc/default/useradd", split="=")
create_mail = defaults["CREATE_MAIL_SPOOL"] create_mail = defaults["CREATE_MAIL_SPOOL"]
defaults["CREATE_MAIL_SPOOL"] = "yes" defaults["CREATE_MAIL_SPOOL"] = "yes"
defaults.serialize() defaults.serialize()
login_defs = LoginDefsParser() login_defs = LoginDefsParser()
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
# clean up # clean up
defaults["CREATE_MAIL_SPOOL"] = create_mail defaults["CREATE_MAIL_SPOOL"] = create_mail
@ -448,7 +454,7 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase):
def testNoLastlog(self): def testNoLastlog(self):
""" useradd: Check if the -l option prevents from being added to the lastlog """ """ useradd: Check if the -l option prevents from being added to the lastlog """
pass # FIXME - add some code here pass # FIXME - add some code here
class TestUseraddWeirdNameTest(unittest.TestCase, ShadowUtilsTestBase): class TestUseraddWeirdNameTest(unittest.TestCase, ShadowUtilsTestBase):
""" Tests addition/removal of usernames that have proven to be problematic in the past. """ Tests addition/removal of usernames that have proven to be problematic in the past.
@ -459,11 +465,11 @@ class TestUseraddWeirdNameTest(unittest.TestCase, ShadowUtilsTestBase):
expected = self.getDefaultUserInfo(username) expected = self.getDefaultUserInfo(username)
expected["pw_name"] = username expected["pw_name"] = username
(status, output) = commands.getstatusoutput("useradd %s" % (username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (username))
if success: if success:
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
else: else:
self.failIfEqual(status, 0, output) self.assertNotEqual(status, 0, output)
return True return True
created = UserInfo() created = UserInfo()
@ -471,12 +477,12 @@ class TestUseraddWeirdNameTest(unittest.TestCase, ShadowUtilsTestBase):
self.assertEqual(created.lazy_compare(expected), True, "FAIL: TestUseraddWeirdName::addAndRemove - could not add a user") self.assertEqual(created.lazy_compare(expected), True, "FAIL: TestUseraddWeirdName::addAndRemove - could not add a user")
# the cleanup method won't help this time # the cleanup method won't help this time
(status, output) = commands.getstatusoutput("userdel -r %s" % (username)) (status, output) = subprocess.getstatusoutput("userdel -r %s" % (username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testNumericName(self): def testNumericName(self):
""" useradd: Test if an user with a purely numerical name can be added (123) """ """ useradd: Test if an user with a purely numerical name can be added (123) - should fail """
return self.addAndRemove("123") return self.addAndRemove("123", False)
def testSambaName(self): def testSambaName(self):
""" useradd: Test if an user with a name with a dollar at the end can be added (joepublic$ ) """ """ useradd: Test if an user with a name with a dollar at the end can be added (joepublic$ ) """
@ -499,99 +505,99 @@ class TestUseraddDefaultsChange(unittest.TestCase, ShadowUtilsTestBase):
def testDefaultsChange(self): def testDefaultsChange(self):
""" useradd: Test overriding default settings (shell, home dir, group) with a -D option """ """ useradd: Test overriding default settings (shell, home dir, group) with a -D option """
save = self.getDefaults() save = self.getDefaults()
new_defs = dict() new_defs = dict()
new_defs["SHELL"] = "/bin/ksh" new_defs["SHELL"] = "/bin/ksh"
new_defs["GROUP"] = "1" new_defs["GROUP"] = "1"
new_defs["HOME"] = "/tmp" new_defs["HOME"] = "/tmp"
command = "useradd -D -s%s -g%s -b%s" % (new_defs["SHELL"], new_defs["GROUP"], new_defs["HOME"]) command = "useradd -D -s%s -g%s -b%s" % (new_defs["SHELL"], new_defs["GROUP"], new_defs["HOME"])
(status, output) = commands.getstatusoutput(command) (status, output) = subprocess.getstatusoutput(command)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
overriden = self.getDefaults() overriden = self.getDefaults()
[ self.assertEqual(overriden[k], new_defs[k]) for k in new_defs.keys() ] [ self.assertEqual(overriden[k], new_defs[k]) for k in new_defs.keys() ]
command = "useradd -D -s%s -g%s -b%s" % (save["SHELL"], save["GROUP"], save["HOME"]) command = "useradd -D -s%s -g%s -b%s" % (save["SHELL"], save["GROUP"], save["HOME"])
(status, output) = commands.getstatusoutput(command) (status, output) = subprocess.getstatusoutput(command)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
class TestUserdel(unittest.TestCase, ShadowUtilsTestBase): class TestUserdel(unittest.TestCase, ShadowUtilsTestBase):
def setUp(self): def setUp(self):
self.username = "test-shadow-utils-userdel" self.username = "test-shadow-utils-userdel"
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testRemoveUserGroup(self): def testRemoveUserGroup(self):
""" userdel: test if userdel removes user's group when he's deleted - regression test for #201379 """ """ userdel: test if userdel removes user's group when he's deleted - regression test for #201379 """
(status, output) = commands.getstatusoutput("userdel -r %s" % (self.username)) (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
# This would fail if we did not have the group removed # This would fail if we did not have the group removed
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("userdel -r %s" % (self.username)) (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): class TestUsermod(unittest.TestCase, ShadowUtilsTestBase):
def setUp(self): def setUp(self):
self.username = "test-shadow-utils-usermod" self.username = "test-shadow-utils-usermod"
(status, output) = commands.getstatusoutput("useradd %s" % (self.username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def tearDown(self): def tearDown(self):
(status, output) = commands.getstatusoutput("userdel -r %s" % (self.username)) (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testAppendToSupplementaryGroup(self): def testAppendToSupplementaryGroup(self):
""" usermod: Test if a user can be added to a supplementary group """ """ usermod: Test if a user can be added to a supplementary group """
add_group = "additional_group" add_group = "additional_group"
(status, output) = commands.getstatusoutput("groupadd %s" % (add_group)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (add_group))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status_mod, output_mod) = commands.getstatusoutput("usermod -a -G %s %s" % (add_group, self.username)) (status_mod, output_mod) = subprocess.getstatusoutput("usermod -a -G %s %s" % (add_group, self.username))
add_group_info = GroupInfo() add_group_info = GroupInfo()
add_group_info.get_info_name(add_group) add_group_info.get_info_name(add_group)
(status, output) = commands.getstatusoutput("groupdel %s" % (add_group)) (status, output) = subprocess.getstatusoutput("groupdel %s" % (add_group))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.failUnlessEqual(status_mod, 0, output_mod) self.assertEqual(status_mod, 0, output_mod)
self.assertEqual(self.username in add_group_info["gr_mem"], True, "User not in supplementary group after usermod -G --append") self.assertEqual(self.username in add_group_info["gr_mem"], True, "User not in supplementary group after usermod -G --append")
def testAppendToSupplementaryGroupLongOption(self): def testAppendToSupplementaryGroupLongOption(self):
""" usermod: Test if a user can be added to a supplementary group via --append rather that -a (regression test for 222540) """ """ usermod: Test if a user can be added to a supplementary group via --append rather that -a (regression test for 222540) """
# this is known to not work on older RHELs - test what we are running # this is known to not work on older RHELs - test what we are running
rhv = RedHatVersion() rhv = RedHatVersion()
runs = rhv.get_info() runs = rhv.get_info()
if rhv.is_rhel(): if rhv.is_rhel():
if runs[1] < 5: if runs[1] < 5:
print "This test makes sense for RHEL5+" print("This test makes sense for RHEL5+")
return return
else: else:
if runs[1] < 6: if runs[1] < 6:
print "This test makes sense for Fedora 6+" print("This test makes sense for Fedora 6+")
return return
type, release, version = RedHatVersion().get_info() type, release, version = RedHatVersion().get_info()
if RedHatVersion().is_rhel(): if RedHatVersion().is_rhel():
if release < 5 or (release == 5 and version < 2): if release < 5 or (release == 5 and version < 2):
print "This test makes sense for RHEL 5.2+" print("This test makes sense for RHEL 5.2+")
return return
add_group = "additional_group" add_group = "additional_group"
(status, output) = commands.getstatusoutput("groupadd %s" % (add_group)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (add_group))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status_mod, output_mod) = commands.getstatusoutput("usermod --append -G %s %s" % (add_group, self.username)) (status_mod, output_mod) = subprocess.getstatusoutput("usermod --append -G %s %s" % (add_group, self.username))
add_group_info = GroupInfo() add_group_info = GroupInfo()
add_group_info.get_info_name(add_group) add_group_info.get_info_name(add_group)
(status, output) = commands.getstatusoutput("groupdel %s" % (add_group)) (status, output) = subprocess.getstatusoutput("groupdel %s" % (add_group))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.failUnlessEqual(status_mod, 0, output_mod) self.assertEqual(status_mod, 0, output_mod)
self.assertEqual(self.username in add_group_info["gr_mem"], True, "User not in supplementary group after usermod -G --append") self.assertEqual(self.username in add_group_info["gr_mem"], True, "User not in supplementary group after usermod -G --append")
@ -599,8 +605,8 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase):
""" usermod: Test if the comment field (used as the Common Name) can be changed """ """ usermod: Test if the comment field (used as the Common Name) can be changed """
new_comment = "zzzzzz" new_comment = "zzzzzz"
(status, output) = commands.getstatusoutput("usermod -c %s %s" % (new_comment, self.username)) (status, output) = subprocess.getstatusoutput("usermod -c %s %s" % (new_comment, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -614,15 +620,15 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase):
created.get_info_name(self.username) created.get_info_name(self.username)
old_home = created["pw_dir"] old_home = created["pw_dir"]
(status, output) = commands.getstatusoutput("usermod -d %s %s" % (new_home, self.username)) (status, output) = subprocess.getstatusoutput("usermod -d %s %s" % (new_home, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created.get_info_name(self.username) created.get_info_name(self.username)
self.assertEqual(created["pw_dir"], new_home) self.assertEqual(created["pw_dir"], new_home)
# revert to old home so we can userdel -r in tearDown # revert to old home so we can userdel -r in tearDown
(status, output) = commands.getstatusoutput("usermod -d %s %s" % (old_home, self.username)) (status, output) = subprocess.getstatusoutput("usermod -d %s %s" % (old_home, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
# FIXME - test if contents of /home directories are transferred with the -m option # FIXME - test if contents of /home directories are transferred with the -m option
# FIXME - test if new home is created if does not exist before # FIXME - test if new home is created if does not exist before
@ -631,19 +637,19 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase):
""" usermod: Test if user's gid can be changed. """ """ usermod: Test if user's gid can be changed. """
new_group = "root" new_group = "root"
# test non-existing group # test non-existing group
(status_fail, output_fail) = commands.getstatusoutput("usermod -g no-such-group %s" % (self.username)) (status_fail, output_fail) = subprocess.getstatusoutput("usermod -g no-such-group %s" % (self.username))
(status, output) = commands.getstatusoutput("usermod -g %s %s" % (new_group, self.username)) (status, output) = subprocess.getstatusoutput("usermod -g %s %s" % (new_group, self.username))
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
left = GroupInfo() left = GroupInfo()
if left.get_info_name(self.username) == None: if left.get_info_name(self.username) == None:
(status_del, output_del) = commands.getstatusoutput("groupdel %s" % (self.username)) (status_del, output_del) = subprocess.getstatusoutput("groupdel %s" % (self.username))
self.failUnlessEqual(status_del, 0, output_del) self.assertEqual(status_del, 0, output_del)
self.failIfEqual(status_fail, 0, output_fail) self.assertNotEqual(status_fail, 0, output_fail)
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.assertEqual(created["pw_gid"], 0) #0 is root group self.assertEqual(created["pw_gid"], 0) #0 is root group
def testLoginChange(self): def testLoginChange(self):
@ -654,24 +660,24 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase):
uid = user["pw_uid"] # UID won't change even when login does uid = user["pw_uid"] # UID won't change even when login does
# test changing to an existing user name # test changing to an existing user name
(status, output) = commands.getstatusoutput("usermod -l root %s" % (self.username)) (status, output) = subprocess.getstatusoutput("usermod -l root %s" % (self.username))
self.failIfEqual(status, 0, output) self.assertNotEqual(status, 0, output)
(status, output) = commands.getstatusoutput("usermod -l %s %s" % (new_login, self.username)) (status, output) = subprocess.getstatusoutput("usermod -l %s %s" % (new_login, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
user.get_info_name(new_login) user.get_info_name(new_login)
self.assertEqual(user["pw_uid"], uid) self.assertEqual(user["pw_uid"], uid)
# revert so we can userdel -r on tearDown # revert so we can userdel -r on tearDown
(status, output) = commands.getstatusoutput("usermod -l %s %s" % (self.username, new_login)) (status, output) = subprocess.getstatusoutput("usermod -l %s %s" % (self.username, new_login))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testShellChange(self): def testShellChange(self):
""" usermod: Test if user's shell can be changed """ """ usermod: Test if user's shell can be changed """
new_shell = "/bin/sh" new_shell = "/bin/sh"
(status, output) = commands.getstatusoutput("usermod -s %s %s" % (new_shell, self.username)) (status, output) = subprocess.getstatusoutput("usermod -s %s %s" % (new_shell, self.username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = UserInfo() created = UserInfo()
created.get_info_name(self.username) created.get_info_name(self.username)
@ -682,7 +688,7 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
self.groupname = "test-shadow-utils-groups" self.groupname = "test-shadow-utils-groups"
def tearDown(self): def tearDown(self):
commands.getstatusoutput("groupdel %s" % (self.groupname)) subprocess.getstatusoutput("groupdel %s" % (self.groupname))
def testAddGroup(self): def testAddGroup(self):
""" groupadd: Basic adding of a group """ """ groupadd: Basic adding of a group """
@ -690,8 +696,8 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
expected = GroupInfo() expected = GroupInfo()
expected["gr_name"] = self.groupname expected["gr_name"] = self.groupname
(status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = GroupInfo() created = GroupInfo()
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
@ -704,18 +710,18 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
expected["gr_name"] = self.groupname expected["gr_name"] = self.groupname
defaults = LoginDefsParser() defaults = LoginDefsParser()
(status, output) = commands.getstatusoutput("groupadd -r %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd -r %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = GroupInfo() created = GroupInfo()
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
self.assertEqual(created["gr_gid"] < defaults["GID_MIN"], True, "FAIL: System group has gid >= GID_MIN") self.assertEqual(created["gr_gid"] < int(defaults["GID_MIN"]), True, "FAIL: System group has gid >= GID_MIN")
self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not add a system group") self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not add a system group")
def testAddExistingGid(self): def testAddExistingGid(self):
""" groupadd: Test if we group with an existing GID can be added """ """ groupadd: Test if we group with an existing GID can be added """
(status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
gname = "%s-2" % (self.groupname) gname = "%s-2" % (self.groupname)
@ -723,12 +729,12 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
# no -o option -> this should fail # no -o option -> this should fail
(status, output) = commands.getstatusoutput("groupadd -g%s %s" % (created["gr_gid"], gname)) (status, output) = subprocess.getstatusoutput("groupadd -g%s %s" % (created["gr_gid"], gname))
self.failIfEqual(status, 0, output) self.assertNotEqual(status, 0, output)
# override with -o option, should pass now # override with -o option, should pass now
(status, output) = commands.getstatusoutput("groupadd -g%s -o %s" % (created["gr_gid"], gname)) (status, output) = subprocess.getstatusoutput("groupadd -g%s -o %s" % (created["gr_gid"], gname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
# test if the new GID is really the same # test if the new GID is really the same
same_gid = GroupInfo() same_gid = GroupInfo()
@ -736,31 +742,30 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
self.assertEqual(same_gid["gr_gid"], created["gr_gid"]) self.assertEqual(same_gid["gr_gid"], created["gr_gid"])
# clean up # clean up
(status, output) = commands.getstatusoutput("groupdel %s" % (gname)) (status, output) = subprocess.getstatusoutput("groupdel %s" % (gname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testOverrideDefaults(self): def testOverrideDefaults(self):
""" groupadd: Test if the defaults can be overriden with the -K option """ """ groupadd: Test if the defaults can be overriden with the -K option """
# this is known to not work on older RHELs - test what we are running # this is known to not work on older RHELs - test what we are running
rhv = RedHatVersion() rhv = RedHatVersion()
runs = rhv.get_info() runs = rhv.get_info()
if rhv.is_rhel(): if rhv.is_rhel():
if runs[1] < 5: if runs[1] < 5:
print "This test makes sense for RHEL5+" print("This test makes sense for RHEL5+")
return return
else: else:
if runs[1] < 6: if runs[1] < 6:
print "This test makes sense for Fedora 6+" print("This test makes sense for Fedora 6+")
return return
GID_MIN = 600 GID_MIN = 600
GID_MAX = 625 GID_MAX = 625
(status, output) = commands.getstatusoutput("groupadd -K GID_MIN=%d -K GID_MAX=%d %s" % (status, output) = subprocess.getstatusoutput("groupadd -K GID_MIN=%d -K GID_MAX=%d %s" %
(GID_MIN, GID_MAX, self.groupname)) (GID_MIN, GID_MAX, self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = GroupInfo() created = GroupInfo()
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
@ -769,41 +774,41 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase):
def testFOption(self): def testFOption(self):
""" groupadd: Tests the -f option of groupadd """ """ groupadd: Tests the -f option of groupadd """
(status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupadd -f %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd -f %s" % (self.groupname))
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
class TestGroupaddInvalidName(unittest.TestCase, ShadowUtilsTestBase): class TestGroupaddInvalidName(unittest.TestCase, ShadowUtilsTestBase):
def testGroupaddInvalidName(self): def testGroupaddInvalidName(self):
""" groupadd: Test adding of a group with an invalid name """ """ groupadd: Test adding of a group with an invalid name """
(status, output) = commands.getstatusoutput("groupadd foo?") (status, output) = subprocess.getstatusoutput("groupadd foo?")
self.assertNotEqual(status, 0, output) self.assertNotEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffggg") #33 chars (status, output) = subprocess.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffggg") #33 chars
self.assertNotEqual(status, 0, output) self.assertNotEqual(status, 0, output)
class TestGroupaddValidName(unittest.TestCase, ShadowUtilsTestBase): class TestGroupaddValidName(unittest.TestCase, ShadowUtilsTestBase):
def testGroupaddValidName(self): def testGroupaddValidName(self):
""" groupadd: Test adding and removing of groups with maximal valid name and name ending with $ """ """ groupadd: Test adding and removing of groups with maximal valid name and name ending with $ """
(status, output) = commands.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffgg") #32 chars (status, output) = subprocess.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffgg") #32 chars
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars (status, output) = subprocess.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffgg") #32 chars (status, output) = subprocess.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffgg") #32 chars
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars (status, output) = subprocess.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase):
def setUp(self): def setUp(self):
self.groupname = "test-shadow-utils-groups" self.groupname = "test-shadow-utils-groups"
(status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def tearDown(self): def tearDown(self):
commands.getstatusoutput("groupdel %s" % (self.groupname)) subprocess.getstatusoutput("groupdel %s" % (self.groupname))
def testChangeGID(self): def testChangeGID(self):
""" groupmod: Test changing a gid of a group """ """ groupmod: Test changing a gid of a group """
@ -811,8 +816,8 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase):
expected["gr_name"] = self.groupname expected["gr_name"] = self.groupname
expected["gr_gid"] = 54321 expected["gr_gid"] = 54321
(status, output) = commands.getstatusoutput("groupmod -g%d %s" % (expected["gr_gid"], self.groupname)) (status, output) = subprocess.getstatusoutput("groupmod -g%d %s" % (expected["gr_gid"], self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
created = GroupInfo() created = GroupInfo()
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
@ -829,22 +834,22 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase):
expected["gr_name"] = self.groupname expected["gr_name"] = self.groupname
expected["gr_gid"] = created["gr_gid"] expected["gr_gid"] = created["gr_gid"]
(status, output) = commands.getstatusoutput("groupadd %s" % (second_name)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (second_name))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
# try to assingn GID of the first group to the second - this should fail without the -o option # try to assingn GID of the first group to the second - this should fail without the -o option
(status, output) = commands.getstatusoutput("groupmod -g%d %s" % (created["gr_gid"], second_name)) (status, output) = subprocess.getstatusoutput("groupmod -g%d %s" % (created["gr_gid"], second_name))
self.failIfEqual(status, 0, output) self.assertNotEqual(status, 0, output)
# should pass with the -o option # should pass with the -o option
(status, output) = commands.getstatusoutput("groupmod -g%d -o %s" % (created["gr_gid"], second_name)) (status, output) = subprocess.getstatusoutput("groupmod -g%d -o %s" % (created["gr_gid"], second_name))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not change GID of an existing group to an existing one") self.assertEqual(created.lazy_compare(expected), True, "FAIL: Could not change GID of an existing group to an existing one")
# clean up # clean up
commands.getstatusoutput("groupdel %s" % (second_name)) subprocess.getstatusoutput("groupdel %s" % (second_name))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testChangeGroupName(self): def testChangeGroupName(self):
""" groupmod: Test changing a group's name """ """ groupmod: Test changing a group's name """
@ -853,8 +858,8 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase):
created = GroupInfo() created = GroupInfo()
created.get_info_name(self.groupname) created.get_info_name(self.groupname)
(status, output) = commands.getstatusoutput("groupmod -n%s %s" % (second_name, self.groupname)) (status, output) = subprocess.getstatusoutput("groupmod -n%s %s" % (second_name, self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
changed = GroupInfo() changed = GroupInfo()
changed.get_info_gid(created["gr_gid"]) changed.get_info_gid(created["gr_gid"])
@ -862,47 +867,47 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase):
self.assertEqual(changed["gr_gid"], created["gr_gid"]) self.assertEqual(changed["gr_gid"], created["gr_gid"])
# change back, so the group could be deleted by tearDown # change back, so the group could be deleted by tearDown
(status, output) = commands.getstatusoutput("groupmod -n%s %s" % (self.groupname, second_name)) (status, output) = subprocess.getstatusoutput("groupmod -n%s %s" % (self.groupname, second_name))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
def testChangeGroupNameExisting(self): def testChangeGroupNameExisting(self):
""" groupmod: Test changing a group's name to an existing one """ """ groupmod: Test changing a group's name to an existing one """
existing = "bin" existing = "bin"
(status, output) = commands.getstatusoutput("groupmod -n%s %s" % (existing, self.groupname)) (status, output) = subprocess.getstatusoutput("groupmod -n%s %s" % (existing, self.groupname))
self.assertNotEqual(status, 0, output) # man groupmod -> 9: group name already in use self.assertNotEqual(status, 0, output) # man groupmod -> 9: group name already in use
def testChangeNonExistingGroup(self): def testChangeNonExistingGroup(self):
""" groupmod: Test properties of a non-existing group """ """ groupmod: Test properties of a non-existing group """
nonexistent = "foobar" nonexistent = "foobar"
(status, output) = commands.getstatusoutput("groupmod -nspameggs %s" % (nonexistent)) (status, output) = subprocess.getstatusoutput("groupmod -nspameggs %s" % (nonexistent))
self.assertNotEqual(status, 0, status) # man groupmod -> 6: specified group doesn't exist self.assertNotEqual(status, 0, status) # man groupmod -> 6: specified group doesn't exist
class TestGroupdel(unittest.TestCase, ShadowUtilsTestBase): class TestGroupdel(unittest.TestCase, ShadowUtilsTestBase):
def testCorrectGroupdel(self): def testCorrectGroupdel(self):
""" groupdel: Basic usage of groupdel """ """ groupdel: Basic usage of groupdel """
self.groupname = "test-shadow-utils-groups" self.groupname = "test-shadow-utils-groups"
(status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupdel %s" % (self.groupname)) (status, output) = subprocess.getstatusoutput("groupdel %s" % (self.groupname))
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
def testGroupdelNoSuchGroup(self): def testGroupdelNoSuchGroup(self):
""" groupdel: Remove non-existing group """ """ groupdel: Remove non-existing group """
(status, output) = commands.getstatusoutput("groupdel foobar") (status, output) = subprocess.getstatusoutput("groupdel foobar")
self.assertNotEqual(status, 0, output) self.assertNotEqual(status, 0, output)
def testRemovePrimaryGroup(self): def testRemovePrimaryGroup(self):
""" groupdel: Remove a primary group of an user """ """ groupdel: Remove a primary group of an user """
username = "test-groupdel-primary" username = "test-groupdel-primary"
(status, output) = commands.getstatusoutput("useradd %s" % (username)) (status, output) = subprocess.getstatusoutput("useradd %s" % (username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
(status, output) = commands.getstatusoutput("groupdel %s" % (username)) (status, output) = subprocess.getstatusoutput("groupdel %s" % (username))
self.assertNotEqual(status, 0, output) self.assertNotEqual(status, 0, output)
# clean up # clean up
(status, output) = commands.getstatusoutput("userdel -r %s" % (username)) (status, output) = subprocess.getstatusoutput("userdel -r %s" % (username))
self.failUnlessEqual(status, 0, output) self.assertEqual(status, 0, output)
class TestPwckGrpck(unittest.TestCase): class TestPwckGrpck(unittest.TestCase):
def setUp(self): def setUp(self):
@ -932,7 +937,7 @@ class TestPwckGrpck(unittest.TestCase):
self.group_file.flush() self.group_file.flush()
command = "pwck -r %s %s" % (self.passwd_path, self.group_path) command = "pwck -r %s %s" % (self.passwd_path, self.group_path)
return commands.getstatusoutput(command) return subprocess.getstatusoutput(command)
def runGrpCheck(self, group, gshadow): def runGrpCheck(self, group, gshadow):
self.group_file.truncate() self.group_file.truncate()
@ -945,19 +950,19 @@ class TestPwckGrpck(unittest.TestCase):
self.group_file.flush() self.group_file.flush()
command = "grpck -r %s %s" % (self.group_path, self.gshadow_path) command = "grpck -r %s %s" % (self.group_path, self.gshadow_path)
return commands.getstatusoutput(command) return subprocess.getstatusoutput(command)
def testValidEntries(self): def testValidEntries(self):
""" pwck: a valid entry """ """ pwck: a valid entry """
status, output = self.runPwckCheck("foo:x:685:0::/dev/null:/bin/bash", "") status, output = self.runPwckCheck("foo:x:685:0::/dev/null:/bin/bash", "")
rhv = RedHatVersion() rhv = RedHatVersion()
runs = rhv.get_info() runs = rhv.get_info()
if rhv.is_rhel(): if rhv.is_rhel():
if runs[1] < 6: if runs[1] < 6:
self.assertEqual(status, 0, output) self.assertEqual(status, 0, output)
else: else:
self.assertNotEqual(status, 0, output) self.assertNotEqual(status, 0, output)
def testNumberOfFields(self): def testNumberOfFields(self):
""" pwck: invalid number of fields in the record """ """ pwck: invalid number of fields in the record """
@ -1006,7 +1011,7 @@ if __name__ == "__main__":
broken_on_rhel4 = { "TestUseradd" : [ "testCustomUID", "testCustomGID" ] } broken_on_rhel4 = { "TestUseradd" : [ "testCustomUID", "testCustomGID" ] }
if os.getuid() != 0: if os.getuid() != 0:
print "This test must be run as root" print("This test must be run as root")
sys.exit(1) sys.exit(1)
unittest.main() unittest.main()

View File

@ -10,4 +10,4 @@
- sanity - sanity
required_packages: required_packages:
- shadow-utils # sanity test needs shadow-utils - shadow-utils # sanity test needs shadow-utils
- python # sanity test needs python - python3-devel # sanity test needs python3