Beispiel #1
0
class MiddlewareHelper(object):
    implements(IHelper)

    def __init__(self, middleware_class):
        self._vumi_helper = VumiApiHelper()
        self._msg_helper = GoMessageHelper()
        self.middleware_class = middleware_class
        self._middlewares = []

        generate_proxies(self, self._vumi_helper)
        generate_proxies(self, self._msg_helper)

    def setup(self):
        return self._vumi_helper.setup(setup_vumi_api=False)

    @inlineCallbacks
    def cleanup(self):
        for mw in self._middlewares:
            yield mw.teardown_middleware()
        yield self._vumi_helper.cleanup()

    @inlineCallbacks
    def create_middleware(self, config=None, middleware_class=None,
                          name='dummy_middleware'):
        worker_helper = self._vumi_helper.get_worker_helper()
        dummy_worker = yield worker_helper.get_worker(
            ToyWorker, self.mk_config({}))
        config = self.mk_config(config or {})
        if middleware_class is None:
            middleware_class = self.middleware_class
        mw = middleware_class(name, config, dummy_worker)
        self._middlewares.append(mw)
        yield mw.setup_middleware()
        returnValue(mw)
Beispiel #2
0
class MiddlewareHelper(object):
    implements(IHelper)

    def __init__(self, middleware_class):
        self._vumi_helper = VumiApiHelper()
        self._msg_helper = GoMessageHelper()
        self.middleware_class = middleware_class
        self._middlewares = []

        generate_proxies(self, self._vumi_helper)
        generate_proxies(self, self._msg_helper)

    def setup(self):
        return self._vumi_helper.setup(setup_vumi_api=False)

    @inlineCallbacks
    def cleanup(self):
        for mw in self._middlewares:
            yield mw.teardown_middleware()
        yield self._vumi_helper.cleanup()

    @inlineCallbacks
    def create_middleware(self,
                          config=None,
                          middleware_class=None,
                          name='dummy_middleware'):
        worker_helper = self._vumi_helper.get_worker_helper()
        dummy_worker = yield worker_helper.get_worker(ToyWorker,
                                                      self.mk_config({}))
        config = self.mk_config(config or {})
        if middleware_class is None:
            middleware_class = self.middleware_class
        mw = middleware_class(name, config, dummy_worker)
        self._middlewares.append(mw)
        yield mw.setup_middleware()
        returnValue(mw)
Beispiel #3
0
class AppWorkerHelper(object):
    implements(IHelper)

    def __init__(self, worker_class, **msg_helper_args):
        self._worker_class = worker_class

        self.vumi_helper = VumiApiHelper()
        self._app_helper = ApplicationHelper(
            self._conversation_type(), self.vumi_helper)
        self.msg_helper = GoMessageHelper(**msg_helper_args)
        self.transport_name = self.msg_helper.transport_name
        self.worker_helper = self.vumi_helper.get_worker_helper(
            self.transport_name)
        self.dispatch_helper = MessageDispatchHelper(
            self.msg_helper, self.worker_helper)

        # Proxy methods from our helpers.
        generate_proxies(self, self._app_helper)
        generate_proxies(self, self.msg_helper)
        generate_proxies(self, self.worker_helper)
        generate_proxies(self, self.dispatch_helper)

    def _worker_name(self):
        return self._worker_class.worker_name

    def _conversation_type(self):
        # This is a guess based on worker_name.
        # TODO: We need a better way to do this, probably involving either the
        #       conversation definition or go.config.
        return self._worker_name().rpartition('_')[0].decode('utf-8')

    def setup(self):
        return self.vumi_helper.setup(setup_vumi_api=False)

    def cleanup(self):
        return self.vumi_helper.cleanup()

    @inlineCallbacks
    def get_app_worker(self, config=None, start=True):
        # Note: We assume that this is called exactly once per test.
        config = self.vumi_helper.mk_config(config or {})
        config.setdefault('worker_name', self._worker_name())
        config.setdefault('transport_name', self.msg_helper.transport_name)
        worker = yield self.get_worker(self._worker_class, config, start)
        # Set up our other bits of helper.
        self.vumi_helper.set_vumi_api(worker.vumi_api)
        self.msg_helper.mdb = worker.vumi_api.mdb
        returnValue(worker)

    @inlineCallbacks
    def start_conversation(self, conversation):
        assert self._get_pending_commands() == [], (
            "Found pending commands while starting conversation, aborting.")
        yield conversation.start()
        yield self.dispatch_commands_to_app()

    @inlineCallbacks
    def stop_conversation(self, conversation):
        assert self._get_pending_commands() == [], (
            "Found pending commands while stopping conversation, aborting.")
        yield conversation.stop_conversation()
        yield self.dispatch_commands_to_app()

    def _get_pending_commands(self):
        return self.worker_helper.get_dispatched('vumi', 'api', VumiApiCommand)

    @inlineCallbacks
    def dispatch_commands_to_app(self):
        pending_commands = self._get_pending_commands()
        self.worker_helper._clear_dispatched('vumi', 'api')
        for command in pending_commands:
            yield self.worker_helper.dispatch_raw(
                "%s.control" % (self._worker_name(),), command)

    @inlineCallbacks
    def dispatch_command(self, command, *args, **kw):
        cmd = VumiApiCommand.command(
            self._worker_name(), command, *args, **kw)
        yield self.worker_helper.dispatch_raw('vumi.api', cmd)
        yield self.dispatch_commands_to_app()

    def get_published_metrics(self, worker):
        metrics = []
        for metric_msg in self.worker_helper.get_dispatched_metrics():
            for name, _aggs, data in metric_msg:
                for _time, value in data:
                    metrics.append((name, value))
        return metrics

    def get_dispatched_app_events(self):
        return self.worker_helper.get_dispatched('vumi', 'event', VumiApiEvent)
