Пример #1
0
    def test_make_default_status_action_class(self):
        action_class = make_default_status_action_class(ActionResponse)
        self.assertIsNotNone(action_class)
        self.assertTrue(issubclass(action_class, BaseStatusAction))

        action = action_class({})
        self.assertEqual(six.text_type(pysoa.__version__), action._version)
        self.assertIsNone(action._build)

        action_class = make_default_status_action_class(Boolean)
        self.assertIsNotNone(action_class)
        self.assertTrue(issubclass(action_class, BaseStatusAction))

        action = action_class({})
        self.assertEqual(six.text_type(conformity.__version__), action._version)
        self.assertIsNone(action._build)
Пример #2
0
    def test_make_default_status_action_class(self):
        class TestServer1(Server):
            pass

        class TestServer2(Server):
            pass

        module1 = mock.MagicMock()
        module1.__name__ = 'neat_service'
        module1.__version__ = '1.6.3'
        module2 = mock.MagicMock()
        module2.__name__ = 'cooler_service'
        module2.__version__ = '3.15.5'
        module2.__build__ = 'cooler_service-3.15.5-af7ed3c'

        TestServer1.__module__ = 'neat_service.server'
        TestServer2.__module__ = 'cooler_service.further.lower.server'

        with mock.patch.dict(sys.modules, {
                'neat_service': module1,
                'cooler_service': module2
        }):
            action_class = make_default_status_action_class(TestServer1)
            assert action_class is not None
            assert issubclass(action_class, BaseStatusAction)

            # noinspection PyArgumentList
            action = action_class()
            assert action is not None
            assert isinstance(action, BaseStatusAction)
            assert action._version == '1.6.3'
            assert action._build is None

            action_class = make_default_status_action_class(TestServer2)
            assert action_class is not None
            assert issubclass(action_class, BaseStatusAction)

            # noinspection PyArgumentList
            action = action_class()
            assert action is not None
            assert isinstance(action, BaseStatusAction)
            assert action._version == '3.15.5'
            assert action._build == 'cooler_service-3.15.5-af7ed3c'
Пример #3
0
    def execute_job(self, job_request):
        """
        Processes and runs the action requests contained in the job and returns a `JobResponse`.

        :param job_request: The job request
        :type job_request: dict

        :return: A `JobResponse` object
        :rtype: JobResponse
        """
        # Run the Job's Actions
        job_response = JobResponse()
        job_switches = RequestSwitchSet(job_request['context']['switches'])
        for i, raw_action_request in enumerate(job_request['actions']):
            action_request = EnrichedActionRequest(
                action=raw_action_request['action'],
                body=raw_action_request.get('body', None),
                switches=job_switches,
                context=job_request['context'],
                control=job_request['control'],
                client=job_request['client'],
                async_event_loop=job_request['async_event_loop'],
            )
            action_in_class_map = action_request.action in self.action_class_map
            if action_in_class_map or action_request.action in ('status', 'introspect'):
                # Get action to run
                if action_in_class_map:
                    action = self.action_class_map[action_request.action](self.settings)
                elif action_request.action == 'introspect':
                    from pysoa.server.action.introspection import IntrospectionAction
                    action = IntrospectionAction(server=self)
                else:
                    if not self._default_status_action_class:
                        from pysoa.server.action.status import make_default_status_action_class
                        self._default_status_action_class = make_default_status_action_class(self.__class__)
                    action = self._default_status_action_class(self.settings)
                # Wrap it in middleware
                wrapper = self.make_middleware_stack(
                    [m.action for m in self.middleware],
                    action,
                )
                # Execute the middleware stack
                try:
                    action_response = wrapper(action_request)
                except ActionError as e:
                    # Error: an error was thrown while running the Action (or Action middleware)
                    action_response = ActionResponse(
                        action=action_request.action,
                        errors=e.errors,
                    )
            else:
                # Error: Action not found.
                action_response = ActionResponse(
                    action=action_request.action,
                    errors=[Error(
                        code=ERROR_CODE_UNKNOWN,
                        message='The action "{}" was not found on this server.'.format(action_request.action),
                        field='action',
                    )],
                )

            job_response.actions.append(action_response)
            if (
                action_response.errors and
                not job_request['control'].get('continue_on_error', False)
            ):
                # Quit running Actions if an error occurred and continue_on_error is False
                break

        return job_response