def test_calls_metadata_endpoint(self, mock_get): """:func:`.metadata.retrieve` calls passed endpoint with GET.""" base = 'https://asdf.com/' app = create_ui_web_app() app.config['METADATA_ENDPOINT'] = base response = mock.MagicMock() with open('tests/data/docmeta.json') as f: mock_content = json.load(f) type(response).json = mock.MagicMock(return_value=mock_content) response.status_code = 200 mock_get.return_value = response with app.app_context(): docmeta_session = metadata.get_session() try: docmeta_session.retrieve('1602.00123') except Exception as e: self.fail('Choked on valid response: %s' % e) try: args, _ = mock_get.call_args except Exception as e: self.fail('Did not call requests.get as expected: %s' % e) self.assertTrue(args[0].startswith(base))
def application(environ, start_response): """WSGI application factory.""" for key, value in environ.items(): if type(value) is str: os.environ[key] = value app = create_ui_web_app() return app(environ, start_response)
def setUpClass(cls): """Spin up ES and index documents.""" os.environ["ELASTICSEARCH_SERVICE_HOST"] = "localhost" os.environ["ELASTICSEARCH_SERVICE_PORT"] = "9201" os.environ["ELASTICSEARCH_SERVICE_PORT_9201_PROTO"] = "http" os.environ["ELASTICSEARCH_VERIFY"] = "false" os.environ["KINESIS_STREAM"] = "MetadataIsAvailable" os.environ["KINESIS_SHARD_ID"] = "0" os.environ["KINESIS_CHECKPOINT_VOLUME"] = tempfile.mkdtemp() os.environ["KINESIS_ENDPOINT"] = "http://127.0.0.1:6568" os.environ["KINESIS_VERIFY"] = "false" os.environ["KINESIS_START_TYPE"] = "TRIM_HORIZON" print("pulling localstack image") _ = subprocess.run( "docker pull atlassianlabs/localstack", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, ) print("starting localstack") start_localstack = subprocess.run( "docker run -d -p 6568:4568 --name ltest atlassianlabs/localstack", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, ) if start_localstack.returncode != 0: raise RuntimeError( f"Could not start localstack: {start_localstack.stdout}." f" Is one already running? Is port 6568 available?") cls.ls_container = start_localstack.stdout.decode("ascii").strip() print(f"localstack started as {cls.ls_container}") cls.client = boto3.client( "kinesis", region_name="us-east-1", endpoint_url="http://localhost:6568", aws_access_key_id="foo", aws_secret_access_key="bar", verify=False, ) print("creating stream ahead of time, to populate with records") cls.client.create_stream(StreamName="MetadataIsAvailable", ShardCount=1) time.sleep(5) print("created stream, ready to test") cls.app = create_ui_web_app()
def application(environ, start_response): """WSGI application factory.""" for key, value in environ.items(): # In some deployment scenarios (e.g. uWSGI on k8s), uWSGI will pass in # the hostname as part of the request environ. This will usually just # be a container ID, which is not helpful for things like building # URLs. We want to keep ``SERVER_NAME`` explicitly configured, either # in config.py or via an os.environ var loaded by config.py. if key == 'SERVER_NAME': continue os.environ[key] = str(value) global __flask_app__ if __flask_app__ is None: __flask_app__ = create_ui_web_app() return __flask_app__(environ, start_response)
def setUpClass(cls): """Spin up ES and index documents.""" os.environ['ELASTICSEARCH_SERVICE_HOST'] = 'localhost' os.environ['ELASTICSEARCH_SERVICE_PORT'] = "9201" os.environ['ELASTICSEARCH_PORT_9201_PROTO'] = "http" os.environ['ELASTICSEARCH_VERIFY'] = 'false' os.environ['KINESIS_STREAM'] = 'MetadataIsAvailable' os.environ['KINESIS_SHARD_ID'] = '0' os.environ['KINESIS_CHECKPOINT_VOLUME'] = tempfile.mkdtemp() os.environ['KINESIS_ENDPOINT'] = 'http://127.0.0.1:6568' os.environ['KINESIS_VERIFY'] = 'false' os.environ['KINESIS_START_TYPE'] = 'TRIM_HORIZON' print('pulling localstack image') pull_localstack = subprocess.run( "docker pull atlassianlabs/localstack", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) print('starting localstack') start_localstack = subprocess.run( "docker run -d -p 6568:4568 --name ltest atlassianlabs/localstack", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if start_localstack.returncode != 0: raise RuntimeError( f'Could not start localstack: {start_localstack.stdout}.' f' Is one already running? Is port 6568 available?') cls.ls_container = start_localstack.stdout.decode('ascii').strip() print(f'localstack started as {cls.ls_container}') cls.client = boto3.client('kinesis', region_name='us-east-1', endpoint_url="http://localhost:6568", aws_access_key_id='foo', aws_secret_access_key='bar', verify=False) print('creating stream ahead of time, to populate with records') cls.client.create_stream(StreamName='MetadataIsAvailable', ShardCount=1) time.sleep(5) print('created stream, ready to test') cls.app = create_ui_web_app()
def test_calls_metadata_endpoint_roundrobin(self, mock_get): """:func:`.metadata.retrieve` calls passed endpoint with GET.""" base = ["https://asdf.com/", "https://asdf2.com/"] app = create_ui_web_app() app.config["METADATA_ENDPOINT"] = ",".join(base) app.config["METADATA_VERIFY_CERT"] = "False" response = mock.MagicMock() with open("tests/data/docmeta.json") as f: mock_content = json.load(f) type(response).json = mock.MagicMock(return_value=mock_content) response.status_code = 200 mock_get.return_value = response with app.app_context(): docmeta_session = metadata.get_session() try: docmeta_session.retrieve("1602.00123") except Exception as ex: self.fail("Choked on valid response: %s" % ex) try: args, _ = mock_get.call_args except Exception as ex: self.fail("Did not call requests.get as expected: %s" % ex) self.assertTrue( args[0].startswith(base[0]), "Expected call to %s" % base[0] ) try: docmeta_session.retrieve("1602.00124") except Exception as ex: self.fail("Choked on valid response: %s" % ex) try: args, _ = mock_get.call_args except Exception as ex: self.fail("Did not call requests.get as expected: %s" % ex) self.assertTrue( args[0].startswith(base[1]), "Expected call to %s" % base[1] )
def application(environ, start_response): """WSGI application factory.""" for key, value in environ.items(): # Copy string WSGI environ to os.environ. This is to get apache # SetEnv vars. It needs to be done before the call to # create_web_app() due to how config is setup from os in # search/config.py. # # In some deployment scenarios (e.g. uWSGI on k8s), uWSGI will pass in # the hostname as part of the request environ. This will usually just # be a container ID, which is not helpful for things like building # URLs. We want to keep ``SERVER_NAME`` explicitly configured, either # in config.py or via an os.environ var loaded by config.py. if key == "SERVER_NAME": continue if type(value) is str: os.environ[key] = value # 'global' actually means module scope, and that is exactly what # we want here. global __flask_app__ if __flask_app__ is None: __flask_app__ = create_ui_web_app() return __flask_app__(environ, start_response)
"""Use this to initialize the search index for testing.""" import json import click from search.factory import create_ui_web_app from search.services import index app = create_ui_web_app() app.app_context().push() @app.cli.command() def create_index(): """Initialize the search index.""" index.current_session().create_index() if __name__ == '__main__': create_index()
def setUp(self): """Initialize an app and install :class:`.Base`.""" self.app = create_ui_web_app() self.client = self.app.test_client()
def setUp(self): """Instantiate the UI application.""" self.app = create_ui_web_app() self.client = self.app.test_client()
def start_agent() -> None: """Start the record processor.""" app = create_ui_web_app() with app.app_context(): process_stream()