def test_reference_properties(self): """Test Reference properties""" self._run_single(CIMProperty('Foo', None, type='reference')) self._run_single(CIMProperty('Foo', CIMInstanceName('CIM_Foo'))) self._run_single(CIMProperty('Foo', CIMInstanceName('CIM_Foo'), qualifiers={'Key': CIMQualifier('Key', True)}))
def test_property_arrays(self): """Property arrays""" self._run_single(CIMProperty('Foo', ['a', 'b', 'c'])) self._run_single(CIMProperty('Foo', None, type='string', is_array=True)) self._run_single(CIMProperty('Foo', [Uint8(x) for x in [1, 2, 3]], qualifiers={'Key': CIMQualifier('Key', True)}))
def test_single_valued(self): """Single-valued properties""" self._run_single(CIMProperty('Spotty', 'Foot')) self._run_single(CIMProperty('Age', Uint16(32))) self._run_single(CIMProperty('Foo', '', type='string')) self._run_single(CIMProperty('Foo', None, type='string')) self._run_single(CIMProperty('Age', None, type='uint16', qualifiers={'Key': CIMQualifier('Key', True)}))
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 make_properties(number, property_profile): """ Construct and return a list of CIMProperty objects for use on CIM classes, that has the specified number of properties according to the specified property profile. """ props = [] for _ in range(0, number): pname = random_name(property_profile.name_len) ptype, pvalue = random_type_value(property_profile.value_profile) if random.random() > property_profile.value_ratio: pvalue = None pquals = [] if property_profile.description_len: pquals.append( CIMQualifier('Description', type='string', value=random_string( property_profile.description_len))) if property_profile.qualifier_set_profile: qset = random.choice( property_profile.qualifier_set_profile.qualifier_sets) for j, qname in enumerate(qset.names): qtype = qset.types[j] _, qvalue = random_type_value(qset.values, type=qtype) pquals.append(CIMQualifier(qname, type=qtype, value=qvalue)) prop = CIMProperty(pname, type=ptype, value=pvalue, qualifiers=pquals) props.append(prop) return props
def test_leaks_CIMInstance_property(): """ Test function with a CIMInstance object that has one property. """ _ = CIMInstance('CIM_Foo', properties=[ CIMProperty('P1', value='p'), ])
def test_all(self): self._run_single('<PROPERTY NAME="Foo" TYPE="uint32"></PROPERTY>', CIMProperty('Foo', None, type='uint32')) self._run_single( '<PROPERTY NAME="Foo" TYPE="uint32"><VALUE>1234</VALUE>' '</PROPERTY>', CIMProperty('Foo', Uint32(1234))) self._run_single( '<PROPERTY NAME="Foo" TYPE="uint32">' '<QUALIFIER NAME="ASSOCIATION" TYPE="boolean">' '<VALUE>TRUE</VALUE></QUALIFIER><VALUE>1234</VALUE></PROPERTY>', CIMProperty( 'Foo', Uint32(1234), qualifiers={'ASSOCIATION': CIMQualifier('ASSOCIATION', True)}))
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 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 CreateInstance(self, namespace, new_instance): # pylint: disable=invalid-name """ Create an instance of the CIM_IndicationFilter class in an Interop namespace of the CIM repository, and if not yet existing create the new namespace in the CIM repository. See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for documentation of validation and description of input parameters, noting extra conditions for this provider as described below: Parameters: namespace (:term:`string`): Must be a valid Interop namespace. new_instance (:class:`~pywbem.CIMInstance`): The following applies regarding its properties: * 'Name' property: This property is required since it defines the name of the new namespace to be created. * 'CreationClassName' property: This property is required and its value must match the class name of the new instance. Raises: :exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER) if namespace is not the Interop namespace in the CIM repository or the Name property does not exist or the other properties cannot be added to the instance. """ self.parameter_is_interop(namespace, new_instance.classname) required_properties = ['Name'] self.validate_required_properties_exist(new_instance, namespace, required_properties) # Validates and possibly modifies the key properties except Name self.fix_key_properties(new_instance) # Note: No test for SourceNamespace valid namespaces because profile # allows creating filters for not-yet-created namespaces # Add missing properties that the might come from CIM_IndicationService # Issue # 2719, Should the following be set by the server or client set_property(new_instance, 'IndividualSubscriptionSupported', True) set_property(new_instance, 'SourceNamespace', CIMProperty('SourceNamespace', None, type='string')) set_property(new_instance, 'Description', "Pywbem mock CIMIndicationFilterProvider instance") # Create the CIM instance for the new namespace in the CIM repository, # by delegating to the default provider method. return super(CIMIndicationFilterProvider, self).CreateInstance(namespace, new_instance)
def test_create_instance(self): """Test record of create instance""" NewInstance = self.create_ciminstance() self.test_recorder.reset() self.test_recorder.stage_pywbem_args(method='CreateInstance', NewInstance=NewInstance, namespace='cim/blah') exc = None obj_name = self.create_ciminstancename() self.test_recorder.stage_pywbem_result(obj_name, exc) self.test_recorder.record_staged() test_yaml = self.loadYamlFile() test_yaml = test_yaml[0] pywbem_request = test_yaml['pywbem_request'] self.assertEqual(pywbem_request['url'], 'http://acme.com:80') operation = pywbem_request['operation'] self.assertEqual(operation['pywbem_method'], 'CreateInstance') returned_new_inst = operation['NewInstance'] self.assertEqual(returned_new_inst['classname'], NewInstance.classname) pd = returned_new_inst['properties'] # compare all properties returned against original self.assertEqual(len(pd), len(NewInstance.properties)) for pn, pv in pd.items(): prop = CIMProperty(pn, pv['value'], type=pv['type'], array_size=pv['array_size'], propagated=pv['propagated'], is_array=pv['is_array'], reference_class=pv['reference_class'], qualifiers=pv['qualifiers'], embedded_object=pv['embedded_object']) # temp display while sorting out datetime issues. # if pv['type'] == 'datetime': # print('datetime prop name:%s orig:%s new:%s' % # (pn, NewInstance.properties[pn], prop)) self.assertEqual( NewInstance.properties[pn], prop, 'Property compare failed orig %s, recreated %s' % (NewInstance.properties[pn], prop))
def make_instances(cls, number, value_profile): """ Construct and return a number of CIMInstance objects with path, with random values for the instance properties. """ insts = [] for _ in range(0, number): inst = CIMInstance(cls.classname) # TODO: Make namespace flexible inst.path = CIMInstanceName(cls.classname, namespace='root/cimv2') for pname, cls_prop in cls.properties.items(): ptype = cls_prop.type _, pvalue = random_type_value(value_profile, type=ptype) inst_prop = CIMProperty(pname, type=ptype, value=pvalue) inst.properties[pname] = inst_prop if cls_prop.qualifiers.get('Key', False): inst.path.keybindings[pname] = pvalue insts.append(inst) return insts
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
dict( in_kwargs={}, exp_result=None, ), TypeError, None, True), ("cim_obj is None", dict( in_kwargs=dict(cim_obj=None, ), exp_result=None, ), TypeError, None, True), ("cim_obj has invalid type", dict( in_kwargs=dict(cim_obj=42, ), exp_result=None, ), TypeError, None, True), ("cim_obj is CIMProperty with PUnit #1 (byte)", dict( in_kwargs=dict(cim_obj=CIMProperty( 'P1', value='foo', qualifiers=[TEST_QUAL_PUNIT1_BYTE])), exp_result=u'B', ), None, None, True), ("cim_obj is CIMMethod with PUnit #1 (byte)", dict( in_kwargs=dict(cim_obj=CIMMethod( 'P1', return_type='uint32', qualifiers=[TEST_QUAL_PUNIT1_BYTE])), exp_result=u'B', ), None, None, True), ("cim_obj is CIMParameter with PUnit #1 (byte)", dict( in_kwargs=dict(cim_obj=CIMParameter( 'P1', type='uint32', qualifiers=[TEST_QUAL_PUNIT1_BYTE])), exp_result=u'B', ), None, None, True), ("cim_obj is CIMProperty with PUnit #2 (byte)",
def test_embeddedobject_properties(self): """Test EmbeddedObject properties""" inst = CIMInstance('Foo_Class', {'one': Uint8(1), 'two': Uint8(2)}) self._run_single(CIMProperty('Foo', inst)) self._run_single(CIMProperty('Foo', [inst]))
# * cls_kwargs: single class def or list of class def # * inst_kwargs: Single inst def or list of class def # * 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 with a class", dict( init_args=[CIMClass], cls_kwargs=dict( classname='CIM_Foo', properties=[ CIMProperty( 'P1', None, type='string', qualifiers=[ CIMQualifier('Key', value=True) ] ), CIMProperty('P2', value='Cheese'), ] ), inst_kwargs=None, qual_kwargs=None, ), None, None, True ), ( "Test with an instance. Note the class used to build instance path", dict( init_args=[CIMInstance], cls_kwargs=dict(
def test_all(self): # Single-valued properties self._run_single(CIMProperty('Spotty', 'Foot')) self._run_single(CIMProperty('Age', Uint16(32))) self._run_single(CIMProperty('Foo', '', type='string')) self._run_single(CIMProperty('Foo', None, type='string')) self._run_single( CIMProperty('Age', None, type='uint16', qualifiers={'Key': CIMQualifier('Key', True)})) # Property arrays self._run_single(CIMProperty('Foo', ['a', 'b', 'c'])) self._run_single(CIMProperty('Foo', None, type='string', is_array=True)) self._run_single( CIMProperty('Foo', [Uint8(x) for x in [1, 2, 3]], qualifiers={'Key': CIMQualifier('Key', True)})) # Reference properties self._run_single(CIMProperty('Foo', None, type='reference')) self._run_single(CIMProperty('Foo', CIMInstanceName('CIM_Foo'))) self._run_single( CIMProperty('Foo', CIMInstanceName('CIM_Foo'), qualifiers={'Key': CIMQualifier('Key', True)})) # EmbeddedObject properties inst = CIMInstance('Foo_Class', {'one': Uint8(1), 'two': Uint8(2)}) self._run_single(CIMProperty('Foo', inst)) self._run_single(CIMProperty('Foo', [inst]))
), ), CIMInstanceName( 'CIM_Foo', 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'),
def CreateInstance(self, namespace, new_instance): # pylint: disable=invalid-name """ Create an instance of the CIM_IndicationFilter class in an Interop namespace of the CIM repository, and if not yet existing create the new namespace in the CIM repository. See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for documentation of validation and description of input parameters, noting extra conditions for this provider as described below: Parameters: namespace (:term:`string`): Must be a valid Interop namespace. new_instance (:class:`~pywbem.CIMInstance`): The following applies regarding its properties: * The 'Filter' and 'Handler' reference properties must exist. * If 'SubscriptionDuration' exists, 'SubscriptionTimeRemaining' will be set. * 'CreationClassName' property: This property is required and its value must match the class name of the new instance. Raises: :exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER) if namespace is not the Interop namespace in the CIM repository or the Name property does not exist or the other properties cannot be added to the instance. """ self.parameter_is_interop(namespace, new_instance.classname) required_properties = ["Filter", "Handler"] self.validate_required_properties_exist(new_instance, namespace, required_properties) # Add missing properties that the might come from CIM_IndicationService new_instance['SubscriptionStartTime'] = CIMDateTime.now() new_instance['TimeOfLastStateChange'] = CIMDateTime.now() # Conditionally add the following properties set_property(new_instance, 'OnFatalErrorPolicy', Uint16(2)) set_property(new_instance, 'RepeatNotificationPolicy', Uint16(2)) set_property(new_instance, 'SubscriptionState', Uint16(2)) if 'SubscriptionDuration' in new_instance: new_instance['SubscriptionTimeRemaining'] = \ new_instance['SubscriptionDuration'] else: new_instance['SubscriptionDuration'] = CIMProperty( 'SubscriptionDuration', None, type='uint64') set_property(new_instance, 'SubscriptionInfo', CIMProperty('SubscriptionInfo', None, type='string')) set_property(new_instance, 'Description', "Pywbem mock CIMIndicationSubscriptionProvider instance") # Create the CIM instance for the new namespace in the CIM repository, # by delegating to the default provider method. return super(CIMIndicationSubscriptionProvider, self).CreateInstance(namespace, new_instance)
def test_leaks_CIMProperty_minimal(): """ Test function with a minimal CIMProperty object (i.e. no qualifiers). """ _ = CIMProperty('P1', value='bla')