Ejemplo n.º 1
0
    def create_cimnamespace_instance(conn, namespace, interop_namespace,
                                     klass):
        """
        Build and execute CreateInstance for an instance of CIM_Namespace using
        the `namespace` parameter as the value of the name property in the
        instance. The instance is created in the `interop_namespace`

        Parameters:

          namespace (:term:`string`):
            Namespace that this instance defines.

          interop_namespace (:term:`string`):
            Interop namespace for this environment.

          klass (:class:`~pywbem.CIMClass`):
            The CIM class CIM_Namespace which is used to create the instance.

        Raises:

            :exc:`~pywbem.CIMError`: For errors encountered with CreateInstance
        """
        properties = NocaseDict([
            ('Name', namespace), ('CreationClassName', klass.classname),
            ('ObjectManagerName', OBJECTMANAGERNAME),
            ('ObjectManagerCreationClassName', OBJECTMANAGERCREATIONCLASSNAME),
            ('SystemName', SYSTEMNAME),
            ('SystemCreationClassName', SYSTEMCREATIONCLASSNAME)
        ])

        new_instance = CIMInstance.from_class(klass,
                                              property_values=properties)
        conn.CreateInstance(new_instance, interop_namespace)
Ejemplo n.º 2
0
 def assert_CIMProperty_attrs(self,
                              obj,
                              name,
                              value,
                              type_=None,
                              class_origin=None,
                              array_size=None,
                              propagated=None,
                              is_array=False,
                              reference_class=None,
                              qualifiers=None,
                              embedded_object=None):
     """
     Verify the attributes of the CIMProperty object in `obj` against
     expected values passed as the remaining arguments.
     """
     self.assertEqual(obj.name, name)
     self.assertEqual(obj.value, value)
     self.assertEqual(obj.type, type_)
     self.assertEqual(obj.class_origin, class_origin)
     self.assertEqual(obj.array_size, array_size)
     self.assertEqual(obj.propagated, propagated)
     self.assertEqual(obj.is_array, is_array)
     self.assertEqual(obj.reference_class, reference_class)
     self.assertEqual(obj.qualifiers, NocaseDict(qualifiers))
     self.assertEqual(obj.embedded_object, embedded_object)
Ejemplo n.º 3
0
 def assert_CIMProperty_attrs(obj,
                              name,
                              value,
                              type_=None,
                              class_origin=None,
                              array_size=None,
                              propagated=None,
                              is_array=False,
                              reference_class=None,
                              qualifiers=None,
                              embedded_object=None):
     """
     Verify the attributes of the CIMProperty object in `obj` against
     expected values passed as the remaining arguments.
     """
     assert obj.name == name
     assert obj.value == value
     assert obj.type == type_
     assert obj.class_origin == class_origin
     assert obj.array_size == array_size
     assert obj.propagated == propagated
     assert obj.is_array == is_array
     assert obj.reference_class == reference_class
     assert obj.qualifiers == NocaseDict(qualifiers)
     assert obj.embedded_object == embedded_object
Ejemplo n.º 4
0
def _filter_classes_for_qualifiers(qualifier_filters, results, names_only, iq):
    """
    Filter the results list for the qualifiers defined by filter
    qualifier: a dictionary with qualifier name as key and Boolean defining
    whether to display or not display if it exists.

    This method only works for boolean qualifiers
    """

    filtered_results = []
    if results:
        assert isinstance(results[0], CIMClass)
    for cls in results:
        assert isinstance(cls, CIMClass)
        show_this_class = True
        for qname, show_if_true in qualifier_filters.items():
            if qname in cls.qualifiers:
                qvalue = cls.qualifiers[qname].value
                show_this = True if qvalue == show_if_true else False
            else:
                show_this = not show_if_true
            if not show_this:
                show_this_class = False
                break
        if show_this_class:
            # If returning instances, honor the names_only option
            if not names_only:
                if not iq:
                    cls.qualifiers = NocaseDict()
                    for p in cls.properties.values():
                        p.qualifiers = NocaseDict()
                    for m in cls.methods.values():
                        m.qualifiers = NocaseDict()
                        for p in m.parameters.values():
                            p.qualifiers = NocaseDict()
            filtered_results.append(cls)
    if names_only:
        filtered_results = [cls.classname for cls in filtered_results]
    return filtered_results
Ejemplo n.º 5
0
    def __init__(self, faked_conn_object):
        """
        Initialize the connection.

          Parameters:

            faked_conn_object (:class:`~pywbem_mock.FakedWBEMConnection`):
              The object providing the CIM repository.
        """

        self.classes = NocaseDict()

        self.conn = faked_conn_object
        self.conn_id = self.conn.conn_id
Ejemplo n.º 6
0
    def _remove_qualifiers(obj):
        """
        Remove all qualifiers from the input object where the object may
        be an CIMInstance or CIMClass. Removes qualifiers from the object and
        from properties, methods, and parameters

        This is used to process the IncludeQualifier parameter for CIMClass
        and CIMInstance objects.

        Parameters:

          obj(:class:`~pywbem.CIMClass` or :class:`~pywbem.Instance`)
            Object from which qualifiers are to be removed
        """
        assert isinstance(obj, (CIMInstance, CIMClass))
        obj.qualifiers = NocaseDict()
        for prop in obj.properties:
            obj.properties[prop].qualifiers = NocaseDict()
        if isinstance(obj, CIMClass):
            for method in obj.methods:
                obj.methods[method].qualifiers = NocaseDict()
                for param in obj.methods[method].parameters:
                    obj.methods[method].parameters[param].qualifiers = \
                        NocaseDict()
Ejemplo n.º 7
0
    def __init__(self, faked_conn_object, cimrepository):
        """
        Initialize the connection.

          Parameters:

            faked_conn_object (FakedWBEMConnection):
              The instance of _FakeWBEMConnection to which this is attached.
              This allows us to use the same objects for qualifiers, instances
              and classes as that object
        """
        super(_MockMOFWBEMConnection, self).__init__(conn=faked_conn_object)

        self.classes = NocaseDict()

        self.repo = cimrepository
Ejemplo n.º 8
0
def test_NocaseDict_unnamedkeys_setitem(testcase, allow, key):
    """
    Test function for NocaseDict.__setitem__() for unnamed keys
    """

    obj = NocaseDict()
    obj.allow_unnamed_keys = allow

    # The code to be tested
    obj[key] = 'foo'

    # Ensure that exceptions raised in the remainder of this function
    # are not mistaken as expected exceptions
    assert testcase.exp_exc_types is None

    assert key in obj  # Uses __contains__()
Ejemplo n.º 9
0
    def __init__(self, cim_object_type):

        super(InMemoryObjectStore, self).__init__(cim_object_type)

        self._copy_names = False

        # Define the dictionary that implements the object store.
        # The keys in this dictionary are the names of the objects and
        # the values the corresponding CIM objects.
        if cim_object_type.__name__ in ("CIMClass", 'CIMQualifierDeclaration'):
            self._data = NocaseDict()
        elif cim_object_type.__name__ == 'CIMInstance':
            self._data = {}
            self._copy_names = True
        else:
            assert False, "InMemoryObjectStore: Invalid input parameter {}." \
                .format(cim_object_type)
Ejemplo n.º 10
0
def test_NocaseDict_unnamedkeys_getitem(testcase, allow, key):
    """
    Test function for NocaseDict.__getitem__() for unnamed keys
    """

    obj = NocaseDict()

    # This is not a very practical use, but necessary to test the function
    obj.allow_unnamed_keys = True
    obj[key] = 'foo'  # Uses __setitem__()
    obj.allow_unnamed_keys = allow

    # The code to be tested
    _ = obj[key]

    # Ensure that exceptions raised in the remainder of this function
    # are not mistaken as expected exceptions
    assert testcase.exp_exc_types is None
Ejemplo n.º 11
0
    def __init__(self, initial_namespace=None):
        """
        Parameters:

          initial_namespace:(:term:`string` or None):
            Optional initial namespace that will be added to
            the CIM repository.
        """

        # Defines the top level NocaseDict() which defines the
        # namespaces in the repository. The keys of this dictionary
        # are namespace names and the values are dictionaries
        # defining the CIM classes, CIM instances, and CIM qualifier
        # declarations where the keys are "classes", "instance", and
        # "qualifiers" and the value for each is an instance of the
        # class InMemoryObjectStore that containe the CIM objects.
        self._repository = NocaseDict()

        # If an initial namespace is defined, add it to the repository
        if initial_namespace:
            self.add_namespace(initial_namespace)
