diff options
Diffstat (limited to 'src/software/test/test_software_file_check.py')
-rwxr-xr-x | src/software/test/test_software_file_check.py | 261 |
1 files changed, 169 insertions, 92 deletions
diff --git a/src/software/test/test_software_file_check.py b/src/software/test/test_software_file_check.py index 0e91bfa..cb09282 100755 --- a/src/software/test/test_software_file_check.py +++ b/src/software/test/test_software_file_check.py @@ -18,31 +18,42 @@ # # Authors: Michal Minar <miminar@redhat.com> # +""" +Unit tests for LMI_SoftwareFileCheck provider. +""" -from common import * import hashlib -import shutil -import socket -import stat +import os +import pywbem +import re +import subprocess +import unittest +from collections import namedtuple -re_checksum = re.compile(r'^([0-9a-fA-F]+)\s+.*') +import common -PassedFlags = namedtuple("PassedFlags", +RE_CHECKSUM = re.compile(r'^([0-9a-fA-F]+)\s+.*') + +PassedFlags = namedtuple("PassedFlags", #pylint: disable=C0103 "exists, type, size, mode, checksum, dev, ltarget, uid, gid, mtime") -class TestSoftwareInstalledPackage(SoftwareBaseTestCase): +class TestSoftwareFileCheck(common.SoftwareBaseTestCase): + #pylint: disable=R0904 + """ + Basic cim operations test. + """ CLASS_NAME = "LMI_SoftwareFileCheck" KEYS = ( "CheckID", "Name", "SoftwareElementID", "SoftwareElementState" , "TargetOperatingSystem", "Version") hash_num2algo = { - 1 : hashlib.md5, - 2 : hashlib.sha1, - 8 : hashlib.sha256, - 9 : hashlib.sha384, - 10 : hashlib.sha512, - 11 : hashlib.sha224 + 1 : hashlib.md5, #pylint: disable=E1101 + 2 : hashlib.sha1, #pylint: disable=E1101 + 8 : hashlib.sha256, #pylint: disable=E1101 + 9 : hashlib.sha384, #pylint: disable=E1101 + 10 : hashlib.sha512, #pylint: disable=E1101 + 11 : hashlib.sha224 #pylint: disable=E1101 } hash_num2cmd = { @@ -63,25 +74,74 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): 11 : 56 } - def make_op(self, nevra, filename): - op = self.op.copy() - m = re_nevra.match(nevra) - name, version = [ m.group(k) for k in ("name", "ver") ] - op["Name"] = filename - op["Version"] = version - op["CheckID"] = "%s#%s" % (name, filename) - op["SoftwareElementState"] = pywbem.Uint16(2) - op["TargetOperatingSystem"] = pywbem.Uint16(36) - op["SoftwareElementID"] = nevra - return op + def make_op(self, pkg, filename, newer=True): + """ + @return object path of LMI_SoftwareFileCheck + """ + objpath = self.objpath.copy() + objpath["Name"] = filename + objpath["Version"] = getattr(pkg, "up_ver" if newer else "ver") + objpath["CheckID"] = "%s#%s" % (pkg.name, filename) + objpath["SoftwareElementState"] = pywbem.Uint16(2) + objpath["TargetOperatingSystem"] = pywbem.Uint16(36) + objpath["SoftwareElementID"] = pkg.get_nevra(newer) + return objpath + + def assertEqualSEID(self, id1, id2): + """ + This is for comparison of SoftwareElementID property values. + """ + if ( not isinstance(id1, basestring) + or not isinstance(id2, basestring)): + return common.SoftwareBaseTestCase.assertEqual(self, id1, id2) + match1 = common.RE_NEVRA.match(id1) + match2 = common.RE_NEVRA.match(id2) + if not match1 or not match2: + return common.SoftwareBaseTestCase.assertEqual(self, id1, id2) + if any( match1.group(g) != match2.group(g) + for g in ('name', 'ver', 'rel', 'arch')): + return common.SoftwareBaseTestCase.assertEqual(self, id1, id2) + epoch1 = match1.group('epoch') + epoch2 = match2.group('epoch') + if not epoch1: + epoch1 = '0' + if not epoch2: + epoch2 = '0' + if epoch1 != epoch2: + return common.SoftwareBaseTestCase.assertEqual(self, id1, id2) + return True + + def assertEqual(self, op1, op2): + """ + This is override for object paths, that allows some differences + (like missing epoch in SoftwareElementID). + """ + if ( not isinstance(op1, pywbem.CIMInstanceName) + or not isinstance(op2, pywbem.CIMInstanceName) + or op1.classname != op2.classname + or op1.namespace != op2.namespace + or op1.keybindings.keys() != op2.keybindings.keys() + or any(op1[a] != op2[a] for a in + ( 'Name', 'Version', 'CheckID', 'SoftwareElementState' + , 'TargetOperatingSystem'))): + return common.SoftwareBaseTestCase.assertEqual(self, op1, op2) + return self.assertEqualSEID( + op1['SoftwareElementID'], op2['SoftwareElementID']) + return True def make_checksum_str(self, csumnum, filename): - return re_checksum.match(subprocess.check_output([ + """ + @return checksum of installed file + """ + return RE_CHECKSUM.match(subprocess.check_output([ self.hash_num2cmd[csumnum], filename])).group(1).lower() - def do_test_symlink(self, op, nevra, f, inst): - target = os.readlink(f) - stats = os.lstat(f) + def do_test_symlink(self, filepath, inst): + """ + Assert some details about symlink. + """ + target = os.readlink(filepath) + stats = os.lstat(filepath) self.assertEqual(inst["FileType"], pywbem.Uint16(3)) self.assertEqual(inst["FileUserID"], stats.st_uid) @@ -96,9 +156,9 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): # modify owner prev_user = inst["FileUserID"] - prev_mtime = inst["LastModificationTime"] - prev_pflags = PassedFlags(*inst["PassedFlags"]) - os.lchown(f, stats.st_uid + 1, -1) + #prev_mtime = inst["LastModificationTime"] + #prev_pflags = PassedFlags(*inst["PassedFlags"]) + os.lchown(filepath, stats.st_uid + 1, -1) inst = self.conn.GetInstance(InstanceName=inst.path) self.assertEqual(inst["FileUserID"], inst["ExpectedFileUserID"] + 1) self.assertEqual(inst["FileUserID"], prev_user + 1) @@ -118,9 +178,10 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertTrue(cur_pflags.mtime) # modify link_target - os.remove(f) - os.symlink("wrong" + "*"*len(inst["ExpectedLinkTarget"]), f) - os.lchown(f, inst["ExpectedFileUserID"], inst["ExpectedFileGroupID"]) + os.remove(filepath) + os.symlink("wrong" + "*"*len(inst["ExpectedLinkTarget"]), filepath) + os.lchown(filepath, inst["ExpectedFileUserID"], + inst["ExpectedFileGroupID"]) inst = self.conn.GetInstance(InstanceName=inst.path) cur_pflags = PassedFlags(*inst["PassedFlags"]) @@ -138,8 +199,11 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertTrue(cur_pflags.gid) self.assertTrue(cur_pflags.mtime) - def do_test_directory(self, op, nevra, f, inst): - stats = os.lstat(f) + def do_test_directory(self, filepath, inst): + """ + Assert some details about directory. + """ + stats = os.lstat(filepath) self.assertEqual(inst["FileType"], pywbem.Uint16(2)) self.assertEqual(inst["FileUserID"], stats.st_uid) @@ -152,8 +216,11 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertEqual(inst["LastModificationTime"], int(stats.st_mtime)) - def do_test_file(self, op, nevra, f, inst): - stats = os.lstat(f) + def do_test_file(self, filepath, inst): + """ + Assert some details about regurar file. + """ + stats = os.lstat(filepath) self.assertEqual(inst["FileType"], pywbem.Uint16(1)) self.assertEqual(inst["FileUserID"], stats.st_uid) @@ -161,7 +228,7 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertEqual(inst["FileMode"], stats.st_mode) self.assertEqual(inst["FileSize"], stats.st_size) self.assertIs(inst["LinkTarget"], None) - csum = self.make_checksum_str(inst['FileChecksumType'], f) + csum = self.make_checksum_str(inst['FileChecksumType'], filepath) self.assertEqual(inst["FileChecksum"].lower(), csum) self.assertEqual(inst["ExpectedLastModificationTime"], inst["LastModificationTime"]) @@ -169,7 +236,7 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): int(stats.st_mtime)) # make it longer - with open(f, "a+") as fobj: + with open(filepath, "a+") as fobj: fobj.write("data\n") inst = self.conn.GetInstance(InstanceName=inst.path) cur_pflags = PassedFlags(*inst["PassedFlags"]) @@ -189,9 +256,10 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertFalse(cur_pflags.mtime) # change file type - os.remove(f) - os.symlink(f, f) - os.lchown(f, inst["ExpectedFileUserID"], inst["ExpectedFileGroupID"]) + os.remove(filepath) + os.symlink(filepath, filepath) + os.lchown(filepath, inst["ExpectedFileUserID"], + inst["ExpectedFileGroupID"]) inst = self.conn.GetInstance(InstanceName=inst.path) cur_pflags = PassedFlags(*inst["PassedFlags"]) self.assertNotEqual(inst["LinkTarget"], inst["ExpectedLinkTarget"]) @@ -213,7 +281,7 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertFalse(cur_pflags.mtime) # remove it - os.remove(f) + os.remove(filepath) inst = self.conn.GetInstance(InstanceName=inst.path) cur_pflags = PassedFlags(*inst["PassedFlags"]) self.assertEqual(inst["LinkTarget"], inst["ExpectedLinkTarget"]) @@ -236,73 +304,82 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertFalse(cur_pflags.gid) self.assertFalse(cur_pflags.mtime) + @common.mark_dangerous def test_get_instance(self): - for nevra, files in pkg_files: - m = re_nevra.match(nevra) - name, version = [ m.group(k) for k in ("name", "ver") ] - if is_installed(nevra=nevra) and not verify_pkg(name): - remove_pkg(name) - if not is_installed(name): - install_pkg(nevra=nevra) - self.assertTrue(is_installed(nevra=nevra)) - for f in files: - op = self.make_op(nevra, f) - - inst = self.conn.GetInstance(InstanceName=op, LocalOnly=False) + """ + Tests GetInstance call. + """ + for pkg in self.pkgdb: + files = self.pkg_files[pkg.name] + if ( common.is_installed(pkg) + and not common.verify_pkg(pkg.name)): + common.remove_pkg(pkg.name) + if not common.is_installed(pkg.name): + common.install_pkg(pkg) + self.assertTrue(common.is_installed(pkg)) + for filepath in files: + objpath = self.make_op(pkg, filepath) + + inst = self.conn.GetInstance( + InstanceName=objpath, + LocalOnly=False) self.assertIsInstance(inst, pywbem.CIMInstance) - self.assertEqual(inst.path, op) + self.assertEqual(inst.path, objpath) for key in self.KEYS: - self.assertEqual(inst[key], op[key]) + if key.lower() == "softwareelementid": + self.assertEqualSEID(inst[key], objpath[key]) + else: + self.assertEqual(inst[key], objpath[key]) self.assertTrue(inst["FileExists"]) self.assertEqual(len(inst["PassedFlags"]), 10) - self.assertTrue(all(f is True for f in inst["PassedFlags"])) + self.assertTrue(all( filepath is True + for filepath in inst["PassedFlags"])) for prop in ( "FileType", "FileUserID", "FileGroupID" , "FileMode", "FileSize", "LinkTarget" , "FileChecksum", "FileModeFlags"): self.assertEqual(inst["Expected"+prop], inst[prop]) - if os.path.islink(f): - self.do_test_symlink(op, nevra, f, inst) - elif os.path.isdir(f): - self.do_test_directory(op, nevra, f, inst) - elif os.path.isfile(f): - self.do_test_file(op, nevra, f, inst) - + if os.path.islink(filepath): + self.do_test_symlink(filepath, inst) + elif os.path.isdir(filepath): + self.do_test_directory(filepath, inst) + elif os.path.isfile(filepath): + self.do_test_file(filepath, inst) + + @common.mark_dangerous def test_invoke_method(self): - for nevra, files in pkg_files: - m = re_nevra.match(nevra) - name, version = [ m.group(k) for k in ("name", "ver") ] - if is_installed(nevra=nevra) and not verify_pkg(name): - remove_pkg(name) - if not is_installed(name): - install_pkg(nevra=nevra) - self.assertTrue(is_installed(nevra=nevra)) - for f in files: - op = self.make_op(nevra, f) - - (rval, oparms) = self.conn.InvokeMethod( + """ + Tests Invoke method invocation. + """ + for pkg in self.pkgdb: + files = self.pkg_files[pkg.name] + if common.is_installed(pkg) and not common.verify_pkg(pkg.name): + common.remove_pkg(pkg.name) + if not common.is_installed(pkg.name): + common.install_pkg(pkg) + self.assertTrue(common.is_installed(pkg)) + for filepath in files: + objpath = self.make_op(pkg, filepath) + + (rval, _) = self.conn.InvokeMethod( MethodName="Invoke", - ObjectName=op) + ObjectName=objpath) self.assertEqual(rval, pywbem.Uint32(0)) # modify file - if os.path.isfile(f): - os.remove(f) + if os.path.isfile(filepath): + os.remove(filepath) else: - os.lchown(f, os.stat(f).st_uid + 1, -1) - (rval, oparms) = self.conn.InvokeMethod( + os.lchown(filepath, os.stat(filepath).st_uid + 1, -1) + (rval, _) = self.conn.InvokeMethod( MethodName="Invoke", - ObjectName=op) + ObjectName=objpath) self.assertEqual(rval, pywbem.Uint32(2)) - @classmethod - def tearDownClass(cls): - for pkg_nevra, files in pkg_files: - name = re_nevra.match(pkg_nevra).group('name') - if is_installed(name) and not verify_pkg(name): - remove_pkg(name) - if not is_installed(name): - install_pkg(nevra=pkg_nevra) +def suite(): + """For unittest loaders.""" + return unittest.TestLoader().loadTestsFromTestCase( + TestSoftwareFileCheck) if __name__ == "__main__": unittest.main() |