def wait_for_url(self, url, timeout_secs): """Wait for url to be ready or timeout.""" logging.info('Waiting for %s', url) for _ in range(timeout_secs): try: code = urllib2.urlopen(url).getcode() if code >= 200 and code < 300: logging.info('%s is ready', url) return except urllib2.URLError: time.sleep(1) raise_and_log_error(TimeoutError('%s not ready' % url), '%s not ready after %s secs' % (url, timeout_secs))
def __validate_service_base_url(self, service_name, timeout=None): service_config = self.__public_service_configs[service_name] base_url = service_config["base_url"] timeout = timeout or self.options.test_service_startup_timeout end_time = time.time() + timeout logging.info('Validating base URL of "%s"...', service_name) try: url = "{base_url}/health".format(base_url=base_url) request = Request(url=url) if "bearer_auth_token" in service_config: request.add_header( "Authorization", "Bearer {}".format(service_config["bearer_auth_token"]), ) context = None if self.options.test_ignore_ssl_cert_verification: context = ssl._create_unverified_context() urlopen(request, context=context) logging.info('"%s" is ready on service endpoint %s', service_name, base_url) return except HTTPError as error: logging.error("%s service endpoint got %s.", service_name, error) raise_and_log_error( ResponseError( "{0} service endpoint got {1}".format(service_name, error), server=base_url, )) except Exception as error: raise_and_log_error( ResponseError( "{0} service endpoint got {1}".format(service_name, error), server=base_url, )) except URLError as error: if time.time() >= end_time: logging.error("Timing out waiting for %s", service_name) raise_and_log_error( TimeoutError(service_name, cause=service_name)) raise
def __validate_service_base_url(self, service_name, timeout=None): service_config = self.__public_service_configs[service_name] base_url = service_config['base_url'] timeout = timeout or self.options.test_service_startup_timeout end_time = time.time() + timeout logging.info('Validating base URL of "%s..."', service_name) try: if 'bearer_auth_token' in service_config: _make_get_request_with_bearer_auth_token( '{base_url}/health'.format(base_url=base_url), service_config['bearer_auth_token']) # Add more authentication cases here. else: urlopen('{base_url}/health'.format(base_url=base_url)) logging.info('"%s" is ready on service endpoint %s', service_name, base_url) return except HTTPError as error: logging.error('%s service endpoint got %s.', service_name, error) raise_and_log_error( ResponseError('{0} service endpoint got {1}'.format( service_name, error), server=base_url)) except Exception as error: raise_and_log_error( ResponseError('{0} service endpoint got {1}'.format( service_name, error), server=base_url)) except URLError as error: if time.time() >= end_time: logging.error('Timing out waiting for %s', service_name) raise_and_log_error( TimeoutError(service_name, cause=service_name)) raise
def wait_on_service(self, service_name, port=None, timeout=None): """Wait for the given service to be available on the specified port. Args: service_name: [string] The service name we we are waiting on. port: [int] The remote port the service is at. timeout: [int] How much time to wait before giving up. Returns: The ForwardedPort entry for this service. """ try: with self.__lock: forwarding = self.__forwarded_ports.get(service_name) if forwarding is None: forwarding = self.__forward_port_to_service(service_name) self.__forwarded_ports[service_name] = forwarding except Exception: logging.exception( 'Exception while attempting to forward ports to "%s"', service_name) raise timeout = timeout or self.options.test_service_startup_timeout end_time = time.time() + timeout logging.info('Waiting on "%s..."', service_name) if port is None: port = self.__service_port_map[service_name] # It seems we have a race condition in the poll # where it thinks the jobs have terminated. # I've only seen this happen once. time.sleep(1) threadid = hex(threading.current_thread().ident) logging.info('WaitOn polling %s from thread %s', service_name, threadid) while forwarding.child.poll() is None: try: # localhost is hardcoded here because we are port forwarding. # timeout=20 is to appease kubectl port forwarding, which will close # if left idle for 30s urlopen('http://localhost:{port}/health'.format( port=forwarding.port), timeout=20) logging.info('"%s" is ready on port %d | %s', service_name, forwarding.port, threadid) return forwarding except HTTPError as error: logging.warning('%s got %s. Ignoring that for now.', service_name, error) return forwarding except (URLError, Exception) as error: if time.time() >= end_time: logging.error('Timing out waiting for %s | %s', service_name, threadid) raise_and_log_error( TimeoutError(service_name, cause=service_name)) time.sleep(2.0) logging.error( 'It appears %s is no longer available.' ' Perhaps the tunnel closed.', service_name) raise_and_log_error( ResponseError('It appears that {0} failed'.format(service_name), server='tunnel'))