Beispiel #4
0
class DjangoVumiApiHelper(object):
    implements(IHelper)

    def __init__(self, use_riak=True):
        # Note: We pass `is_sync=True` to the VumiApiHelper because a Django
        #       test case cannot be async. We define a property lower down that
        #       proxies `is_sync` from the VumiApiHelper we're wrapping so that
        #       we can be used by other helpers more easily.
        self._vumi_helper = VumiApiHelper(is_sync=True, use_riak=use_riak)
        self.use_riak = use_riak  # So create_user_profile() knows what to do.

        generate_proxies(self, self._vumi_helper)
        # TODO: Better/more generic way to do this patching?
        self._settings_patches = []

    def setup(self, setup_vumi_api=True):
        # We defer `setup_vumi_api` until we've patched Django.
        self._vumi_helper.setup(False)
        self.replace_django_bits()
        if setup_vumi_api:
            return self.setup_vumi_api()

    def cleanup(self):
        self._vumi_helper.cleanup()
        self.restore_django_bits()
        for patch in reversed(self._settings_patches):
            patch.disable()

    @property
    def is_sync(self):
        return self._vumi_helper.is_sync

    @property
    def amqp_connection(self):
        return getattr(self._vumi_helper, 'django_amqp_connection', None)

    def replace_django_bits(self):
        self._replace_settings()
        self._replace_post_save_hooks()

    def _replace_settings(self):
        # We do this redis manager hackery here because we might use it from
        # Django-land before setting (or without) up a vumi_api.
        # TODO: Find a nicer way to give everything the same fake redis.
        pcfg = self._vumi_helper._persistence_helper._config_overrides
        pcfg['redis_manager']['FAKE_REDIS'] = self.get_redis_manager()

        vumi_config = self.mk_config(settings.VUMI_API_CONFIG)
        self.patch_settings(VUMI_API_CONFIG=vumi_config)

    def _replace_post_save_hooks(self):
        has_listeners = lambda: post_save.has_listeners(get_user_model())
        assert has_listeners(), (
            "User model has no post_save listeners. Make sure"
            " DjangoVumiApiHelper is cleaned up properly in earlier tests.")
        post_save.disconnect(
            sender=get_user_model(),
            dispatch_uid='go.base.models.create_user_profile')
        assert not has_listeners(), (
            "User model still has post_save listeners. Make sure"
            " DjangoVumiApiHelper is cleaned up properly in earlier tests.")
        post_save.connect(
            self.create_user_profile,
            sender=get_user_model(),
            dispatch_uid='DjangoVumiApiHelper.create_user_profile')

    def restore_django_bits(self):
        post_save.disconnect(
            sender=get_user_model(),
            dispatch_uid='DjangoVumiApiHelper.create_user_profile')
        post_save.connect(
            base_models.create_user_profile,
            sender=get_user_model(),
            dispatch_uid='go.base.models.create_user_profile')

    @proxyable
    def get_client(self, username='******', password='******'):
        client = Client()
        client.login(username=username, password=password)
        return client

    @proxyable
    def patch_settings(self, **kwargs):
        patch = override_settings(**kwargs)
        patch.enable()
        self._settings_patches.append(patch)

    @proxyable
    def make_django_user(self, email='*****@*****.**', password='******',
                         first_name="Test", last_name="User", superuser=False):
        if superuser:
            user = get_user_model().objects.create_superuser(
                email=email, password=password)
        else:
            user = get_user_model().objects.create_user(
                email=email, password=password)
        user.first_name = first_name
        user.last_name = last_name
        user.save()
        user_api = base_utils.vumi_api_for_user(user)
        return self.get_user_helper(user_api.user_account_key)

    def create_user_profile(self, sender, instance, created, **kwargs):
        if not created:
            return

        if not self.use_riak:
            # Just create the account key, no actual user.
            base_models.UserProfile.objects.create(
                user=instance, user_account=uuid.uuid4())
            return

        user_helper = self.make_user(
            unicode(instance.email), enable_search=True,
            django_user_pk=instance.pk)
        base_models.UserProfile.objects.create(
            user=instance, user_account=user_helper.account_key)