Ejemplo n.º 12
0
    def GetClass(self, *args, **kwargs):
        """Retrieve a CIM class from the local repository of this class.

        For a description of the parameters, see
        :meth:`pywbem.WBEMConnection.GetClass`.
        """
        cname = args[0] if args else kwargs['ClassName']

        try:
            cc = self.classes[self.default_namespace][cname]
        except KeyError:
            if self.conn is None:
                ce = CIMError(CIM_ERR_NOT_FOUND, cname)
                raise ce
            cc = self.conn.GetClass(*args, **kwargs)
            try:
                self.classes[self.default_namespace][cc.classname] = cc
            except KeyError:
                self.classes[self.default_namespace] = \
                    NocaseDict({cc.classname: cc})

        if 'LocalOnly' in kwargs and not kwargs['LocalOnly']:
            if cc.superclass:
                try:
                    del kwargs['ClassName']
                except KeyError:
                    pass
                if args:
                    args = args[1:]
                super_ = self.GetClass(cc.superclass, *args, **kwargs)
                for prop in super_.properties.values():
                    if prop.name not in cc.properties:
                        cc.properties[prop.name] = prop
                for meth in super_.methods.values():
                    if meth.name not in cc.methods:
                        cc.methods[meth.name] = meth
        return cc
Ejemplo n.º 13
0
    def GetClass(self, *args, **kwargs):
        """Retrieve a CIM class from the local classes store if it exists
        there or from the client interface defined by self.conn.

        For a description of the parameters, see
        :meth:`pywbem.WBEMConnection.GetClass`.
        """

        cname = args[0] if args else kwargs['ClassName']
        ns = kwargs.get('namespace', self.default_namespace)

        try:
            cc = self.classes[ns][cname]
        except KeyError:
            cc = self.conn.GetClass(*args, **kwargs)
            try:
                self.classes[ns][cc.classname] = cc
            except KeyError:
                self.classes[ns] = NocaseDict({cc.classname: cc})

        if 'LocalOnly' in kwargs and not kwargs['LocalOnly']:
            if cc.superclass:
                try:
                    del kwargs['ClassName']
                except KeyError:
                    pass
                if args:
                    args = args[1:]
                super_ = self.GetClass(cc.superclass, *args, **kwargs)
                for prop in super_.properties.values():
                    if prop.name not in cc.properties:
                        cc.properties[prop.name] = prop
                for meth in super_.methods.values():
                    if meth.name not in cc.methods:
                        cc.methods[meth.name] = meth
        return cc
Ejemplo n.º 14
0
        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)])]),
    CIMInstance('Foo', path=CIMInstanceName('Foo',
                                            keybindings=NocaseDict(P2="P2"))),
]


