class GridRouterTests(TestCase):
    """
    Test the states and transitions of ``GridRouter``.
    """
    def test_grid_router(self):
        run_state_machine_as_test(lambda: GridRouterStateMachine(self))

    @given(ip=ipv4_addresses(),
           deploy_config=deployment_configuration(),
           details=subscription_details())
    def test_pods_to_routes(self, ip, deploy_config, details):
        reactor = object()
        service = _GridRouterService(reactor)
        deployment = create_deployment(deploy_config, details, model)
        pod = derive_pod(model, deployment, ip)
        service.set_pods([pod])
        mapping = service.route_mapping()
        self.assertThat(
            mapping,
            AfterPreprocessing(
                lambda m: {
                    tub_id: address
                    for (tub_id, (pod, address)) in m.iteritems()
                },
                Equals({
                    details.introducer_tub_id:
                    (ip, details.introducer_port_number),
                    details.storage_tub_id: (ip, details.storage_port_number),
                }),
            ),
        )

    @given(ip=ipv4_addresses(),
           deploy_config=deployment_configuration(),
           details=subscription_details())
    def test_proxy(self, ip, deploy_config, details):
        network = MemoryReactor()
        clock = Clock()
        reactor = FakeReactor(network, clock)
        service = _GridRouterService(reactor)
        deployment = create_deployment(deploy_config, details, model)
        pod = derive_pod(model, deployment, ip)
        service.set_pods([pod])
        factory = service.factory()
        protocol = factory.buildProtocol(None)

        transport = StringTransport()
        protocol.makeConnection(transport)
        protocol.dataReceived(
            (u"GET /id/{} HTTP/1.1\r\n"
             u"Host: example.invalid\r\n"
             u"\r\n").format(details.introducer_tub_id).encode("ascii"))

        self.assertThat(
            network.connectors.pop(0).getDestination(),
            Equals(IPv4Address("TCP", ip, details.introducer_port_number)),
        )
Esempio n. 2
0
 def test_subscription_manager_not_listening(self):
     """
     If the subscription manager doesn't accept the new subscription,
     ``provision_subscription`` returns a ``Deferred`` that
     fails with the details.
     """
     details = subscription_details().example()
     d = signup.provision_subscription(
         broken_client(),
         details,
     )
     self.failureResultOf(d)
 def test_subscription_manager_not_listening(self):
     """
     If the subscription manager doesn't accept the new subscription,
     ``provision_subscription`` returns a ``Deferred`` that
     fails with the details.
     """
     details = subscription_details().example()
     d = signup.provision_subscription(
         broken_client(),
         details,
     )
     self.failureResultOf(d)