def test_get_reader_config_file(self): # For paths matching re_config_file, TFTPBackend.get_reader() returns # a Deferred that will yield a BytesReader. cluster_uuid = factory.getRandomUUID() self.patch(tftp_module, 'get_cluster_uuid').return_value = (cluster_uuid) mac = factory.getRandomMACAddress("-") config_path = compose_config_path(mac) backend = TFTPBackend(self.make_dir(), b"http://example.com/") # python-tx-tftp sets up call context so that backends can discover # more about the environment in which they're running. call_context = { "local": (factory.getRandomIPAddress(), factory.getRandomPort()), "remote": (factory.getRandomIPAddress(), factory.getRandomPort()), } @partial(self.patch, backend, "get_config_reader") def get_config_reader(params): params_json = json.dumps(params) params_json_reader = BytesReader(params_json) return succeed(params_json_reader) reader = yield context.call(call_context, backend.get_reader, config_path) output = reader.read(10000) # The addresses provided by python-tx-tftp in the call context are # passed over the wire as address:port strings. expected_params = { "mac": mac, "local": call_context["local"][0], # address only. "remote": call_context["remote"][0], # address only. "cluster_uuid": cluster_uuid, } observed_params = json.loads(output) self.assertEqual(expected_params, observed_params)
def test_tftp_service(self): # A TFTP service is configured and added to the top-level service. interfaces = [ factory.getRandomIPAddress(), factory.getRandomIPAddress(), ] self.patch( plugin, "get_all_interface_addresses", lambda: interfaces) config = { "tftp": { "generator": "http://candlemass/solitude", "root": self.tempdir, "port": factory.getRandomPort(), }, } options = Options() options["config-file"] = self.write_config(config) service_maker = ProvisioningServiceMaker("Harry", "Hill") service = service_maker.makeService(options) tftp_services = service.getServiceNamed("tftp") # The "tftp" service is a multi-service containing UDP servers for # each interface defined by get_all_interface_addresses(). self.assertIsInstance(tftp_services, MultiService) services = [ tftp_services.getServiceNamed(interface) for interface in interfaces ] expected_backend = MatchesAll( IsInstance(TFTPBackend), AfterPreprocessing( lambda backend: backend.base.path, Equals(config["tftp"]["root"])), AfterPreprocessing( lambda backend: backend.generator_url.geturl(), Equals(config["tftp"]["generator"]))) expected_protocol = MatchesAll( IsInstance(TFTP), AfterPreprocessing( lambda protocol: protocol.backend, expected_backend)) expected_service = MatchesAll( IsInstance(UDPServer), AfterPreprocessing( lambda service: len(service.args), Equals(2)), AfterPreprocessing( lambda service: service.args[0], # port Equals(config["tftp"]["port"])), AfterPreprocessing( lambda service: service.args[1], # protocol expected_protocol)) self.assertThat(services, AllMatch(expected_service)) # Only the interface used for each service differs. self.assertEqual( [svc.kwargs for svc in services], [{"interface": interface} for interface in interfaces])
def test_patch(self): config = RabbitServerResources(hostname=factory.getRandomString(), port=factory.getRandomPort()) self.useFixture(config) self.useFixture(RabbitServerSettings(config)) self.assertEqual("%s:%d" % (config.hostname, config.port), settings.RABBITMQ_HOST) self.assertEqual("guest", settings.RABBITMQ_PASSWORD) self.assertEqual("guest", settings.RABBITMQ_USERID) self.assertEqual("/", settings.RABBITMQ_VIRTUAL_HOST) self.assertTrue(settings.RABBITMQ_PUBLISH)
def test_patch(self): config = RabbitServerResources( hostname=factory.getRandomString(), port=factory.getRandomPort()) self.useFixture(config) self.useFixture(RabbitServerSettings(config)) self.assertEqual( "%s:%d" % (config.hostname, config.port), settings.RABBITMQ_HOST) self.assertEqual("guest", settings.RABBITMQ_PASSWORD) self.assertEqual("guest", settings.RABBITMQ_USERID) self.assertEqual("/", settings.RABBITMQ_VIRTUAL_HOST) self.assertTrue(settings.RABBITMQ_PUBLISH)
def test_get_reader_config_file(self): # For paths matching re_config_file, TFTPBackend.get_reader() returns # a Deferred that will yield a BytesReader. cluster_uuid = factory.getRandomUUID() self.patch(tftp_module, 'get_cluster_uuid').return_value = ( cluster_uuid) mac = factory.getRandomMACAddress(b"-") config_path = compose_config_path(mac) backend = TFTPBackend(self.make_dir(), b"http://example.com/") # python-tx-tftp sets up call context so that backends can discover # more about the environment in which they're running. call_context = { "local": ( factory.getRandomIPAddress(), factory.getRandomPort()), "remote": ( factory.getRandomIPAddress(), factory.getRandomPort()), } @partial(self.patch, backend, "get_config_reader") def get_config_reader(params): params_json = json.dumps(params) params_json_reader = BytesReader(params_json) return succeed(params_json_reader) reader = yield context.call( call_context, backend.get_reader, config_path) output = reader.read(10000) # The addresses provided by python-tx-tftp in the call context are # passed over the wire as address:port strings. expected_params = { "mac": mac, "local": call_context["local"][0], # address only. "remote": call_context["remote"][0], # address only. "cluster_uuid": cluster_uuid, } observed_params = json.loads(output) self.assertEqual(expected_params, observed_params)
def test_tftp_service(self): # A TFTP service is configured and added to the top-level service. interfaces = [ factory.getRandomIPAddress(), factory.getRandomIPAddress(), ] self.patch(plugin, "get_all_interface_addresses", lambda: interfaces) config = { "tftp": { "generator": "http://candlemass/solitude", "root": self.tempdir, "port": factory.getRandomPort(), }, } options = Options() options["config-file"] = self.write_config(config) service_maker = ProvisioningServiceMaker("Harry", "Hill") service = service_maker.makeService(options) tftp_services = service.getServiceNamed("tftp") # The "tftp" service is a multi-service containing UDP servers for # each interface defined by get_all_interface_addresses(). self.assertIsInstance(tftp_services, MultiService) services = [ tftp_services.getServiceNamed(interface) for interface in interfaces ] expected_backend = MatchesAll( IsInstance(TFTPBackend), AfterPreprocessing(lambda backend: backend.base.path, Equals(config["tftp"]["root"])), AfterPreprocessing(lambda backend: backend.generator_url.geturl(), Equals(config["tftp"]["generator"]))) expected_protocol = MatchesAll( IsInstance(TFTP), AfterPreprocessing(lambda protocol: protocol.backend, expected_backend)) expected_service = MatchesAll( IsInstance(UDPServer), AfterPreprocessing(lambda service: len(service.args), Equals(2)), AfterPreprocessing( lambda service: service.args[0], # port Equals(config["tftp"]["port"])), AfterPreprocessing( lambda service: service.args[1], # protocol expected_protocol)) self.assertThat(services, AllMatch(expected_service)) # Only the interface used for each service differs. self.assertEqual([svc.kwargs for svc in services], [{ "interface": interface } for interface in interfaces])
def make_url(): """Create an arbitrary URL.""" return 'http://example.com:%d/%s/' % ( factory.getRandomPort(), factory.getRandomString(), )
def test_getRandomPort_returns_int(self): self.assertIsInstance(factory.getRandomPort(), int)
def getUniqueServiceNameAndPort(self): # getUniqueString() generates an invalid service name name = 'My-Test-Service-%d' % next(self.count) port = factory.getRandomPort() return name, port