@pytest.mark.parametrize(
Ejemplo n.º 15
0
    def test_wbemserver_basic(self, tst_namespace):
        # pylint: disable=no-self-use
        """
        Test the basic functions that access server information. This test
        creates the CIM repository and adds classes and instances for
        the WBEMServer tests that involve namespaces, brand, profiles and
        a subset of the central_instance tests.  It includes no tests for
        errors. The primary goal of this test was to develop the mechanisms
        for easily getting classes and instances into the repo and to provide
        a basic test of functionality.
        """

        # Build the wbem server mock using the  WbemServerMock default test
        # data except that we define the interop namespace
        mock_wbemserver = WbemServerMock(interop_ns=tst_namespace)
        server = mock_wbemserver.wbem_server

        # Build instances for get_central instance
        # using central methodology, i.e. ElementConformsToProfile

        # Test basic brand, version, namespace methods
        assert server.namespace_classname == 'CIM_Namespace'

        assert server.url == 'http://FakedUrl:5988'

        assert server.brand == "Mock_Test"
        # assert server.version == "2.15.0"
        assert server.interop_ns == tst_namespace
        assert set(server.namespaces) == set([tst_namespace])

        # Test basic profiles methods
        org_vm = ValueMapping.for_property(server, server.interop_ns,
                                           'CIM_RegisteredProfile',
                                           'RegisteredOrganization')

        for inst in server.profiles:
            org = org_vm.tovalues(inst['RegisteredOrganization'])
            name = inst['RegisteredName']
            vers = inst['RegisteredVersion']

            tst_tup = (org, name, vers)
            pass_tst = False
            for tup in mock_wbemserver.registered_profiles:
                if tst_tup == tup:
                    pass_tst = True
                    break
            assert pass_tst

        sel_prof = server.get_selected_profiles(registered_org='DMTF',
                                                registered_name='Indications')
        assert len(sel_prof) == 1
        for inst in sel_prof:
            assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF'
            assert inst['RegisteredName'] == 'Indications'

        # Test case insensitive matching
        sel_prof = server.get_selected_profiles(registered_org='DmtF',
                                                registered_name='inDiCations')
        assert len(sel_prof) == 1
        for inst in sel_prof:
            assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF'
            assert inst['RegisteredName'] == 'Indications'

        sel_prof = server.get_selected_profiles(registered_org='DMTF')
        assert len(sel_prof) == 3
        for inst in sel_prof:
            assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF'

        # Simple get_cental_instance.
        # profile_path, central_class=None,
        #                       scoping_class=None, scoping_path=None
        profile_insts = server.get_selected_profiles(
            registered_org='SNIA',
            registered_name='Server',
            registered_version='1.1.0')
        profile_path = profile_insts[0].path
        insts = server.get_central_instances(profile_path, 'CIM_ObjectManager')
        assert len(insts) == 1
        kb = NocaseDict([
            ('SystemCreationClassName', 'CIM_ComputerSystem'),
            ('SystemName', mock_wbemserver.system_name),
            ('CreationClassName', 'CIM_ObjectManager'),
            ('Name', 'FakeObjectManager'),
        ])
        assert insts[0] == CIMInstanceName('CIM_ObjectManager',
                                           keybindings=kb,
                                           namespace=tst_namespace,
                                           host=server.conn.host)
Ejemplo n.º 16
0
    def register_provider(self, conn, provider, namespaces=None,
                          schema_pragma_files=None, verbose=None):
        # pylint: disable=line-too-long
        """
        Register the provider object for specific namespaces and CIM classes.
        Registering a provider tells the FakedWBEMConnection that the provider
        implementation provided with this call as the `provider` parameter is
        to be executed as the request response method for the namespaces
        defined in the `namespaces` parameter, the provider type defined in the
        provider 'provider_type` attribute of the `provider` and the classes
        defined in the provider `provider_classnames` attribute of the
        `provider`.

        The provider registration process includes:

        1. Validation that the namespaces defined for the provider exist.
        2. Validation that the superclass of the provider is consistent with
           the `provider_type` attribute defined in the provider.
        3. Installation of any CIM classes defined by the provider
           (`provider_classnames` attribute) including installation of
           dependencies for these classes using the `schema_pragma_files` to
           locate the search directories for dependencies.
        4. Adding the provider to the registry of user_providers so that any
           of the request methods defined for the `provider_type` are
           passed to this provider in place of the default request processors.
        5. Execute post_register_setup() call to the provider to allow the
           provider to perform any special setup functionality.

        Providers can only be registered for the following request response
        methods:

        1. provider_type = 'instance-write': defines methods for CreateInstance,
           ModifyInstance, and DeleteInstance requests within a subclass of
           the `InstanceWriteProvider` class.

        2. provider_type = 'method': defines a InvokeMethod method within
           a subclass of the `MethodProvider` class.

        Each classname in a particular namespace may have at most one provider
        registered

        Parameters:

          conn  (:class:`~pywbem_mock.FakedWBEMConnection`):
            Defines the attributes of the connection. Used to issue requests to
            create instances in the Interop namespace for all existing
            namespaces that do not have instances of CIM_Namespace defined

          provider (instance of subclass of :class:`pywbem_mock.InstanceWriteProvider` or :class:`pywbem_mock.MethodProvider`):
            The methods in this subclass override the corresponding methods in
            the superclass. The method call parameters must be the same
            as the default method in the superclass and it must return
            data in the same format if the default method returns data.
             This
            class must contain variables `provider_type` and
            `provider_classnames` that define the type of provider and the CIM
            classes that the provider serves.

          namespaces (:term:`string` or list of :term:`string`):
            Namespace or namespaces for which the provider is being registered.

            If `None`, the default namespace of the connection will be set to
            the built-in default namespace

          schema_pragma_files (:term:`py:iterable` of :term:`string` or :term:`string`):
            Path names of schema pragma files for the set of CIM
            classes that make up a schema such as the DMTF schema. These files
            must contain include pragams defining the file location of the
            classes to be compiled for the defined provider and for any
            dependencies required to compile those classes.  The directory
            containing each schema pragma file is passed to the MOF compiler as
            the search path for compile dependencies.

            see :class:`pywbem.MOFCompiler` for more information on the
            `search_paths` parameter.

          verbose (:class:`py:bool`):
            Display details on actions

        Raises:

            TypeError: Invalid provider_type retrieved from provider or
                       provider_type does not match superlclass. or the
                       namespace parameter is invalid.
            ValueError: provider_type retrieved from provider is not a
                        valid type string.
            ValueError: classnames parameter not a valid string or iterable or
                        namespace does not exist in repository.
        """  # noqa: E501
        # pylint: enable=line-too-long

        if schema_pragma_files:
            if isinstance(schema_pragma_files, six.string_types):
                schema_pragma_files = [schema_pragma_files]

        try:
            provider_type = provider.provider_type
            provider_classnames = provider.provider_classnames
        except AttributeError as ae:
            raise TypeError(
                _format("Attributes provider_type and provider_classnames "
                        "required in provider. exception {}",
                        ae))
        if provider_type == 'instance-write':
            if not isinstance(provider, InstanceWriteProvider):
                raise TypeError(
                    _format("Provider argument {0!A} is not a "
                            "valid subclass of InstanceWriteProvider. ",
                            provider))
        elif provider_type == 'method':
            if not isinstance(provider, MethodProvider):
                raise TypeError(
                    _format("Provider argument {0!A} is not a "
                            "valid subclass of MethodProvider. ",
                            provider))
        else:
            raise ValueError(
                _format("Provider_type argument {0!A} is not a valid provider "
                        "type. Valid provider types are {1!A}.",
                        provider_type, self.PROVIDER_TYPES))

        if provider_classnames is None:
            raise ValueError(
                _format('Classnames argument must be string '
                        'or list of strings. None not allowed.'))

        if namespaces is None:
            namespaces = [conn.default_namespace]

        if isinstance(namespaces, six.string_types):
            namespaces = [namespaces]

        if not isinstance(namespaces, (list, tuple)):
            raise TypeError(
                _format('Namespace argument {0|A} must be a string or '
                        'list/tuple but is {1}',
                        namespaces, type(namespaces)))

        for namespace in namespaces:
            if not isinstance(namespace, six.string_types):
                raise TypeError(
                    _format('Namespace "{0!A}" in namespaces argument not '
                            'a string. ', namespace))

            if namespace not in conn.namespaces:
                raise ValueError(
                    _format('Namespace "{0!A}" in namespaces argument not '
                            'in CIM repository. '
                            'Existing namespaces are: {1!A}. ',
                            namespace, conn.namespaces))

        if isinstance(provider_classnames, six.string_types):
            provider_classnames = [provider_classnames]
        assert isinstance(provider_classnames, (list, tuple))

        for classname in provider_classnames:
            assert isinstance(classname, six.string_types)

        # For each namespace in which the provider is to be registered,
        # if the class is not in that namespace, either compile it in if
        # pragmas exist or generate an exception if no pragmas exist

        for namespace in namespaces:
            for classname in provider_classnames:
                # pylint: disable=protected-access
                if not conn._mainprovider.class_exists(namespace, classname):
                    if schema_pragma_files:
                        conn.compile_schema_classes(
                            provider_classnames,
                            schema_pragma_files,
                            namespace=namespace,
                            verbose=verbose)
                    else:
                        raise ValueError(
                            _format('Class "{0!A}" does not exist in '
                                    'namespace {1!A} of the CIM repository '
                                    'and no schema pragma files were specified',
                                    classname, namespace))
            if namespace not in self._registry:
                self._registry[namespace] = NocaseDict()

            # Add classnames for the namespace
            for classname in provider_classnames:
                if classname not in self._registry[namespace]:
                    self._registry[namespace][classname] = {}
                self._registry[namespace][classname][provider_type] = provider

        if verbose:
            _format("Provider {0!A} registered: classes:[{1!A}],  "
                    "type: {1!A} namespaces:{2!A}",
                    provider.__class__.__name__,
                    ", ".join(provider_classnames),
                    provider_type, ", ".join(namespaces))

        # Call post_register_setup.  Since this method is defined in the
        # default provider methods (MethodProvider, etc.) any exception is
        # caught as an error.
        provider.post_register_setup(conn)
Ejemplo n.º 17
0
def runtestcase(testcase):
    """Run a single test case."""

    tc_name = tc_getattr("", testcase, "name")
    # tc_desc = tc_getattr(tc_name, testcase, "description", None)
    tc_ignore_test = tc_getattr(tc_name, testcase, "ignore_test", False)
    tc_ignore_python_version = tc_getattr(tc_name, testcase,
                                          "ignore_python_version", None)
    tc_ignore_debug_comparison = tc_getattr(tc_name, testcase,
                                            "ignore_debug_comparison", False)

    # Determine if this test case should be skipped
    if tc_ignore_test:
        pytest.skip("Test case has 'ignore_test' set")
        return
    if six.PY2 and tc_ignore_python_version == 2 or \
            six.PY3 and tc_ignore_python_version == 3:
        pytest.skip("Test case has 'ignore_python_version' set")
        return

    pywbem_request = tc_getattr(tc_name, testcase, "pywbem_request")
    exp_http_request = tc_getattr(tc_name, testcase, "http_request", None)
    http_response = tc_getattr(tc_name, testcase, "http_response", None)
    exp_pywbem_response = tc_getattr(tc_name, testcase, "pywbem_response")

    mock_adapter = requests_mock.Adapter()

    # Setup requests_mock for one WBEM operation
    if exp_http_request is not None:
        exp_http_exception = tc_getattr(tc_name, http_response, "exception",
                                        None)
        if exp_http_exception is None:
            body = tc_getattr(tc_name, http_response, "data")
            # body is a UTF-8 encoded byte string
            body = utf8_with_surrogate_issues(body)
            params = {
                "content": body,
                "headers": tc_getattr(tc_name, http_response, "headers", None),
                "status_code": tc_getattr(tc_name, http_response, "status")
            }
        else:
            callback_name = exp_http_exception
            try:
                callback_func = getattr(Callback(), callback_name)
            except AttributeError:
                raise ClientTestError("Unknown exception callback: %s" %
                                      callback_name)
            params = {"text": callback_func}

        method = tc_getattr(tc_name, exp_http_request, "verb")
        url = tc_getattr(tc_name, exp_http_request, "url")

        mock_adapter.register_uri(method=method, url=url, **params)

    conn = pywbem.WBEMConnection(
        url=tc_getattr(tc_name, pywbem_request, "url"),
        creds=tc_getattr(tc_name, pywbem_request, "creds"),
        default_namespace=tc_getattr(tc_name, pywbem_request, "namespace"),
        timeout=tc_getattr(tc_name, pywbem_request, "timeout"),
        stats_enabled=tc_getattr(tc_name, pywbem_request, "stats-enabled",
                                 False))

    conn.session.mount(conn.scheme + '://', mock_adapter)

    conn.debug = tc_getattr(tc_name, pywbem_request, "debug", False)

    op = tc_getattr(tc_name, pywbem_request, "operation")
    # Example:
    #  "operation": {
    #      "pywbem_method": "GetInstance",
    #      "InstanceName": {
    #          "pywbem_object": "CIMInstanceName",
    #          "classname": "PyWBEM_Person",
    #          "keybindings": {
    #              "Name": "Fritz"
    #          }
    #      },
    #      "LocalOnly": False
    #  }

    op_name = tc_getattr(tc_name, op, "pywbem_method")
    op_args = OrderedDict()
    for arg_name in op:
        if arg_name == "pywbem_method":
            continue
        op_args[arg_name] = obj(op[arg_name], tc_name)
    try:
        op_call = getattr(conn, op_name)

    except AttributeError:
        raise ClientTestError("Unknown operation name: %s" % op_name)

    # Invoke the PyWBEM operation to be tested
    raised_exception = None
    raised_traceback_str = None  # avoid pylint warning used-before-assignment
    try:
        result = op_call(**op_args)
    except Exception as exc:  # pylint: disable=broad-except
        raised_exception = exc
        stringio = six.StringIO()
        traceback.print_exc(file=stringio)
        raised_traceback_str = stringio.getvalue()
        stringio.close()
        result = None

    # Validate PyWBEM result and exceptions.
    # We validate exceptions before validating the HTTP request, because
    # an exception might have been raised on the way down before the
    # request was actually made.

    exp_exception = tc_getattr(tc_name, exp_pywbem_response, "exception", None)
    exp_cim_status = tc_getattr(tc_name, exp_pywbem_response, "cim_status", 0)
    exp_error_instances = tc_getattr_list(tc_name, exp_pywbem_response,
                                          "error_instances", None)

    # get the optional expected request and reply sizes if specified. The
    # default is None if not specified
    exp_request_len = tc_getattr_list(tc_name, exp_pywbem_response,
                                      "request_len", None)
    exp_reply_len = tc_getattr_list(tc_name, exp_pywbem_response, "reply_len",
                                    None)
    # get the expected result.  This may be either the the definition
    # of a value or cimobject or a list of values or cimobjects or
    # a named tuple of results.
    exp_result = tc_getattr_list(tc_name, exp_pywbem_response, "result", None)

    exp_pull_result = tc_getattr(tc_name, exp_pywbem_response, "pullresult",
                                 None)

    if exp_pull_result and exp_result:
        raise ClientTestError("Result and pull result attributes are are not "
                              "compatible.")

    if exp_exception is not None and exp_result is not None:
        raise ClientTestError("The 'result' and 'exception' attributes in "
                              "'pywbem_result' are not compatible.")
    if exp_cim_status != 0 and exp_result is not None:
        raise ClientTestError("The 'result' and 'cim_status' attributes in "
                              "'pywbem_result' are not compatible.")

    if exp_cim_status != 0:
        exp_exception = 'CIMError'

    if exp_exception is not None:
        if raised_exception is None:
            raise AssertionError("Testcase %s: A %s exception was "
                                 "expected to be raised by PyWBEM "
                                 "operation %s, but no exception was "
                                 "actually raised." %
                                 (tc_name, exp_exception, op_name))
        if raised_exception.__class__.__name__ != exp_exception:
            raise AssertionError(
                "Testcase %s: A %s exception was "
                "expected to be raised by PyWBEM "
                "operation %s, but a different "
                "exception was actually raised:\n"
                "%s\n" %
                (tc_name, exp_exception, op_name, raised_traceback_str))
        if isinstance(raised_exception,
                      (pywbem.CIMXMLParseError, pywbem.XMLParseError)):
            req = raised_exception.request_data  # pylint: disable=no-member
            if req != conn.last_raw_request:
                raise AssertionError(
                    "Testcase %s: The %s exception raised by "
                    "PyWBEM operation %s has unexpected "
                    "CIM-XML request data:\n"
                    "%s\n"
                    "Expected CIM-XML request data:\n"
                    "%s\n" % (tc_name, raised_exception.__class__.__name__,
                              op_name, req, conn.last_raw_request))
            resp = raised_exception.response_data  # pylint: disable=no-member
            if resp != conn.last_raw_reply:
                raise AssertionError(
                    "Testcase %s: The %s exception raised by "
                    "PyWBEM operation %s has unexpected "
                    "CIM-XML response data:\n"
                    "%s\n"
                    "Expected CIM-XML response data:\n"
                    "%s\n" % (tc_name, raised_exception.__class__.__name__,
                              op_name, resp, conn.last_raw_reply))
    else:
        if raised_exception is not None:
            raise AssertionError("Testcase %s: No exception was "
                                 "expected to be raised by PyWBEM "
                                 "operation %s, but an exception was "
                                 "actually raised:\n"
                                 "%s\n" %
                                 (tc_name, op_name, raised_traceback_str))

    # Validate HTTP request produced by PyWBEM

    if exp_http_request is not None:

        http_request = mock_adapter.last_request

        exp_verb = tc_getattr(tc_name, exp_http_request, "verb")
        assert http_request.method == exp_verb
        exp_headers = tc_getattr(tc_name, exp_http_request, "headers", {})
        for header_name in exp_headers:
            act_header = http_request.headers[header_name]
            exp_header = exp_headers[header_name]
            assert act_header == exp_header, \
                "Value of %s header in HTTP request is: %s " \
                "(expected: %s)" % (header_name, act_header, exp_header)
        exp_data = tc_getattr(tc_name, exp_http_request, "data", None)
        if exp_data:
            assertXMLEqual(http_request.body, exp_data, "HTTP request")
            if not tc_ignore_debug_comparison and conn.debug:
                if conn.last_raw_request:
                    assertXMLEqual(conn.last_raw_request, exp_data,
                                   "conn.last_raw_request")
                if conn.last_request:
                    assertXMLEqual(conn.last_request, exp_data,
                                   "conn.last_request")

    if http_response is not None:
        exp_response_data = tc_getattr(tc_name, http_response, "data", None)
        if exp_response_data:
            if not tc_ignore_debug_comparison and conn.debug:
                if conn.last_raw_reply:
                    assertXMLEqual(conn.last_raw_reply, exp_response_data,
                                   "conn.last_raw_reply")
                if conn.last_reply:
                    assertXMLEqual(conn.last_reply, exp_response_data,
                                   "conn.last_reply")

    if exp_request_len is not None:
        assert exp_request_len == conn.last_request_len

        if conn.stats_enabled:
            snapshot = conn.statistics.snapshot()
            assert len(snapshot) == 1  # one operation; one stat

            for name, stats in snapshot:  # pylint: disable=unused-variable
                stat = stats
            assert stat.count == 1
            assert stat.min_request_len == stat.max_request_len
            assert stat.min_request_len == exp_request_len

    if exp_reply_len is not None:
        assert exp_reply_len == conn.last_reply_len, \
            "Reply lengths do not match. exp %s rcvd %s" % \
            (exp_reply_len, conn.last_reply_len)

        if conn.stats_enabled:
            snapshot = conn.statistics.snapshot()
            assert len(snapshot) == 1  # one operation; one stat

            for name, stats in snapshot:
                stat = stats
            assert stat.count == 1, "Expected a single statistic"
            assert stat.min_reply_len == stat.max_reply_len
            assert stat.min_reply_len == exp_reply_len

    # Continue with validating the result

    if isinstance(raised_exception, pywbem.CIMError):
        # pylint: disable=no-member
        cim_status = raised_exception.status_code
        # pylint: disable=no-member
        error_instances = raised_exception.instances
    else:
        cim_status = 0
        error_instances = None

    assert cim_status == exp_cim_status, \
        "Error in WBEMConnection operation CIM status code. " \
        "Expected %s; received %s" % \
        (exp_cim_status, cim_status)

    exp_error_inst_objs = obj(exp_error_instances, tc_name)
    assert error_instances == exp_error_inst_objs, \
        "Error in WBEMConnection operation error instances.\n" \
        "Expected: %s\nReceived: %s" % \
        (exp_error_inst_objs, error_instances)

    # Returns either exp_result or exp_pull_result
    if exp_result is not None:
        exp_result_obj = obj(exp_result, tc_name)

        # The testcase can only specify lists but not tuples, so we
        # tolerate tuple/list mismatches:
        act_type = type(result)
        if act_type == tuple:
            act_type = list
        exp_type = type(exp_result_obj)
        # pylint: disable=unidiomatic-typecheck
        if act_type != exp_type:
            show_diff(None, type(exp_result_obj), type(result), 'type')
            raise AssertionError("PyWBEM CIM result type is not"
                                 " as expected.")

        # The testcase can only specify dicts but not NocaseDicts, so we
        # tolerate such mismatches (in case of InvokeMethod):
        if isinstance(exp_result_obj, list) and \
           len(exp_result_obj) == 2 and \
           isinstance(exp_result_obj[1], dict):
            _exp_result_obj = (exp_result_obj[0],
                               NocaseDict(exp_result_obj[1]))
        else:
            _exp_result_obj = exp_result_obj

        # If result items are tuple, convert to lists. This is for
        # class ref and assoc results.
        if isinstance(result, list) and \
                result and isinstance(result[0], tuple):
            _result = []
            for item in result:
                if isinstance(item, tuple):
                    _result.append(list(item))
                else:
                    _result.append(item)
        else:
            _result = result

        if _result != _exp_result_obj:
            # TODO 2016/07 AM: Improve the presentation of the difference
            show_diff(conn, repr(exp_result_obj), repr(_result), 'data')
            raise AssertionError("WBEMConnection operation method result "
                                 "is not as expected.")

    # if this is a pull result, compare the components of expected
    # and actual results. Pull results return a tuple
    elif exp_pull_result is not None:
        exp_pull_result_obj = result_tuple(exp_pull_result, tc_name)

        # Result length should be the same as expected result
        if len(result) != len(exp_pull_result_obj):
            show_diff(conn, len(conn, exp_pull_result_obj), len(result),
                      'tuple size')
            raise AssertionError("PyWBEM CIM result type is not"
                                 " as expected.")
        # eos is required result
        if result.eos != exp_pull_result_obj.eos:
            show_diff(conn, exp_pull_result_obj.eos, result.eos, 'result.eos')
            raise AssertionError("WBEMConnection operation method result "
                                 "is not as expected.")

        # Context is required result
        # NOTE: pyaml does not natively support tuples. It supports very
        #   simple tuples but only with single objects and in block mode.
        exp_context = tuple(exp_pull_result_obj.context) \
            if exp_pull_result_obj.context \
            else None
        if result.context != exp_context:
            show_diff(conn, repr(str_tuple(exp_context)),
                      repr(str_tuple(result.context)), 'result.context')
            raise AssertionError("WBEMConnection operation method result "
                                 "is not as expected.")

        if "instances" in exp_pull_result:
            _result = result.instances
            _exp_result = exp_pull_result_obj.instances
        elif "paths" in exp_pull_result:
            _result = result.paths
            _exp_result = exp_pull_result_obj.paths
        else:
            raise AssertionError("WBEMConnection operation method result "
                                 "is not as expected. No 'instances' "
                                 "or 'paths' component.")

        if _result != _exp_result:
            # TODO 2016/07 AM: Improve the presentation of the diff.
            show_diff(conn, repr(_exp_result), repr(_result), 'result data')
            raise AssertionError("WBEMConnection operation method "
                                 "result is not as expected.")

        # TODO redo as indexed loop to compare all items.

    else:
        assert result is None, \
            "PyWBEM CIM result is not None: %s" % repr(result)
Ejemplo n.º 18
0
 def __init__(self):
     # Dictionary of registered providers.
     # Hierarchy of dictionaries is [namespace][classname][type]
     # value is provider object.
     self._registry = NocaseDict()
Ejemplo n.º 19
0
    def CreateClass(self, *args, **kwargs):
        """
        Override the CreateClass method in MOFWBEMConnection. NOTE: This is
        currently only used by the compiler.  The methods of Fake_WBEMConnectin
        go directly to the repository, not through this method.
        This modifies the overridden method to add validation.

        For a description of the parameters, see
        :meth:`pywbem.WBEMConnection.CreateClass`.
        """
        cc = args[0] if args else kwargs['NewClass']
        namespace = self.default_namespace
        class_store = self.repo.get_class_store(namespace)

        if cc.superclass:
            # Since this may cause additional GetClass calls
            # IncludeQualifiers = True insures reference properties on
            # instances with aliases get built correctly.
            try:
                self.GetClass(cc.superclass,
                              LocalOnly=True,
                              IncludeQualifiers=True)
            except CIMError as ce:
                if ce.status_code == CIM_ERR_NOT_FOUND:
                    raise CIMError(
                        CIM_ERR_INVALID_SUPERCLASS,
                        _format(
                            "Cannot create class {0!A} in namespace "
                            "{1!A} because its superclass {2!A} does "
                            "not exist", cc.classname, self.getns(),
                            cc.superclass),
                        conn_id=self.conn_id)
                raise

        # Class created in local repo before tests because that allows
        # tests that may actually include this class to succeed in
        # the test code below.
        try:
            # The following generates an exception for each new ns
            self.classes[self.default_namespace][cc.classname] = cc
        except KeyError:
            self.classes[namespace] = NocaseDict({cc.classname: cc})

        # Validate that references and embedded instance properties, methods,
        # etc. have classes that exist in repo. This  also institates the
        # mechanism that gets insures that prerequisite classes are inserted
        # into the repo.
        objects = list(cc.properties.values())
        for meth in cc.methods.values():
            objects += list(meth.parameters.values())

        for obj in objects:
            # Validate that reference_class exists in repo
            if obj.type == 'reference':
                try:
                    self.GetClass(obj.reference_class,
                                  LocalOnly=True,
                                  IncludeQualifiers=True)
                except KeyError:
                    raise CIMError(CIM_ERR_INVALID_PARAMETER,
                                   obj.reference_class)
                # TODO: When we hook to server this returns to CIMError
                except CIMError as ce:
                    if ce.status_code == CIM_ERR_NOT_FOUND:
                        raise CIMError(
                            CIM_ERR_INVALID_PARAMETER,
                            _format(
                                "Class {0!A} referenced by element {1!A} "
                                "of class {2!A} in namespace {3!A} does "
                                "not exist", obj.reference_class, obj.name,
                                cc.classname, self.getns()),
                            conn_id=self.conn_id)
                    raise

            elif obj.type == 'string':
                if 'EmbeddedInstance' in obj.qualifiers:
                    eiqualifier = obj.qualifiers['EmbeddedInstance']
                    try:
                        self.GetClass(eiqualifier.value,
                                      LocalOnly=True,
                                      IncludeQualifiers=False)
                    except KeyError:
                        raise CIMError(CIM_ERR_INVALID_PARAMETER,
                                       eiqualifier.value)
                    except CIMError as ce:
                        if ce.status_code == CIM_ERR_NOT_FOUND:
                            raise CIMError(
                                CIM_ERR_INVALID_PARAMETER,
                                _format(
                                    "Class {0!A} specified by "
                                    "EmbeddInstance qualifier on element "
                                    "{1!A} of class {2!A} in namespace "
                                    "{3!A} does not exist", eiqualifier.value,
                                    obj.name, cc.classname, self.getns()),
                                conn_id=self.conn_id)
                        raise

        # TODO: Review this. Changed from conn to repo
        ccr = self.repo._resolve_class(  # pylint: disable=protected-access
            cc, namespace, self.repo.get_qualifier_store(namespace))

        # If the class exists, update it. Otherwise create it
        # TODO: Validate that this is correct behavior. That is what the
        # original MOFWBEMConnection does.
        if class_store.exists(ccr.classname):
            class_store.update(ccr.classname, ccr)
        else:
            class_store.create(ccr.classname, ccr)
        self.classes[namespace][ccr.classname] = ccr
Ejemplo n.º 20
0
    def InvokeMethod(self, methodname, localobject, params):
        # pylint: disable=invalid-name
        """
        Dispatcher for the InvokeMethod provider method.

        This method performs validations and if successful, routes the provider
        method call either to a registered provider, or to the default provider.

        Parameters:

          methodname (string): Method name

          localobject (CIMInstanceName or CIMClassName): Target object, with
            namespace set. Types are validated.

          params (NocaseDict): Input parameters, as follows:
            * key (string): Parameter name.
            * value (CIMParameter): Parameter value.
            Types are validated.

        Returns:

          A tuple of (returnvalue, outparams), with these tuple items:

            * returnvalue (CIM data type): Return value.

            * outparams (NocaseDict): Output parameters, with:
              * key (string): Parameter name
              * value (CIM data type): Parameter value
        """

        namespace = localobject.namespace

        # Verify the input parameter types (type errors have already been
        # raised during checks in WBEMConnection.InvokeMethod(), and in
        # FakedWBEMConnection._mock_methodcall()).
        assert isinstance(namespace, six.string_types)
        assert isinstance(methodname, six.string_types)
        assert isinstance(localobject, (CIMInstanceName, CIMClassName))
        assert isinstance(params, NocaseDict)

        # Verify that the namespace exists in the CIM repository.
        self.validate_namespace(namespace)

        class_store = self.cimrepository.get_class_store(namespace)
        instance_store = self.cimrepository.get_instance_store(namespace)

        if isinstance(localobject, CIMInstanceName):
            # instance-level use

            # Get the creation class of the target instance from the CIM
            # repository, verifying that it exists.
            try:
                klass = class_store.get(localobject.classname)
            except KeyError:
                raise CIMError(
                    CIM_ERR_INVALID_CLASS,
                    _format(
                        "Creation class {0!A} of target instance does "
                        "not exist in namespace {1!A} of the CIM "
                        "repository.", localobject.classname, namespace))

            # Verify that the target instance exists in the CIM repository.
            if not instance_store.object_exists(localobject):
                raise CIMError(
                    CIM_ERR_NOT_FOUND,
                    _format(
                        "Target instance does not exist in the CIM "
                        "repository: {0!A}", localobject))

        else:
            assert isinstance(localobject, CIMClassName)
            # class-level use

            # Get the target class from the CIM repository, verifying that it
            # exists.
            try:
                klass = class_store.get(localobject.classname)
            except KeyError:
                raise CIMError(
                    CIM_ERR_NOT_FOUND,
                    _format(
                        "Target class {0!A} does not exist in namespace "
                        "{1!A} of the CIM repository.", localobject.classname,
                        namespace))

        # Verify that the class exposes the CIM method.
        if methodname not in klass.methods:
            raise CIMError(
                CIM_ERR_METHOD_NOT_FOUND,
                _format(
                    "Method {0!A} is not exposed by class {1!A} in "
                    "namespace {2!A} of the CIM repository.", methodname,
                    klass.classname, namespace))
        method = klass.methods[methodname]

        if isinstance(localobject, CIMClassName):
            # class-level use

            # Verify that the method is static.
            # Note: A similar check for instance-level use is not appropriate
            # because static methods can be invoked on instances or on classes.
            static_qual = method.qualifiers.get('Static')
            static_value = static_qual.value if static_qual else False
            if not static_value:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "Non-static method {0!A} in class {1!A} in "
                        "namespace {2!A} cannot be invoked on a class "
                        "object.", methodname, klass.classname, namespace))

        # Verify that the input parameters are defined by the method and have
        # the correct type-related attributes.
        for pn in params:
            assert isinstance(pn, six.string_types)
            param_in = params[pn]
            assert isinstance(param_in, CIMParameter)

            if pn not in method.parameters:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "The specified input parameter {0!A} is not "
                        "defined in method {1!A} of class {2!A} in "
                        "namespace {3!A} of the CIM repository", pn,
                        methodname, klass.classname, namespace))

            param_cls = method.parameters[pn]

            in_qual = method.qualifiers.get('In')
            in_value = in_qual.value if in_qual else True
            if not in_value:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "The specified input parameter {0!A} is "
                        "defined as an output-only parameter according to "
                        "its method {1!A} of class {2!A} in namespace "
                        "{3!A} of the CIM repository", pn, methodname,
                        klass.classname, namespace))

            if param_in.type != param_cls.type:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "The specified input parameter {0!A} has "
                        "incorrect type={1!A}, but should have type={2!A} "
                        "according to its method {3!A} in class {4!A} in "
                        "namespace {5!A} of the CIM repository", pn,
                        param_in.type, param_cls.type, methodname,
                        klass.classname, namespace))

            if param_in.is_array != param_cls.is_array:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "The specified input parameter {0!A} has "
                        "incorrect is_array={1!A}, but should have "
                        "is_array={2!A} "
                        "according to its method {3!A} in class {4!A} in "
                        "namespace {5!A} of the CIM repository", pn,
                        param_in.is_array, param_cls.is_array, methodname,
                        klass.classname, namespace))

            if param_in.embedded_object != param_cls.embedded_object:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "The specified input parameter {0!A} has "
                        "incorrect embedded_object={1!A}, but should have "
                        "embedded_object={2!A} "
                        "according to its method {3!A} in class {4!A} in "
                        "namespace {5!A} of the CIM repository", pn,
                        param_in.embedded_object, param_cls.embedded_object,
                        methodname, klass.classname, namespace))

        # Determine the provider to be used.
        provider = self.provider_registry.get_registered_provider(
            namespace, 'method', klass.classname)
        if not provider:
            provider = self.default_method_provider

        # Call the provider method
        result = provider.InvokeMethod(methodname, localobject, params)

        # Verify provider method result
        if not isinstance(result, (list, tuple)):
            raise TypeError(
                _format(
                    "InvokeMethod provider method returned invalid type: "
                    "{0}. Must return list/tuple (return value, output "
                    "parameters)", type(result)))
        if len(result) != 2:
            raise ValueError(
                _format(
                    "InvokeMethod provider method returned invalid number "
                    "of items: {0}. Must be list/tuple (return value, "
                    "output parameters)", len(result)))
        return_value = result[0]
        output_params = result[1]

        # Map the more flexible way output parameters can be returned from
        # the provider method to what _mock_methodcall() expects
        output_params_dict = NocaseDict()
        if isinstance(output_params, Sequence):
            # sequence of CIMParameter
            for param in output_params:
                if not isinstance(param, CIMParameter):
                    raise TypeError(
                        _format(
                            "InvokeMethod provider method returned invalid "
                            "type for item in output parameters "
                            "sequence: {0}. Item type must be "
                            "CIMParameter", type(param)))
                output_params_dict[param.name] = param.value
        elif isinstance(output_params, Mapping):
            # mapping of name:value or name:CIMParameter
            for pname in output_params:
                pvalue = output_params[pname]
                if isinstance(pvalue, CIMParameter):
                    pvalue = pvalue.value
                else:
                    # Perform check for valid CIM data type:
                    try:
                        cimtype(pvalue)
                    except TypeError:
                        new_exc = TypeError(
                            _format(
                                "InvokeMethod provider method returned "
                                "invalid type for value in output "
                                "parameters mapping: {0}. Value type must "
                                "be a CIM data type or CIMParameter",
                                type(pvalue)))
                        new_exc.__cause__ = None
                        raise new_exc
                    except ValueError:
                        # Empty array
                        pass
                output_params_dict[pname] = pvalue
        else:
            raise TypeError(
                _format(
                    "InvokeMethod provider method returned invalid type "
                    "for output parameters: {0}. Must be "
                    "Sequence(CIMParameter) or "
                    "Mapping(name: value/CIMParameter)", type(output_params)))

        return return_value, output_params_dict