Beispiel #5
0
class AppWorkerHelper(object):
    implements(IHelper)

    def __init__(self, worker_class, **msg_helper_args):
        self._worker_class = worker_class

        self.vumi_helper = VumiApiHelper()
        self._app_helper = ApplicationHelper(self._conversation_type(),
                                             self.vumi_helper)
        self.msg_helper = GoMessageHelper(**msg_helper_args)
        self.transport_name = self.msg_helper.transport_name
        self.worker_helper = self.vumi_helper.get_worker_helper(
            self.transport_name)
        self.dispatch_helper = MessageDispatchHelper(self.msg_helper,
                                                     self.worker_helper)

        # Proxy methods from our helpers.
        generate_proxies(self, self._app_helper)
        generate_proxies(self, self.msg_helper)
        generate_proxies(self, self.worker_helper)
        generate_proxies(self, self.dispatch_helper)

    def _worker_name(self):
        return self._worker_class.worker_name

    def _conversation_type(self):
        # This is a guess based on worker_name.
        # TODO: We need a better way to do this, probably involving either the
        #       conversation definition or go.config.
        return self._worker_name().rpartition('_')[0].decode('utf-8')

    def setup(self):
        return self.vumi_helper.setup(setup_vumi_api=False)

    def cleanup(self):
        return self.vumi_helper.cleanup()

    @inlineCallbacks
    def get_app_worker(self, config=None, start=True):
        # Note: We assume that this is called exactly once per test.
        config = self.vumi_helper.mk_config(config or {})
        config.setdefault('worker_name', self._worker_name())
        config.setdefault('transport_name', self.msg_helper.transport_name)
        worker = yield self.get_worker(self._worker_class, config, start)
        # Set up our other bits of helper.
        self.vumi_helper.set_vumi_api(worker.vumi_api)
        self.msg_helper.mdb = worker.vumi_api.mdb
        returnValue(worker)

    @inlineCallbacks
    def start_conversation(self, conversation):
        assert self._get_pending_commands() == [], (
            "Found pending commands while starting conversation, aborting.")
        yield conversation.start()
        yield self.dispatch_commands_to_app()

    @inlineCallbacks
    def stop_conversation(self, conversation):
        assert self._get_pending_commands() == [], (
            "Found pending commands while stopping conversation, aborting.")
        yield conversation.stop_conversation()
        yield self.dispatch_commands_to_app()

    def _get_pending_commands(self):
        return self.worker_helper.get_dispatched('vumi', 'api', VumiApiCommand)

    @inlineCallbacks
    def dispatch_commands_to_app(self):
        pending_commands = self._get_pending_commands()
        self.worker_helper._clear_dispatched('vumi', 'api')
        for command in pending_commands:
            yield self.worker_helper.dispatch_raw(
                "%s.control" % (self._worker_name(), ), command)

    @inlineCallbacks
    def dispatch_command(self, command, *args, **kw):
        cmd = VumiApiCommand.command(self._worker_name(), command, *args, **kw)
        yield self.worker_helper.dispatch_raw('vumi.api', cmd)
        yield self.dispatch_commands_to_app()

    def get_published_metrics(self, worker):
        metrics = []
        for metric_msg in self.worker_helper.get_dispatched_metrics():
            for name, _aggs, data in metric_msg:
                for _time, value in data:
                    metrics.append((name, value))
        return metrics

    def get_dispatched_app_events(self):
        return self.worker_helper.get_dispatched('vumi', 'event', VumiApiEvent)
