Esempio n. 1
0
    def test_start_router_component_invalid_type(self):
        """
        Trying to start a component with an invalid type fails.
        """
        log_list = []

        r = router.RouterWorkerSession(config=self.config, reactor=reactor)
        r.log = make_logger(observer=log_list.append, log_level="debug")

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {u"name": u"realm1", u'roles': []}

        r.start_router_realm("realm1", realm_config)

        component_config = {
            "type": u"notathingcrossbarsupports",
            "realm": u"realm1"
        }

        with self.assertRaises(ApplicationError) as e:
            r.start_router_component("newcomponent", component_config)

        self.assertEqual(e.exception.error,
                         u"crossbar.error.invalid_configuration")

        self.assertEqual(len(r.get_router_components()), 0)
Esempio n. 2
0
    def test_start_router_component_wrong_baseclass(self):
        """
        Starting a class-based router component fails when the application
        session isn't derived from ApplicationSession.
        """
        r = router.RouterWorkerSession(config=self.config, reactor=reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {u"name": u"realm1", u'roles': []}

        r.start_router_realm("realm1", realm_config)

        component_config = {
            "type": u"class",
            "classname": u"crossbar.worker.test.examples.badclass.AppSession",
            "realm": u"realm1"
        }

        with self.assertRaises(ApplicationError) as e:
            r.start_router_component("newcomponent", component_config)

        self.assertIn(("session not derived of ApplicationSession"),
                      str(e.exception.args[0]))

        self.assertEqual(len(r.get_router_components()), 0)
Esempio n. 3
0
    def test_threads(self):
        """
        A basic WSGI app can be ran, with subresources
        """
        temp_reactor = SelectReactor()
        r = router.RouterWorkerSession(config=self.config,
                                       reactor=temp_reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {u"name": u"realm1", u'roles': []}

        threads = 20

        r.start_router_realm("realm1", realm_config)
        r.start_router_transport(
            "component1", {
                u"type": u"web",
                u"endpoint": {
                    u"type": u"tcp",
                    u"port": 8080
                },
                u"paths": {
                    u"/": {
                        "module": u"crossbar.worker.test.test_router",
                        "object": u"sleep",
                        "type": u"wsgi",
                        "maxthreads": threads,
                    }
                }
            })

        deferreds = []
        results = []

        for i in range(threads):
            d = treq.get("http://localhost:8080/", reactor=temp_reactor)
            d.addCallback(treq.content)
            d.addCallback(results.append)
            deferreds.append(d)

        def done(_):
            max_concurrency = max([int(x) for x in results])

            assert max_concurrency == threads, "Maximum concurrency was %s, not %s" % (
                max_concurrency, threads)
            temp_reactor.stop()

        defer.DeferredList(deferreds).addCallback(done)

        def escape():
            if temp_reactor.running:
                temp_reactor.stop()

        temp_reactor.callLater(1, escape)
        temp_reactor.run()
Esempio n. 4
0
    def test_root_not_required(self):
        """
        Not including a '/' path will mean that path has a 404, but children
        will still be routed correctly.
        """
        temp_reactor = SelectReactor()
        r = router.RouterWorkerSession(config=self.config,
                                       reactor=temp_reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {u"name": u"realm1", u'roles': []}

        # Make a file
        self.cbdir.child('file.txt').setContent(b"hello!")

        r.start_router_realm("realm1", realm_config)
        r.start_router_transport(
            "component1", {
                u"type": u"web",
                u"endpoint": {
                    u"type": u"tcp",
                    u"port": 8080
                },
                u"paths": {
                    u"static": {
                        "directory": self.cbdir.asTextMode().path,
                        "type": u"static"
                    }
                }
            })

        # Make a request to the WSGI app.
        d1 = treq.get("http://localhost:8080/", reactor=temp_reactor)
        d1.addCallback(lambda resp: self.assertEqual(resp.code, 404))

        d2 = treq.get("http://localhost:8080/static/file.txt",
                      reactor=temp_reactor)
        d2.addCallback(treq.content)
        d2.addCallback(self.assertEqual, b"hello!")

        def done(results):
            for item in results:
                if not item[0]:
                    raise item[1]

        d = defer.DeferredList([d1, d2])
        d.addCallback(done)
        d.addCallback(lambda _: temp_reactor.stop())

        def escape():
            if temp_reactor.running:
                temp_reactor.stop()

        temp_reactor.callLater(1, escape)
        temp_reactor.run()
Esempio n. 5
0
    def test_basic_subresources(self):
        """
        A basic WSGI app can be ran, with subresources
        """
        temp_reactor = SelectReactor()
        r = router.RouterWorkerSession(config=self.config,
                                       reactor=temp_reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {
            u"name": u"realm1",
            u'roles': []
        }

        r.start_router_realm(u"realm1", realm_config)
        r.start_router_transport(
            "component1",
            {
                u"type": u"web",
                u"endpoint": {
                    u"type": u"tcp",
                    u"port": 8080
                },
                u"paths": {
                    u"/": {
                        "module": u"crossbar.worker.test.test_router",
                        "object": u"hello",
                        "type": u"wsgi"
                    },
                    u"json": {
                        "type": u"json",
                        "value": {}
                    }
                }
            })

        # Make a request to the /json endpoint, which is technically a child of
        # the WSGI app, but is not served by WSGI.
        d = treq.get("http://localhost:8080/json", reactor=temp_reactor)
        d.addCallback(treq.content)
        d.addCallback(self.assertEqual, b"{}")
        d.addCallback(lambda _: temp_reactor.stop())

        def escape():
            if temp_reactor.running:
                temp_reactor.stop()

        temp_reactor.callLater(1, escape)
        temp_reactor.run()

        return d
Esempio n. 6
0
    def test_start_router_component(self):
        """
        Starting a class-based router component works.
        """
        r = router.RouterWorkerSession(config=self.config, reactor=reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {
            u"name":
            u"realm1",
            u'roles': [{
                u'name':
                u'anonymous',
                u'permissions': [{
                    u'subscribe': True,
                    u'register': True,
                    u'call': True,
                    u'uri': u'*',
                    u'publish': True
                }]
            }]
        }

        r.start_router_realm(u"realm1", realm_config)

        permissions = RouterPermissions(u'', True, True, True, True, True)
        routera = r._router_factory.get(u'realm1')
        routera.add_role(
            RouterRoleStaticAuth(router,
                                 'anonymous',
                                 default_permissions=permissions))

        component_config = {
            u"type": u"class",
            u"classname":
            u"crossbar.worker.test.examples.goodclass.AppSession",
            u"realm": u"realm1"
        }

        r.start_router_component("newcomponent", component_config)

        self.assertEqual(len(r.get_router_components()), 1)
        self.assertEqual(r.get_router_components()[0]["id"], "newcomponent")

        self.assertEqual(len(_), 1)
        _.pop()  # clear this global state
Esempio n. 7
0
    def test_basic(self):
        """
        We can instantiate a RouterWorkerSession.
        """
        log_list = []

        r = router.RouterWorkerSession(config=self.config, reactor=reactor)
        r.log = make_logger(observer=log_list.append, log_level="debug")

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        # XXX depends on log-text; perhaps a little flaky...
        self.assertIn("running as", log_list[-1]["log_format"])
Esempio n. 8
0
    def test_start_router_component_fails(self):
        """
        Trying to start a class-based router component that gets an error on
        importing fails.
        """
        log_list = []

        r = router.RouterWorkerSession(config=self.config, reactor=reactor)
        r.log = make_logger(observer=log_list.append, log_level="debug")

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {
            u"name":
            u"realm1",
            u'roles': [{
                u'name':
                u'anonymous',
                u'permissions': [{
                    u'subscribe': True,
                    u'register': True,
                    u'call': True,
                    u'uri': u'*',
                    u'publish': True
                }]
            }]
        }

        r.start_router_realm("realm1", realm_config)

        component_config = {
            "type": u"class",
            "classname": u"thisisathing.thatdoesnot.exist",
            "realm": u"realm1"
        }

        with self.assertRaises(ApplicationError) as e:
            r.start_router_component("newcomponent", component_config)

        self.assertIn(
            "Failed to import class 'thisisathing.thatdoesnot.exist'",
            str(e.exception.args[0]))

        self.assertEqual(len(r.get_router_components()), 0)
Esempio n. 9
0
    def test_basic(self):
        """
        We can instantiate a RouterWorkerSession.
        """
        log_list = []

        r = router.RouterWorkerSession(config=self.config, reactor=reactor)
        r.log = make_logger(observer=log_list.append, log_level="debug")

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        # Should have 35 registers, all for the management interface
        self.assertEqual(len(transport._get(Register)), 35)
        # XXX depends on log-text; perhaps a little flaky...
        self.assertIn("running as", log_list[-1]["log_format"])
Esempio n. 10
0
    def test_basic(self):
        """
        A basic WSGI app can be ran.
        """
        temp_reactor = SelectReactor()
        r = router.RouterWorkerSession(config=self.config,
                                       reactor=temp_reactor)

        # Open the transport
        transport = FakeWAMPTransport(r)
        r.onOpen(transport)

        realm_config = {u"name": u"realm1", u'roles': []}

        r.start_router_realm("realm1", realm_config)
        r.start_router_transport(
            "component1", {
                u"type": u"web",
                u"endpoint": {
                    u"type": u"tcp",
                    u"port": 8080
                },
                u"paths": {
                    u"/": {
                        "module": u"crossbar.worker.test.test_router",
                        "object": u"hello",
                        "type": u"wsgi"
                    }
                }
            })

        # Make a request to the WSGI app.
        d = treq.get("http://localhost:8080/", reactor=temp_reactor)
        d.addCallback(treq.content)
        d.addCallback(self.assertEqual, b"hello!")
        d.addCallback(lambda _: temp_reactor.stop())

        def escape():
            if temp_reactor.running:
                temp_reactor.stop()

        temp_reactor.callLater(1, escape)
        temp_reactor.run()

        return d