Ejemplo n.º 21
0
    def ModifyInstance(self,
                       ModifiedInstance,
                       IncludeQualifiers=None,
                       PropertyList=None):
        # pylint: disable=invalid-name
        """
        Dispatcher for the ModifyInstance provider method.

        This method performs validations and if successful, routes the provider
        method call either to a registered provider, or to the default provider.
        """

        # Verify the input parameter types (type errors have already been
        # raised during checks in the WBEMConnection operation).
        assert isinstance(ModifiedInstance, CIMInstance)
        assert isinstance(IncludeQualifiers, (bool, type(None)))
        assert isinstance(PropertyList,
                          (six.string_types, list, tuple, type(None)))
        assert isinstance(ModifiedInstance.path, CIMInstanceName)

        # Verify equality of the class names in the modified instance.
        if ModifiedInstance.classname.lower() != \
                ModifiedInstance.path.classname.lower():
            raise CIMError(
                CIM_ERR_INVALID_PARAMETER,
                _format(
                    "Modified instance has inconsistent class names: "
                    "{0!A} in the instance, and {1!A} in the instance "
                    "path.", ModifiedInstance.classname,
                    ModifiedInstance.path.classname))

        # Verify that the namespace exists in the CIM repository.
        namespace = ModifiedInstance.path.namespace
        self.validate_namespace(namespace)

        class_store = self.cimrepository.get_class_store(namespace)
        instance_store = self.cimrepository.get_instance_store(namespace)

        # Get creation class from CIM repository and verify that it exists.
        # The CIMClass objects in the class store of the repository have all
        # exposed properties (i.e. defined and inherited, having resolved all
        # overrides), qualifiers, and classorigjn information.
        try:
            creation_class = class_store.get(ModifiedInstance.classname)
        except KeyError:
            raise CIMError(
                CIM_ERR_INVALID_CLASS,
                _format(
                    "Creation class {0!A} of modified instance does not "
                    "exist in namespace {1!A} of the CIM repository.",
                    ModifiedInstance.classname, namespace))

        # Get instance to be modified from CIM repository.
        try:
            instance = instance_store.get(ModifiedInstance.path)
        except KeyError:
            raise CIMError(
                CIM_ERR_NOT_FOUND,
                _format(
                    "Instance to be modified does not exist in the CIM "
                    "repository: {0!A}", ModifiedInstance.path))

        # Verify that the properties in the property list are exposed by the
        # creation class, and reduce property list to be unique.
        if PropertyList is None:
            property_list = None
        else:
            property_list = []
            property_dict = NocaseDict()
            for pn in PropertyList:
                if pn not in creation_class.properties:
                    raise CIMError(
                        CIM_ERR_INVALID_PARAMETER,
                        _format(
                            "Property {0!A} in PropertyList does not "
                            "exist in creation class {1!A} in namespace "
                            "{2!A} of the CIM repository", pn,
                            ModifiedInstance.classname, namespace))
                if pn not in property_dict:
                    property_dict[pn] = True
                    property_list.append(pn)

        # Verify that the properties in the modified instance are exposed by the
        # creation class and have the correct type-related attributes.
        # Strictly, we would only need to verify the properties to be modified
        # as reduced by the PropertyList.

        for pn in ModifiedInstance.properties:

            self._validate_property(pn, ModifiedInstance, creation_class,
                                    namespace, class_store)

            prop_inst = ModifiedInstance.properties[pn]
            prop_cls = creation_class.properties[pn]
            # See issue #2449. This test never executed since if the key
            # properties changed, the original instance get would have already
            # failed.
            if prop_cls.qualifiers.get('key', False) and \
                    prop_inst.value != instance[pn]:
                raise CIMError(
                    CIM_ERR_INVALID_PARAMETER,
                    _format(
                        "Property {0!A} in the instance is a key "
                        "property and thus cannot be modified, "
                        "according to its creation class {1!A} in "
                        "namespace {2!A} of the CIM repository", pn,
                        ModifiedInstance.classname, namespace))

        # The providers are guaranteed to get a deep copy of the original
        # modified instance since they may update properties.
        modified_instance = deepcopy(ModifiedInstance)

        # Reduce modified_instance to have just the properties to be modified
        if property_list is not None:

            # Add class default values for properties not specified in
            # ModifiedInstance.
            for pn in property_list:
                if pn not in modified_instance:
                    # If the property in the class does not have a default
                    # value, it is None.
                    modified_instance[pn] = creation_class.properties[pn].value

            # Remove properties from modified_instance that are not in
            # PropertyList.
            for pn in list(modified_instance):
                if pn not in property_dict:
                    del modified_instance[pn]

        # Adjust the lexical case of the properties in the modified instance to
        # the lexical case they have in the creation class.
        for pn in modified_instance.properties:
            inst_prop = modified_instance.properties[pn]
            cl_prop = creation_class.properties[pn]
            if inst_prop.name != cl_prop.name:
                inst_prop.name = cl_prop.name  # changes modified_instance

        # Determine the provider to be used. Note that a registered provider
        # always has all provider methods for the provider type, either
        # implemented or inherited.
        provider = self.provider_registry.get_registered_provider(
            namespace, 'instance-write', modified_instance.classname)
        if not provider:
            provider = self.default_instance_write_provider

        # Call the provider method.
        result = provider.ModifyInstance(modified_instance,
                                         IncludeQualifiers=IncludeQualifiers)

        # Verify provider method result.
        assert result is None
