Esempio n. 1
0
    def get_next_event(self, session, timeout=None):
        """
        """
        event_queue_key = 'eventqueue:%s' % session.redis_key

        logger.debug('Waiting for event to be popped out of event queue: %s' %
                     event_queue_key)

        blob = self.redis.brpop([event_queue_key], timeout=timeout)

        logger.debug('Popping one event from the session event queue %s' %
                     event_queue_key)

        if not blob:
            return

        key, pack = blob
        event = GAPushEvent.from_dict(data=msgpack.unpackb(pack))
        resources = [
            GAResource(name=event.entity.rest_name, value=event.entity.id)
        ]
        request = GARequest(action=GARequest.ACTION_READ, resources=resources)
        context = GAContext(request=request, session=session)
        context.object = event.entity

        # operation_manager = GAOperationsController(context=context,
        #                                            logic_controller=self.core_controller.logic_controller,
        #                                            storage_controller=self.core_controller.storage_controller)
        # operation_manager.run()

        # if not context.has_errors:
        #     logger.debug('Returning one event to session %s' % session.redis_key)
        return event
    def test_populate_context_for_assign_with_resource(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        context.request.content = ['1', '2', '3']

        context.clear_errors()
        with patch.object(FakeStorageController, 'get', return_value=GAStoragePluginQueryResponse.init_with_errors([GAError(type=GAError.TYPE_NOTFOUND, title='', description='', property_name='')])):
            operations_controller._populate_context_for_assign_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(len(context.errors), 3)
            self.assertEquals(len(context.objects), 0)
            self.assertEquals(context.errors[0].type, GAError.TYPE_NOTFOUND)
            self.assertEquals(context.errors[1].type, GAError.TYPE_NOTFOUND)
            self.assertEquals(context.errors[2].type, GAError.TYPE_NOTFOUND)

        context.clear_errors()
        with patch.object(FakeStorageController, 'get', return_value=GAStoragePluginQueryResponse.init_with_data(data=tstdk.GAEnterprise(name='test'))):
            operations_controller._populate_context_for_assign_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(len(context.errors), 0)
            self.assertEquals(len(context.objects), 3)
            self.assertEquals(context.objects[0].name, 'test')
            self.assertEquals(context.objects[1].name, 'test')
            self.assertEquals(context.objects[2].name, 'test')
Esempio n. 3
0
    def test_perform_delegate(self):
        """
        """
        plugin1 = LogicPlugin1()
        plugin2 = LogicPlugin2()

        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[plugin1, plugin2],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)

        self.assertEquals(context.object, 'modified by LogicPlugin1')

        logic_controller.perform_delegate(delegate='nope',
                                          context=context)  # should not crash
Esempio n. 4
0
    def test_copy(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        context.add_error(GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1'))
        context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2'))
        context.add_error(GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3'))
        context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise()))
        context.add_event(GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise()))
        context.add_event(GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise()))

        context.object = tstdk.GAEnterprise(name='enterprise1')
        context.objects = [tstdk.GAEnterprise(name='enterprise2'), tstdk.GAEnterprise(name='enterprise3')]

        context_copy = context.copy()

        self.assertEquals(context_copy.session.uuid, session.uuid)
        self.assertEquals(context_copy.request.action, GARequest.ACTION_READ)
        self.assertEquals([obj.name for obj in context_copy.objects], [obj.name for obj in context.objects])
        self.assertEquals(context_copy.object.name, context.object.name)

        self.assertEquals(context_copy.has_errors, True)
        self.assertEquals(len(context_copy.errors), 3)

        self.assertEquals(context_copy.has_events, True)
        self.assertEquals(len(context_copy.events), 3)
Esempio n. 5
0
    def get_next_event(self, session, timeout=None):
        """
        """
        event_queue_key = 'eventqueue:%s' % session.redis_key

        logger.debug('Waiting for event to be popped out of event queue: %s' % event_queue_key)

        blob = self.redis.brpop([event_queue_key], timeout=timeout)

        logger.debug('Popping one event from the session event queue %s' % event_queue_key)

        if not blob:
            return

        key, pack = blob
        event = GAPushEvent.from_dict(data=msgpack.unpackb(pack))
        resources = [GAResource(name=event.entity.rest_name, value=event.entity.id)]
        request = GARequest(action=GARequest.ACTION_READ, resources=resources)
        context = GAContext(request=request, session=session)
        context.object = event.entity

        # operation_manager = GAOperationsController(context=context,
        #                                            logic_controller=self.core_controller.logic_controller,
        #                                            storage_controller=self.core_controller.storage_controller)
        # operation_manager.run()

        # if not context.has_errors:
        #     logger.debug('Returning one event to session %s' % session.redis_key)
        return event
