def start_local_api(name, port, api, method, asynchronous=False, listener=None): log_startup_message(name) if config.FORWARD_EDGE_INMEM: port = get_free_tcp_port() PROXY_LISTENERS[api] = (api, port, listener) if asynchronous: thread = start_thread(method, port, quiet=True) return thread else: method(port)
def _build_executor_endpoint( self, service_endpoint: ServiceEndpoint) -> ExecutorEndpoint: port = get_free_tcp_port() LOG.debug( "Creating service endpoint for function %s executor %s", self.function_version.qualified_arn, self.id, ) executor_endpoint = ExecutorEndpoint(port, service_endpoint=service_endpoint) LOG.debug( "Finished creating service endpoint for function %s executor %s", self.function_version.qualified_arn, self.id, ) return executor_endpoint
def proxy_server(proxy_listener, host="127.0.0.1", port=None) -> str: """ Create a temporary proxy server on a random port (or the specified port) with the given proxy listener for the duration of the context manager. """ from localstack.services.generic_proxy import start_proxy_server host = host port = port or get_free_tcp_port() thread = start_proxy_server(port, bind_address=host, update_listener=proxy_listener) url = f"http://{host}:{port}" assert poll_condition(lambda: is_port_open(port), timeout=5), f"server on port {port} did not start" yield url thread.stop()
def start_http_server( test_port: int = None, invocations: List = None, invocation_handler: Callable = None) -> Tuple[int, List, FuncThread]: # Note: leave imports here to avoid import errors (e.g., "flask") for CLI commands from localstack.services.generic_proxy import ProxyListener from localstack.services.infra import start_proxy class TestListener(ProxyListener): def forward_request(self, **kwargs): if invocation_handler: kwargs = invocation_handler(**kwargs) invocations.append(kwargs) return 200 test_port = test_port or get_free_tcp_port() invocations = invocations or [] proxy = start_proxy(test_port, update_listener=TestListener()) return test_port, invocations, proxy
def get_moto_server(timeout=10) -> MotoServer: """ Returns the MotoServer singleton or creates it and waits for it to become ready. """ global _server, _mutex with _mutex: if _server: return _server _server = MotoServer(port=get_free_tcp_port(), host=constants.BIND_HOST) _server.start() if not _server.wait_is_up(timeout): raise TimeoutError("gave up waiting for moto server on %s" % _server.url) return _server
def http_server(handler, host="127.0.0.1", port=None) -> str: """ Create a temporary http server on a random port (or the specified port) with the given handler for the duration of the context manager. Example usage: def handler(request, data): print(request.method, request.path, data) with testutil.http_server(handler) as url: requests.post(url, json={"message": "hello"}) """ from localstack.utils.server.http2_server import run_server host = host port = port or get_free_tcp_port() thread = run_server(port, [host], handler=handler, asynchronous=True) url = f"http://{host}:{port}" assert poll_condition(lambda: is_port_open(port), timeout=5), f"server on port {port} did not start" yield url thread.stop()
def start_moto_server(key, port, name=None, backend_port=None, asynchronous=False, update_listener=None) -> MotoServerProperties: # TODO: refactor this method! the name and parameters suggest that a server is started, but it actually only adds # a proxy listener around the already started motoserver singleton. # TODO: remove asynchronous parameter (from all calls to this function) # TODO: re-think backend_port parameter (still needed since determined by motoserver singleton?) if not name: name = key log_startup_message(name) if not backend_port: if config.FORWARD_EDGE_INMEM: backend_port = motoserver.get_moto_server().port elif config.USE_SSL or update_listener: backend_port = get_free_tcp_port() if backend_port or config.FORWARD_EDGE_INMEM: start_proxy_for_service(key, port, backend_port, update_listener) server = motoserver.get_moto_server() return MotoServerProperties(server._thread, server.port)
def get_multiserver_or_free_service_port(): if config.FORWARD_EDGE_INMEM: return multiserver.get_moto_server_port() return get_free_tcp_port()