Ejemplo n.º 22
0
    def test_wbemserver_basic(self, tst_namespace):
        """
        Test the basic functions that access server information. This test
        creates the mock repository and adds classes and instances for
        the WBEMServer tests that involve namespaces, brand, profiles and
        a subset of the central_instance tests.  It includes no tests for
        errors. The primary goal of this test was to develop the mechanisms
        for easily getting classes and instances into the repo and to provide
        a basic test of functionality.
        """
        system_name = 'Mock_Test_subscription_mgr'
        object_manager_name = 'MyFakeObjectManager'
        conn = self.build_class_repo(tst_namespace)
        server = WBEMServer(conn)

        # Build CIM_ObjectManager instance
        om_inst = self.build_obj_mgr_inst(conn, tst_namespace, system_name,
                                          object_manager_name)

        # build CIM_Namespace instances
        test_namespaces = [tst_namespace, 'root/cimv2']

        self.build_cimnamespace_insts(conn, tst_namespace, system_name,
                                      object_manager_name, test_namespaces)

        # Build RegisteredProfile instances
        profiles = [('DMTF', 'Indications', '1.1.0'),
                    ('DMTF', 'Profile Registration', '1.0.0'),
                    ('SNIA', 'Server', '1.2.0'), ('SNIA', 'Server', '1.1.0'),
                    ('SNIA', 'SMI-S', '1.2.0')]

        self.build_reg_profile_insts(conn, tst_namespace, profiles)

        # Build instances for get_central instance
        # Using central methodology, i.e. ElementConformsToProfile

        # Element conforms for SNIA server to object manager
        prof_inst = server.get_selected_profiles(registered_org='SNIA',
                                                 registered_name='Server',
                                                 registered_version='1.1.0')

        self.build_elementconformstoprofile_inst(conn, tst_namespace,
                                                 prof_inst[0].path,
                                                 om_inst.path)

        # Test basic brand, version, namespace methods
        assert server.namespace_classname == 'CIM_Namespace'

        assert server.url == 'http://FakedUrl'

        assert server.brand == "OpenPegasus"
        assert server.version == "2.15.0"
        assert server.interop_ns == tst_namespace
        assert set(server.namespaces) == set([tst_namespace, 'root/cimv2'])

        # Test basic profiles methods
        org_vm = ValueMapping.for_property(server, server.interop_ns,
                                           'CIM_RegisteredProfile',
                                           'RegisteredOrganization')

        for inst in server.profiles:
            org = org_vm.tovalues(inst['RegisteredOrganization'])
            name = inst['RegisteredName']
            vers = inst['RegisteredVersion']

            tst_tup = (org, name, vers)
            pass_tst = False
            for tup in profiles:
                if tst_tup == tup:
                    pass_tst = True
                    break
            assert pass_tst

        sel_prof = server.get_selected_profiles(registered_org='DMTF',
                                                registered_name='Indications')
        assert len(sel_prof) == 1
        for inst in sel_prof:
            assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF'
            assert inst['RegisteredName'] == 'Indications'

        sel_prof = server.get_selected_profiles(registered_org='DMTF')
        assert len(sel_prof) == 2
        for inst in sel_prof:
            assert org_vm.tovalues(inst['RegisteredOrganization']) == 'DMTF'

        # Simple get_cental_instance.
        # profile_path, central_class=None,
        #                       scoping_class=None, scoping_path=None
        profile_insts = server.get_selected_profiles(
            registered_org='SNIA',
            registered_name='Server',
            registered_version='1.1.0')
        profile_path = profile_insts[0].path
        insts = server.get_central_instances(profile_path, 'CIM_ObjectManager')
        print('central inst %s' % insts[0])
        assert len(insts) == 1
        kb = NocaseDict([('SystemCreationClassName', 'CIM_ComputerSystem'),
                         ('SystemName', system_name),
                         ('CreationClassName', 'CIM_ObjectManager')])
        assert insts[0] == CIMInstanceName('CIM_ObjectManager',
                                           keybindings=kb,
                                           namespace=tst_namespace,
                                           host=conn.host)
