def setup_for_parameter(self, server, integer_type, is_array, valuemap, values): # pylint: disable=redefined-outer-name """ Return a new ValueMapping object that is set up for a CIM parameter with the specified data type and valuemap and values qualifiers. """ test_parm = CIMParameter(PARMNAME, type=integer_type, is_array=is_array) if valuemap is not None: test_parm.qualifiers['ValueMap'] = \ CIMQualifier('ValueMap', valuemap, 'string') if values is not None: test_parm.qualifiers['Values'] = \ CIMQualifier('Values', values, 'string') test_meth = CIMMethod(METHNAME, return_type='string') test_meth.parameters[PARMNAME] = test_parm test_class = CIMClass(CLASSNAME) test_class.methods[METHNAME] = test_meth self.conn.GetClass = Mock(return_value=test_class) vm = ValueMapping.for_parameter(server, NAMESPACE, CLASSNAME, METHNAME, PARMNAME) return vm
def test_all(self): self._run_single(CIMClass('CIM_Foo')) self._run_single(CIMClass('CIM_Foo', superclass='CIM_bar')) self._run_single(CIMClass( 'CIM_CollectionInSystem', qualifiers={'ASSOCIATION': CIMQualifier('ASSOCIATION', True, overridable=False), 'Aggregation': CIMQualifier('Aggregation', True, overridable=False), 'Version': CIMQualifier('Version', '2.6.0', tosubclass=False, translatable=False), 'Description': CIMQualifier('Description', 'CIM_CollectionInSystem is an ' \ 'association used to establish a ' \ 'parent-child relationship between a ' \ 'collection and an \'owning\' System ' \ 'such as an AdminDomain or '\ 'ComputerSystem. A single collection '\ 'should not have both a ' \ 'CollectionInOrganization and a ' \ 'CollectionInSystem association.', translatable=True)}, properties={'Parent': CIMProperty( 'Parent', None, type='reference', reference_class='CIM_System', qualifiers={'Key': CIMQualifier('Key', True, overridable=False), 'Aggregate': CIMQualifier('Aggregate', True, overridable=False), 'Max': CIMQualifier('Max', Uint32(1))}), 'Child': CIMProperty( 'Child', None, type='reference', reference_class='CIM_Collection', qualifiers={'Key': CIMQualifier('Key', True, overridable=False)} ) } ))
def setup_for_property(self, valuemap, values): """ Set up a test class with a ValueMap/Values qualified property, and mock the GetClass() method to return that test class. """ test_prop = CIMProperty(PROPNAME, None, 'uint8') test_prop.qualifiers['ValueMap'] = \ CIMQualifier('ValueMap', valuemap, 'string') test_prop.qualifiers['Values'] = \ CIMQualifier('Values', values, 'string') test_class = CIMClass(CLASSNAME) test_class.properties[PROPNAME] = test_prop self.conn.GetClass = Mock(return_value=test_class)
def test_leaks_CIMClass_method(): """ Test function with a CIMClass object that has one method. """ _ = CIMClass('CIM_Foo', methods=[ CIMMethod('M1', return_type='string'), ])
def make_class(properties): """ Construct and return a CIMClass object from the specified properties. """ cname = random_name(Range(8, 16)) cls = CIMClass(cname, properties=properties) return cls
def test_leaks_CIMClass_qualifier(): """ Test function with a CIMClass object that has one qualifier. """ _ = CIMClass('CIM_Foo', qualifiers=[ CIMQualifier('Q1', value='q'), ])
def test_leaks_CIMClass_property(): """ Test function with a CIMClass object that has one property. """ _ = CIMClass('CIM_Foo', properties=[ CIMProperty('P1', value=None, type='string'), ])
def test_leaks_CIMClass_minimal(): """ Test function with a minimal CIMClass object (i.e. no members, no path). """ _ = CIMClass( 'CIM_Foo', superclass='CIM_Super', )
def setup_for_method(self, server, type_, valuemap, values): """ Return a new ValueMapping object that is set up for a CIM method with the specified data type and valuemap and values qualifiers. """ test_meth = CIMMethod(METHNAME, return_type=type_) if valuemap is not None: test_meth.qualifiers['ValueMap'] = \ CIMQualifier('ValueMap', valuemap, 'string') if values is not None: test_meth.qualifiers['Values'] = \ CIMQualifier('Values', values, 'string') test_class = CIMClass(CLASSNAME) test_class.methods[METHNAME] = test_meth self.conn.GetClass = Mock(return_value=test_class) vm = ValueMapping.for_method(server, NAMESPACE, CLASSNAME, METHNAME) return vm
def setup_for_property(self, server, type_, valuemap, values): """ Return a new ValueMapping object that is set up for a CIM property with the specified data type and valuemap and values qualifiers. """ test_prop = CIMProperty(PROPNAME, value=None, type=type_) if valuemap is not None: test_prop.qualifiers['ValueMap'] = \ CIMQualifier('ValueMap', valuemap, 'string') if values is not None: test_prop.qualifiers['Values'] = \ CIMQualifier('Values', values, 'string') test_class = CIMClass(CLASSNAME) test_class.properties[PROPNAME] = test_prop self.conn.GetClass = Mock(return_value=test_class) vm = ValueMapping.for_property(server, NAMESPACE, CLASSNAME, PROPNAME) return vm
def create_registered_conn(providers): """ Create and return a FakedWBEMConnection object that has the specified providers registered. providers: Providers to be registered. providers are specified as tuple(prov_class, cim_class, namespace): - prov_class: Python class of provider - cim_class: Name of CIM class for the provider - namespace: CIM namespace for the provider """ # Build a FakedWBEMConnection that has the required namespaces, classes # and registered providers in its provider registry. conn = FakedWBEMConnection() for item in providers: prov_class, cim_class, namespace = item # Make sure the namespace exists in the CIM repository if namespace not in conn.namespaces: conn.add_namespace(namespace) # Make sure the CIM class exists in the CIM repository class_store = conn.cimrepository.get_class_store(namespace) if not class_store.object_exists(cim_class): # An empty class is sufficient for this purpose: class_obj = CIMClass(cim_class) conn.add_cimobjects(class_obj, namespace=namespace) # Create the provider object, setting up its provider classes prov_class.provider_classnames = cim_class prov_obj = prov_class(conn.cimrepository) # Register the provider conn.register_provider(prov_obj, namespaces=namespace) return conn
def build_classes(): """ Function that builds and returns a single class: CIM_Foo that will to be used as a test class for the mock class tests. """ # build the key properties qkey = {'Key': CIMQualifier('Key', True)} dkey = {'Description': CIMQualifier('Description', 'blah blah')} # build the CIMClass with properties and methods. c = CIMClass('CIM_FooDirLoad', qualifiers=dkey, properties={ 'InstanceID': CIMProperty('InstanceID', None, qualifiers=qkey, type='string', class_origin='CIM_Foo', propagated=False) }, methods={ 'Delete': CIMMethod('Delete', 'uint32', qualifiers=dkey, class_origin='CIM_Foo', propagated=False), 'Fuzzy': CIMMethod('Fuzzy', 'string', qualifiers=dkey, class_origin='CIM_Foo', propagated=False) }) # add the objects to the mock repository CONN.add_cimobjects(c) # noqa: F821
def test_objectstore(testcase, init_args, cls_kwargs, inst_kwargs, qual_kwargs): # pylint: disable=unused-argument """ Simple test inserts one inst of defined type and tests retrievail methods. """ namespace = "root/cimv2" # Setup the ObjectStore xxx_repo = InMemoryObjectStore(*init_args) if cls_kwargs: cls = CIMClass(**cls_kwargs) if inst_kwargs: inst = CIMInstance(**inst_kwargs) inst.path = CIMInstanceName.from_instance( cls, inst, namespace=namespace, host='me', strict=True) if qual_kwargs: qual = CIMQualifierDeclaration(**qual_kwargs) # Is this instance or class test if inst_kwargs: name = inst.path obj = inst elif qual_kwargs: name = qual.name obj = qual else: name = cls.classname obj = cls # The code to be tested. The test include adding , getting, and deleting # with the various inspection methods and testing for # correct returns # Create the object in the object store xxx_repo.create(name, obj) # confirm that exists works assert xxx_repo.object_exists(name) # Test repository object count; len() assert xxx_repo.len() == 1 # Test get the object and test for same object rtn_obj = xxx_repo.get(name) # Test that the properties have changed indicating the deepcopy # Uses only class and instance because we they have properties if isinstance(obj, (CIMClass, CIMInstance)): for prop in obj.properties: assert rtn_obj.properties[prop] is not \ obj.properties[prop] # Test same object return on two gets rtn_obj2 = xxx_repo.get(name) assert rtn_obj2 == rtn_obj # Test that return with copy gets same object. rtn_obj3 = xxx_repo.get(name, copy=True) assert rtn_obj3 == obj # Test that with copy=True the property ids have changed between the # two gets indicating that the deepcopy was executed. if isinstance(obj, (CIMClass, CIMInstance)): for prop in obj.properties: assert rtn_obj.properties[prop] is not \ rtn_obj3.properties[prop] names = list(xxx_repo.iter_names()) assert len(names) == 1 assert names[0] == name objs = list(xxx_repo.iter_values()) assert len(objs) == 1 assert objs[0] == obj objs = list(xxx_repo.iter_values(copy=True)) assert len(objs) == 1 assert objs[0] == obj if isinstance(obj, (CIMClass, CIMInstance)): for prop in obj.properties: assert objs[0].properties[prop] is not \ obj.properties[prop] # Test update # Test update; should work. Note that this test does not modify the # object before creating the copy. obj2 = obj.copy() xxx_repo.update(name, obj2) assert xxx_repo.get(name) == obj2 # Test valid delete of object xxx_repo.delete(name) assert not xxx_repo.object_exists(name) assert xxx_repo.len() == 0 # Test errors # Test update with unknown object; should fail try: xxx_repo.update(name, obj) except KeyError: pass # Test delete nonexistent entity; should fail try: xxx_repo.delete(name) assert False except KeyError: pass # Test get non existent entity; should fail try: xxx_repo.get(name) assert False except KeyError: pass # Test exists; entity should not exist assert not xxx_repo.object_exists(name) # Test create with existing object xxx_repo.create(name, obj) # Test duplicate create; should fail try: xxx_repo.create(name, obj) assert False except ValueError: pass
# The code to be tested repo.remove_namespace(test_ns) assert exp_ns not in repo.namespaces else: with pytest.raises(exp_exc.__class__) as exec_info: # The code to be tested repo.remove_namespace(test_ns) print(exec_info) TEST_OBJECTS = [ CIMClass('Foo', properties=[ CIMProperty('P1', None, type='string', qualifiers=[CIMQualifier('Key', value=True)])]), CIMClass('Bar', properties=[ CIMProperty('P2', None, type='string', qualifiers=[CIMQualifier('Key', value=True)])]), CIMInstance('Foo', path=CIMInstanceName('Foo', keybindings=NocaseDict(P1="P1"))), CIMInstance('Bar', path=CIMInstanceName('Bar', keybindings=NocaseDict(P2="P2"))), CIMQualifierDeclaration('Qual1', type='string'), CIMQualifierDeclaration('Qual2', type='string'), ] TEST_OBJECTS2 = [ CIMClass('Foo', properties=[ CIMProperty('P2', None, type='string', qualifiers=[CIMQualifier('Key', value=True)])]),
def test_objectstore(testcase, init_args, cls_kwargs, inst_kwargs, qual_kwargs): # pylint: disable=unused-argument """ Simple test inserts one inst of defined type and tests retrievail methods. """ namespace = "root/cimv2" # Setup the ObjectStore xxx_repo = InMemoryObjectStore(*init_args) if cls_kwargs: cls = CIMClass(**cls_kwargs) if inst_kwargs: inst = CIMInstance(**inst_kwargs) inst.path = CIMInstanceName.from_instance( cls, inst, namespace=namespace, host='me', strict=True) if qual_kwargs: qual = CIMQualifierDeclaration(**qual_kwargs) # is this instance or class test if inst_kwargs: name = inst.path obj = inst elif qual_kwargs: name = qual.name obj = qual else: name = cls.classname obj = cls # The code to be tested. The test include adding and testing the # various inspection methods for correct returns # Create the object in the object store xxx_repo.create(name, obj) # confirm that exists works assert xxx_repo.exists(name) # Test len() assert xxx_repo.len() == 1 # Test get the object and test for same object rtn_obj = xxx_repo.get(name) assert rtn_obj == obj names = [n for n in xxx_repo.iter_names()] assert len(names) == 1 assert names[0] == name objs = [x for x in xxx_repo.iter_values()] assert len(objs) == 1 assert objs[0] == obj # Test update # Test update; should work obj2 = obj.copy() xxx_repo.update(name, obj2) assert xxx_repo.get(name) == obj2 # Test valid delete of object xxx_repo.delete(name) assert not xxx_repo.exists(name) assert xxx_repo.len() == 0 # Test errors # Test update with unknown object; should fail try: xxx_repo.update(name, obj) except KeyError: pass # Test delete nonexistent entity; should fail try: xxx_repo.delete(name) assert False except KeyError: pass # Test get non existent entity; should fail try: xxx_repo.get(name) assert False except KeyError: pass # Test exists; entity should not exist assert not xxx_repo.exists(name) # Test create with existing object xxx_repo.create(name, obj) # Test duplicate create; should fail try: xxx_repo.create(name, obj) assert False except ValueError: pass
keybindings=dict( Chicken='Ham', Beans=Uint8(43), ), ), ), ( "CIMClass with 10 properties, last one different", CIMClass( 'CIM_Foo', properties=[ CIMProperty('P1', 'V1', type='string'), CIMProperty('P2', 'V2', type='string'), CIMProperty('P3', 'V3', type='string'), CIMProperty('P4', 'V4', type='string'), CIMProperty('P5', 'V5', type='string'), CIMProperty('P6', 'V6', type='string'), CIMProperty('P7', 'V7', type='string'), CIMProperty('P8', 'V8', type='string'), CIMProperty('P9', 'V9', type='string'), CIMProperty('P10', 'V10', type='string'), ], ), CIMClass( 'CIM_Foo', properties=[ CIMProperty('P1', 'V1', type='string'), CIMProperty('P2', 'V2', type='string'), CIMProperty('P3', 'V3', type='string'), CIMProperty('P4', 'V4', type='string'), CIMProperty('P5', 'V5', type='string'),
minutes=44, seconds=55, microseconds=654321), exp_type_name=u'datetime', ), None, None, True), ("Object is a CIMDateTime object", dict( obj=CIMDateTime('20140924193040.654321+000'), exp_type_name=u'datetime', ), None, None, True), # Other CIM object tests ( "Object is a CIMClass object", dict( obj=CIMClass('CIM_Foo'), exp_type_name=u'string', # embedded object ), None, None, True), ( "Object is a CIMInstance object", dict( obj=CIMInstance('CIM_Foo'), exp_type_name=u'string', # embedded object ), None, None, True), ("Object is a CIMInstanceName object",
("Empty two-segment namespace where first segment already exists", dict( namespace='interop/def', namespace_content={'interop/def': []}, exp_namespace=u'interop/def', ), None, None, True), ("Non-existing top level namespace", dict( namespace='abc', namespace_content={}, exp_namespace=None, ), CIMError, None, True), ("Non-empty top level namespace containing a class", dict( namespace='abc', namespace_content={'abc': [CIMClass('Foo')]}, exp_namespace=None, ), CIMError, None, True), ("Non-empty top level namespace containing a qualifier type", dict( namespace='abc', namespace_content={'abc': [CIMQualifierDeclaration('Foo', 'string')]}, exp_namespace=None, ), CIMError, None, True), ("Existing Interop namespace", dict( namespace='interop', namespace_content={}, exp_namespace=None, ), CIMError, None, True), ("Second non-existing Interop namespace",
# * init_args: Tuple of positional arguments to is_subclass(). # * init_kwargs: Dict of keyword arguments to is_subclass(). # * exp_attrs: Dict of expected attributes of resulting object. # * exp_exc_types: Expected exception type(s), or None. # * exp_warn_types: Expected warning type(s), or None. # * condition: Boolean condition for testcase to run, or 'pdb' for # debugger ("Test of valid call with strings that returns True", dict( init_args=['CIM_ObjectManager', 'CIM_ManagedElement'], exp_attrs=True, ), None, None, True), ("Test of valid call with CIM objects that returns True", dict( init_args=[ CIMClass('CIM_ObjectManager', superclass='CIM_WBEMService'), CIMClass('CIM_ManagedElement', superclass=None), ], exp_attrs=True, ), None, None, True), ("Test of valid call with CIM object and string that returns True", dict( init_args=[ CIMClass('CIM_ObjectManager', superclass='CIM_WBEMService'), 'CIM_ManagedElement', ], exp_attrs=True, ), None, None, True), ("Test of valid call with string and CIM object that returns True", dict( init_args=[