#!/usr/bin/env python from common import * import shutil import socket import stat class TestSoftwareInstalledPackage(SoftwareBaseTestCase): CLASS_NAME = "LMI_SoftwareInstalledPackage" KEYS = ( "Software", "System") def make_op(self, name, epoch, ver, rel, arch, ses=2): op = self.op.copy() pkg_op = pywbem.CIMInstanceName( classname="LMI_SoftwarePackage", namespace="root/cimv2", keybindings={ "Name" : name, "SoftwareElementID" : make_nevra( name, epoch, ver, rel, arch, "ALWAYS"), "SoftwareElementState" : pywbem.Uint16(ses), "TargetOperatingSystem" : pywbem.Uint16(36), "Version" : ver }) system_op = pywbem.CIMInstanceName( classname="CIM_ComputerSystem", namespace="root/cimv2", keybindings={ "CreationClassName" : "CIM_ComputerSystem", "Name" : socket.gethostname() }) op["Software"] = pkg_op op["System"] = system_op return op 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) 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) 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) def test_enum_instances(self): pkg = packages[0] if is_installed(*pkg[:5]): remove_pkg(pkg.name) insts1 = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) install_pkg(*pkg[:5]) 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)) self.assertTrue(all(isinstance(inst, pywbem.CIMInstanceName) for inst in insts1)) insts1 = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) 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.assertTrue(all(inst['Software'] == inst.path['Software'] for inst in insts1)) self.assertTrue(all(inst['System'] == inst.path['System'] for inst in insts1)) 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"] iname = self.conn.CreateInstance(NewInstance=inst) self.assertIsInstance(iname, pywbem.CIMInstanceName) op["Software"]["SoftwareElementState"] = pywbem.Uint16(2) self.assertEqual(iname["Software"], op["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)) with self.assertRaises(pywbem.CIMError) as cm: self.conn.CreateInstance(NewInstance=inst) self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_ALREADY_EXISTS) 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) 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)) op = self.make_op(name, epoch, ver, rel, arch) (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", ObjectName=op) 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 (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", ObjectName=op) # symlink must pass self.assertEqual(len(oparms["Failed"]), cnt_bad) os.remove(f) # 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 (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", ObjectName=op) # dir must pass self.assertEqual(len(oparms["Failed"]), cnt_bad) # modify read access of directory os.chmod(f, 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 cnt_bad += 1 (rval, oparms) = self.conn.InvokeMethod( MethodName="CheckIntegrity", ObjectName=op) 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"])) 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) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", ObjectName=op) 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)) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", ObjectName=op_up) self.assertEqual(rval, pywbem.Uint16(0)) self.assertEqual(len(oparms), 1) self.assertEqual(oparms["Installed"], op_up["Software"]) with self.assertRaises(pywbem.CIMError) as cm: self.conn.InvokeMethod( MethodName="Update", ObjectName=op) self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_NOT_FOUND) remove_pkg(pkg.name) install_pkg(*pkg[:5]) self.assertTrue(is_installed(*pkg[:5])) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", ObjectName=op, Epoch=pkg.epoch, Version=pkg.ver, Release=pkg.rel) self.assertEqual(rval, pywbem.Uint16(0)) self.assertEqual(oparms["Installed"], op["Software"]) (rval, oparms) = self.conn.InvokeMethod( MethodName="Update", ObjectName=op, 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) if __name__ == "__main__": unittest.main()