diff options
Diffstat (limited to 'src/software/test/test_software_installed_package.py')
-rwxr-xr-x | src/software/test/test_software_installed_package.py | 278 |
1 files changed, 159 insertions, 119 deletions
diff --git a/src/software/test/test_software_installed_package.py b/src/software/test/test_software_installed_package.py index 54b8f35..c6e64de 100755 --- a/src/software/test/test_software_installed_package.py +++ b/src/software/test/test_software_installed_package.py @@ -18,198 +18,240 @@ # # Authors: Michal Minar <miminar@redhat.com> # +""" +Unit tests for LMI_SoftwareInstalledPackage provider. +""" -from common import * +import os +import pywbem import shutil import socket import stat +import unittest -class TestSoftwareInstalledPackage(SoftwareBaseTestCase): +import common +import rpmcache + +class TestSoftwareInstalledPackage(common.SoftwareBaseTestCase): + """ + Basic cim operations test. + """ CLASS_NAME = "LMI_SoftwareInstalledPackage" KEYS = ("Software", "System") - def make_op(self, name, epoch, ver, rel, arch, ses=2): - op = self.op.copy() + def make_op(self, pkg, newer=True, ses=2): + """ + @param ses SoftwareElementState property value + @return object path of LMI_SoftwareInstaledPackage + """ + objpath = self.objpath.copy() pkg_op = pywbem.CIMInstanceName( classname="LMI_SoftwarePackage", namespace="root/cimv2", keybindings={ - "Name" : name, - "SoftwareElementID" : make_nevra( - name, epoch, ver, rel, arch, "ALWAYS"), + "Name" : pkg.name, + "SoftwareElementID" : pkg.get_nevra(newer, 'ALWAYS'), "SoftwareElementState" : pywbem.Uint16(ses), "TargetOperatingSystem" : pywbem.Uint16(36), - "Version" : ver }) + "Version" : getattr(pkg, 'up_ver' if newer else 'ver') }) system_op = pywbem.CIMInstanceName( classname="Linux_ComputerSystem", namespace="root/cimv2", keybindings={ "CreationClassName" : "Linux_ComputerSystem", "Name" : socket.gethostname() }) - op["Software"] = pkg_op - op["System"] = system_op - return op + objpath["Software"] = pkg_op + objpath["System"] = system_op + return objpath + @common.mark_dangerous def test_get_instance(self): - for pkg in packages: - if not is_installed(*pkg[:5]): - install_pkg(*pkg[:5]) - op = self.make_op(*pkg[:5]) - inst = self.conn.GetInstance(InstanceName=op, LocalOnly=False) + """ + Tests GetInstance call on packages from our rpm cache. + TODO: test this in non-dangerous way + """ + for pkg in self.pkgdb: + if not common.is_installed(pkg): + common.install_pkg(pkg) + objpath = self.make_op(pkg) + inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) self.assertIsSubclass(inst.path.classname, self.CLASS_NAME) self.assertIsSubclass( - inst.path['System'].classname, op['System'].classname) - self.assertIsSubclass( - inst.path['Software'].classname, op['Software'].classname) + inst.path['System'].classname, objpath['System'].classname) + self.assertIsSubclass(inst.path['Software'].classname, + objpath['Software'].classname) self.assertEqual(len(inst.path.keys()), 2) for key in self.KEYS: self.assertEqual(inst[key], inst.path[key]) - self.assertEqual(inst['Software'], op['Software']) - remove_pkg(pkg.name) - op['Software']['SoftwareElementState'] = pywbem.Uint16(1) - with self.assertRaises(pywbem.CIMError) as cm: - self.conn.GetInstance(InstanceName=op, LocalOnly=False) - self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) + self.assertEqual(inst['Software'], objpath['Software']) + common.remove_pkg(pkg.name) + objpath['Software']['SoftwareElementState'] = pywbem.Uint16(1) + with self.assertRaises(pywbem.CIMError) as cmngr: + self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) + self.assertEqual(cmngr.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) + @common.mark_dangerous def test_enum_instances(self): - pkg = packages[0] - if is_installed(*pkg[:5]): - remove_pkg(pkg.name) + """ + Tests EnumInstances call. + TODO: test this in non-dangerous way + """ + pkg = self.pkgdb[0] + if common.is_installed(pkg.name): + common.remove_pkg(pkg.name) insts1 = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) - install_pkg(*pkg[:5]) + common.install_pkg(pkg) insts2 = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) self.assertEqual(len(insts1) + 1, len(insts2)) - op = self.make_op(*pkg[:5]) - self.assertIn(op['Software'], (inst['Software'] for inst in insts2)) + objpath = self.make_op(pkg) + self.assertIn(objpath['Software'], + (inst['Software'] for inst in insts2)) self.assertTrue(all(isinstance(inst, pywbem.CIMInstanceName) for inst in insts1)) insts1 = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) - remove_pkg(pkg.name) + common.remove_pkg(pkg.name) insts2 = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) self.assertEqual(len(insts2) + 1, len(insts1)) - self.assertIn(op['Software'], (inst['Software'] for inst in insts1)) + self.assertIn(objpath['Software'], + (inst['Software'] for inst in insts1)) self.assertTrue(all(inst['Software'] == inst.path['Software'] for inst in insts1)) self.assertTrue(all(inst['System'] == inst.path['System'] for inst in insts1)) + @common.mark_dangerous def test_create_instance(self): - for pkg in packages: - if is_installed(pkg.name): - remove_pkg(pkg.name) - self.assertFalse(is_installed(pkg.name)) - op = self.make_op(*(list(pkg[:5]) + [1])) - inst = pywbem.CIMInstance(classname=op.classname, path=op) - inst["Software"] = op["Software"] - inst["System"] = op["System"] + """ + Tests CreateInstance call. + """ + for pkg in self.pkgdb: + if common.is_installed(pkg.name): + common.remove_pkg(pkg.name) + self.assertFalse(common.is_installed(pkg.name)) + objpath = self.make_op(pkg, ses=1) + inst = pywbem.CIMInstance(classname=objpath.classname, path=objpath) + inst["Software"] = objpath["Software"] + inst["System"] = objpath["System"] iname = self.conn.CreateInstance(NewInstance=inst) self.assertIsInstance(iname, pywbem.CIMInstanceName) - op["Software"]["SoftwareElementState"] = pywbem.Uint16(2) - self.assertEqual(iname["Software"], op["Software"]) + objpath["Software"]["SoftwareElementState"] = pywbem.Uint16(2) + self.assertEqual(iname["Software"], objpath["Software"]) self.assertIsInstance(iname["System"], pywbem.CIMInstanceName) self.assertIsSubclass(iname["System"].classname, - op["System"].classname) - self.assertEqual(set(iname.keys()), set(op.keys())) - self.assertTrue(is_installed(pkg.name)) + objpath["System"].classname) + self.assertEqual(set(iname.keys()), set(objpath.keys())) + self.assertTrue(common.is_installed(pkg.name)) - with self.assertRaises(pywbem.CIMError) as cm: + with self.assertRaises(pywbem.CIMError) as cmngr: self.conn.CreateInstance(NewInstance=inst) - self.assertEqual(cm.exception.args[0], + self.assertEqual(cmngr.exception.args[0], pywbem.CIM_ERR_ALREADY_EXISTS) - + + @common.mark_dangerous def test_delete_instance(self): - for pkg in packages: - if not is_installed(pkg.name): - install_pkg(*pkg[:5]) - self.assertTrue(is_installed(pkg.name)) - op = self.make_op(*pkg[:5]) - self.conn.DeleteInstance(op) - self.assertFalse(is_installed(pkg.name)) - with self.assertRaises(pywbem.CIMError) as cm: - self.conn.DeleteInstance(op) - self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) + """ + Tests DeleteInstance call. + """ + for pkg in self.pkgdb: + if not common.is_installed(pkg.name): + common.install_pkg(pkg) + self.assertTrue(common.is_installed(pkg.name)) + objpath = self.make_op(pkg) + self.conn.DeleteInstance(objpath) + self.assertFalse(common.is_installed(pkg.name)) + with self.assertRaises(pywbem.CIMError) as cmngr: + self.conn.DeleteInstance(objpath) + self.assertEqual(cmngr.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) + @common.mark_dangerous def test_check_integrity(self): - for pkg_nevra, files in pkg_files: - m = re_nevra.match(pkg_nevra) - name, epoch, ver, rel, arch = [ m.group(k) for k in ( - "name", "epoch", "ver", "rel", "arch") ] - if ( (is_installed(nevra=pkg_nevra) and not verify_pkg(name)) - or (is_installed(name) and not is_installed(nevra=pkg_nevra))) : - remove_pkg(name) - if not is_installed(name): - install_pkg(name, epoch, ver, rel, arch) - self.assertTrue(is_installed(nevra=pkg_nevra)) + """ + Tests CheckIntegrity call. + TODO: test this in non-dangerous way + """ + for pkg in self.pkgdb: + files = self.pkg_files[pkg.name] + if ( ( common.is_installed(pkg) + and not common.verify_pkg(pkg.name)) + or ( common.is_installed(pkg.name) + and not common.is_installed(pkg))) : + common.remove_pkg(pkg.name) + if not common.is_installed(pkg.name): + common.install_pkg(pkg) + self.assertTrue(common.is_installed(pkg)) - op = self.make_op(name, epoch, ver, rel, arch) + objpath = self.make_op(pkg) (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", - ObjectName=op) + ObjectName=objpath) self.assertEqual(rval, pywbem.Uint32(0)) # check passed self.assertEqual(len(oparms), 1) self.assertIn("Failed", oparms) self.assertEqual(len(oparms["Failed"]), 0) cnt_bad = 0 - for f in files: - stats = os.lstat(f) - if os.path.islink(f): # modify symbolic link - target = os.readlink(f) - os.remove(f) - os.symlink(target, f) # just touch symlink + for file_path in files: + stats = os.lstat(file_path) + if os.path.islink(file_path): # modify symbolic link + target = os.readlink(file_path) + os.remove(file_path) + os.symlink(target, file_path) # just touch symlink (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", - ObjectName=op) + ObjectName=objpath) # symlink must pass self.assertEqual(len(oparms["Failed"]), cnt_bad) - os.remove(f) + os.remove(file_path) # now change target - os.symlink("wrong_link_target", f) - elif os.path.isdir(f): # check directory - os.chmod(f, stats.st_mode) # just touch dir + os.symlink("wrong_link_target", file_path) + elif os.path.isdir(file_path): # check directory + os.chmod(file_path, stats.st_mode) # just touch dir (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", - ObjectName=op) + ObjectName=objpath) # dir must pass self.assertEqual(len(oparms["Failed"]), cnt_bad) # modify read access of directory - os.chmod(f, stats.st_mode ^ stat.S_IROTH) + os.chmod(file_path, stats.st_mode ^ stat.S_IROTH) else: # modify regular file # just touch file - this is enough to make it fail - with open(f, "w+") as fobj: pass + with open(file_path, "w+"): + pass cnt_bad += 1 (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", - ObjectName=op) + ObjectName=objpath) self.assertEqual(rval, pywbem.Uint32(1)) self.assertEqual(len(oparms), 1) self.assertIn("Failed", oparms) self.assertEqual(len(oparms["Failed"]), cnt_bad) - self.assertIn(f, (p["Name"] for p in oparms["Failed"])) + self.assertIn(file_path, (p["Name"] for p in oparms["Failed"])) + @common.mark_dangerous def test_method_update(self): - for pkg in packages: - if not pkg.updatable: continue - if is_installed(pkg.name) and not is_installed(*pkg[:5]): - remove_pkg(pkg.name) - if not is_installed(pkg.name): - install_pkg(*pkg[:5]) - self.assertTrue(is_installed(*pkg[:5])) - - op = self.make_op(*pkg[:5]) - op_up = self.make_op(pkg.name, pkg.up_epoch, - pkg.up_ver, pkg.up_rel, pkg.arch) + """ + Tests Update method invocation. + """ + for pkg in self.pkgdb: + if ( common.is_installed(pkg.name) + and not common.is_installed(pkg, False)): + common.remove_pkg(pkg.name) + if not common.is_installed(pkg.name): + common.install_pkg(pkg, False) + self.assertTrue(common.is_installed(pkg, False)) + + objpath = self.make_op(pkg, False) + op_up = self.make_op(pkg) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", - ObjectName=op) + ObjectName=objpath) self.assertEqual(rval, pywbem.Uint16(1)) self.assertEqual(len(oparms), 1) self.assertIn("Installed", oparms) self.assertEqual(oparms["Installed"], op_up["Software"]) - self.assertTrue(is_installed(pkg.name, pkg.up_epoch, - pkg.up_ver, pkg.up_rel, pkg.arch)) + self.assertTrue(common.is_installed(pkg)) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", @@ -217,44 +259,42 @@ class TestSoftwareInstalledPackage(SoftwareBaseTestCase): self.assertEqual(rval, pywbem.Uint16(0)) self.assertEqual(len(oparms), 1) self.assertEqual(oparms["Installed"], op_up["Software"]) + self.assertTrue(common.is_installed(pkg.name)) + self.assertFalse(common.is_installed(pkg, False)) - with self.assertRaises(pywbem.CIMError) as cm: + with self.assertRaises(pywbem.CIMError) as cmngr: self.conn.InvokeMethod( MethodName="Update", - ObjectName=op) - self.assertEqual(cm.exception.args[0], + ObjectName=objpath) + self.assertEqual(cmngr.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) - remove_pkg(pkg.name) - install_pkg(*pkg[:5]) - self.assertTrue(is_installed(*pkg[:5])) + common.remove_pkg(pkg.name) + common.install_pkg(pkg, False) + self.assertTrue(common.is_installed(pkg, False)) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", - ObjectName=op, + ObjectName=objpath, Epoch=pkg.epoch, Version=pkg.ver, Release=pkg.rel) self.assertEqual(rval, pywbem.Uint16(0)) - self.assertEqual(oparms["Installed"], op["Software"]) - + self.assertEqual(oparms["Installed"], objpath["Software"]) + (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", - ObjectName=op, + ObjectName=objpath, Epoch=pkg.up_epoch, Version=pkg.up_ver, Release=pkg.up_rel) self.assertEqual(rval, pywbem.Uint16(1)) self.assertEqual(oparms["Installed"], op_up["Software"]) - @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( + TestSoftwareInstalledPackage) if __name__ == "__main__": unittest.main() |