def test_validate_router_config_existing_router(self): """ If an existing router is already listening to the specified channel, then a config error should be raised """ channel = yield self.create_channel(self.api.service, self.redis, properties={ 'type': 'telnet', 'config': { 'twisted_endpoint': 'tcp:0', }, }) config = self.create_router_config(config={ 'test': 'pass', 'channel': channel.id }) router = Router(self.api, config) yield router.save() router.start(self.api.service) with self.assertRaises(InvalidRouterConfig) as e: yield FromAddressRouter.validate_router_config( self.api, {'channel': channel.id}) self.assertEqual( e.exception.message, "Router {} is already routing channel {}".format( router.id, channel.id))
def test_get_destination_list(self): """Getting the destination list of a router should return a list of destination ids for that router""" router_config = self.create_router_config() router = Router(self.api, router_config) router.start(self.api.service) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) self.assertEqual(router.get_destination_list(), [destination.id])
def test_start(self): """start should start the router worker with the correct config""" config = self.create_router_config() router = Router(self.api, config) router.start(self.service) router_worker = self.service.namedServices[router.id] self.assertEqual(router_worker.parent, self.service) router_worker_config = config['config'] for k, v in router_worker_config.items(): self.assertEqual(router_worker.config[k], v)
def test_stop(self): """stop should stop the router worker if it is running""" config = self.create_router_config() router = Router(self.api, config) router.start(self.service) self.assertIn(router.id, self.service.namedServices) yield router.stop() self.assertNotIn(router.id, self.service.namedServices)
def create_router(self, request, body): """Create a new router""" router = Router(self, body) yield router.validate_config() router.start(self.service) yield router.save() returnValue(response( request, 'router created', (yield router.status()), code=http.CREATED ))
def test_destinations_are_passed_to_router_worker(self): """The destination configs should be passed to the router worker when the router is started.""" config = self.create_router_config() router = Router(self.api, config) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) router.start(self.service) router_worker = self.service.namedServices[router.id] self.assertEqual(router_worker.config['destinations'], [destination.destination_config])
def test_from_id(self): """from_id should be able to restore a router, given just the id""" config = self.create_router_config() router = Router(self.api, config) yield router.save() router.start(self.api.service) restored_router = yield Router.from_id(self.api, router.router_config['id']) self.assertEqual(router.router_config, restored_router.router_config) self.assertEqual(router.router_worker, restored_router.router_worker)
def test_from_id(self): """from_id should be able to restore a router, given just the id""" config = self.create_router_config() router = Router(self.api, config) yield router.save() router.start(self.api.service) restored_router = yield Router.from_id( self.api, router.router_config['id']) self.assertEqual(router.router_config, restored_router.router_config) self.assertEqual(router.router_worker, restored_router.router_worker)
def test_destinations_are_passed_to_router_worker(self): """The destination configs should be passed to the router worker when the router is started.""" config = self.create_router_config() router = Router(self.api, config) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) router.start(self.service) router_worker = self.service.namedServices[router.id] self.assertEqual( router_worker.config['destinations'], [destination.destination_config])
def test_stop_already_stopped(self): """Calling stop on a non-running router should not raise any exceptions""" config = self.create_router_config() router = Router(self.api, config) router.start(self.service) self.assertIn(router.id, self.service.namedServices) yield router.stop() self.assertNotIn(router.id, self.service.namedServices) yield router.stop()
def test_destinations_restored_on_router_from_id(self): """Creating a router object from id should also restore the destinations for that router""" router_config = self.create_router_config() router = Router(self.api, router_config) router.start(self.api.service) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) yield router.save() restored_router = yield Router.from_id(self.api, router.id) self.assertEqual(router.destinations.keys(), restored_router.destinations.keys()) self.assertEqual( router.destinations[destination.id].destination_config, restored_router.destinations[destination.id].destination_config)
def test_destinations_restored_on_router_from_id(self): """Creating a router object from id should also restore the destinations for that router""" router_config = self.create_router_config() router = Router(self.api, router_config) router.start(self.api.service) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) yield router.save() restored_router = yield Router.from_id(self.api, router.id) self.assertEqual( router.destinations.keys(), restored_router.destinations.keys()) self.assertEqual( router.destinations[destination.id].destination_config, restored_router.destinations[destination.id].destination_config)