Esempio n. 6
0
    def test_copy(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        context.add_error(
            GAError(type=GAError.TYPE_INVALID,
                    title='title1',
                    description='description2',
                    suggestion='nope',
                    property_name='prop1'))
        context.add_error(
            GAError(type=GAError.TYPE_CONFLICT,
                    title='title2',
                    description='description3',
                    suggestion='nope',
                    property_name='prop2'))
        context.add_error(
            GAError(type=GAError.TYPE_NOTFOUND,
                    title='title3',
                    description='description4',
                    suggestion='nope',
                    property_name='prop3'))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_UPDATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_CREATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_DELETE,
                        entity=tstdk.GAEnterprise()))

        context.object = tstdk.GAEnterprise(name='enterprise1')
        context.objects = [
            tstdk.GAEnterprise(name='enterprise2'),
            tstdk.GAEnterprise(name='enterprise3')
        ]

        context_copy = context.copy()

        self.assertEquals(context_copy.session.uuid, session.uuid)
        self.assertEquals(context_copy.request.action, GARequest.ACTION_READ)
        self.assertEquals([obj.name for obj in context_copy.objects],
                          [obj.name for obj in context.objects])
        self.assertEquals(context_copy.object.name, context.object.name)

        self.assertEquals(context_copy.has_errors, True)
        self.assertEquals(len(context_copy.errors), 3)

        self.assertEquals(context_copy.has_events, True)
        self.assertEquals(len(context_copy.events), 3)
Esempio n. 7
0
    def test_perform_delegate_with_no_plugin(self):
        """
        """
        core_controller = GACoreController(garuda_uuid='test-garuda', redis_info={'host': '127.0.0.1', 'port': '6379', 'db': 6}, authentication_plugins=[])
        logic_controller = GALogicController(plugins=[], core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)
Esempio n. 8
0
    def test_make_response_for_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        context.object = enterprise1

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, enterprise1)
Esempio n. 9
0
    def test_make_response_for_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        context.object = enterprise1

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, enterprise1)
Esempio n. 10
0
    def test_perform_read_operation_with_errors_right_away(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.performed_delegates = []
        context.object = 'obj'

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        context.performed_delegates = []
        context.add_error('fake')
        operations_controller._perform_read_operation()
        self.assertEquals(context.performed_delegates, [])
Esempio n. 11
0
    def test_populate_parent_object_if_needed_with_error(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        enterprise1.parent_type = 'fake'
        enterprise1.parent_id = 'fake'
        context.object = enterprise1

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        with patch.object(self.fake_storage_controller, 'get', return_value=GAStoragePluginQueryResponse.init_with_error(error_type='error', title='', description='')):
            operations_controller._populate_parent_object_if_needed()
            self.assertEquals(len(context.errors), 1)
Esempio n. 12
0
    def test_populate_parent_object_if_needed(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        enterprise1.parent_type = 'fake'
        enterprise1.parent_id = 'yyyy'
        context.object = enterprise1

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        with patch.object(self.fake_storage_controller, 'get', return_value=GAStoragePluginQueryResponse.init_with_data(data=tstdk.GAUser(username='******'))):
            operations_controller._populate_parent_object_if_needed()

        self.assertEquals(context.parent_object.username, 'test')
        self.assertEquals(context.parent_object.rest_name, 'user')
Esempio n. 13
0
    def test_perform_delegate_with_no_plugin(self):
        """
        """
        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)
Esempio n. 14
0
    def test_perform_delegate(self):
        """
        """
        plugin1 = LogicPlugin1()
        plugin2 = LogicPlugin2()

        core_controller = GACoreController(garuda_uuid='test-garuda', redis_info={'host': '127.0.0.1', 'port': '6379', 'db': 6}, authentication_plugins=[])
        logic_controller = GALogicController(plugins=[plugin1, plugin2], core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)

        self.assertEquals(context.object, 'modified by LogicPlugin1')

        logic_controller.perform_delegate(delegate='nope', context=context)  # should not crash
Esempio n. 15
0
    def test_populate_context_for_create_with_resource(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        operations_controller._populate_context_for_create_with_resource(resource=GAResource(name='fake', value=None))
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_INVALID)
        context.clear_errors()

        request.content = {'name': 'the_enterprise'}
        with patch.object(FakeStorageController, 'instantiate', return_value=tstdk.GAEnterprise()):
            operations_controller._populate_context_for_create_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(context.object.name, 'the_enterprise')
Esempio n. 16
0
    def test_perform_push(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        context.request.action = GARequest.ACTION_ASSIGN
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'parent')
        self.assertEquals(context.events[0].action, GARequest.ACTION_UPDATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_CREATE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_CREATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_UPDATE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_UPDATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_DELETE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_DELETE)
        context.clear_events()