Ejemplo n.º 23
0
    def inst_from_class(klass,
                        namespace=None,
                        property_values=None,
                        include_null_properties=True,
                        include_path=True,
                        strict=False,
                        include_class_origin=False):
        """
        Build a new CIMInstance from the input CIMClass using the
        property_values dictionary to complete properties and the other
        parameters to filter properties, validate the properties, and
        optionally set the path component of the CIMInstance.  If any of the
        properties in the class have default values, those values are passed
        to the instance unless overridden by the property_values dictionary.
        No CIMProperty qualifiers are included in the created instance and the
        `class_origin` attribute is transfered from the class only if the
        `include_class_origin` parameter is True

        Parameters:
          klass (:class:`pywbem:CIMClass`)
            CIMClass from which the instance will be constructed.  This
            class must include qualifiers and should include properties
            from any superclasses to be sure it includes all properties
            that are to be built into the instance. Properties may be
            excluded from the instance by not including them in the `klass`
            parameter.

          namespace (:term:`string`):
            Namespace in the WBEMConnection used to retrieve the class or
            `None` if the default_namespace is to be used.

          property_values (dictionary):
            Dictionary containing name/value pairs where the names are the
            names of properties in the class and the properties are the
            property values to be set into the instance. If a property is in
            the property_values dictionary but not in the class an ValueError
            exception is raised.

          include_null_properties (:class:`py:bool`):
            Determines if properties with Null values are included in the
            instance.

            If `True` they are included in the instance returned.

            If `False` they are not included in the instance returned

         inclued_class_origin  (:class:`py:bool`):
            Determines if ClassOrigin information is included in the returned
            instance.

            If None or False, class origin information is not included.

            If True, class origin information is included.

          include_path (:class:`py:bool`:):
            If `True` the CIMInstanceName path is build and inserted into
            the new instance.  If `strict` all key properties must be in the
            instance.

          strict (:class:`py:bool`:):
            If `True` and `include_path` is set, all key properties must be in
            the instance so that

            If not `True` The path component is created even if not all
            key properties are in the created instance.

        Returns:
            Returns an instance with the defined properties and optionally
            the path set.  No qualifiers are included in the returned instance
            and the existence of ClassOrigin depends on the
            `include_class_origin` parameter. The value of each property is
            either the value from the `property_values` dictionary, the
            default_value from the class or Null(unless
            `include_null_properties` is False). All other attributes of each
            property are the same as the corresponding class property.

        Raises:
           ValueError if there are conflicts between the class and
           property_values dictionary or strict is set and the class is not
           complete.
        """
        class_name = klass.classname
        inst = CIMInstance(class_name)
        for p in property_values:
            if p not in klass.properties:
                raise ValueError('Property Name %s in property_values but '
                                 'not in class %s' % (p, class_name))
        for cp in klass.properties:
            ip = klass.properties[cp].copy()
            ip.qualifiers = NocaseDict()
            if not include_class_origin:
                ip.class_origin = None
            if ip.name in property_values:
                ip.value = property_values[ip.name]
            if include_null_properties:
                inst[ip.name] = ip
            else:
                if ip.value:
                    inst[ip.name] = ip

        if include_path:
            inst.path = CIMInstanceName.from_instance(klass,
                                                      inst,
                                                      namespace,
                                                      strict=strict)
        return inst
