def check_server(self, path, msg, max_attempts): attempts = 0 backoff = 1 while True: try: body, code = self.client.request('GET', path) if (code == 200) and ("true" in body): return if attempts > max_attempts: raise error.StripeError( "The master search server does not seem to be responding " + "to /healthcheck. Try running bin/local-start-servers " + "manually to make sure that the servers start up correctly." ) except error.HTTPConnectionError as e: pass attempts += 1 backoff *= 2 util.logger.info('(# %i) Sleeping for %is while server %s' % (attempts, backoff, msg)) time.sleep(backoff)
def run_input(self, cmd_line_args): # Don't print out to stdout. options_dict = TestCaseGenerator.opt_parse( map(lambda x: str(x), cmd_line_args)) options_dict['dictionary_path'] = self.dictionary_path options_dict['should_print'] = False test_case_input = TestCaseGenerator(options_dict).generate_test_case() files = test_case_input['files'] keys = test_case_input['keys'] path = self.TEST_CASE_PATH util.logger.info('Writing tree to %s', path) self.write_files(files, path) util.logger.info('Starting servers') self.start_servers() util.logger.info('Waiting for server to come up') self.check_server(self.uri('/healthcheck'), 'starts', 3) util.logger.info('Indexing %s', path) self.index(path) util.logger.info('Waiting for servers to finish indexing') self.check_server(self.uri('/isIndexed'), 'indexes', 8) responses = [] start_time = time.time() for key in keys: body, code = self.execute_query(key) try: parsed = util.json.loads(body) responses.append([parsed['results'], code]) except: raise error.StripeError( 'The search for %s returned invalid JSON: %s' % (key, body)) end_time = time.time() average_response_time = (end_time - start_time) / len(keys) return { 'wall_clock_time': average_response_time, 'output': map(lambda x: x[0], responses), 'input': cmd_line_args, 'level': self.LEVEL, 'exitstatus': 0, }
def fetch_s3_resource(self, url): try: content, status_code = self.http_client.request("get", url) except error.HTTPConnectionError: err = util.exception_as() msg = ("There was an error while connecting to fetch " "the url %s. Please check your connectivity. If there " "continues to be an issue, please let us know at " "[email protected]. The specific error is:\n" % (url,)) raise error.StripeError(msg + str(err)) if status_code == 200: return content elif status_code == 403: msg = ("We received a 403 while fetching the url %s. " "This probably means that you are trying to get " "something that doesn't actually exist." % (url,)) raise error.StripeError(msg) else: msg = ("We received the unexpected response code %i while " "fetching the url %s." % (status_code, url,)) raise error.StripeError(msg)
def __init__(self, ids_or_urls=[], options={}): util.mkdir_p(self.test_cases_path()) if not os.path.isfile(http_client.certs_path()): msg = ("You seem to have deleted the file of certificates " "that shipped with this repo. It should exist " "at %s" % http_client.certs_path()) raise error.StripeError(msg) if ids_or_urls == []: util.logger.info('No test case supplied. Randomly choosing among defaults.') ids_or_urls = [SystemRandom().choice(self.DEFAULT_TEST_CASES)] self.test_cases = map(lambda token: TestCase(self, token), ids_or_urls) self.options = options headers = { 'User-Agent': 'Stripe TestHarness/%s' % (self.VERSION,), } self.http_client = http_client.new_default_http_client(headers=headers, verify_ssl_certs=True)
def check_server(self, path, msg, max_attempts): attempts = 0 backoff = 1 while True: try: if attempts > max_attempts: raise error.StripeError("Unable to start server up") body, code = self.client.request('GET', path) if (code == 200) and ("true" in body): return except error.HTTPConnectionError as e: attempts += 1 backoff *= 2 util.logger.info('(# %i) Sleeping for %is while server %s' % (attempts, backoff, msg)) time.sleep(backoff)
def load(self): if self.json: return self.json try: f = open(self.dump_path(), "r") self.json = util.json.load(f) f.close() return self.json except IOError: pass util.logger.info('Fetching. URL: %s', self.url) content = self.harness.fetch_s3_resource(self.url) try: self.json = util.json.loads(content) except ValueError: # Decoding the JSON failed. msg = ("There was a problem parsing the test case. We expected " "JSON. We received: %s" % (content,)) raise error.StripeError(msg) return self.json