Beispiel #6
0
class RouterWorkerHelper(object):
    implements(IHelper)

    def __init__(self, worker_class, **msg_helper_args):
        self._worker_class = worker_class
        msg_helper_kw = {}
        if msg_helper_args is not None:
            msg_helper_kw.update(msg_helper_args)

        self.vumi_helper = VumiApiHelper()
        self._router_helper = RouterHelper(
            self._router_type(), self.vumi_helper)
        self.msg_helper = GoMessageHelper(**msg_helper_kw)

        # Proxy methods from our helpers.
        generate_proxies(self, self._router_helper)
        generate_proxies(self, self.msg_helper)

        self.ri = RouterConnectorHelper(
            'ri_conn', self.vumi_helper, self.msg_helper)

        self.ro = RouterConnectorHelper(
            'ro_conn', self.vumi_helper, self.msg_helper)

    def _worker_name(self):
        return self._worker_class.worker_name

    def _router_type(self):
        # This is a guess based on worker_name.
        # We need a better way to do this.
        return self._worker_name().rpartition('_')[0].decode('utf-8')

    def setup(self):
        self.vumi_helper.setup(setup_vumi_api=False)

    @inlineCallbacks
    def cleanup(self):
        yield self.ro.cleanup()
        yield self.ri.cleanup()
        yield self.msg_helper.cleanup()
        yield self.vumi_helper.cleanup()

    @inlineCallbacks
    def get_router_worker(self, config=None, start=True):
        # Note: We assume that this is called exactly once per test.
        config = self.vumi_helper.mk_config(config or {})
        config.setdefault('worker_name', self._worker_name())
        config.setdefault('ri_connector_name', self.ri.connector_name)
        config.setdefault('ro_connector_name', self.ro.connector_name)
        worker = yield self.ri.get_worker(self._worker_class, config, start)
        # Set up our other bits of helper.
        self.vumi_helper.set_vumi_api(worker.vumi_api)
        self.msg_helper.mdb = worker.vumi_api.mdb
        returnValue(worker)

    @inlineCallbacks
    def start_router(self, router):
        assert self._get_pending_commands() == [], (
            "Found pending commands while starting router, aborting.")
        user_helper = yield self.vumi_helper.get_or_create_user()
        router_api = user_helper.user_api.get_router_api(
            router.router_type, router.key)
        yield router_api.start_router(router)
        yield self.dispatch_commands_to_router()

    @inlineCallbacks
    def stop_router(self, router):
        assert self._get_pending_commands() == [], (
            "Found pending commands while stopping router, aborting.")
        user_helper = yield self.vumi_helper.get_or_create_user()
        router_api = user_helper.user_api.get_router_api(
            router.router_type, router.key)
        yield router_api.stop_router(router)
        yield self.dispatch_commands_to_router()

    def _get_pending_commands(self):
        return self.ri.get_dispatched('vumi', 'api', VumiApiCommand)

    @inlineCallbacks
    def dispatch_commands_to_router(self):
        pending_commands = self._get_pending_commands()
        self.ri._worker_helper._clear_dispatched('vumi', 'api')
        for command in pending_commands:
            yield self.ri.dispatch_raw(
                "%s.control" % (self._worker_name(),), command)

    @inlineCallbacks
    def dispatch_command(self, command, *args, **kw):
        cmd = VumiApiCommand.command(
            self._worker_name(), command, *args, **kw)
        yield self.dispatch_raw('vumi.api', cmd)
        yield self.dispatch_commands_to_router()

    def get_published_metrics(self, worker):
        return [
            (metric.name, value)
            for metric, ((time, value),) in worker.metrics._oneshot_msgs]

    def get_dispatched_router_events(self):
        return self.get_dispatched('vumi', 'event', VumiApiEvent)