Ejemplo n.º 24
0
    def CreateClass(self, *args, **kwargs):
        """
        Override the CreateClass method in MOFWBEMConnection. NOTE: This is
        currently only used by the compiler.  The methods of Fake_WBEMConnectin
        go directly to the repository, not through this method.
        This modifies the overridden method to add validation.

        For a description of the parameters, see
        :meth:`pywbem.WBEMConnection.CreateClass`.
        """
        cc = args[0] if args else kwargs['NewClass']
        namespace = self.getns()

        try:
            self.compile_ordered_classnames.append(cc.classname)

            # The following generates an exception for each new ns
            self.classes[self.default_namespace][cc.classname] = cc
        except KeyError:
            self.classes[namespace] = \
                NocaseDict({cc.classname: cc})

        # Validate that references and embedded instance properties, methods,
        # etc. have classes that exist in repo. This  also institates the
        # mechanism that gets insures that prerequisite classes are inserted
        # into the repo.
        objects = list(cc.properties.values())
        for meth in cc.methods.values():
            objects += list(meth.parameters.values())

        for obj in objects:
            # Validate that reference_class exists in repo
            if obj.type == 'reference':
                try:
                    self.GetClass(obj.reference_class,
                                  LocalOnly=True,
                                  IncludeQualifiers=True)
                except CIMError as ce:
                    if ce.status_code == CIM_ERR_NOT_FOUND:
                        raise CIMError(
                            CIM_ERR_INVALID_PARAMETER,
                            _format(
                                "Class {0!A} referenced by element {1!A} "
                                "of class {2!A} in namespace {3!A} does "
                                "not exist", obj.reference_class, obj.name,
                                cc.classname, self.getns()),
                            conn_id=self.conn_id)
                    raise

            elif obj.type == 'string':
                if 'EmbeddedInstance' in obj.qualifiers:
                    eiqualifier = obj.qualifiers['EmbeddedInstance']
                    try:
                        self.GetClass(eiqualifier.value,
                                      LocalOnly=True,
                                      IncludeQualifiers=False)
                    except CIMError as ce:
                        if ce.status_code == CIM_ERR_NOT_FOUND:
                            raise CIMError(
                                CIM_ERR_INVALID_PARAMETER,
                                _format(
                                    "Class {0!A} specified by "
                                    "EmbeddInstance qualifier on element "
                                    "{1!A} of class {2!A} in namespace "
                                    "{3!A} does not exist", eiqualifier.value,
                                    obj.name, cc.classname, self.getns()),
                                conn_id=self.conn_id)
                        raise

        ccr = self.conn._resolve_class(  # pylint: disable=protected-access
            cc, namespace, self.qualifiers[namespace])
        if namespace not in self.classes:
            self.classes[namespace] = NocaseDict()
        self.classes[namespace][ccr.classname] = ccr

        try:
            self.class_names[namespace].append(ccr.classname)
        except KeyError:
            self.class_names[namespace] = [ccr.classname]
