Esempio n. 1
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

    httpretty.httpretty.allow_net_connect = False

    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")

    # Setup HTTPretty 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 = utf8_with_surrogate_issues(body)
            params = {
                "body":
                body,
                "adding_headers":
                tc_getattr(tc_name, http_response, "headers", None),
                "status":
                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 = {"body": callback_func}

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

        httpretty.register_uri(method=method, uri=uri, **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.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
    try:
        result = op_call(**op_args)
        raised_exception = None

    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))
        elif 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, 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, 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 = httpretty.last_request()
        assert not isinstance(http_request, HTTPrettyRequestEmpty), \
            "HTTP request is empty"
        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)
Esempio n. 2
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'), ]


@pytest.mark.parametrize(
    # Testcases for Inmemory repository object management tests

    # Each list item is a testcase tuple with these items:
    # * desc: Short testcase description.
    # * init_args - list of Classes, instances, etc. to populate repo
    # * init_objs - list of CIM objects that will be used to initialize the
    #   repository when it is created.
    # * condition: Boolean condition for testcase to run, or 'pdb' for debugger
    def test_wbemserver_basic(self, tst_namespace):
        # pylint: disable=no-self-use
        """
        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.
        """

        # 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'

        assert server.brand == "OpenPegasus"
        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', 'MyFakeObjectManager'),
        ])
        assert insts[0] == CIMInstanceName('CIM_ObjectManager',
                                           keybindings=kb,
                                           namespace=tst_namespace,
                                           host=server.conn.host)
