diff --git a/gating.yaml b/gating.yaml new file mode 100644 index 0000000..475371b --- /dev/null +++ b/gating.yaml @@ -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} diff --git a/tests/sanity/Makefile b/tests/sanity/Makefile index 386221b..bd3b425 100644 --- a/tests/sanity/Makefile +++ b/tests/sanity/Makefile @@ -72,6 +72,6 @@ $(METADATA): Makefile @echo "TestTime: 5m" >> $(METADATA) @echo "RunFor: $(PACKAGE_NAME)" >> $(METADATA) @echo "Requires: $(PACKAGE_NAME)" >> $(METADATA) - @echo "Requires: python" >> $(METADATA) + @echo "Requires: python3-devel" >> $(METADATA) rhts-lint $(METADATA) diff --git a/tests/sanity/runtest.sh b/tests/sanity/runtest.sh index cb2a2b5..4568229 100755 --- a/tests/sanity/runtest.sh +++ b/tests/sanity/runtest.sh @@ -3,9 +3,13 @@ . /usr/share/beakerlib/beakerlib.sh || exit 1 rlJournalStart -rlFileBackup --clean /etc/default/useradd- /etc/default/useradd +rlFileBackup --clean /etc/default/useradd- /etc/default/useradd /etc/nsswitch.conf 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 rlFileRestore diff --git a/tests/sanity/sanity_test.py b/tests/sanity/sanity_test.py index e9c45c2..c0919e7 100755 --- a/tests/sanity/sanity_test.py +++ b/tests/sanity/sanity_test.py @@ -15,7 +15,7 @@ TODO: import unittest import pwd import grp -import commands +import subprocess import os import os.path import sys @@ -24,14 +24,14 @@ import tempfile import rpm import shutil -from UserDict import UserDict +from collections import UserDict class RedHatVersion(object): def __init__(self, type=None, version=None, release=None): self.type = type self.version = version self.release = release - self.rhel = False + self.rhel = False def __eq__( self, other): """ @@ -64,9 +64,12 @@ class RedHatVersion(object): rhel_versions[ver_rpm] = 6 if ver_rpm in rhel_versions.keys(): 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): - return self.rhel + return self.rhel def get_info(self): """ @@ -80,11 +83,11 @@ class RedHatVersion(object): mi.pattern('name', rpm.RPMMIRE_GLOB, 'redhat-release*') if mi: - self.rhel = True + self.rhel = True return ('RHEL',) + self.__get_rhel_info(mi) else: mi = ts.dbMatch('name','fedora-release') - self.rhel = False + self.rhel = False if mi.count() != 0: return ('Fedora',) + self.__get_fedora_info(mi) @@ -189,17 +192,20 @@ class LoginDefsParser(UserDict): def __init__(self, path="/etc/login.defs",split=None): self.path = path + self.data = [] UserDict.__init__(self) try: defs = open(path) 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: if line.startswith('#'): continue fields = line.split(split) if len(fields) != 2: continue # yeah, we're dirty self.data[fields[0]] = fields[1] + defs.close() def serialize(self): output = open(self.path, "w+") @@ -245,7 +251,7 @@ class ShadowUtilsTestBase: """ Handy routines """ def getDefaults(self): # 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: raise RuntimeError("Could not get the default values for useradd") 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" def tearDown(self): - commands.getstatusoutput("userdel -r %s" % (self.username)) + subprocess.getstatusoutput("userdel -r %s" % (self.username)) def testBasicAdd(self): """ useradd: Tests basic adding of a user """ expected = self.getDefaultUserInfo(self.username) runme = "useradd %s" % (self.username) - (status, output) = commands.getstatusoutput(runme) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput(runme) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -281,9 +287,9 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): def testExistingUser(self): """ useradd: Test that user with an existing name cannot be added """ - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) - self.assertNotEqual(commands.getstatusoutput("useradd %s" % (self.username))[0], 0, "FAIL: User that already exists added") + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) + self.assertNotEqual(subprocess.getstatusoutput("useradd %s" % (self.username))[0], 0, "FAIL: User that already exists added") def testCustomUID(self): """ useradd: Adding an user with a specific UID """ @@ -293,8 +299,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): expected["pw_uid"] = UID runme = "useradd %s -u %d" % (self.username, UID) - (status, output) = commands.getstatusoutput(runme) - self.failUnlessEqual(status, 0, "Issued command: %s\n" % (runme) + "Got from useradd: %s\n" % (output)) + (status, output) = subprocess.getstatusoutput(runme) + self.assertEqual(status, 0, "Issued command: %s\n" % (runme) + "Got from useradd: %s\n" % (output)) created = UserInfo() created.get_info_name(self.username) @@ -302,7 +308,7 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): def testNegativeUID(self): """ 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): """ useradd: Adding a user with a specific existing UID """ @@ -311,20 +317,20 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): expected = self.getDefaultUserInfo(self.username) 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 - (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 - (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 - (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.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) def testCustomGID(self): @@ -333,8 +339,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): expected = self.getDefaultUserInfo(self.username) expected["pw_gid"] = GID - (status, output) = commands.getstatusoutput("useradd %s -g %d" % (self.username, GID)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s -g %d" % (self.username, GID)) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -346,8 +352,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): expected = self.getDefaultUserInfo(self.username) expected["pw_shell"] = shell - (status, output) = commands.getstatusoutput("useradd %s -s %s" % (self.username, shell)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s -s %s" % (self.username, shell)) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -355,14 +361,14 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): def testCustomHome(self): """ useradd: Adding an user with a specific home directory """ - home = "/tmp/useradd-test" - os.mkdir(home) + home = "/tmp/useradd-test" + os.mkdir(home) expected = self.getDefaultUserInfo(self.username) expected["pw_dir"] = home - (status, output) = commands.getstatusoutput("useradd %s -d %s" % (self.username, home)) - shutil.rmtree(home) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s -d %s" % (self.username, home)) + shutil.rmtree(home) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -375,19 +381,19 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): # system account with no home dir expected = self.getDefaultUserInfo(self.username) - (status, output) = commands.getstatusoutput("useradd -r %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd -r %s" % (self.username)) + self.assertEqual(status, 0, output) created = UserInfo() 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(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") def testAddToMoreGroups(self): """ useradd: Creating an user that belongs to more than one group """ - (status, output) = commands.getstatusoutput("useradd -G bin %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd -G bin %s" % (self.username)) + self.assertEqual(status, 0, output) gr_bin = GroupInfo() gr_bin.get_info_name("bin") @@ -397,8 +403,8 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): def testAddWithCommonName(self): """ useradd: Specifying a comment (user for account name) """ comment = "zzzzzz" - (status, output) = commands.getstatusoutput("useradd -c %s %s" % (comment, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd -c %s %s" % (comment, self.username)) + self.assertEqual(status, 0, output) created = UserInfo() 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 """ defaults = LoginDefsParser() - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) import stat 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(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): """ useradd: Check whether the mail spool gets created when told to""" # set up creating of mail spool defaults = LoginDefsParser("/etc/default/useradd", split="=") - + create_mail = defaults["CREATE_MAIL_SPOOL"] defaults["CREATE_MAIL_SPOOL"] = "yes" defaults.serialize() login_defs = LoginDefsParser() - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) # clean up defaults["CREATE_MAIL_SPOOL"] = create_mail @@ -448,7 +454,7 @@ class TestUseradd(ShadowUtilsTestBase, unittest.TestCase): def testNoLastlog(self): """ useradd: Check if the -l option prevents from being added to the lastlog """ pass # FIXME - add some code here - + class TestUseraddWeirdNameTest(unittest.TestCase, ShadowUtilsTestBase): """ 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["pw_name"] = username - (status, output) = commands.getstatusoutput("useradd %s" % (username)) + (status, output) = subprocess.getstatusoutput("useradd %s" % (username)) if success: - self.failUnlessEqual(status, 0, output) + self.assertEqual(status, 0, output) else: - self.failIfEqual(status, 0, output) + self.assertNotEqual(status, 0, output) return True 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") # the cleanup method won't help this time - (status, output) = commands.getstatusoutput("userdel -r %s" % (username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("userdel -r %s" % (username)) + self.assertEqual(status, 0, output) def testNumericName(self): - """ useradd: Test if an user with a purely numerical name can be added (123) """ - return self.addAndRemove("123") + """ useradd: Test if an user with a purely numerical name can be added (123) - should fail """ + return self.addAndRemove("123", False) def testSambaName(self): """ 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): """ useradd: Test overriding default settings (shell, home dir, group) with a -D option """ save = self.getDefaults() - + new_defs = dict() new_defs["SHELL"] = "/bin/ksh" new_defs["GROUP"] = "1" new_defs["HOME"] = "/tmp" command = "useradd -D -s%s -g%s -b%s" % (new_defs["SHELL"], new_defs["GROUP"], new_defs["HOME"]) - (status, output) = commands.getstatusoutput(command) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput(command) + self.assertEqual(status, 0, output) overriden = self.getDefaults() [ 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"]) - (status, output) = commands.getstatusoutput(command) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput(command) + self.assertEqual(status, 0, output) class TestUserdel(unittest.TestCase, ShadowUtilsTestBase): def setUp(self): self.username = "test-shadow-utils-userdel" - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) def testRemoveUserGroup(self): """ 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)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username)) + self.assertEqual(status, 0, output) # This would fail if we did not have the group removed - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) - (status, output) = commands.getstatusoutput("userdel -r %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username)) + self.assertEqual(status, 0, output) class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): def setUp(self): self.username = "test-shadow-utils-usermod" - (status, output) = commands.getstatusoutput("useradd %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (self.username)) + self.assertEqual(status, 0, output) def tearDown(self): - (status, output) = commands.getstatusoutput("userdel -r %s" % (self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("userdel -r %s" % (self.username)) + self.assertEqual(status, 0, output) def testAppendToSupplementaryGroup(self): """ usermod: Test if a user can be added to a supplementary group """ add_group = "additional_group" - (status, output) = commands.getstatusoutput("groupadd %s" % (add_group)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (add_group)) + 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.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.failUnlessEqual(status_mod, 0, output_mod) + self.assertEqual(status, 0, output) + 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") def testAppendToSupplementaryGroupLongOption(self): """ 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 - rhv = RedHatVersion() - runs = rhv.get_info() - if rhv.is_rhel(): - if runs[1] < 5: - print "This test makes sense for RHEL5+" - return - else: - if runs[1] < 6: - print "This test makes sense for Fedora 6+" - return + # this is known to not work on older RHELs - test what we are running + rhv = RedHatVersion() + runs = rhv.get_info() + if rhv.is_rhel(): + if runs[1] < 5: + print("This test makes sense for RHEL5+") + return + else: + if runs[1] < 6: + print("This test makes sense for Fedora 6+") + return - type, release, version = RedHatVersion().get_info() - if RedHatVersion().is_rhel(): + type, release, version = RedHatVersion().get_info() + if RedHatVersion().is_rhel(): 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 add_group = "additional_group" - (status, output) = commands.getstatusoutput("groupadd %s" % (add_group)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (add_group)) + 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.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.failUnlessEqual(status_mod, 0, output_mod) + self.assertEqual(status, 0, output) + 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") @@ -599,8 +605,8 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): """ usermod: Test if the comment field (used as the Common Name) can be changed """ new_comment = "zzzzzz" - (status, output) = commands.getstatusoutput("usermod -c %s %s" % (new_comment, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -c %s %s" % (new_comment, self.username)) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -614,15 +620,15 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): created.get_info_name(self.username) old_home = created["pw_dir"] - (status, output) = commands.getstatusoutput("usermod -d %s %s" % (new_home, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -d %s %s" % (new_home, self.username)) + self.assertEqual(status, 0, output) created.get_info_name(self.username) self.assertEqual(created["pw_dir"], new_home) # revert to old home so we can userdel -r in tearDown - (status, output) = commands.getstatusoutput("usermod -d %s %s" % (old_home, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -d %s %s" % (old_home, self.username)) + self.assertEqual(status, 0, output) # 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 @@ -631,19 +637,19 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): """ usermod: Test if user's gid can be changed. """ new_group = "root" # test non-existing group - (status_fail, output_fail) = commands.getstatusoutput("usermod -g no-such-group %s" % (self.username)) - (status, output) = commands.getstatusoutput("usermod -g %s %s" % (new_group, self.username)) + (status_fail, output_fail) = subprocess.getstatusoutput("usermod -g no-such-group %s" % (self.username)) + (status, output) = subprocess.getstatusoutput("usermod -g %s %s" % (new_group, self.username)) created = UserInfo() created.get_info_name(self.username) left = GroupInfo() if left.get_info_name(self.username) == None: - (status_del, output_del) = commands.getstatusoutput("groupdel %s" % (self.username)) - self.failUnlessEqual(status_del, 0, output_del) + (status_del, output_del) = subprocess.getstatusoutput("groupdel %s" % (self.username)) + self.assertEqual(status_del, 0, output_del) - self.failIfEqual(status_fail, 0, output_fail) - self.failUnlessEqual(status, 0, output) + self.assertNotEqual(status_fail, 0, output_fail) + self.assertEqual(status, 0, output) self.assertEqual(created["pw_gid"], 0) #0 is root group def testLoginChange(self): @@ -654,24 +660,24 @@ class TestUsermod(unittest.TestCase, ShadowUtilsTestBase): uid = user["pw_uid"] # UID won't change even when login does # test changing to an existing user name - (status, output) = commands.getstatusoutput("usermod -l root %s" % (self.username)) - self.failIfEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -l root %s" % (self.username)) + self.assertNotEqual(status, 0, output) - (status, output) = commands.getstatusoutput("usermod -l %s %s" % (new_login, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -l %s %s" % (new_login, self.username)) + self.assertEqual(status, 0, output) user.get_info_name(new_login) self.assertEqual(user["pw_uid"], uid) # revert so we can userdel -r on tearDown - (status, output) = commands.getstatusoutput("usermod -l %s %s" % (self.username, new_login)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -l %s %s" % (self.username, new_login)) + self.assertEqual(status, 0, output) def testShellChange(self): """ usermod: Test if user's shell can be changed """ new_shell = "/bin/sh" - (status, output) = commands.getstatusoutput("usermod -s %s %s" % (new_shell, self.username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("usermod -s %s %s" % (new_shell, self.username)) + self.assertEqual(status, 0, output) created = UserInfo() created.get_info_name(self.username) @@ -682,7 +688,7 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): self.groupname = "test-shadow-utils-groups" def tearDown(self): - commands.getstatusoutput("groupdel %s" % (self.groupname)) + subprocess.getstatusoutput("groupdel %s" % (self.groupname)) def testAddGroup(self): """ groupadd: Basic adding of a group """ @@ -690,8 +696,8 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): expected = GroupInfo() expected["gr_name"] = self.groupname - (status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname)) + self.assertEqual(status, 0, output) created = GroupInfo() created.get_info_name(self.groupname) @@ -704,18 +710,18 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): expected["gr_name"] = self.groupname defaults = LoginDefsParser() - (status, output) = commands.getstatusoutput("groupadd -r %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd -r %s" % (self.groupname)) + self.assertEqual(status, 0, output) created = GroupInfo() 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") def testAddExistingGid(self): """ groupadd: Test if we group with an existing GID can be added """ - (status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname)) + self.assertEqual(status, 0, output) gname = "%s-2" % (self.groupname) @@ -723,12 +729,12 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): created.get_info_name(self.groupname) # no -o option -> this should fail - (status, output) = commands.getstatusoutput("groupadd -g%s %s" % (created["gr_gid"], gname)) - self.failIfEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd -g%s %s" % (created["gr_gid"], gname)) + self.assertNotEqual(status, 0, output) # override with -o option, should pass now - (status, output) = commands.getstatusoutput("groupadd -g%s -o %s" % (created["gr_gid"], gname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd -g%s -o %s" % (created["gr_gid"], gname)) + self.assertEqual(status, 0, output) # test if the new GID is really the same same_gid = GroupInfo() @@ -736,31 +742,30 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): self.assertEqual(same_gid["gr_gid"], created["gr_gid"]) # clean up - (status, output) = commands.getstatusoutput("groupdel %s" % (gname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupdel %s" % (gname)) + self.assertEqual(status, 0, output) def testOverrideDefaults(self): """ 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 - rhv = RedHatVersion() - runs = rhv.get_info() - if rhv.is_rhel(): - if runs[1] < 5: - print "This test makes sense for RHEL5+" - return - else: - if runs[1] < 6: - print "This test makes sense for Fedora 6+" - return + # this is known to not work on older RHELs - test what we are running + rhv = RedHatVersion() + runs = rhv.get_info() + if rhv.is_rhel(): + if runs[1] < 5: + print("This test makes sense for RHEL5+") + return + else: + if runs[1] < 6: + print("This test makes sense for Fedora 6+") + return - GID_MIN = 600 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)) - self.failUnlessEqual(status, 0, output) + self.assertEqual(status, 0, output) created = GroupInfo() created.get_info_name(self.groupname) @@ -769,41 +774,41 @@ class TestGroupadd(unittest.TestCase, ShadowUtilsTestBase): def testFOption(self): """ groupadd: Tests the -f option of groupadd """ - (status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname)) + 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) class TestGroupaddInvalidName(unittest.TestCase, ShadowUtilsTestBase): def testGroupaddInvalidName(self): """ 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) - (status, output) = commands.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffggg") #33 chars + (status, output) = subprocess.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffggg") #33 chars self.assertNotEqual(status, 0, output) class TestGroupaddValidName(unittest.TestCase, ShadowUtilsTestBase): def testGroupaddValidName(self): """ 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) - (status, output) = commands.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars + (status, output) = subprocess.getstatusoutput("groupadd aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars 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) - (status, output) = commands.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars + (status, output) = subprocess.getstatusoutput("groupdel aaaaabbbbbcccccdddddeeeeefffffg\$") #32 chars self.assertEqual(status, 0, output) class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): def setUp(self): self.groupname = "test-shadow-utils-groups" - (status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname)) + self.assertEqual(status, 0, output) def tearDown(self): - commands.getstatusoutput("groupdel %s" % (self.groupname)) + subprocess.getstatusoutput("groupdel %s" % (self.groupname)) def testChangeGID(self): """ groupmod: Test changing a gid of a group """ @@ -811,8 +816,8 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): expected["gr_name"] = self.groupname expected["gr_gid"] = 54321 - (status, output) = commands.getstatusoutput("groupmod -g%d %s" % (expected["gr_gid"], self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupmod -g%d %s" % (expected["gr_gid"], self.groupname)) + self.assertEqual(status, 0, output) created = GroupInfo() created.get_info_name(self.groupname) @@ -829,22 +834,22 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): expected["gr_name"] = self.groupname expected["gr_gid"] = created["gr_gid"] - (status, output) = commands.getstatusoutput("groupadd %s" % (second_name)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (second_name)) + self.assertEqual(status, 0, output) # 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)) - self.failIfEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupmod -g%d %s" % (created["gr_gid"], second_name)) + self.assertNotEqual(status, 0, output) # should pass with the -o option - (status, output) = commands.getstatusoutput("groupmod -g%d -o %s" % (created["gr_gid"], second_name)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupmod -g%d -o %s" % (created["gr_gid"], second_name)) + 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") # clean up - commands.getstatusoutput("groupdel %s" % (second_name)) - self.failUnlessEqual(status, 0, output) + subprocess.getstatusoutput("groupdel %s" % (second_name)) + self.assertEqual(status, 0, output) def testChangeGroupName(self): """ groupmod: Test changing a group's name """ @@ -853,8 +858,8 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): created = GroupInfo() created.get_info_name(self.groupname) - (status, output) = commands.getstatusoutput("groupmod -n%s %s" % (second_name, self.groupname)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupmod -n%s %s" % (second_name, self.groupname)) + self.assertEqual(status, 0, output) changed = GroupInfo() changed.get_info_gid(created["gr_gid"]) @@ -862,47 +867,47 @@ class TestGroupmod(unittest.TestCase, ShadowUtilsTestBase): self.assertEqual(changed["gr_gid"], created["gr_gid"]) # change back, so the group could be deleted by tearDown - (status, output) = commands.getstatusoutput("groupmod -n%s %s" % (self.groupname, second_name)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupmod -n%s %s" % (self.groupname, second_name)) + self.assertEqual(status, 0, output) def testChangeGroupNameExisting(self): """ groupmod: Test changing a group's name to an existing one """ 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 def testChangeNonExistingGroup(self): """ groupmod: Test properties of a non-existing group """ 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 class TestGroupdel(unittest.TestCase, ShadowUtilsTestBase): def testCorrectGroupdel(self): """ groupdel: Basic usage of groupdel """ self.groupname = "test-shadow-utils-groups" - (status, output) = commands.getstatusoutput("groupadd %s" % (self.groupname)) - self.failUnlessEqual(status, 0, output) - (status, output) = commands.getstatusoutput("groupdel %s" % (self.groupname)) + (status, output) = subprocess.getstatusoutput("groupadd %s" % (self.groupname)) + self.assertEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("groupdel %s" % (self.groupname)) self.assertEqual(status, 0, output) def testGroupdelNoSuchGroup(self): """ groupdel: Remove non-existing group """ - (status, output) = commands.getstatusoutput("groupdel foobar") + (status, output) = subprocess.getstatusoutput("groupdel foobar") self.assertNotEqual(status, 0, output) def testRemovePrimaryGroup(self): """ groupdel: Remove a primary group of an user """ username = "test-groupdel-primary" - (status, output) = commands.getstatusoutput("useradd %s" % (username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("useradd %s" % (username)) + self.assertEqual(status, 0, output) - (status, output) = commands.getstatusoutput("groupdel %s" % (username)) + (status, output) = subprocess.getstatusoutput("groupdel %s" % (username)) self.assertNotEqual(status, 0, output) # clean up - (status, output) = commands.getstatusoutput("userdel -r %s" % (username)) - self.failUnlessEqual(status, 0, output) + (status, output) = subprocess.getstatusoutput("userdel -r %s" % (username)) + self.assertEqual(status, 0, output) class TestPwckGrpck(unittest.TestCase): def setUp(self): @@ -932,7 +937,7 @@ class TestPwckGrpck(unittest.TestCase): self.group_file.flush() command = "pwck -r %s %s" % (self.passwd_path, self.group_path) - return commands.getstatusoutput(command) + return subprocess.getstatusoutput(command) def runGrpCheck(self, group, gshadow): self.group_file.truncate() @@ -945,19 +950,19 @@ class TestPwckGrpck(unittest.TestCase): self.group_file.flush() command = "grpck -r %s %s" % (self.group_path, self.gshadow_path) - return commands.getstatusoutput(command) + return subprocess.getstatusoutput(command) def testValidEntries(self): """ pwck: a valid entry """ status, output = self.runPwckCheck("foo:x:685:0::/dev/null:/bin/bash", "") - rhv = RedHatVersion() - runs = rhv.get_info() - if rhv.is_rhel(): - if runs[1] < 6: - self.assertEqual(status, 0, output) - else: - self.assertNotEqual(status, 0, output) + rhv = RedHatVersion() + runs = rhv.get_info() + if rhv.is_rhel(): + if runs[1] < 6: + self.assertEqual(status, 0, output) + else: + self.assertNotEqual(status, 0, output) def testNumberOfFields(self): """ pwck: invalid number of fields in the record """ @@ -1006,7 +1011,7 @@ if __name__ == "__main__": broken_on_rhel4 = { "TestUseradd" : [ "testCustomUID", "testCustomGID" ] } if os.getuid() != 0: - print "This test must be run as root" + print("This test must be run as root") sys.exit(1) unittest.main() diff --git a/tests/tests.yml b/tests/tests.yml index 09f4769..5735db9 100644 --- a/tests/tests.yml +++ b/tests/tests.yml @@ -10,4 +10,4 @@ - sanity required_packages: - shadow-utils # sanity test needs shadow-utils - - python # sanity test needs python + - python3-devel # sanity test needs python3