Ejemplo n.º 25
0
    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)])
             ]),
    CIMInstance('Foo',
Ejemplo n.º 26
0
    def CreateClass(self, *args, **kwargs):
        """
        Override the CreateClass method in BaseRepositoryConnection.
        Implements creation of the class through the connected client and
        also sets it in the local client (NocaseDict).

        This Create class implementation is special for the MOF compiler
        because it includes the logic to retrieve classes missing from the
        repository but required to define a correct repository.  That includes
        superclasses and other classes referenced by the class being defined.

        For a description of the parameters, see
        :meth:`pywbem.WBEMConnection.CreateClass`.
        """
        cc = args[0] if args else kwargs['NewClass']
        ns = kwargs.get('namespace', self.default_namespace)

        if cc.superclass:
            # Since this may cause additional GetClass calls
            # IncludeQualifiers = True insures reference properties on
            # instances with aliases get built correctly.
            try:
                self.GetClass(cc.superclass, namespace=ns, LocalOnly=True,
                              IncludeQualifiers=True)
            except CIMError as ce:
                if ce.status_code == CIM_ERR_NOT_FOUND:
                    raise CIMError(
                        CIM_ERR_INVALID_SUPERCLASS,
                        _format("Cannot create class {0!A} in namespace "
                                "{1!A} because its superclass {2!A} does "
                                "not exist",
                                cc.classname, self.getns(), cc.superclass),
                        conn_id=self.conn_id)
                raise

        # Class created in local repo before tests because that allows
        # tests that may actually include this class to succeed in
        # the test code below without previously putting the class into
        # the repository defined by conn.
        try:
            # The following generates an exception for each new ns
            self.classes[ns][cc.classname] = cc
        except KeyError:
            self.classes[ns] = NocaseDict([(cc.classname, cc)])

        # Validate that references and embedded instance properties, methods,
        # etc. have classes that exist in repo. This  also institates the
        # mechanism that gets insures that prerequisite classes are inserted
        # into the repo.
        objects = list(cc.properties.values())
        for meth in cc.methods.values():
            objects += list(meth.parameters.values())

        for obj in objects:
            # Validate that reference_class exists in repo
            if obj.type == 'reference':
                try:
                    self.GetClass(obj.reference_class, namespace=ns,
                                  LocalOnly=True, IncludeQualifiers=True)
                except KeyError:
                    raise CIMError(CIM_ERR_INVALID_PARAMETER,
                                   obj.reference_class)
                except CIMError as ce:
                    if ce.status_code == CIM_ERR_NOT_FOUND:
                        raise CIMError(
                            CIM_ERR_INVALID_PARAMETER,
                            _format("Class {0!A} referenced by element {1!A} "
                                    "of class {2!A} in namespace {3!A} does "
                                    "not exist",
                                    obj.reference_class, obj.name,
                                    cc.classname, self.getns()),
                            conn_id=self.conn_id)
                    # NOTE: Only delete when this is total failure
                    del self.classes[ns][cc.classname]
                    raise

            elif obj.type == 'string':
                if 'EmbeddedInstance' in obj.qualifiers:
                    eiqualifier = obj.qualifiers['EmbeddedInstance']
                    # The DMTF spec allows the value to be None
                    if eiqualifier.value is None:
                        continue
                    try:
                        self.GetClass(eiqualifier.value, namespace=ns,
                                      LocalOnly=True,
                                      IncludeQualifiers=False)
                    except KeyError:
                        raise CIMError(CIM_ERR_INVALID_PARAMETER,
                                       eiqualifier.value)
                    except CIMError as ce:
                        if ce.status_code == CIM_ERR_NOT_FOUND:
                            raise CIMError(
                                CIM_ERR_INVALID_PARAMETER,
                                _format("Class {0!A} specified by "
                                        "EmbeddInstance qualifier on element "
                                        "{1!A} of class {2!A} in namespace "
                                        "{3!A} does not exist",
                                        eiqualifier.value, obj.name,
                                        cc.classname, self.getns()),
                                conn_id=self.conn_id)
                        # Only delete when total failure
                        del self.classes[ns][cc.classname]
                        raise
        self.conn.CreateClass(cc, namespace=ns)