Ejemplo n.º 1
0
    def testMethodWithNoParams(self):
        createFakeAPI()

        from rpc import Bridge
        bridge = Bridge.DynamicBridge()

        msg = ('{"jsonrpc":"2.0","method":"Host.getCapabilities","params":{},"'
               'id":"505ebe58-4fd7-45c6-8195-61e3a6d1dce9"}')

        obj = json.loads(msg, 'utf-8')
        mangledMethod = obj.get("method").replace(".", "_")
        params = obj.get('params', [])
        method = getattr(bridge, mangledMethod)
        self.assertEquals(method(**params), 'My capabilites')
Ejemplo n.º 2
0
 def _prepareJSONRPCBinding(self):
     if config.getboolean('vars', 'jsonrpc_enable'):
         try:
             from rpc import Bridge
             from rpc.BindingJsonRpc import BindingJsonRpc
             from yajsonrpc.stompReactor import StompDetector
         except ImportError:
             self.log.warn('Unable to load the json rpc server module. '
                           'Please make sure it is installed.')
         else:
             bridge = Bridge.DynamicBridge()
             json_binding = BindingJsonRpc(bridge)
             self.bindings['jsonrpc'] = json_binding
             stomp_detector = StompDetector(json_binding)
             self._acceptor.add_detector(stomp_detector)
Ejemplo n.º 3
0
    def testMethodWithManyOptionalAttributes(self):
        createFakeAPI()

        from rpc import Bridge
        bridge = Bridge.DynamicBridge()

        msg = ('{"jsonrpc":"2.0","method":"Host.fenceNode","params":{"addr":"r'
               'ack05-pdu01-lab4.tlv.redhat.com","port":"","agent":"apc_snmp",'
               '"username":"******","password":"******","action":"off","op'
               'tions":"port=15"},"id":"c212299f-42b5-485d-b9ba-bc9880628743"'
               '}')
        obj = json.loads(msg, 'utf-8')

        mangledMethod = obj.get("method").replace(".", "_")
        params = obj.get('params', [])

        method = getattr(bridge, mangledMethod)
        self.assertEquals(method(**params), 'on')
Ejemplo n.º 4
0
    def _validate(self, api_mod):
        bridge = Bridge.DynamicBridge()

        for class_name, class_obj in self._get_api_classes(api_mod):
            apiObj = getattr(api_mod, class_name)
            ctorArgs = apiObj.ctorArgs
            ctor_defaults = []

            spec_class = class_name
            if spec_class == 'Global':
                spec_class = 'Host'

            for method_name, method_obj in inspect.getmembers(
                    class_obj, inspect.ismethod):
                cmd = '%s_%s' % (spec_class, method_name)
                if cmd in self.IGNORED_CMDS:
                    continue

                # gather default args from ctor
                if method_name == '__init__':
                    ctor_defaults = self._get_default_args(method_obj)
                    continue

                # ignore private methods
                if method_name.startswith('_'):
                    continue

                try:
                    # get args from schema
                    method_args = bridge._getArgList(spec_class, method_name)
                except KeyError:
                    raise AssertionError('Missing method %s.%s' % (
                                         spec_class, method_name))
                    continue

                # inspect apiobj and gather args and default args
                args = ctorArgs + self._get_args(method_obj)
                default_args = ctor_defaults + self._get_default_args(
                    method_obj)

                # check len equality
                if len(args) != len(method_args):
                    raise AssertionError(self._prep_msg(class_name,
                                         method_name, method_args, args))
                for marg in method_args:
                    # verify optional arg
                    if marg.startswith('*'):
                        if not marg[1:] in default_args:
                            raise AssertionError(
                                self._prep_msg(class_name, method_name,
                                               method_args, args))
                        continue
                    # verify args from schema in apiobj args
                    if not bridge._symNameFilter(marg) in args:
                        raise AssertionError(self._prep_msg(
                            class_name, method_name, method_args, args))
                try:
                    # verify ret value with entry in command_info
                    ret = bridge._getRetList(spec_class, method_name)
                    ret_info = Bridge.command_info.get(cmd, {}).get('ret')
                    if not ret_info and not ret:
                        continue
                    if ret_info == 'status':
                        continue
                    if not ret_info or not ret:
                        raise AssertionError('wrong return type for ' + cmd)
                except KeyError:
                    raise AssertionError('Missing ret %s.%s' % (
                                         spec_class, method_name))