diff options
author | Michal Minar <miminar@redhat.com> | 2013-10-31 15:50:13 +0100 |
---|---|---|
committer | Michal Minar <miminar@redhat.com> | 2013-11-04 10:58:12 +0100 |
commit | 06eea83409b4356306f85c4ecb32d7376b62d086 (patch) | |
tree | c068fde01627a9aa3722de1110bb65fd66a62350 | |
parent | 9796619a1923e01737a5f96c2e3b8fc3e9401156 (diff) | |
download | openlmi-providers-06eea83409b4356306f85c4ecb32d7376b62d086.tar.gz openlmi-providers-06eea83409b4356306f85c4ecb32d7376b62d086.tar.xz openlmi-providers-06eea83409b4356306f85c4ecb32d7376b62d086.zip |
software: refactored tests
Base software tests on LmiTestCase from lmi.test.base. Use lmi.shell
abstractions instead of plain pywbem.
-rw-r--r-- | src/software/test/base.py | 85 | ||||
-rw-r--r-- | src/software/test/rpmcache.py | 11 | ||||
-rw-r--r-- | src/software/test/test_hosted_software_collection.py | 90 | ||||
-rw-r--r-- | src/software/test/test_hosted_software_identity_resource.py | 86 | ||||
-rw-r--r-- | src/software/test/test_installed_software_identity.py | 176 | ||||
-rw-r--r-- | src/software/test/test_member_of_software_collection.py | 109 | ||||
-rw-r--r-- | src/software/test/test_resource_for_software_identity.py | 92 | ||||
-rw-r--r-- | src/software/test/test_software_identity.py | 61 | ||||
-rw-r--r-- | src/software/test/test_software_identity_checks.py | 75 | ||||
-rw-r--r-- | src/software/test/test_software_identity_file_check.py | 254 | ||||
-rw-r--r-- | src/software/test/test_software_identity_resource.py | 162 | ||||
-rw-r--r-- | src/software/test/test_system_software_collection.py | 27 |
12 files changed, 506 insertions, 722 deletions
diff --git a/src/software/test/base.py b/src/software/test/base.py index cea0bd7..6b6f7f9 100644 --- a/src/software/test/base.py +++ b/src/software/test/base.py @@ -23,36 +23,13 @@ Common utilities and base class for all software tests. import itertools import os -import pywbem import tempfile -import unittest from subprocess import check_output +from lmi.test.lmibase import LmiTestCase import repository import rpmcache -def mark_dangerous(method): - """ - Decorator for methods of unittest.TestCase subclasses, that - skips dangerous tests, if environment variable does not allow them. - """ - if os.environ.get('LMI_RUN_DANGEROUS', '0') == '1': - return method - else: - return unittest.skip("This test is marked as dangerous.")(method) - -def mark_tedious(method): - """ - Decorator for methods of unittest.TestCase subclasses, that - skips tedious tests. Those running for very long time and usually - need a lot of memory. Environment variable "LMI_RUN_TEDIOUS" can - allow them - """ - if os.environ.get('LMI_RUN_TEDIOUS', '0') == '1': - return method - else: - return unittest.skip("This test is marked as tedious.")(method) - def get_pkg_files(pkg): """ Tries to make a the heterogenous and smallest set of test files from @@ -86,7 +63,7 @@ def get_pkg_files(pkg): out += list(files) return out -class SoftwareBaseTestCase(unittest.TestCase): #pylint: disable=R0904 +class SoftwareBaseTestCase(LmiTestCase): #pylint: disable=R0904 """ Base class for all LMI Software test classes. """ @@ -137,21 +114,9 @@ class SoftwareBaseTestCase(unittest.TestCase): #pylint: disable=R0904 return False def __init__(self, *args, **kwargs): - unittest.TestCase.__init__(self, *args, **kwargs) + LmiTestCase.__init__(self, *args, **kwargs) self.longMessage = True #pylint: disable=C0103 - def setUp(self): - unittest.TestCase.setUp(self) - self.objpath = pywbem.CIMInstanceName( - namespace="root/cimv2", - classname=self.CLASS_NAME, - keybindings=pywbem.NocaseDict( - dict((k, None) for k in self.KEYS))) - if "CreationClassName" in self.KEYS: - self.objpath["CreationClassName"] = self.CLASS_NAME - if "SystemCreationClassName" in self.KEYS: - self.objpath["SystemCreationClassName"] = "PG_ComputerSystem" - def install_pkg(self, pkg, newer=True, repolist=None): """ Use this method instead of function in rpmcache in tests. @@ -168,52 +133,14 @@ class SoftwareBaseTestCase(unittest.TestCase): #pylint: disable=R0904 repolist = self.test_repos return rpmcache.ensure_pkg_installed(pkg, newer, repolist) - def assertIsSubclass(self, cls, base_cls): #pylint: disable=C0103 - """ - Checks, whether cls is subclass of base_cls from CIM perspective. - @param cls name of subclass - @param base_cls name of base class - """ - if not isinstance(cls, basestring): - raise TypeError("cls must be a string") - if not isinstance(base_cls, basestring): - raise TypeError("base_cls must be a string") - return self.assertTrue(pywbem.is_subclass(self.conn, - "root/cimv2", base_cls, cls)) - - def assertEqual(self, fst, snd, *args, **kwargs): - """ - Modify assertEqual for instance names comparing only important - properties. - """ - if ( isinstance(fst, pywbem.CIMInstanceName) - and isinstance(snd, pywbem.CIMInstanceName) - and fst.classname == snd.classname - and fst.namespace == snd.namespace - and fst.keybindings == snd.keybindings): - return True - unittest.TestCase.assertEqual(self, fst, snd, *args, **kwargs) - - #pylint: disable=C0103 - def assertRaisesCIM(self, cim_err_code, func, *args, **kwds): - """ - This test passes if given function called with supplied arguements - raises pywbem.CIMError with given cim error code. - """ - with self.assertRaises(pywbem.CIMError) as cm: - func(*args, **kwds) - self.assertEqual(cim_err_code, cm.exception.args[0]) - @classmethod def setUpClass(cls): #pylint: disable=C0103 - cls.url = os.environ.get('LMI_CIMOM_URL', 'http://localhost:5988') - cls.user = os.environ.get('LMI_CIMOM_USERNAME', '') - cls.password = os.environ.get('LMI_CIMOM_PASSWORD', '') - cls.broker = os.environ.get('LMI_CIMOM_BROKER', None) - cls.conn = pywbem.WBEMConnection(cls.url, (cls.user, cls.password)) + LmiTestCase.setUpClass.im_func(cls) + # TODO: make dangerous tests work reliably # this is just a temporary solution cls.run_dangerous = False + cls.test_repos = os.environ.get( 'LMI_SOFTWARE_TEST_REPOS', '').split(',') use_cache = os.environ.get('LMI_SOFTWARE_USE_CACHE', '0') == '1' diff --git a/src/software/test/rpmcache.py b/src/software/test/rpmcache.py index bce5778..d314bfa 100644 --- a/src/software/test/rpmcache.py +++ b/src/software/test/rpmcache.py @@ -66,7 +66,7 @@ RE_PKG_INFO = re.compile( re.MULTILINE | re.DOTALL | re.IGNORECASE) # matching packages won't be selected for dangerous tests RE_AVAILABLE_EXCLUDE = re.compile( - r'^(kernel|tog-pegasus|sblim|openlmi).*') + r'^(kernel|tog-pegasus|sblim|.*openlmi).*') # Maximum number of packages, that will be selected for testing / 2 # There are 2 sets of test packages (safe and dangerous). When running @@ -93,11 +93,7 @@ def _is_sane_package(pkg_name): :returns: Whether the given package can be included among tested ones. :rtype: boolean """ - if pkg_name.startswith('openlmi'): - return False - if pkg_name in ('sblim-sfcb', 'tog-pegasus', 'sblim-cmpi-base'): - return False - return True + return RE_AVAILABLE_EXCLUDE.match(pkg_name) is None def _match_nevr(match): """ @@ -159,8 +155,7 @@ def _filter_duplicates(installed, avail_str): continue if prev_match and prev_match.group('name') != match.group('name'): if ( len(cur_package_matches) > 1 - and not cur_package_matches[0].group('name') in installed - and not RE_AVAILABLE_EXCLUDE.match(match.group('name'))): + and not cur_package_matches[0].group('name') in installed): pkgs = [ _match2pkg(m) for m in cur_package_matches ] dups_list.append(pkgs) cur_package_matches = [] diff --git a/src/software/test/test_hosted_software_collection.py b/src/software/test/test_hosted_software_collection.py index 6f4adc3..12db36e 100644 --- a/src/software/test/test_hosted_software_collection.py +++ b/src/software/test/test_hosted_software_collection.py @@ -23,7 +23,6 @@ Unit tests for LMI_MemberOfSoftwareCollection provider. """ import pywbem -import socket import unittest import base @@ -45,110 +44,89 @@ class TestHostedSoftwareCollection(base.SoftwareBaseTestCase): @param ses SoftwareElementState property value @return object path of SoftwareIdentity """ - objpath = self.objpath.copy() - objpath["Antecedent"] = pywbem.CIMInstanceName( - classname="PG_ComputerSystem", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "PG_ComputerSystem", - "Name" : socket.getfqdn() - })) - objpath["Dependent"] = pywbem.CIMInstanceName( - classname="LMI_SystemSoftwareCollection", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ + return self.cim_class.new_instance_name({ + "Antecedent" : self.system_iname, + "Dependent" : self.ns.LMI_SystemSoftwareCollection. \ + new_instance_name({ "InstanceID" : "LMI:LMI_SystemSoftwareCollection" - })) - return objpath + }) + }) def test_get_instance(self): """ Tests GetInstance call on packages from our rpm cache. """ objpath = self.make_op() - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertIsInstance(inst, pywbem.CIMInstance) - self.assertEqual(inst.path, objpath) - self.assertEqual(sorted(inst.properties.keys()), sorted(self.KEYS)) - self.assertEqual(objpath, inst.path) + inst = objpath.to_instance() + self.assertNotEqual(inst, None) + self.assertCIMNameEqual(inst.path, objpath) + self.assertEqual(sorted(inst.properties()), sorted(self.KEYS)) + self.assertCIMNameEqual(objpath, inst.path) for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) + self.assertCIMNameEqual(getattr(inst, key), getattr(inst.path, key)) # try with CIM_ prefix - antecedent = objpath["Antecedent"].copy() - objpath["Antecedent"].classname = "CIM_ComputerSystem" - - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertIsInstance(inst, pywbem.CIMInstance) - objpath["Antecedent"] = antecedent.copy() - self.assertEqual(objpath, inst.path) - self.assertEqual(sorted(inst.properties.keys()), sorted(self.KEYS)) - for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) + objpath.Antecedent.wrapped_object.classname = "CIM_ComputerSystem" + + inst = objpath.to_instance() + self.assertNotEqual(inst, None) # try with CIM_ prefix also for CreationClassName - objpath["Antecedent"]["CreationClassName"] = "CIM_ComputerSystem" + objpath.Antecedent.wrapped_object["CreationClassName"] = \ + "CIM_ComputerSystem" - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertIsInstance(inst, pywbem.CIMInstance) - objpath["Antecedent"] = antecedent.copy() - self.assertEqual(objpath, inst.path) - self.assertEqual(sorted(inst.properties.keys()), sorted(self.KEYS)) - for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) + inst = objpath.to_instance() + self.assertNotEqual(inst, None) def test_enum_instances(self): """ Tests EnumInstances call on installed packages. """ objpath = self.make_op() - insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) + insts = self.cim_class.instances() self.assertEqual(1, len(insts)) - self.assertEqual(objpath, insts[0].path) - self.assertEqual(sorted(insts[0].properties.keys()), sorted(self.KEYS)) - self.assertEqual(objpath, insts[0].path) + self.assertCIMNameEqual(objpath, insts[0].path) + self.assertEqual(sorted(insts[0].properties()), sorted(self.KEYS)) for key in self.KEYS: - self.assertEqual(insts[0][key], insts[0].path[key]) + self.assertCIMNameEqual( + getattr(insts[0], key), getattr(insts[0].path, key)) def test_enum_instance_names(self): """ Tests EnumInstanceNames call on installed packages. """ objpath = self.make_op() - inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) + inames = self.cim_class.instance_names() self.assertEqual(1, len(inames)) - self.assertEqual(objpath, inames[0]) + self.assertCIMNameEqual(objpath, inames[0]) def test_get_antecedent_referents(self): """ Test ReferenceNames for ComputerSystem. """ objpath = self.make_op() - refs = self.conn.AssociatorNames( - AssocClass='LMI_HostedSoftwareCollection', + refs = objpath.Antecedent.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Antecedent", - ObjectName=objpath["Antecedent"], ResultRole="Dependent", ResultClass='LMI_SystemSoftwareCollection') self.assertEqual(len(refs), 1) ref = refs[0] - self.assertEqual(objpath["Dependent"], ref) + self.assertCIMNameEqual(objpath.Dependent, ref) - @base.mark_dangerous def test_get_dependent_referents(self): """ Test ReferenceNames for SystemSoftwareCollection. """ objpath = self.make_op() - refs = self.conn.AssociatorNames( - AssocClass='LMI_HostedSoftwareCollection', - ObjectName=objpath["Dependent"], + refs = objpath.Dependent.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Dependent", ResultRole="Antecedent", - ResultClass="PG_ComputerSystem") + ResultClass=self.system_cs_name) self.assertEqual(1, len(refs)) ref = refs[0] - self.assertEqual(objpath["Antecedent"], ref) + self.assertCIMNameEqual(objpath.Antecedent, ref) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_hosted_software_identity_resource.py b/src/software/test/test_hosted_software_identity_resource.py index df4b11f..09e09f2 100644 --- a/src/software/test/test_hosted_software_identity_resource.py +++ b/src/software/test/test_hosted_software_identity_resource.py @@ -23,7 +23,6 @@ Unit tests for LMI_HostedSoftwareIdentityResource provider. """ import pywbem -import socket import unittest import base @@ -48,24 +47,16 @@ class TestHostedSoftwareIdentityResource(base.SoftwareBaseTestCase): """ @return object path of HostedSoftwareIdentityResource association """ - objpath = self.objpath.copy() - objpath["Antecedent"] = pywbem.CIMInstanceName( - classname="PG_ComputerSystem", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "PG_ComputerSystem", - "Name" : socket.getfqdn() - })) - objpath["Dependent"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentityResource", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "LMI_SoftwareIdentityResource", - "SystemCreationClassName" : "PG_ComputerSystem", - "SystemName" : socket.getfqdn(), - "Name" : repo.repoid - })) - return objpath + return self.cim_class.new_instance_name({ + "Antecedent" : self.system_iname, + "Dependent" : self.ns.LMI_SoftwareIdentityResource. \ + new_instance_name({ + "CreationClassName" : "LMI_SoftwareIdentityResource", + "SystemCreationClassName" : self.system_cs_name, + "SystemName" : self.SYSTEM_NAME, + "Name" : repo.repoid + }) + }) def test_get_instance(self): """ @@ -73,13 +64,15 @@ class TestHostedSoftwareIdentityResource(base.SoftwareBaseTestCase): """ for repo in self.repodb: objpath = self.make_op(repo) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(objpath, inst.path, + inst = objpath.to_instance() + self.assertCIMNameEqual(objpath, inst.path, "Object paths should match for repo %s" % repo.repoid) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), - 'OP is missing \"%s\" key for repo "%s".' % (key, repo.repoid)) - self.assertEqual(inst[key], inst.path[key], + self.assertIn(key, inst.properties(), + 'OP is missing \"%s\" key for repo "%s".' % + (key, repo.repoid)) + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key), 'Key property "%s" does not match for repo "%s"!' % (key, repo.repoid)) @@ -87,34 +80,31 @@ class TestHostedSoftwareIdentityResource(base.SoftwareBaseTestCase): """ Tests EnumInstanceNames call on repositories from our cache. """ - inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) + inames = self.cim_class.instance_names() repos = { r.repoid: r for r in self.repodb } self.assertEqual(len(repos), len(inames)) for iname in inames: - self.assertIn('Dependent', iname) - self.assertIn('Name', iname['Dependent']) - self.assertIn(iname['Dependent']['Name'], repos) - objpath = self.make_op(repos[iname['Dependent']['Name']]) - self.assertEqual(objpath, iname) + self.assertIn(iname.Dependent.Name, repos) + objpath = self.make_op(repos[iname.Dependent.Name]) + self.assertCIMNameEqual(objpath, iname) def test_enum_instances(self): """ Tests EnumInstanceNames call on repositories from our cache. """ - inames = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) + inames = self.cim_class.instances() repos = { r.repoid: r for r in self.repodb } self.assertEqual(len(repos), len(inames)) for inst in inames: - self.assertIn('Dependent', inst) - self.assertIn('Name', inst['Dependent']) - repoid = inst['Dependent']['Name'] + repoid = inst.Dependent.Name self.assertIn(repoid, repos) objpath = self.make_op(repos[repoid]) - self.assertEqual(objpath, inst.path) + self.assertCIMNameEqual(objpath, inst.path) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), 'OP is missing \"%s\" key for repo "%s".' % (key, repoid)) - self.assertEqual(inst[key], inst.path[key], + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key), 'Key property "%s" does not match for repo "%s"!' % (key, repoid)) @@ -126,21 +116,18 @@ class TestHostedSoftwareIdentityResource(base.SoftwareBaseTestCase): return repo = self.repodb[0] objpath = self.make_op(repo) - refs = self.conn.AssociatorNames( - AssocClass='LMI_HostedSoftwareIdentityResource', + refs = objpath.Antecedent.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Antecedent", - ObjectName=objpath["Antecedent"], ResultRole="Dependent", ResultClass="LMI_SoftwareIdentityResource") repos = {r.repoid: r for r in self.repodb} for ref in refs: - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource") - self.assertIn("Name", ref.keys()) - objpath = self.make_op(repos[ref["Name"]]) - self.assertEqual(objpath["Dependent"], ref) - del repos[ref["Name"]] + objpath = self.make_op(repos[ref.Name]) + self.assertCIMNameEqual(objpath.Dependent, ref) + del repos[ref.Name] self.assertEqual(0, len(repos)) def test_get_dependent_referents(self): @@ -149,14 +136,13 @@ class TestHostedSoftwareIdentityResource(base.SoftwareBaseTestCase): """ for repo in self.repodb: objpath = self.make_op(repo=repo) - refs = self.conn.AssociatorNames( - AssocClass='LMI_HostedSoftwareIdentityResource', + refs = objpath.Dependent.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Dependent", - ObjectName=objpath["Dependent"], ResultRole="Antecedent", - ResultClass="PG_ComputerSystem") + ResultClass=self.system_cs_name) self.assertEqual(len(refs), 1) - self.assertEqual(objpath["Antecedent"], refs[0]) + self.assertCIMNameEqual(objpath.Antecedent, refs[0]) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_installed_software_identity.py b/src/software/test/test_installed_software_identity.py index bff7b72..ee79318 100644 --- a/src/software/test/test_installed_software_identity.py +++ b/src/software/test/test_installed_software_identity.py @@ -23,9 +23,11 @@ Unit tests for LMI_InstalledSoftwareIdentity provider. """ import pywbem -import socket import unittest +from lmi.test.lmibase import enable_lmi_exceptions +from lmi.test.util import mark_dangerous +from lmi.test.util import mark_tedious import base import rpmcache @@ -41,22 +43,14 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): """ @return object path of InstalledSoftwareIdentity association """ - objpath = self.objpath.copy() - objpath["System"] = pywbem.CIMInstanceName( - classname="PG_ComputerSystem", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "PG_ComputerSystem", - "Name" : socket.getfqdn() - })) - objpath["InstalledSoftware"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentity", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + pkg.get_nevra(newer=newer, - with_epoch="ALWAYS") - })) - return objpath + return self.cim_class.new_instance_name({ + "System" : self.system_iname, + "InstalledSoftware" : self.ns.LMI_SoftwareIdentity. \ + new_instance_name({ + "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(newer=newer, with_epoch="ALWAYS") + }) + }) def test_get_instance(self): """ @@ -64,75 +58,72 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): """ for pkg in self.safe_pkgs: objpath = self.make_op(pkg) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(inst.path, objpath, + inst = objpath.to_instance() + self.assertCIMNameEqual(inst.path, objpath, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), "OP is missing \"%s\" key for package %s"%(key, pkg)) - self.assertEqual(inst[key], inst.path[key]) - self.assertEqual(objpath, inst.path, + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key)) + self.assertCIMNameEqual(objpath, inst.path, "Object paths should match for package %s"%pkg) # try it now with CIM_ComputerSystem - system = objpath["System"].copy() - objpath["System"].classname = "CIM_ComputerSystem" - objpath["System"]["CreationClassName"] = "CIM_ComputerSystem" + system = objpath.System.copy() + objpath.System.wrapped_object.classname = "CIM_ComputerSystem" + objpath.System.wrapped_object.CreationClassName = \ + "CIM_ComputerSystem" - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(objpath, inst.path, + inst = objpath.to_instance() + self.assertCIMNameEqual(objpath, inst.path, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), "OP is missing \"%s\" key for package %s"%(key, pkg)) - self.assertEqual(system, inst["System"]) - self.assertEqual(objpath, inst.path) + self.assertCIMNameEqual(system, inst.System) + self.assertCIMNameEqual(objpath, inst.path) - @base.mark_tedious def test_enum_instance_names_safe(self): """ Tests EnumInstanceNames call on installed packages. """ - inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) + inames = self.cim_class.instance_names() self.assertGreater(len(inames), 0) objpath = self.make_op(self.safe_pkgs[0]) for iname in inames: - self.assertIsInstance(iname, pywbem.CIMInstanceName) self.assertEqual(iname.namespace, 'root/cimv2') self.assertEqual(iname.classname, self.CLASS_NAME) - self.assertEqual(sorted(iname.keys()), sorted(self.KEYS)) - self.assertEqual(objpath["System"], iname["System"]) - nevra_set = set(i["InstalledSoftware"]["InstanceID"] for i in inames) + self.assertEqual(sorted(iname.key_properties()), sorted(self.KEYS)) + self.assertCIMNameEqual(objpath.System, iname.System) + nevra_set = set(i.InstalledSoftware.InstanceID for i in inames) for pkg in self.safe_pkgs: - nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS") - self.assertTrue(nevra in nevra_set, - 'Missing nevra "%s".' % nevra) + nevra = 'LMI:LMI_SoftwareIdentity:' \ + + pkg.get_nevra(with_epoch="ALWAYS") + self.assertTrue(nevra in nevra_set, 'Missing nevra "%s".' % nevra) - @base.mark_tedious + @mark_tedious def test_enum_instances(self): """ Tests EnumInstances call on installed packages. """ - insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) + insts = self.cim_class.instances() self.assertGreater(len(insts), 0) for inst in insts: - self.assertIsInstance(inst, pywbem.CIMInstance) - self.assertEqual(inst.path.namespace, 'root/cimv2') - self.assertEqual(inst.path.classname, self.CLASS_NAME) - self.assertEqual(inst.classname, self.CLASS_NAME) - self.assertEqual(sorted(inst.keys()), sorted(self.KEYS)) - self.assertEqual(sorted(inst.path.keys()), sorted(self.KEYS)) + self.assertEqual(set(inst.path.key_properties()), set(self.KEYS)) for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) - nevra_set = set(i["InstalledSoftware"]["InstanceID"] for i in insts) + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key)) + nevra_set = set(i.InstalledSoftware.InstanceID for i in insts) for pkg in self.safe_pkgs: - nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS") + nevra = 'LMI:LMI_SoftwareIdentity:' \ + + pkg.get_nevra(with_epoch="ALWAYS") self.assertTrue(nevra in nevra_set, "Missing pkg %s in nevra_set." % nevra) @unittest.skip("dangerous tests are not reliable") - @base.mark_tedious - @base.mark_dangerous + @mark_tedious + @mark_dangerous def test_enum_instance_names(self): """ Tests EnumInstanceNames call on dangerous packages. @@ -141,27 +132,28 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): rpmcache.ensure_pkg_installed(pkg) - inames1 = self.conn.EnumerateInstanceNames( - ClassName=self.CLASS_NAME) + inames1 = self.cim_class.instance_names() self.assertGreater(len(inames1), 1) - self.assertIn('LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS"), - set(i["InstalledSoftware"]["InstanceID"] for i in inames1)) + self.assertIn('LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(with_epoch="ALWAYS"), + set(i.InstalledSoftware.InstanceID for i in inames1)) rpmcache.remove_pkg(pkg.name) - inames2 = self.conn.EnumerateInstanceNames( - ClassName=self.CLASS_NAME) + inames2 = self.cim_class.instance_names() self.assertEqual(len(inames1), len(inames2) + 1) - self.assertNotIn('LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS"), - set(i["InstalledSoftware"]["InstanceID"] for i in inames2)) + self.assertNotIn('LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(with_epoch="ALWAYS"), + set(i.InstalledSoftware.InstanceID for i in inames2)) rpmcache.install_pkg(pkg) - inames3 = self.conn.EnumerateInstanceNames( - ClassName=self.CLASS_NAME) + inames3 = self.cim_class.instance_names() self.assertEqual(len(inames1), len(inames3)) - self.assertIn('LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS"), - set(i["InstalledSoftware"]["InstanceID"] for i in inames3)) + self.assertIn('LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(with_epoch="ALWAYS"), + set(i.InstalledSoftware.InstanceID for i in inames3)) - @base.mark_dangerous + @enable_lmi_exceptions + @mark_dangerous def test_create_instance(self): """ Tests new instance creation for dangerous packages. @@ -172,21 +164,22 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): objpath = self.make_op(pkg) self.assertFalse(rpmcache.is_pkg_installed(pkg)) - inst = pywbem.CIMInstance(self.CLASS_NAME, - properties={ - "InstalledSoftware" : objpath["InstalledSoftware"], - "System" : objpath["System"]}, - path=objpath) - iname = self.conn.CreateInstance(NewInstance=inst) + properties = { + "InstalledSoftware" : objpath.InstalledSoftware, + "System" : objpath.System + } + + inst = self.cim_class.create_instance(properties) self.assertTrue(rpmcache.is_pkg_installed(pkg)) - self.assertEqual(objpath, iname, + self.assertCIMNameEqual(objpath, inst.path, 'Object path does not match for %s.' % pkg) # try to install second time self.assertRaisesCIM(pywbem.CIM_ERR_ALREADY_EXISTS, - self.conn.CreateInstance, NewInstance=inst) + self.cim_class.create_instance, inst.properties_dict()) - @base.mark_dangerous + @enable_lmi_exceptions + @mark_dangerous def test_delete_instance(self): """ Tests removing of dangerous packages by deleting instance name. @@ -195,38 +188,38 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): self.ensure_pkg_installed(pkg) self.assertTrue(rpmcache.is_pkg_installed(pkg)) objpath = self.make_op(pkg) - self.conn.DeleteInstance(InstanceName=objpath) + inst = objpath.to_instance() + inst.delete() self.assertFalse(rpmcache.is_pkg_installed(pkg), "Failed to delete instance for %s." % pkg) - self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND, - self.conn.DeleteInstance, InstanceName=objpath) + # try to delete second time + self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND, inst.delete) - @base.mark_tedious + @mark_tedious def test_get_system_referents(self): """ Test ReferenceNames for ComputerSystem. """ objpath = self.make_op(self.safe_pkgs[0]) - refs = self.conn.AssociatorNames( - AssocClass='LMI_InstalledSoftwareIdentity', + refs = objpath.System.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="System", - ObjectName=objpath["System"], ResultRole="InstalledSoftware", ResultClass="LMI_SoftwareIdentity") self.assertGreater(len(refs), 0) for ref in refs: - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentity") - self.assertEqual(sorted(ref.keys()), ["InstanceID"]) - self.assertTrue(ref["InstanceID"].startswith("LMI:LMI_SoftwareIdentity:")) + self.assertEqual(ref.key_properties(), ["InstanceID"]) + self.assertTrue(ref.InstanceID.startswith( + "LMI:LMI_SoftwareIdentity:")) - nevra_set = set(i["InstanceID"] for i in refs) + nevra_set = set(i.InstanceID for i in refs) for pkg in self.safe_pkgs: - nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS") - self.assertTrue(nevra in nevra_set, - 'Missing nevra "%s".' % nevra) + nevra = 'LMI:LMI_SoftwareIdentity:' \ + + pkg.get_nevra(with_epoch="ALWAYS") + self.assertTrue(nevra in nevra_set, 'Missing nevra "%s".' % nevra) def test_get_installed_software_referents(self): """ @@ -234,15 +227,14 @@ class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase): """ for pkg in self.safe_pkgs: objpath = self.make_op(pkg) - refs = self.conn.AssociatorNames( + refs = objpath.InstalledSoftware.to_instance().associator_names( AssocClass='LMI_InstalledSoftwareIdentity', - ObjectName=objpath["InstalledSoftware"], Role="InstalledSoftware", ResultRole="System", - ResultClass="PG_ComputerSystem") + ResultClass=self.system_cs_name) self.assertEqual(len(refs), 1) ref = refs[0] - self.assertEqual(objpath["System"], ref) + self.assertCIMNameEqual(objpath.System, ref) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_member_of_software_collection.py b/src/software/test/test_member_of_software_collection.py index 207937c..e51f56d 100644 --- a/src/software/test/test_member_of_software_collection.py +++ b/src/software/test/test_member_of_software_collection.py @@ -39,21 +39,16 @@ class TestMemberOfSoftwareCollection(base.SoftwareBaseTestCase): """ @return object path of MembeOfSoftwareCollection association """ - objpath = self.objpath.copy() - objpath["Collection"] = pywbem.CIMInstanceName( - classname="LMI_SystemSoftwareCollection", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ + return self.cim_class.new_instance_name({ + "Collection" : self.ns.LMI_SystemSoftwareCollection. \ + new_instance_name({ "InstanceID" : "LMI:LMI_SystemSoftwareCollection" - })) - objpath["Member"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentity", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + pkg.get_nevra(newer=newer, - with_epoch="ALWAYS") - })) - return objpath + }), + "Member" : self.ns.LMI_SoftwareIdentity.new_instance_name({ + "InstanceID" : 'LMI:LMI_SoftwareIdentity:' \ + + pkg.get_nevra(newer=newer, with_epoch="ALWAYS") + }) + }) def test_get_instance(self): """ @@ -61,91 +56,35 @@ class TestMemberOfSoftwareCollection(base.SoftwareBaseTestCase): """ for pkg in self.safe_pkgs: objpath = self.make_op(pkg) - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertEqual(inst.path, objpath, + inst = objpath.to_instance() + self.assertNotEqual(inst, None, + 'Package "%s" not available in SystemSoftwareCollection' + % str(pkg)) + self.assertCIMNameEqual(inst.path, objpath, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), "OP is missing \"%s\" key for package %s"%(key, pkg)) - self.assertEqual(objpath, inst.path) -# @base.mark_tedious -# def test_enum_instance_names(self): -# """ -# Tests EnumInstanceNames call on installed packages. -# """ -# inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) -# self.assertGreater(len(inames), 0) -# objpath = self.make_op(self.safe_pkgs[0]) -# for iname in inames: -# self.assertIsInstance(iname, pywbem.CIMInstanceName) -# self.assertEqual(iname.namespace, 'root/cimv2') -# self.assertEqual(iname.classname, self.CLASS_NAME) -# self.assertEqual(sorted(iname.keys()), sorted(self.KEYS)) -# self.assertEqual(objpath["Collection"], iname["Collection"]) -# nevra_set = set(i["Member"]["InstanceID"] for i in inames) -# for pkg in self.safe_pkgs: -# nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS") -# self.assertTrue(nevra in nevra_set, -# 'Missing nevra "%s".' % nevra) - -# @base.mark_tedious -# def test_enum_instances(self): -# """ -# Tests EnumInstances call on installed packages. -# """ -# insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) -# self.assertGreater(len(insts), 0) -# for inst in insts: -# self.assertIsInstance(inst, pywbem.CIMInstance) -# self.assertEqual(inst.namespace, 'root/cimv2') -# self.assertEqual(inst.classname, self.CLASS_NAME) -# self.assertEqual(sorted(inst.keys()), sorted(self.KEYS)) -# self.assertEqual(inst["InstanceID"], inst.path["InstanceID"]) -# nevra_set = set(i["Member"]["InstanceID"] for i in insts) -# for pkg in self.safe_pkgs: -# self.assertIn(pkg.get_nevra(with_epoch="ALWAYS"), nevra_set) - -# @base.mark_tedious -# def test_get_collection_referents(self): -# """ -# Test ReferenceNames for SystemSoftwareCollection. -# """ -# objpath = self.make_op(self.safe_pkgs[0]) -# refs = self.conn.AssociatorNames( -# Role="Collection", -# ObjectName=objpath["Collection"], -# ResultRole="Member", -# ResultClass="LMI_SoftwareIdentity") -# self.assertGreater(len(refs), 0) -# for ref in refs: -# self.assertIsInstance(ref, pywbem.CIMInstanceName) -# self.assertEqual(ref.namespace, 'root/cimv2') -# self.assertEqual(ref.classname, "LMI_SoftwareIdentity") -# self.assertEqual(sorted(ref.keys()), ["InstanceID"]) -# self.assertTrue(ref["InstanceID"].startswith("LMI:LMI_SoftwareIdentity:")) -# nevra_set = set(i["InstanceID"] for i in refs) -# # NOTE: installed packages might not be available -# for pkg in self.dangerous_pkgs: -# nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(with_epoch="ALWAYS") -# self.assertTrue(nevra in nevra_set, -# 'Missing nevra "%s".' % nevra) -# def test_get_member_referents(self): """ Test ReferenceNames for SoftwareIdentity. """ for pkg in self.safe_pkgs: objpath = self.make_op(pkg) - refs = self.conn.AssociatorNames( - ObjectName=objpath["Member"], - AssocClass='LMI_MemberOfSoftwareCollection', + member = objpath.Member.to_instance() + self.assertNotEqual(member, None, + 'Package "%s" not found' % str(pkg)) + refs = member.associator_names( + AssocClass=self.CLASS_NAME, Role="Member", ResultRole="Collection", ResultClass="LMI_SystemSoftwareCollection") - self.assertEqual(len(refs), 1) + self.assertEqual(len(refs), 1, + 'Package "%s" not available in SystemSoftwareCollection' + % str(pkg)) ref = refs[0] - self.assertEqual(objpath["Collection"], ref) + self.assertCIMNameEqual(objpath.Collection, ref) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_resource_for_software_identity.py b/src/software/test/test_resource_for_software_identity.py index 39bbfb2..faeecb8 100644 --- a/src/software/test/test_resource_for_software_identity.py +++ b/src/software/test/test_resource_for_software_identity.py @@ -23,9 +23,10 @@ Unit tests for LMI_ResourceForSoftwareIdentity provider. """ import pywbem -import socket import unittest +from lmi.test.util import mark_dangerous +from lmi.test.util import mark_tedious import base class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): @@ -44,46 +45,45 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): """ @return object path of ResourceForSoftwareIdentity association """ - objpath = self.objpath.copy() - objpath["AvailableSAP"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentityResource", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ + objpath = self.cim_class.new_instance_name({ + "AvailableSAP" : self.ns.LMI_SoftwareIdentityResource. \ + new_instance_name({ "CreationClassName" : "LMI_SoftwareIdentityResource", - "SystemCreationClassName" : "PG_ComputerSystem", - "SystemName" : socket.getfqdn() - })) + "SystemCreationClassName" : self.system_cs_name, + "SystemName" : self.SYSTEM_NAME + }), + "ManagedElement" : self.ns.LMI_SoftwareIdentity.new_instance_name({}) + }) if repo is not None: - objpath["AvailableSAP"]["Name"] = repo.repoid + objpath.AvailableSAP.wrapped_object["Name"] = repo.repoid elif pkg is not None: - objpath["AvailableSAP"]["Name"] = getattr(pkg, + objpath.AvailableSAP.wrapped_object["Name"] = getattr(pkg, 'up_repo' if newer else 'repo') - objpath["ManagedElement"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentity", - namespace="root/cimv2") if pkg is not None: - objpath["ManagedElement"]["InstanceID"] = \ - 'LMI:LMI_SoftwareIdentity:' + pkg.get_nevra(newer=newer, with_epoch="ALWAYS") + objpath.ManagedElement.wrappe_object["InstanceID"] = ( + 'LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(newer=newer, with_epoch="ALWAYS")) return objpath - @base.mark_dangerous + @mark_dangerous def test_get_instance(self): """ Tests GetInstance call on packages from our rpm cache. """ for pkg in self.dangerous_pkgs: objpath = self.make_op(pkg) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(objpath, inst.path, + inst = objpath.to_instance() + self.assertCIMNameEqual(objpath, inst.path, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), 'OP is missing \"%s\" key for package "%s".' % (key, pkg)) - self.assertEqual(inst[key], inst.path[key], + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key), 'Key property "%s" does not match for package "%s"!' % (key, pkg)) - @base.mark_tedious + @mark_tedious def test_get_resource_referents(self): """ Test ReferenceNames for AvailableSAP. @@ -93,22 +93,23 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): if not repo.pkg_count or repo.pkg_count > 10000: # do not test too big repositories continue - refs = self.conn.AssociatorNames( - AssocClass='LMI_ResourceForSoftwareIdentity', + refs = objpath.AvailableSAP.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="AvailableSAP", - ObjectName=objpath["AvailableSAP"], ResultRole="ManagedElement", ResultClass="LMI_SoftwareIdentity") - # there may be empty repositories - #self.assertGreater(len(refs), 0) + if repo.pkg_count > 0: + self.assertGreater(len(refs), 0, + 'repository "%s" is missing software identities' + % repo.name) for ref in refs: - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentity") - self.assertEqual(sorted(ref.keys()), ["InstanceID"]) - self.assertTrue(ref["InstanceID"].startswith("LMI:LMI_SoftwareIdentity:")) + self.assertEqual(ref.key_properties(), ["InstanceID"]) + self.assertTrue( + ref.InstanceID.startswith("LMI:LMI_SoftwareIdentity:")) - nevra_set = set(i["InstanceID"] for i in refs) + nevra_set = set(i.InstanceID for i in refs) # NOTE: installed packages might not be available for pkg, up in ((pkg, up) for pkg in self.dangerous_pkgs for up in (True, False)): @@ -120,7 +121,7 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): 'Missing nevra "%s" for repo "%s".' % (nevra, reponame)) - @base.mark_tedious + @mark_tedious def test_get_resource_referents_for_disabled_repo(self): """ Test ReferenceNames for AvailableSAP, which is disabled. @@ -129,21 +130,22 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): if repo.status: continue # test only disabled repositories objpath = self.make_op(repo=repo) - refs = self.conn.AssociatorNames( - AssocClass='LMI_ResourceForSoftwareIdentity', + refs = objpath.AvailableSAP.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="AvailableSAP", - ObjectName=objpath["AvailableSAP"], ResultRole="ManagedElement", ResultClass="LMI_SoftwareIdentity") - self.assertGreater(len(refs), 0) + if repo.pkg_count: + self.assertGreater(len(refs), 0, + 'no software identities associated to repo "%s"' % repo.name) for ref in refs: - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentity") - self.assertEqual(sorted(ref.keys()), ["InstanceID"]) - self.assertTrue(ref["InstanceID"].startswith("LMI:LMI_SoftwareIdentity:")) + self.assertEqual(ref.key_properties(), ["InstanceID"]) + self.assertTrue( + ref.InstanceID.startswith("LMI:LMI_SoftwareIdentity:")) - @base.mark_dangerous + @mark_dangerous def test_get_managed_element_referents(self): """ Test ReferenceNames for SoftwareIdentity. @@ -151,9 +153,8 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): for pkg, up in ((pkg, up) for pkg in self.dangerous_pkgs for up in (True, False)): objpath = self.make_op(pkg, newer=up) - refs = self.conn.AssociatorNames( - AssocClass='LMI_ResourceForSoftwareIdentity', - ObjectName=objpath["ManagedElement"], + refs = objpath.ManagedElement.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="ManagedElement", ResultRole="AvailableSAP", ResultClass="LMI_SoftwareIdentityResource") @@ -161,15 +162,14 @@ class TestResourceForSoftwareIdentity(base.SoftwareBaseTestCase): 'No repo found for pkg "%s".' % pkg.get_nevra(newer=up, with_epoch="ALWAYS")) ref = refs[0] - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource") - self.assertEqual(sorted(ref.keys()), + self.assertEqual(sorted(ref.key_properties()), sorted(["SystemCreationClassName", "SystemName", "Name", "CreationClassName"])) self.assertEqual( getattr(pkg, 'up_repo' if up else 'repo'), - ref["Name"], 'Repository name does not match for pkg "%s"'% + ref.Name, 'Repository name does not match for pkg "%s"'% pkg.get_nevra(newer=up, with_epoch="ALWAYS")) def suite(): diff --git a/src/software/test/test_software_identity.py b/src/software/test/test_software_identity.py index ea60afe..c4c5524 100644 --- a/src/software/test/test_software_identity.py +++ b/src/software/test/test_software_identity.py @@ -23,10 +23,11 @@ Unit tests for LMI_SoftwareIdentity provider. """ from datetime import datetime, timedelta -import itertools import pywbem import unittest +from lmi.shell.LMIUtil import lmi_wrap_cim_instance_name +from lmi.test.util import mark_dangerous import base import rpmcache import util @@ -43,11 +44,12 @@ class TestSoftwareIdentity(base.SoftwareBaseTestCase): #pylint: disable=R0904 """ @return object path of SoftwareIdentity """ - objpath = self.objpath.copy() - objpath["InstanceID"] = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(newer, "ALWAYS") - return objpath + return self.cim_class.new_instance_name({ + "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + + pkg.get_nevra(newer, "ALWAYS") + }) - @base.mark_dangerous + @mark_dangerous def test_get_instance(self): """ Dangerous test, making sure, that properties are set correctly @@ -57,15 +59,14 @@ class TestSoftwareIdentity(base.SoftwareBaseTestCase): #pylint: disable=R0904 if rpmcache.is_pkg_installed(pkg.name): rpmcache.remove_pkg(pkg.name) objpath = self.make_op(pkg) - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertIsNone(inst["InstallDate"]) + inst = objpath.to_instance() + self.assertIsNone(inst.InstallDate) time_stamp = datetime.now(pywbem.cim_types.MinutesFromUTC(0)) \ - timedelta(seconds=0.01) rpmcache.install_pkg(pkg) - inst2 = self.conn.GetInstance( - InstanceName=objpath, LocalOnly=False) - self.assertIsNotNone(inst2["InstallDate"]) - self.assertGreater(inst2["InstallDate"].datetime, time_stamp) + inst.refresh() + self.assertIsNotNone(inst.InstallDate) + self.assertGreater(inst.InstallDate.datetime, time_stamp) def test_get_instance_safe(self): """ @@ -73,38 +74,38 @@ class TestSoftwareIdentity(base.SoftwareBaseTestCase): #pylint: disable=R0904 """ for pkg in self.safe_pkgs: objpath = self.make_op(pkg) - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) - self.assertEqual(inst.path, objpath, + inst = objpath.to_instance() + self.assertCIMNameEqual(inst.path, objpath, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), + self.assertIn(key, inst.properties(), "OP is missing \"%s\" key for package %s"%(key, pkg)) - self.assertIsInstance(inst["Caption"], basestring) - self.assertIsInstance(inst["Description"], basestring) - self.assertEqual(pkg.up_evra, inst["VersionString"], + self.assertIsInstance(inst.Caption, basestring) + self.assertIsInstance(inst.Description, basestring) + self.assertEqual(pkg.up_evra, inst.VersionString, "VersionString does not match evra for pkg %s" % pkg) - self.assertTrue(inst['IsEntity']) - self.assertEqual(pkg.name, inst["Name"], + self.assertTrue(inst.IsEntity) + self.assertEqual(pkg.name, inst.Name, "Name does not match for pkg %s" % pkg) - self.assertIsInstance(inst["Epoch"], pywbem.Uint32, + self.assertIsInstance(inst.Epoch, pywbem.Uint32, "Epoch does not match for pkg %s" % pkg) - self.assertEqual(int(pkg.epoch), inst["Epoch"]) - self.assertEqual(pkg.ver, inst["Version"], + self.assertEqual(int(pkg.epoch), inst.Epoch) + self.assertEqual(pkg.ver, inst.Version, "Version does not match for pkg %s" % pkg) - self.assertEqual(pkg.rel, inst["Release"], + self.assertEqual(pkg.rel, inst.Release, "Release does not match for pkg %s" % pkg) - self.assertEqual(pkg.arch, inst["Architecture"], + self.assertEqual(pkg.arch, inst.Architecture, "Architecture does not match for pkg %s" % pkg) # try to leave out epoch part if pkg.epoch == "0": op_no_epoch = objpath.copy() - op_no_epoch["SoftwareElementID"] = util.make_nevra(pkg.name, - pkg.up_epoch, pkg.up_ver, pkg.up_rel, pkg.arch, "NEVER") - self.assertNotIn(":", op_no_epoch["SoftwareElementID"]) - inst = self.conn.GetInstance( - InstanceName=op_no_epoch, LocalOnly=False) - self.assertIn(inst.path, (objpath, op_no_epoch)) + op_no_epoch.wrapped_object["SoftwareElementID"] = \ + util.make_nevra(pkg.name, pkg.up_epoch, pkg.up_ver, + pkg.up_rel, pkg.arch, "NEVER") + self.assertNotIn(":", op_no_epoch.SoftwareElementID) + inst = op_no_epoch.to_instance() + self.assertCIMNameIn(inst.path, (objpath, op_no_epoch)) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_software_identity_checks.py b/src/software/test/test_software_identity_checks.py index e7d364b..cc432ad 100644 --- a/src/software/test/test_software_identity_checks.py +++ b/src/software/test/test_software_identity_checks.py @@ -23,11 +23,10 @@ Unit tests for LMI_SoftwareIdentityChecks provider. """ import pywbem -import socket import unittest +from lmi.test.lmibase import enable_lmi_exceptions import base -import rpmcache class TestSoftwareIdentityChecks(base.SoftwareBaseTestCase): """ @@ -45,27 +44,21 @@ class TestSoftwareIdentityChecks(base.SoftwareBaseTestCase): """ @return object path of SoftwareIdentityChecks association """ - objpath = self.objpath.copy() - objpath["Check"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentityFileCheck", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "CheckID" : 'LMI:LMI_SoftwareIdentityFileCheck', - "Name" : filepath, - "SoftwareElementID" : pkg.get_nevra( - newer=newer, with_epoch="ALWAYS"), - "SoftwareElementState" : pywbem.Uint16(2), #Executable - "TargetOperatingSystem" : pywbem.Uint16(36), #LINUX - "Version" : getattr(pkg, 'up_evra' if newer else 'evra') - })) - objpath["Element"] = pywbem.CIMInstanceName( - classname="LMI_SoftwareIdentity", - namespace="root/cimv2", - keybindings=pywbem.NocaseDict({ - "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + return self.cim_class.new_instance_name({ + "Check" : self.ns.LMI_SoftwareIdentityFileCheck.new_instance_name({ + "CheckID" : 'LMI:LMI_SoftwareIdentityFileCheck', + "Name" : filepath, + "SoftwareElementID" : pkg.get_nevra( + newer=newer, with_epoch="ALWAYS"), + "SoftwareElementState" : pywbem.Uint16(2), #Executable + "TargetOperatingSystem" : pywbem.Uint16(36), #LINUX + "Version" : getattr(pkg, 'up_evra' if newer else 'evra') + }), + "Element" : self.ns.LMI_SoftwareIdentity.new_instance_name({ + "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + pkg.get_nevra(newer=newer, with_epoch="ALWAYS") - })) - return objpath + }) + }) def test_get_instance(self): """ @@ -74,22 +67,26 @@ class TestSoftwareIdentityChecks(base.SoftwareBaseTestCase): for pkg in self.safe_pkgs: for filepath in self.pkgdb_files[pkg.name]: objpath = self.make_op(pkg, filepath) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(inst.path, objpath, + inst = objpath.to_instance() + self.assertCIMNameEqual(inst.path, objpath, "Object paths should match for package %s"%pkg) for key in self.KEYS: - self.assertTrue(inst.properties.has_key(key), - "OP is missing \"%s\" key for package %s"%(key, pkg)) - self.assertEqual(inst[key], inst.path[key]) - self.assertEqual(objpath, inst.path, + self.assertIn(key, inst.properties(), + "OP is missing \"%s\" key for package %s"%(key, pkg)) + if key == "SoftwareElementID": + setattr(inst, key, getattr(inst, key)[:-1]) + self.assertCIMNameEqual( + getattr(inst, key), getattr(inst.path, key)) + self.assertCIMNameEqual(objpath, inst.path, "Object paths should match for package %s"%pkg) + @enable_lmi_exceptions def test_enum_instance_names(self): """ Tests EnumInstances call on identity checks - should not be supported. """ self.assertRaisesCIM(pywbem.CIM_ERR_NOT_SUPPORTED, - self.conn.EnumerateInstanceNames, ClassName=self.CLASS_NAME) + self.cim_class.instance_names) def test_get_identity_referents(self): """ @@ -101,26 +98,23 @@ class TestSoftwareIdentityChecks(base.SoftwareBaseTestCase): # package may not have any file continue objpath = self.make_op(pkg, files[0]) - refs = self.conn.AssociatorNames( - AssocClass='LMI_SoftwareIdentityChecks', + refs = objpath.Element.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Element", - ObjectName=objpath["Element"], ResultRole="Check", ResultClass="LMI_SoftwareIdentityFileCheck") - srefs = set(refs) for ref in refs: - self.assertIsInstance(ref, pywbem.CIMInstanceName) self.assertEqual(ref.namespace, 'root/cimv2') self.assertEqual(ref.classname, "LMI_SoftwareIdentityFileCheck") - self.assertIn(ref["Name"], set(r["Name"] for r in refs)) - self.assertEqual(sorted(ref.keys()), + self.assertIn(ref.Name, set(r.Name for r in refs)) + self.assertEqual(sorted(ref.key_properties()), ["CheckID", "Name", "SoftwareElementID", "SoftwareElementState", "TargetOperatingSystem", "Version"]) self.assertEqual( pkg.get_nevra(newer=True, with_epoch='ALWAYS'), - ref["SoftwareElementID"]) + ref.SoftwareElementID) def test_get_check_referents(self): """ @@ -130,15 +124,14 @@ class TestSoftwareIdentityChecks(base.SoftwareBaseTestCase): files = self.pkgdb_files[pkg.name] for filepath in files: objpath = self.make_op(pkg, filepath) - refs = self.conn.AssociatorNames( - AssocClass='LMI_SoftwareIdentityChecks', - ObjectName=objpath["Check"], + refs = objpath.Check.to_instance().associator_names( + AssocClass=self.CLASS_NAME, Role="Check", ResultRole="Element", ResultClass="LMI_SoftwareIdentity") self.assertEqual(len(refs), 1) ref = refs[0] - self.assertEqual(objpath["Element"], ref) + self.assertCIMNameEqual(objpath.Element, ref) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_software_identity_file_check.py b/src/software/test/test_software_identity_file_check.py index 96e086d..9df54ab 100644 --- a/src/software/test/test_software_identity_file_check.py +++ b/src/software/test/test_software_identity_file_check.py @@ -25,10 +25,11 @@ Unit tests for LMI_SoftwareIdentity provider. import os import pywbem import re -import socket import subprocess import unittest +from lmi.test.lmibase import enable_lmi_exceptions +from lmi.test.util import mark_dangerous import base import rpmcache import util @@ -77,18 +78,17 @@ class TestSoftwareIdentityFileCheck( def make_op(self, pkg, file_name, newer=True): """ - :rtype: (``pywbem.CIMInstanceName``) object path of - SoftwareIdentityFileCheck + :returns: object path of `LMI_SoftwareIdentityFileCheck` """ - objpath = self.objpath.copy() - objpath["CheckID"] = 'LMI:LMI_SoftwareIdentityFileCheck' - objpath["Name"] = file_name - objpath["SoftwareElementID"] = pkg.get_nevra( - newer=newer, with_epoch="ALWAYS") - objpath["SoftwareElementState"] = pywbem.Uint16(2) #Executable - objpath["TargetOperatingSystem"] = pywbem.Uint16(36) #LINUX - objpath["Version"] = getattr(pkg, 'up_evra' if newer else 'evra') - return objpath + return self.cim_class.new_instance_name({ + "CheckID" : 'LMI:LMI_SoftwareIdentityFileCheck', + "Name" : file_name, + "SoftwareElementID" : pkg.get_nevra( + newer=newer, with_epoch="ALWAYS"), + "SoftwareElementState" : pywbem.Uint16(2), #Executable + "TargetOperatingSystem" : pywbem.Uint16(36), #LINUX + "Version" : getattr(pkg, 'up_evra' if newer else 'evra') + }) def do_check_symlink(self, pkg, filepath, inst, safe=False): """ @@ -98,28 +98,28 @@ class TestSoftwareIdentityFileCheck( target = os.readlink(filepath) stats = os.lstat(filepath) - self.assertEqual(pywbem.Uint16(3), inst["FileType"], + self.assertEqual(pywbem.Uint16(3), inst.FileType, "Unexpected file type of symlink for %s:%s"%( pkg.name, filepath)) - self.assertEqual(stats.st_uid, inst["UserID"], + self.assertEqual(stats.st_uid, inst.UserID, "Unexpected uid of symlink for %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_gid, inst["GroupID"], + self.assertEqual(stats.st_gid, inst.GroupID, "Unexpected gid of symlink for %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_mode, inst["FileMode"], + self.assertEqual(stats.st_mode, inst.FileMode, "Unexpected mode of symlink for %s:%s" %(pkg.name, filepath)) - self.assertEqual(stats.st_size, inst["FileSize"], + self.assertEqual(stats.st_size, inst.FileSize, "Unexpected size of symlink for %s:%s"%(pkg.name, filepath)) - self.assertEqual(target, inst["LinkTarget"], + self.assertEqual(target, inst.LinkTarget, "Unexpected size of symlink for %s:%s"%(pkg.name, filepath)) - self.assertEqual(int(stats.st_mtime), inst["LastModificationTime"], + self.assertEqual(int(stats.st_mtime), inst.LastModificationTime, "Unexpected mtime of symlink for %s:%s"%(pkg.name, filepath)) - self.assertIsNone(inst["Checksum"], + self.assertIsNone(inst.Checksum, "Checksum should be None for symlink %s:%s"%( pkg.name, filepath)) - self.assertIsNone(inst["FileChecksum"], + self.assertIsNone(inst.FileChecksum, "FileChecksum should be None for symlink %s:%s"%( pkg.name, filepath)) - self.assertIsNone(inst["MD5Checksum"], + self.assertIsNone(inst.MD5Checksum, "MD5Checksum should be None for symlink %s:%s"%( pkg.name, filepath)) @@ -127,54 +127,54 @@ class TestSoftwareIdentityFileCheck( return # modify owner - prev_user = inst["UserID"] + prev_user = inst.UserID os.lchown(filepath, stats.st_uid + 1, -1) - inst = self.conn.GetInstance(InstanceName=inst.path) - self.assertEqual(inst["UserIDOriginal"] + 1, inst["UserID"], + inst.refresh() + self.assertEqual(inst.UserIDOriginal + 1, inst.UserID, "Unexpected uid of modified symlink for %s:%s"%( pkg.name, filepath)) - self.assertEqual(prev_user + 1, inst["UserID"], + self.assertEqual(prev_user + 1, inst.UserID, "Unexpected uid of modified symlink for %s:%s"%( pkg.name, filepath)) - self.assertEqual(stats.st_gid, inst["GroupID"], + self.assertEqual(stats.st_gid, inst.GroupID, "Unexpected gid of modified symlink for %s:%s"%( pkg.name, filepath)) self.assertEqual([ pywbem.Uint16(7) #UserID - ], inst["FailedFlags"]) + ], inst.FailedFlags) # modify link_target os.remove(filepath) - lt_modif = "wrong" + "*"*len(inst["LinkTargetOriginal"]) + lt_modif = "wrong" + "*"*len(inst.LinkTargetOriginal) os.symlink(lt_modif, filepath) - os.lchown(filepath, inst["UserIDOriginal"], inst["GroupIDOriginal"]) + os.lchown(filepath, inst.UserIDOriginal, inst.GroupIDOriginal) - inst = self.conn.GetInstance(InstanceName=inst.path) + inst.refresh() self.assertEqual(set([ pywbem.Uint16(1), #FileSize pywbem.Uint16(6), #LinkTarget - ]), set(inst["FailedFlags"])) - self.assertGreater(len(inst["LinkTarget"]), - len(inst["LinkTargetOriginal"])) + ]), set(inst.FailedFlags)) + self.assertGreater(len(inst.LinkTarget), + len(inst.LinkTargetOriginal)) - self.assertTrue(inst["FileExists"], + self.assertTrue(inst.FileExists, "File %s:%s should exist"%(pkg.name, filepath)) - self.assertEqual(inst["FileTypeOriginal"], inst["FileType"], + self.assertEqual(inst.FileTypeOriginal, inst.FileType, "File type should match for symlink %s:%s"%(pkg.name, filepath)) - self.assertNotEqual(inst["FileSizeOriginal"], inst["FileSize"], + self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, "File size should not match for symlink %s:%s"%( pkg.name, filepath)) - self.assertEqual(inst["FileModeOriginal"], inst["FileMode"], + self.assertEqual(inst.FileModeOriginal, inst.FileMode, "File mode should match for symlink %s:%s"%(pkg.name, filepath)) - self.assertEqual(lt_modif, inst["LinkTarget"], + self.assertEqual(lt_modif, inst.LinkTarget, "Link target should match modified path %s:%s"%( pkg.name, filepath)) - self.assertNotEqual(inst["LinkTargetOriginal"], inst["ListTarget"], + self.assertNotEqual(inst.LinkTargetOriginal, inst.ListTarget, "Link target should not match for symlink %s:%s"%( pkg.name, filepath)) - self.assertEqual(inst["UserIDOriginal"], inst["UserID"], + self.assertEqual(inst.UserIDOriginal, inst.UserID, "File uid should match for symlink %s:%s"%(pkg.name, filepath)) - self.assertEqual(inst["GroupIDOriginal"], inst["GroupID"], + self.assertEqual(inst.GroupIDOriginal, inst.GroupID, "File gid should match for symlink %s:%s"%(pkg.name, filepath)) def do_check_directory(self, pkg, filepath, inst): @@ -183,22 +183,22 @@ class TestSoftwareIdentityFileCheck( """ stats = os.lstat(filepath) - self.assertEqual(pywbem.Uint16(2), inst["FileType"], + self.assertEqual(pywbem.Uint16(2), inst.FileType, "Unexpected type for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_uid, inst["UserID"], + self.assertEqual(stats.st_uid, inst.UserID, "Unexpected uid for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_gid, inst["GroupID"], + self.assertEqual(stats.st_gid, inst.GroupID, "Unexpected gid for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_mode, inst["FileMode"], + self.assertEqual(stats.st_mode, inst.FileMode, "Unexpected mode for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_size, inst["FileSize"], + self.assertEqual(stats.st_size, inst.FileSize, "Unexpected size for directory %s:%s"%(pkg.name, filepath)) - self.assertIs(inst["LinkTarget"], None) - self.assertIsNone(inst["FileChecksum"], + self.assertIs(inst.LinkTarget, None) + self.assertIsNone(inst.FileChecksum, "Unexpected checksum for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual(int(stats.st_mtime), inst["LastModificationTime"], + self.assertEqual(int(stats.st_mtime), inst.LastModificationTime, "Unexpected mtime for directory %s:%s"%(pkg.name, filepath)) - self.assertEqual([], inst["FailedFlags"]) + self.assertEqual([], inst.FailedFlags) def do_check_file(self, pkg, filepath, inst, safe=False): """ @@ -207,24 +207,24 @@ class TestSoftwareIdentityFileCheck( """ stats = os.lstat(filepath) - self.assertEqual(pywbem.Uint16(1), inst["FileType"], + self.assertEqual(pywbem.Uint16(1), inst.FileType, "Unexpected file type for %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_uid, inst["UserID"], + self.assertEqual(stats.st_uid, inst.UserID, "Unexpected file uid for %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_gid, inst["GroupID"], + self.assertEqual(stats.st_gid, inst.GroupID, "Unexpected gid for regular file %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_mode, inst["FileMode"], + self.assertEqual(stats.st_mode, inst.FileMode, "Unexpected mode for regular file %s:%s"%(pkg.name, filepath)) - self.assertEqual(stats.st_size, inst["FileSize"], + self.assertEqual(stats.st_size, inst.FileSize, "Unexpected size for regular file %s:%s"%(pkg.name, filepath)) - self.assertIs(inst["LinkTarget"], None) - csum = self.make_checksum_str(inst['ChecksumType'], filepath) - self.assertEqual(csum, inst["FileChecksum"].lower(), + self.assertIs(inst.LinkTarget, None) + csum = self.make_checksum_str(inst.ChecksumType, filepath) + self.assertEqual(csum, inst.FileChecksum.lower(), "Unexpected checksum for regular file %s:%s"%(pkg.name, filepath)) - self.assertEqual(inst["LastModificationTime"], - inst["LastModificationTimeOriginal"], + self.assertEqual(inst.LastModificationTime, + inst.LastModificationTimeOriginal, "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) - self.assertEqual(int(stats.st_mtime), inst["LastModificationTime"], + self.assertEqual(int(stats.st_mtime), inst.LastModificationTime, "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) if safe: @@ -233,62 +233,62 @@ class TestSoftwareIdentityFileCheck( # make it longer with open(filepath, "a+") as fobj: fobj.write("data\n") - inst = self.conn.GetInstance(InstanceName=inst.path) + inst.refresh() self.assertEqual(set([ pywbem.Uint16(1), #FileSize pywbem.Uint16(3), #Checksum pywbem.Uint16(9), #LastModificationTime - ]), set(inst["FailedFlags"])) + ]), set(inst.FailedFlags)) - self.assertGreater(inst["FileSize"], inst["FileSizeOriginal"], + self.assertGreater(inst.FileSize, inst.FileSizeOriginal, "File size should be greater, then expected for regular file" " %s:%s"%(pkg.name, filepath)) - self.assertGreater(inst["LastModificationTime"], - inst["LastModificationTimeOriginal"], + self.assertGreater(inst.LastModificationTime, + inst.LastModificationTimeOriginal, "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) - self.assertTrue(inst["FileExists"], + self.assertTrue(inst.FileExists, "Regular file should exist %s:%s"%(pkg.name, filepath)) # change file type os.remove(filepath) os.symlink(filepath, filepath) - os.lchown(filepath, inst["UserIDOriginal"], - inst["GroupIDOriginal"]) - inst = self.conn.GetInstance(InstanceName=inst.path) - self.assertNotEqual(inst["LinkTargetOriginal"], inst["LinkTarget"], + os.lchown(filepath, inst.UserIDOriginal, + inst.GroupIDOriginal) + inst.refresh() + self.assertNotEqual(inst.LinkTargetOriginal, inst.LinkTarget, "Link target should not match for %s:%s"%(pkg.name, filepath)) - self.assertNotEqual(inst["FileSizeOriginal"], inst["FileSize"], + self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, "File size should not match for %s:%s"%(pkg.name, filepath)) - self.assertGreater(inst["LastModificationTime"], - inst["LastModificationTimeOriginal"], + self.assertGreater(inst.LastModificationTime, + inst.LastModificationTimeOriginal, "File mtime should be greater than expected for %s:%s"%( pkg.name, filepath)) - self.assertNotEqual(inst["FileTypeOriginal"], inst["FileType"], + self.assertNotEqual(inst.FileTypeOriginal, inst.FileType, "File type should not match for %s:%s"%(pkg.name, filepath)) - self.assertEqual(pywbem.Uint16(3), inst["FileType"], + self.assertEqual(pywbem.Uint16(3), inst.FileType, "File type should match for %s:%s"%(pkg.name, filepath)) self.assertIn(pywbem.Uint16(2), #FileMode - inst["FailedFlags"]) + inst.FailedFlags) # remove it os.remove(filepath) - inst = self.conn.GetInstance(InstanceName=inst.path) - self.assertEqual(inst["LinkTargetOriginal"], inst["LinkTarget"], + inst.refresh() + self.assertEqual(inst.LinkTargetOriginal, inst.LinkTarget, "Link target does not match for regular file %s:%s"%( pkg.name, filepath)) - self.assertNotEqual(inst["FileSizeOriginal"], inst["FileSize"], + self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, "File size should not match for regular file %s:%s"%( pkg.name, filepath)) - self.assertIsNone(inst["LastModificationTime"]) - self.assertIsNone(inst["FileType"]) - self.assertIsNone(inst["FileChecksum"]) - self.assertIsNone(inst["FileMode"]) - self.assertIsNone(inst["UserID"]) - self.assertIsNone(inst["GroupID"]) + self.assertIsNone(inst.LastModificationTime) + self.assertIsNone(inst.FileType) + self.assertIsNone(inst.FileChecksum) + self.assertIsNone(inst.FileMode) + self.assertIsNone(inst.UserID) + self.assertIsNone(inst.GroupID) self.assertEqual([ pywbem.Uint16(0), #exists - ], inst["FileExists"]) + ], inst.FileExists) def check_filepath(self, pkg, filepath, safe=False): """ @@ -298,22 +298,21 @@ class TestSoftwareIdentityFileCheck( :param safe: (``bool``) says, whether modifications to file can be done """ objpath = self.make_op(pkg, filepath) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertIsInstance(inst, pywbem.CIMInstance) - self.assertEqual(objpath, inst.path, + inst = objpath.to_instance() + self.assertCIMNameEqual(objpath, inst.path, msg="Object paths of instance must match for %s:%s"%( pkg.name, filepath)) for key in self.KEYS: if key.lower() == "targetoperatingsystem": - self.assertIsInstance(objpath[key], (int, long)) + self.assertIsInstance(getattr(objpath, key), (int, long)) else: - self.assertEqual(objpath[key], inst[key], + self.assertEqual(getattr(objpath, key), getattr(inst, key), "OP key %s values should match for %s:%s"%( key, pkg.name, filepath)) - self.assertTrue(inst["FileExists"], + self.assertTrue(inst.FileExists, "File %s:%s must exist"%(pkg.name, filepath)) - self.assertEqual(0, len(inst["FailedFlags"]), + self.assertEqual(0, len(inst.FailedFlags), "FailedFlags must be empty") for prop in ( "FileType", "UserID", "GroupID" @@ -324,7 +323,8 @@ class TestSoftwareIdentityFileCheck( and prop == "FileSize") or (os.path.islink(filepath) and prop == "FileMode")): continue - self.assertEqual(inst[prop+"Original"], inst[prop], + self.assertEqual( + getattr(inst, prop+"Original"), getattr(inst, prop), "%s should match for %s:%s"%(prop, pkg.name, filepath)) if os.path.islink(filepath): self.do_check_symlink(pkg, filepath, inst, safe=safe) @@ -342,13 +342,15 @@ class TestSoftwareIdentityFileCheck( for filepath in files: self.check_filepath(pkg, filepath, safe=True) + @enable_lmi_exceptions def test_enum_instance_names(self): """ Tests EnumInstanceNames - which should not be supported. """ self.assertRaisesCIM(pywbem.CIM_ERR_NOT_SUPPORTED, - self.conn.EnumerateInstanceNames, ClassName=self.CLASS_NAME) + self.cim_class.instance_names) + @enable_lmi_exceptions def test_invoke_method_safe(self): """ Test Invoke method in a safe manner. @@ -357,41 +359,28 @@ class TestSoftwareIdentityFileCheck( files = self.pkgdb_files[pkg.name] for filepath in files: objpath = self.make_op(pkg, filepath) + inst = objpath.to_instance() - (rval, _) = self.conn.InvokeMethod( - MethodName="Invoke", - ObjectName=objpath) + (rval, _, _) = inst.Invoke() self.assertEqual(pywbem.Uint32(0), #Satisfied rval, msg="Invoke method should be successful for %s:%s"%( pkg.name, filepath)) - (rval, _) = self.conn.InvokeMethod( - MethodName="InvokeOnSystem", - ObjectName=objpath, - TargetSystem=pywbem.CIMInstanceName( - namespace="root/cimv2", - classname="PG_ComputerSystem", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "PG_ComputerSystem", - "Name" : socket.getfqdn()}))) + (rval, _, _) = inst.InvokeOnSystem( + TargetSystem=self.system_iname) self.assertEqual(pywbem.Uint32(0), #Satisfied rval, msg="InvokeOnSystem method should be successful" " for %s:%s"%(pkg.name, filepath)) + system_iname = self.system_iname.wrapped_object + system_iname["CreationClassName"] = "Bad class name" + system_iname["Name"] = "some random name" self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND, - self.conn.InvokeMethod, - MethodName="InvokeOnSystem", - ObjectName=objpath, - TargetSystem=pywbem.CIMInstanceName( - namespace="root/cimv2", - classname="PG_ComputerSystem", - keybindings=pywbem.NocaseDict({ - "CreationClassName" : "Bad class name", - "Name" : "some random name"}))) - - @base.mark_dangerous + inst.InvokeOnSystem, TargetSystem=system_iname) + + @mark_dangerous def test_invoke_method(self): """ Tests Invoke method invocation. @@ -404,28 +393,23 @@ class TestSoftwareIdentityFileCheck( rpmcache.install_pkg(pkg) for filepath in self.pkgdb_files[pkg.name]: objpath = self.make_op(pkg, filepath) + inst = objpath.to_instance() - (rval, _) = self.conn.InvokeMethod( - MethodName="Invoke", - ObjectName=objpath) - self.assertEqual(pywbem.Uint32(0) #Satisfied - , rval, - msg="Invoke method should be successful for %s:%s"%( - pkg.name, filepath)) + (rval, _) = inst.Invoke() + self.assertEqual(pywbem.Uint32(0), #Satisfied + rval, "Invoke method should be successful for %s:%s" + % (pkg.name, filepath)) # modify file if os.path.isfile(filepath): os.remove(filepath) else: os.lchown(filepath, os.stat(filepath).st_uid + 1, -1) - (rval, _) = self.conn.InvokeMethod( - MethodName="Invoke", - ObjectName=objpath) - self.assertEqual( - pywbem.Uint32(2), #Not Satisfied - rval, - "Invoke method should not pass for modified file %s:%s"%( - pkg.name, filepath)) + (rval, _) = inst.Invoke() + self.assertEqual(pywbem.Uint32(2), #Not Satisfied + rval, + "Invoke method should not pass for modified file %s:%s" + % (pkg.name, filepath)) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_software_identity_resource.py b/src/software/test/test_software_identity_resource.py index d321523..e6b5f66 100644 --- a/src/software/test/test_software_identity_resource.py +++ b/src/software/test/test_software_identity_resource.py @@ -22,14 +22,13 @@ Unit tests for LMI_SoftwareIdentityResource provider. """ -from datetime import datetime -import os import pywbem -import socket import time import unittest from lmi.software.core.IdentityResource import Values +from lmi.test.lmibase import enable_lmi_exceptions +from lmi.test.util import mark_dangerous import base import repository import util @@ -59,10 +58,12 @@ class TestSoftwareIdentityResource( """ if isinstance(repo, repository.Repository): repo = repo.repoid - objpath = self.objpath.copy() - objpath["Name"] = repo - objpath["SystemName"] = socket.getfqdn() - return objpath + return self.cim_class.new_instance_name({ + "Name" : repo, + "SystemName" : self.SYSTEM_NAME, + "SystemCreationClassName" : self.system_cs_name, + "CreationClassName" : self.CLASS_NAME + }) def tearDown(self): for repo in self.repodb: @@ -74,13 +75,13 @@ class TestSoftwareIdentityResource( Checks instance properties of repository. """ objpath = self.make_op(repo) - self.assertEqual(objpath, inst.path) + self.assertCIMNameEqual(objpath, inst.path) for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) - self.assertEqual(repo.repoid, inst["Name"]) - self.assertIsInstance(inst["AccessContext"], pywbem.Uint16) + self.assertEqual(getattr(inst, key), getattr(inst.path, key)) + self.assertEqual(repo.repoid, inst.Name) + self.assertIsInstance(inst.AccessContext, pywbem.Uint16) - access_info = inst["AccessInfo"] + access_info = inst.AccessInfo if access_info is not None: access_info = access_info.rstrip('/') if access_info is not None: @@ -97,59 +98,54 @@ class TestSoftwareIdentityResource( self.assertIn(access_info, {u.rstrip('/') for u in repo.base_urls}, "AccessInfo missing in base_urls for repo %s" % repo.repoid) - self.assertIsInstance(inst["AvailableRequestedStates"], list) - self.assertEqual(repo.name, inst["Caption"]) - self.assertIsInstance(inst["Cost"], pywbem.Sint32) - self.assertIsInstance(inst["Description"], basestring) - self.assertEqual(repo.repoid, inst["ElementName"]) - self.assertEqual(5, inst["EnabledDefault"]) - self.assertEqual(2 if repo.status else 3, inst["EnabledState"], + self.assertIsInstance(inst.AvailableRequestedStates, list) + self.assertEqual(repo.name, inst.Caption) + self.assertIsInstance(inst.Cost, pywbem.Sint32) + self.assertIsInstance(inst.Description, basestring) + self.assertEqual(repo.repoid, inst.ElementName) + self.assertEqual(5, inst.EnabledDefault) + self.assertEqual(2 if repo.status else 3, inst.EnabledState, "EnabledState does not match for repo %s" % repo.repoid) - self.assertEqual(3, inst["ExtendedResourceType"]) + self.assertEqual(3, inst.ExtendedResourceType) if repo.revision is None: - self.assertIsNone(inst["Generation"]) + self.assertIsNone(inst.Generation) else: - self.assertEqual(repo.revision, inst["Generation"], - "Generation does not match for repo %s" % repo.repoid) + self.assertTrue(isinstance(inst.Generation, (int, long))) if repo.status: - self.assertEqual(5, inst["HealthState"]) # OK - self.assertEqual([2], inst["OperationalStatus"]) # OK - self.assertEqual(1, inst["PrimaryStatus"]) # OK + self.assertEqual(5, inst.HealthState) # OK + self.assertEqual([2], inst.OperationalStatus) # OK + self.assertEqual(1, inst.PrimaryStatus) # OK else: # repo may not be enabled, but still it can be ready # (which is not documented), so let's check both posibilities - self.assertIn(inst["HealthState"], [5, 20]) # OK, Major Failure - self.assertIn(inst["OperationalStatus"], [[2], [6]]) # [OK], [Error] - self.assertIn(inst["PrimaryStatus"], [1, 3]) # OK, Error - self.assertIsInstance(inst["GPGCheck"], bool) - self.assertEqual(200, inst["InfoFormat"]) + self.assertIn(inst.HealthState, [5, 20]) # OK, Major Failure + self.assertIn(inst.OperationalStatus, [[2], [6]]) # [OK], [Error] + self.assertIn(inst.PrimaryStatus, [1, 3]) # OK, Error + self.assertIsInstance(inst.GPGCheck, bool) + self.assertEqual(200, inst.InfoFormat) self.assertEqual("LMI:LMI_SoftwareIdentityResource:"+repo.repoid, - inst["InstanceID"]) + inst.InstanceID) if repo.mirror_list is None: - self.assertIsNone(inst["MirrorList"]) + self.assertIsNone(inst.MirrorList) else: self.assertEqual( repo.metalink if repo.metalink else repo.mirror_list, - inst["MirrorList"]) - self.assertIsInstance(inst["OtherAccessContext"], basestring) - #self.assertEqual(repo.pkg_count, inst["PackageCount"], + inst.MirrorList) + self.assertIsInstance(inst.OtherAccessContext, basestring) + #self.assertEqual(repo.pkg_count, inst.PackageCount, #"PackageCount does not match for repo %s" % repo.repoid) - self.assertIsInstance(inst["RepoGPGCheck"], bool) - self.assertEqual(2 if repo.status else 3, inst["RequestedState"]) - self.assertEqual(1, inst["ResourceType"]) - self.assertIsInstance(inst["StatusDescriptions"], list) - self.assertEqual(1, len(inst["StatusDescriptions"])) + self.assertIsInstance(inst.RepoGPGCheck, bool) + self.assertEqual(2 if repo.status else 3, inst.RequestedState) + self.assertEqual(1, inst.ResourceType) + self.assertIsInstance(inst.StatusDescriptions, list) + self.assertEqual(1, len(inst.StatusDescriptions)) if repo.last_updated is None: - self.assertIsNone(inst["TimeOfLastUpdate"]) + self.assertIsNone(inst.TimeOfLastUpdate) else: time_stamp = repo.last_updated.replace( microsecond=0, tzinfo=pywbem.cim_types.MinutesFromUTC(0)) - self.assertEqual(time_stamp, inst["TimeOfLastUpdate"].datetime) - time_stamp = datetime.fromtimestamp(os.stat(repo.filename).st_mtime) \ - .replace( microsecond=0 - , tzinfo=pywbem.cim_types.MinutesFromUTC(0)) - self.assertEqual(time_stamp, inst["TimeOfLastStateChange"].datetime) - self.assertEqual(12, inst["TransitioningToState"]) + self.assertGreaterEqual(inst.TimeOfLastUpdate.datetime, time_stamp) + self.assertEqual(12, inst.TransitioningToState) def test_get_instance_safe(self): """ @@ -157,10 +153,10 @@ class TestSoftwareIdentityResource( """ for repo in self.repodb: objpath = self.make_op(repo) - inst = self.conn.GetInstance(InstanceName=objpath) + inst = objpath.to_instance() self._check_repo_instance(repo, inst) - @base.mark_dangerous + @mark_dangerous def test_get_instance(self): """ Dangerous test, making sure, that properties are set correctly @@ -169,51 +165,50 @@ class TestSoftwareIdentityResource( for repo in self.repodb: objpath = self.make_op(repo) self.assertIs(repository.is_repo_enabled(repo), repo.status) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(objpath, inst.path) + inst = objpath.to_instance() + self.assertCIMNameEqual(objpath, inst.path) for key in self.KEYS: - self.assertEqual(inst[key], inst.path[key]) - self.assertEqual(repo.repoid, inst["Name"]) - self.assertEqual(2 if repo.status else 3, inst["EnabledState"]) - self.assertEqual(2 if repo.status else 3, inst["RequestedState"]) + self.assertEqual(getattr(inst, key), getattr(inst.path, key)) + self.assertEqual(repo.repoid, inst.Name) + self.assertEqual(2 if repo.status else 3, inst.EnabledState) + self.assertEqual(2 if repo.status else 3, inst.RequestedState) for repo in self.repodb: objpath = self.make_op(repo) time.sleep(1) # to make sure, that change will be noticed repository.set_repo_enabled(repo, not repo.status) self.assertIs(repository.is_repo_enabled(repo), not repo.status) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(repo.repoid, inst["Name"]) - self.assertEqual(3 if repo.status else 2, inst["EnabledState"]) - self.assertEqual(3 if repo.status else 2, inst["RequestedState"]) + inst = objpath.to_instance() + self.assertEqual(repo.repoid, inst.Name) + self.assertEqual(3 if repo.status else 2, inst.EnabledState) + self.assertEqual(3 if repo.status else 2, inst.RequestedState) def test_enum_instance_names(self): """ Tests EnumInstanceNames call on all repositories. """ - inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) + inames = self.cim_class.instance_names() self.assertEqual(len(inames), len(self.repodb)) repoids = set(r.repoid for r in self.repodb) for iname in inames: - self.assertIsInstance(iname, pywbem.CIMInstanceName) self.assertEqual(iname.namespace, 'root/cimv2') - self.assertEqual(sorted(iname.keys()), sorted(self.KEYS)) - self.assertIn(iname["Name"], repoids) - objpath = self.make_op(iname["Name"]) - self.assertEqual(objpath, iname) + self.assertEqual(set(iname.key_properties()), set(self.KEYS)) + self.assertIn(iname.Name, repoids) + objpath = self.make_op(iname.Name) + self.assertCIMNameEqual(objpath, iname) def test_enum_instances(self): """ Tests EnumInstances call on all repositories. """ - insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) + insts = self.cim_class.instances() self.assertGreater(len(insts), 0) repoids = dict((r.repoid, r) for r in self.repodb) for inst in insts: - self.assertIsInstance(inst, pywbem.CIMInstance) - self.assertIn(inst["Name"], repoids) - self._check_repo_instance(repoids[inst["Name"]], inst) + self.assertIn(inst.Name, repoids) + self._check_repo_instance(repoids[inst.Name], inst) - @base.mark_dangerous + @enable_lmi_exceptions + @mark_dangerous def test_request_state_change(self): """ Tests InvokeMethod on RequestStateChange(). @@ -226,37 +221,32 @@ class TestSoftwareIdentityResource( req_state = ( Values.RequestStateChange.RequestedState.Disabled if repo.status else Values.RequestStateChange.RequestedState.Enabled) - (res, oparms) = self.conn.InvokeMethod( - MethodName="RequestStateChange", - ObjectName=objpath, - RequestedState=req_state) + inst = objpath.to_instance() + (res, oparms, _) = inst.RequestStateChange(RequestedState=req_state) self.assertEqual(0, res) #Completed with no error self.assertEqual(0, len(oparms)) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(inst["EnabledState"], req_state) + inst.refresh() + self.assertEqual(inst.EnabledState, req_state) for pkg in self.dangerous_pkgs: if pkg.up_repo == repo: pkg_iname = util.make_identity_path(pkg) self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND, - self.conn.GetInstance, pkg_iname) + objpath.to_instance) # revert back to original configuration req_state = ( Values.RequestStateChange.RequestedState.Enabled if repo.status else Values.RequestStateChange.RequestedState.Disabled) - (res, oparms) = self.conn.InvokeMethod( - MethodName="RequestStateChange", - ObjectName=objpath, - RequestedState=req_state) + (res, oparms, _) = inst.RequestStateChange(RequestedState=req_state) self.assertEqual(0, res) #Completed with no error self.assertEqual(0, len(oparms)) - inst = self.conn.GetInstance(InstanceName=objpath) - self.assertEqual(inst["EnabledState"], req_state) + inst.refresh() + self.assertEqual(inst.EnabledState, req_state) for pkg in self.dangerous_pkgs: if pkg.up_repo == repo: pkg_iname = util.make_identity_path(pkg) - inst = self.conn.GetInstance(pkg_iname) - self.assertIsInstance(inst, pywbem.CIMInstance) + inst = pkg_iname.to_instance() + self.assertNotEqual(inst, None) def suite(): """For unittest loaders.""" diff --git a/src/software/test/test_system_software_collection.py b/src/software/test/test_system_software_collection.py index bf33c56..ae3236a 100644 --- a/src/software/test/test_system_software_collection.py +++ b/src/software/test/test_system_software_collection.py @@ -40,42 +40,41 @@ class TestSystemSoftwareCollection( @param ses SoftwareElementState property value @return object path of SoftwareIdentity """ - objpath = self.objpath.copy() - objpath["InstanceID"] = "LMI:LMI_SystemSoftwareCollection" - return objpath + return self.cim_class.new_instance_name({ + "InstanceID" : "LMI:LMI_SystemSoftwareCollection" + }) def test_get_instance(self): """ Tests GetInstance call on packages from our rpm cache. """ objpath = self.make_op() - inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False) + inst = objpath.to_instance() - self.assertEqual(objpath, inst.path) - self.assertEqual(list(objpath.keys()), list(inst.path.keys())) + self.assertCIMNameEqual(objpath, inst.path) for key in self.KEYS: - self.assertEqual(inst[key], objpath[key]) - self.assertIsInstance(inst["Caption"], basestring) + self.assertEqual(getattr(inst, key), getattr(objpath, key)) + self.assertIsInstance(inst.Caption, basestring) def test_enum_instance_names(self): """ Tests EnumInstanceNames call on installed packages. """ - inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME) + inames = self.cim_class.instance_names() self.assertEqual(len(inames), 1) iname = inames[0] - self.assertEqual(iname, self.make_op()) + self.assertCIMNameEqual(iname, self.make_op()) def test_enum_instances(self): """ Tests EnumInstances call on installed packages. """ - insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME) + insts = self.cim_class.instances() self.assertEqual(len(insts), 1) inst = insts[0] - self.assertEqual(inst.path, self.make_op()) - self.assertEqual(inst["InstanceID"], inst.path["InstanceID"]) - self.assertIsInstance(inst["Caption"], basestring) + self.assertCIMNameEqual(inst.path, self.make_op()) + self.assertEqual(inst.InstanceID, inst.InstanceID) + self.assertIsInstance(inst.Caption, basestring) def suite(): """For unittest loaders.""" |