Esempio n. 4
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
Esempio n. 5
0
    def CreateClass(self, *args, **kwargs):
        """
        Override the CreateClass method in MOFWBEMConnection

        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]
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
 def __init__(self):
     # Dictionary of registered providers.
     # Hierarchy of dictionaries is [namespace][classname][type]
     # value is provider object.
     self._registry = NocaseDict()
Esempio n. 9
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)
Esempio n. 10
0
class ProviderRegistry(object):
    """
    This class defines the provider registry with methods to register
    a provider and to get the registered provider for a particular classname
    namespace, and provider_type.
    """
    #: Allowed provider types
    PROVIDER_TYPES = ["instance-write", "method"]

    def __init__(self):
        # Dictionary of registered providers.
        # Hierarchy of dictionaries is [namespace][classname][type]
        # value is provider object.
        self._registry = NocaseDict()

    def __repr__(self):
        return _format(
            "ProviderRegistry("
            "registry={s._registry}, ",
            s=self)

    def display_registered_providers(self, dest=None):
        """
        Generate display of registry in readable form and return the string
        Output format is:
        <namespace>:
          <classname>: CIM_Namespace provider: NamespaceProvider type: instance

        For example:

            Registered Providers:
            namespace: root/cimv2
              CIM_Foo      instance  UserInstanceTestProvider
              CIM_Foo      method    UserMethodTestProvider
            namespace: root/cimv3
              CIM_Foo      instance  UserInstanceTestProvider
              CIM_Foo      method    UserMethodTestProvider

        Parameters:

          dest (:term:`string`):
            File path of an output file. If `None`, the output is written to
            stdout.
        """
        def print_ljust(rows):
            """
            Print left justified and column aligned row of rows where each item
            is either a string or list of strings
            """

            widths = [max(map(len, col)) for col in zip(*rows)]
            for row in rows:
                _uprint(dest, u"  {0}".format(
                    u"  ".join((val.ljust(width) for
                                (val, width) in zip(row, widths)))))

        _uprint(dest, "Registered Providers:")
        for ns in self.provider_namespaces():
            rows = []
            _uprint(dest, _format(u'namespace: {0}', ns))
            for class_name in sorted(self.provider_classes(ns)):
                for type_ in sorted(self.provider_types(ns, class_name)):
                    provider = self.provider_obj(ns, class_name, type_)
                    provider_cn = provider.__class__.__name__
                    rows.append([class_name, type_, provider_cn])
            print_ljust(rows)

    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)

    def get_registered_provider(self, namespace, provider_type, classname):
        """
        Get the  user-defined provider registered for this namespace,
        provider_type, and classname.

        If no provider is registered, return `None`.

        Parameters:

          namespace (:term:`string`):
            The namespace in which the request will be executed.

          provider_type (:term:`string`):
            String containing keyword ('instance-write' or 'method') defining
            the type of provider.

          classname (:term:`string`):
            Name of the class defined for the operation

        Returns:

          Instance of :class:`~pywbem_mock.BaseProvider`: The registered
          provider.

          None if the registry is empty, the classname is not in the registry
          or the namespace is not in the registry or the entry for the
          classname, namespace and provider type is not defined.
        """

        if not self._registry or namespace not in self._registry:
            return None

        if classname not in self._registry[namespace]:
            return None

        # Return None if requested type is not registered type
        if provider_type not in self.provider_types(namespace, classname):
            return None

        return self.provider_obj(namespace, classname, provider_type)

    def provider_namespaces(self):
        """
        Get list of namespaces for registered providers. The returned
        list is case sensitive.

        Returns:
            list of :term:`string`: namespaces
            for which providers are registered.
        """
        return list(self._registry.keys())

    def provider_classes(self, namespace):
        """
        Get case insensitive iterable  of the classes for providers registered
        for a namespace. The returned list is case sensitive.

        Parameters:

          namespace (:term:`string`):
            The namespace in which the request will be executed.

        Returns:
            list of :term:`string`:
            Names of classes registered for namespace

        Raises:
          KeyError: if namespace invalid
        """

        return list(self._registry[namespace].keys())

    def provider_types(self, namespace, classname):
        """
        Get provider types for a namespace and classname.  This is
        a case-sensitive list.

        Parameters:

          namespace (:term:`string`):
            The namespace in which the request will be executed.

          classname (:term:`string`):
            Name of the class defined for the operation.

        Returns:
            list of :term:`string`: Strings defining provider
            types for the defined namespace and classname

        Raises:
          KeyError: if namespace invalid
        """
        return list(self._registry[namespace][classname].keys())

    def provider_obj(self, namespace, classname, provider_type):
        """
        Get the registered provider object (instance of the registered
        provider) for namespace, provider classname, and provider_type.

        Parameters:

          namespace (:term:`string`):
            The namespace in which the request will be executed.

          classname (:term:`string`):
            Name of the class defined for the operation.

          provider_type (:term:`string`):
            String containing keyword ('instance-write' or 'method') defining
            the type of provider.

        Returns:
            The registered object

        Raises:
          KeyError: if namespace or classname invalid
        """
        return self._registry[namespace][classname][provider_type]

    def iteritems(self):
        """
        Return an iterator through the registered provider items. Each item
        is a tuple(namespace, classname, provider_type, provider_obj),
        with:

          namespace (:term:`string`):
            The namespace in which the request will be executed.

          classname (:term:`string`):
            Name of the class defined for the operation.

          provider_type (:term:`string`):
            String containing keyword ('instance-write' or 'method') defining
            the type of provider.

          provider_obj (:class:`~pywbem_mock.BaseProvider`):
            The registered provider.
        """
        ns_dict = self._registry
        for ns in ns_dict:
            cln_dict = ns_dict[ns]
            for cln in cln_dict:
                pt_dict = cln_dict[cln]
                for pt in pt_dict:
                    pobj = pt_dict[pt]
                    yield (ns, cln, pt, pobj)

    def load(self, other):
        """
        Replace the data in this object with the data from the other object.

        This is used to restore the object from a serialized state, without
        changing its identity.
        """
        # pylint: disable=protected-access
        self._registry = other._registry
Esempio n. 11
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)
Esempio n. 12
0
class InMemoryRepository(BaseRepository):
    """
    A CIM repository that maintains its data in memory.
    """
    # Documentation for the methods and properties inherited from
    # ~pywbem_mock:`BaseObjectStore` is also inherited in the pywbem
    # documentation. Therefore the methods in this class have no documentation
    # string.

    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)

    def print_repository(self, dest=None, ):
        """
        Print the CIM repository to a destination. This displays information on
        the items in the data base and is only a diagnostic tool.

        Parameters:
          dest (:term:`string`):
            File path of an output file. If `None`, the output is written to
            stdout.
        """
        def objstore_info(objstore_name):
            """
            Display the data for the object store
            """
            for ns in self._repository:
                if objstore_name == 'class':
                    store = self.get_class_store(ns)
                elif objstore_name == 'qualifier':
                    store = self.get_qualifier_store(ns)
                else:
                    assert objstore_name == 'instance'
                    store = self.get_instance_store(ns)

                rtn_str = u'Namespace: {} Repo: {} len:{}\n'.format(
                    ns, objstore_name, store.len())
                for val in store.iter_values():
                    rtn_str += (u'{}\n'.format(val))
                return rtn_str

        namespaces = ",".join(self._repository.keys())
        _uprint(dest, _format(u'NAMESPACES: {0}', namespaces))
        _uprint(dest, _format(u'QUALIFIERS: {0}', objstore_info('qualifier')))
        _uprint(dest, _format(u'CLASSES: {0}', objstore_info('class')))
        _uprint(dest, _format(u'INSTANCES: {0}', objstore_info('instance')))

    def validate_namespace(self, namespace):
        if namespace is None:
            raise ValueError("Namespace argument must not be None")

        namespace = namespace.strip('/')
        try:
            self._repository[namespace]
        except KeyError:
            raise KeyError('Namespace "{}" does not exist in repository'.
                           format(namespace))

    def add_namespace(self, namespace):
        if namespace is None:
            raise ValueError("Namespace argument must not be None")

        namespace = namespace.strip('/')

        if namespace in self._repository:
            raise ValueError('Namespace "{}" already in repository'.
                             format(namespace))

        self._repository[namespace] = {}

        # Create the data store for each of the object types.
        self._repository[namespace]['classes'] = InMemoryObjectStore(CIMClass)

        self._repository[namespace]['instances'] = InMemoryObjectStore(
            CIMInstance)

        self._repository[namespace]['qualifiers'] = InMemoryObjectStore(
            CIMQualifierDeclaration)

    def remove_namespace(self, namespace):
        self.validate_namespace(namespace)
        namespace = namespace.strip('/')

        if self.get_class_store(namespace).len() != 0 or \
                self.get_qualifier_store(namespace).len() != 0 or \
                self.get_instance_store(namespace).len() != 0:
            raise ValueError('Namespace {} removal invalid. Namespace not '
                             'empty'.format(namespace))

        del self._repository[namespace]

    @property
    def namespaces(self):
        return list(self._repository)

    def get_class_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['classes']

    def get_instance_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['instances']

    def get_qualifier_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['qualifiers']
Esempio n. 13
0
class InMemoryRepository(BaseRepository):
    """
    A CIM repository that maintains the data in memory using the
    methods defined in its superclass (~pywbem_mock:`BaseObjectStore`).

    *New in pywbem 1.0 as experimental and finalized in 1.2.*

    This implementation creates the repository as multi-level dictionary
    elements to define the namespaces and within each
    namespace the CIM classes, CIM instances, and CIM qualifiers in the
    repository.
    """

    # Documentation for the methods and properties  isinherited from
    # ~pywbem_mock:`BaseObjectStore` by sphinx when building documentaton.
    # Therefore the methods in this class have no documentation
    # string unless they add or modify documentation in the parent class or
    # are not defined in the parent class. Any method that needs to modifyu
    # the base method documentation must copy the base class documentation.

    def __init__(self, initial_namespace=None):
        """
        Initialize an empty in-memory CIM repository and optionally add a
        namespace in the repository..

        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", "instances", 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)

    def __repr__(self):
        """Display summary of the repository"""
        return _format("InMemoryRepository(data={s._repository})", s=self)

    def print_repository(
        self,
        dest=None,
    ):
        """
        Print the CIM repository to a destination. This displays information on
        the items in the data base and is only a diagnostic tool.

        Parameters:
          dest (:term:`string`):
            File path of an output file. If `None`, the output is written to
            stdout.
        """
        def objstore_info(objstore_name):
            """
            Display the data for the object store
            """
            for ns in self._repository:
                if objstore_name == 'class':
                    store = self.get_class_store(ns)
                elif objstore_name == 'qualifier':
                    store = self.get_qualifier_store(ns)
                else:
                    assert objstore_name == 'instance'
                    store = self.get_instance_store(ns)

                rtn_str = u'Namespace: {} Repo: {} len:{}\n'.format(
                    ns, objstore_name, store.len())
                for val in store.iter_values():
                    rtn_str += (u'{}\n'.format(val))
                return rtn_str

        namespaces = ",".join(self._repository.keys())
        _uprint(dest, _format(u'NAMESPACES: {0}', namespaces))
        _uprint(dest, _format(u'QUALIFIERS: {0}', objstore_info('qualifier')))
        _uprint(dest, _format(u'CLASSES: {0}', objstore_info('class')))
        _uprint(dest, _format(u'INSTANCES: {0}', objstore_info('instance')))

    def validate_namespace(self, namespace):
        if namespace is None:
            raise ValueError("Namespace argument must not be None")

        namespace = namespace.strip('/')
        try:
            self._repository[namespace]
        except KeyError:
            raise KeyError(
                'Namespace "{}" does not exist in repository'.format(
                    namespace))

    def add_namespace(self, namespace):
        if namespace is None:
            raise ValueError("Namespace argument must not be None")

        namespace = namespace.strip('/')

        if namespace in self._repository:
            raise ValueError(
                'Namespace "{}" already in repository'.format(namespace))

        self._repository[namespace] = {}

        # Create the data store for each of the object types.
        self._repository[namespace]['classes'] = InMemoryObjectStore(CIMClass)

        self._repository[namespace]['instances'] = InMemoryObjectStore(
            CIMInstance)

        self._repository[namespace]['qualifiers'] = InMemoryObjectStore(
            CIMQualifierDeclaration)

    def remove_namespace(self, namespace):
        self.validate_namespace(namespace)
        namespace = namespace.strip('/')

        if self.get_class_store(namespace).len() != 0 or \
                self.get_qualifier_store(namespace).len() != 0 or \
                self.get_instance_store(namespace).len() != 0:
            raise ValueError('Namespace {} removal invalid. Namespace not '
                             'empty'.format(namespace))

        del self._repository[namespace]

    @property
    def namespaces(self):
        # pylint: disable=invalid-overridden-method
        # This puts just the dict keys (i.e. namespaces) into the list
        return NocaseList(self._repository)

    def get_class_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['classes']

    def get_instance_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['instances']

    def get_qualifier_store(self, namespace):
        if namespace is None:
            raise ValueError("Namespace None not permitted.")
        namespace = namespace.strip('/')
        self.validate_namespace(namespace)
        return self._repository[namespace]['qualifiers']

    def load(self, other):
        """
        Replace the data in this object with the data from the other object.

        This is used to restore the object from a serialized state, without
        changing its identity.
        """
        # pylint: disable=protected-access
        self._repository = other._repository
Esempio n. 14
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',