def test_update_app_config(self): config = AppConfig() config.update_server_config(app__verbose=True, multi_dataset__dataroot="datadir") vars = config.server_config.changes_from_default() self.assertCountEqual(vars, [("app__verbose", True, False), ("multi_dataset__dataroot", "datadir", None)]) config = AppConfig() config.update_default_dataset_config(app__scripts=(), app__inline_scripts=()) vars = config.server_config.changes_from_default() self.assertCountEqual(vars, []) config = AppConfig() config.update_default_dataset_config(app__scripts=[], app__inline_scripts=[]) vars = config.default_dataset_config.changes_from_default() self.assertCountEqual(vars, []) config = AppConfig() config.update_default_dataset_config(app__scripts=("a", "b"), app__inline_scripts=["c", "d"]) vars = config.default_dataset_config.changes_from_default() self.assertCountEqual(vars, [("app__scripts", ["a", "b"], []), ("app__inline_scripts", ["c", "d"], [])])
def main(): parser = argparse.ArgumentParser( "A script to check hosted configuration files") parser.add_argument("config_file", help="the configuration file") parser.add_argument( "-s", "--show", default=False, action="store_true", help= "print the configuration. NOTE: this may print secret values to stdout", ) args = parser.parse_args() app_config = AppConfig() try: app_config.update_from_config_file(args.config_file) app_config.complete_config() except Exception as e: print(f"Error: {str(e)}") print("FAIL:", args.config_file) sys.exit(1) if args.show: yaml_config = app_config.config_to_dict() yaml.dump(yaml_config, sys.stdout) print("PASS:", args.config_file) sys.exit(0)
def test_handle_data_source__errors_when_passed_zero_or_two_dataroots(self): file_name = self.custom_app_config( dataroot=f"{FIXTURES_ROOT}", config_file_name="two_data_roots.yml", dataset_datapath=f"{FIXTURES_ROOT}/pbmc3k-CSC-gz.h5ad", ) config = AppConfig() config.update_from_config_file(file_name) with self.assertRaises(ConfigurationError): config.server_config.handle_data_source() file_name = self.custom_app_config(config_file_name="zero_roots.yml") config = AppConfig() config.update_from_config_file(file_name) with self.assertRaises(ConfigurationError): config.server_config.handle_data_source()
def data_with_tmp_tiledb_annotations(ext: MatrixDataType): tmp_dir = tempfile.mkdtemp() fname = { MatrixDataType.H5AD: f"{PROJECT_ROOT}/example-dataset/pbmc3k.h5ad", MatrixDataType.CXG: "test/fixtures/pbmc3k.cxg", }[ext] data_locator = DataLocator(fname) config = AppConfig() config.update_server_config( app__flask_secret_key="secret", multi_dataset__dataroot=data_locator.path, authentication__type="test", authentication__insecure_test_environment=True, ) config.update_default_dataset_config( embeddings__names=["umap"], presentation__max_categories=100, diffexp__lfc_cutoff=0.01, user_annotations__type="hosted_tiledb_array", user_annotations__hosted_tiledb_array__db_uri="postgresql://*****:*****@localhost:5432", user_annotations__hosted_tiledb_array__hosted_file_directory=tmp_dir, ) config.complete_config() data = MatrixDataLoader(data_locator.abspath()).open(config) annotations = AnnotationsHostedTileDB(tmp_dir, DbUtils("postgresql://*****:*****@localhost:5432"),) return data, tmp_dir, annotations
def test_config(self): check_config_script = os.path.join(PROJECT_ROOT, "backend", "czi_hosted", "eb", "check_config.py") with tempfile.TemporaryDirectory() as tempdir: configfile = os.path.join(tempdir, "config.yaml") app_config = AppConfig() app_config.update_server_config( multi_dataset__dataroot=f"{FIXTURES_ROOT}") app_config.write_config(configfile) command = ["python", check_config_script, configfile] # test failure mode (flask_secret_key not set) env = os.environ.copy() env.pop("CXG_SECRET_KEY", None) with self.assertRaises( subprocess.CalledProcessError) as exception_context: subprocess.check_output(command, env=env) output = str(exception_context.exception.stdout, "utf-8") self.assertTrue( output.startswith( "Error: Invalid type for attribute: app__flask_secret_key, expected type str, got NoneType" )) self.assertEqual(exception_context.exception.returncode, 1) # test passing case env = os.environ.copy() env["CXG_SECRET_KEY"] = "secret" output = subprocess.check_output(command, env=env) output = str(output, "utf-8") self.assertTrue(output.startswith("PASS"))
def data_with_tmp_annotations(ext: MatrixDataType, annotations_fixture=False): tmp_dir = tempfile.mkdtemp() annotations_file = path.join(tmp_dir, "test_annotations.csv") if annotations_fixture: shutil.copyfile(f"{FIXTURES_ROOT}/pbmc3k-annotations.csv", annotations_file) fname = { MatrixDataType.H5AD: f"{PROJECT_ROOT}/example-dataset/pbmc3k.h5ad", MatrixDataType.CXG: f"{FIXTURES_ROOT}/pbmc3k.cxg", }[ext] data_locator = DataLocator(fname) config = AppConfig() config.update_server_config( app__flask_secret_key="secret", single_dataset__obs_names=None, single_dataset__var_names=None, single_dataset__datapath=data_locator.path, ) config.update_default_dataset_config( embeddings__names=["umap"], presentation__max_categories=100, diffexp__lfc_cutoff=0.01, ) config.complete_config() data = MatrixDataLoader(data_locator.abspath()).open(config) annotations = AnnotationsLocalFile(None, annotations_file) return data, tmp_dir, annotations
def test_configfile_no_dataset_section(self): # test a config file without a dataset section with tempfile.TemporaryDirectory() as tempdir: configfile = os.path.join(tempdir, "config.yaml") with open(configfile, "w") as fconfig: config = """ server: app: flask_secret_key: secret multi_dataset: dataroot: test_dataroot """ fconfig.write(config) app_config = AppConfig() app_config.update_from_config_file(configfile) server_changes = app_config.server_config.changes_from_default() dataset_changes = app_config.default_dataset_config.changes_from_default( ) self.assertEqual( server_changes, [("app__flask_secret_key", "secret", None), ("multi_dataset__dataroot", "test_dataroot", None)], ) self.assertEqual(dataset_changes, [])
def test_auth_oauth_session(self): # test with session cookies app_config = AppConfig() app_config.update_server_config(app__flask_secret_key="secret") app_config.update_server_config( authentication__params_oauth__session_cookie=True, ) self.auth_flow(app_config)
def get_config(self, **kwargs): file_name = self.custom_app_config( dataroot=f"{FIXTURES_ROOT}", config_file_name=self.config_file_name, **kwargs ) config = AppConfig() config.update_from_config_file(file_name) return config
def test_environment_variable_errors(self): # no name app_config = AppConfig() app_config.external_config.environment = [ dict(required=True, path=["this", "is", "a", "path"]) ] with self.assertRaises(ConfigurationError) as config_error: app_config.complete_config() self.assertEqual(config_error.exception.message, "environment: 'name' is missing") # required has wrong type app_config = AppConfig() app_config.external_config.environment = [ dict(name="myenvar", required="optional", path=["this", "is", "a", "path"]) ] with self.assertRaises(ConfigurationError) as config_error: app_config.complete_config() self.assertEqual(config_error.exception.message, "environment: 'required' must be a bool") # no path app_config = AppConfig() app_config.external_config.environment = [ dict(name="myenvar", required=True) ] with self.assertRaises(ConfigurationError) as config_error: app_config.complete_config() self.assertEqual(config_error.exception.message, "environment: 'path' is missing") # required environment variable is not set app_config = AppConfig() app_config.external_config.environment = [ dict(name="THIS_ENV_IS_NOT_SET", required=True, path=["this", "is", "a", "path"]) ] with self.assertRaises(ConfigurationError) as config_error: app_config.complete_config() self.assertEqual( config_error.exception.message, "required environment variable 'THIS_ENV_IS_NOT_SET' not set")
def test_config_for_single_dataset(self): file_name = self.custom_app_config( config_file_name="single_dataset.yml", dataset_datapath=f"{FIXTURES_ROOT}/pbmc3k.cxg" ) config = AppConfig() config.update_from_config_file(file_name) config.server_config.handle_single_dataset(self.context) self.assertIsNotNone(config.server_config.matrix_data_cache_manager) file_name = self.custom_app_config( config_file_name="single_dataset_with_about.yml", about="www.cziscience.com", dataset_datapath=f"{FIXTURES_ROOT}/pbmc3k.cxg", ) config = AppConfig() config.update_from_config_file(file_name) with self.assertRaises(ConfigurationError): config.server_config.handle_single_dataset(self.context)
def setUp(self): self.data_file = DataLocator( f"{PROJECT_ROOT}/example-dataset/pbmc3k.h5ad") config = AppConfig() config.update_server_config( single_dataset__datapath=self.data_file.path) config.update_server_config(app__flask_secret_key="secret") config.complete_config() self.data = AnndataAdaptor(self.data_file, config)
def test_mapping_creation_returns_map_of_server_and_dataset_config(self): config = AppConfig() mapping = config.default_dataset_config.create_mapping( config.default_config) self.assertIsNotNone(mapping["server__app__verbose"]) self.assertIsNotNone(mapping["dataset__presentation__max_categories"]) self.assertIsNotNone( mapping["dataset__user_annotations__ontology__obo_location"]) self.assertIsNotNone( mapping["server__multi_dataset__allowed_matrix_types"])
def test_get_dataset_config_returns_default_dataset_config_for_single_datasets( self): datapath = f"{FIXTURES_ROOT}/1e4dfec4-c0b2-46ad-a04e-ff3ffb3c0a8f.h5ad" file_name = self.custom_app_config( dataset_datapath=datapath, config_file_name=self.config_file_name) config = AppConfig() config.update_from_config_file(file_name) self.assertEqual(config.get_dataset_config(""), config.default_dataset_config)
def test_handle_adaptor(self, mock_tiledb_context): custom_config = self.custom_app_config( dataroot=f"{FIXTURES_ROOT}", cxg_tile_cache_size=10, cxg_num_reader_threads=2 ) config = AppConfig() config.update_from_config_file(custom_config) config.server_config.handle_adaptor() mock_tiledb_context.assert_called_once_with( {"sm.tile_cache_size": 10, "sm.num_reader_threads": 2, "vfs.s3.region": "us-east-1"} )
def test_handle_app___can_use_envar_port(self): config = self.get_config(port=24) self.assertEqual(config.server_config.app__port, 24) # Note if the port is set in the config file it will NOT be overwritten by a different envvar os.environ["CXG_SERVER_PORT"] = "4008" self.config = AppConfig() self.config.update_server_config(app__flask_secret_key="secret") self.config.server_config.handle_app(self.context) self.assertEqual(self.config.server_config.app__port, 4008) del os.environ["CXG_SERVER_PORT"]
def test_auth_oauth_cookie(self): # test with specified cookie app_config = AppConfig() app_config.update_server_config(app__flask_secret_key="secret") app_config.update_server_config( authentication__params_oauth__session_cookie=False, authentication__params_oauth__cookie=dict(key="test_cxguser", httponly=True, max_age=60), ) self.auth_flow(app_config, "test_cxguser")
def test_aws_secrets_manager(self, mock_get_secret_key): mock_get_secret_key.return_value = { "oauth_client_secret": "mock_oauth_secret", "db_uri": "mock_db_uri", } configfile = self.custom_external_config( aws_secrets_manager_region="us-west-2", aws_secrets_manager_secrets=[ dict( name="my_secret", values=[ dict(key="flask_secret_key", path=["server", "app", "flask_secret_key"], required=False), dict( key="db_uri", path=[ "dataset", "user_annotations", "hosted_tiledb_array", "db_uri" ], required=True, ), dict( key="oauth_client_secret", path=[ "server", "authentication", "params_oauth", "client_secret" ], required=True, ), ], ) ], config_file_name="secret_external_config.yaml", ) app_config = AppConfig() app_config.update_from_config_file(configfile) app_config.server_config.single_dataset__datapath = f"{FIXTURES_ROOT}/pbmc3k.cxg" app_config.server_config.app__flask_secret_key = "original" app_config.server_config.single_dataset__datapath = f"{FIXTURES_ROOT}/pbmc3k.cxg" app_config.complete_config() self.assertEqual(app_config.server_config.app__flask_secret_key, "original") self.assertEqual( app_config.server_config. authentication__params_oauth__client_secret, "mock_oauth_secret") self.assertEqual( app_config.default_dataset_config. user_annotations__hosted_tiledb_array__db_uri, "mock_db_uri")
def test_handle_embeddings__checks_data_file_types(self): file_name = self.custom_app_config( embedding_names=["name1", "name2"], enable_reembedding="true", dataset_datapath=f"{FIXTURES_ROOT}/pbmc3k-CSC-gz.h5ad", anndata_backed="true", config_file_name=self.config_file_name, ) config = AppConfig() config.update_from_config_file(file_name) config.server_config.complete_config(self.context) with self.assertRaises(ConfigurationError): config.default_dataset_config.handle_embeddings()
def get_basic_config(self): config = AppConfig() config.update_server_config( single_dataset__obs_names=None, single_dataset__var_names=None, ) config.update_server_config(app__flask_secret_key="secret") config.update_default_dataset_config( embeddings__names=["umap"], presentation__max_categories=100, diffexp__lfc_cutoff=0.01, ) return config
def test_handle_diffexp(self, mock_tiledb_config): custom_config_file = self.custom_app_config( dataroot=f"{FIXTURES_ROOT}", cpu_multiplier=3, diffexp_max_workers=1, target_workunit=4, config_file_name=self.config_file_name, ) config = AppConfig() config.update_from_config_file(custom_config_file) config.server_config.handle_diffexp() # called with the min of diffexp_max_workers and cpus*cpu_multiplier mock_tiledb_config.assert_called_once_with(1, 4)
def test_handle_data_locator_can_read_from_dataroot(self, mock_discover_region_name): mock_discover_region_name.return_value = "us-west-2" dataroot = { "d1": {"base_url": "set1", "dataroot": "/path/to/set1_datasets/"}, "d2": {"base_url": "set2/subdir", "dataroot": "s3://hosted-cellxgene-dev"}, } file_name = self.custom_app_config( dataroot=dataroot, config_file_name=self.config_file_name, data_locater_region_name="true" ) config = AppConfig() config.update_from_config_file(file_name) config.server_config.handle_data_locator() self.assertEqual(config.server_config.data_locator__s3__region_name, "us-west-2") mock_discover_region_name.assert_called_once_with("s3://hosted-cellxgene-dev")
def test_dict_update_single_config_from_path_and_value(self): """Update a config parameter that has a value of dict""" # the path leads to a dict config param, set the config parameter to the new value config = AppConfig() config.update_single_config_from_path_and_value( ["server", "authentication", "params_oauth", "cookie"], dict(key="mykey1", max_age=100)) self.assertEqual( config.server_config.authentication__params_oauth__cookie, dict(key="mykey1", max_age=100)) # the path leads to an entry within a dict config param, the value is simple config = AppConfig() config.server_config.authentication__params_oauth__cookie = dict( key="mykey1", max_age=100) config.update_single_config_from_path_and_value( ["server", "authentication", "params_oauth", "cookie", "httponly"], True, ) self.assertEqual( config.server_config.authentication__params_oauth__cookie, dict(key="mykey1", max_age=100, httponly=True))
def test_get_default_config_correctly_reads_default_config_file(self): app_default_config = AppConfig().default_config expected_config = yaml.load(default_config, Loader=yaml.Loader) server_config = app_default_config["server"] dataset_config = app_default_config["dataset"] expected_server_config = expected_config["server"] expected_dataset_config = expected_config["dataset"] self.assertDictEqual(app_default_config, expected_config) self.assertDictEqual(server_config, expected_server_config) self.assertDictEqual(dataset_config, expected_dataset_config)
def setUp(self): self.config_file_name = f"{unittest.TestCase.id(self).split('.')[-1]}.yml" self.config = AppConfig() self.config.update_server_config(app__flask_secret_key="secret") self.config.update_server_config(multi_dataset__dataroot=FIXTURES_ROOT) self.dataset_config = self.config.default_dataset_config self.config.complete_config() message_list = [] def noop(message): message_list.append(message) messagefn = noop self.context = dict(messagefn=messagefn, messages=message_list)
def test_access_error(self): with tempfile.TemporaryDirectory() as dirname: self.make_temporay_datasets(dirname, 1) app_config = AppConfig() m = MatrixDataCacheManager(max_cached=3, timelimit_s=1) # use the 0 datasets self.use_dataset(m, dirname, app_config, 0) self.check_datasets(m, dirname, [0]) # use the 0 datasets, but this time a DatasetAccessError is raised. # verify that dataset is removed from the cache. self.use_dataset_with_error(m, dirname, app_config, 0) self.check_datasets(m, dirname, [])
def test_auth_test_single(self): app_config = AppConfig() app_config.update_server_config(app__flask_secret_key="secret") app_config.update_server_config( authentication__type="test", single_dataset__datapath=f"{self.dataset_dataroot}/pbmc3k.cxg") app_config.update_server_config( authentication__insecure_test_environment=True) app_config.complete_config() with test_server(app_config=app_config) as server: session = requests.Session() config = session.get(f"{server}/api/v0.2/config").json() userinfo = session.get(f"{server}/api/v0.2/userinfo").json() self.assertFalse(userinfo["userinfo"]["is_authenticated"]) self.assertIsNone(userinfo["userinfo"]["username"]) self.assertTrue( config["config"]["authentication"]["requires_client_login"]) self.assertTrue(config["config"]["parameters"]["annotations"]) login_uri = config["config"]["authentication"]["login"] logout_uri = config["config"]["authentication"]["logout"] self.assertEqual(login_uri, "/login") self.assertEqual(logout_uri, "/logout") response = session.get(f"{server}/{login_uri}") # check that the login redirect worked self.assertEqual(response.history[0].status_code, 302) self.assertEqual(response.url, f"{server}/") config = session.get(f"{server}/api/v0.2/config").json() userinfo = session.get(f"{server}/api/v0.2/userinfo").json() self.assertTrue(userinfo["userinfo"]["is_authenticated"]) self.assertEqual(userinfo["userinfo"]["username"], "test_account") self.assertTrue(config["config"]["parameters"]["annotations"]) response = session.get(f"{server}/{logout_uri}") # check that the logout redirect worked self.assertEqual(response.history[0].status_code, 302) self.assertEqual(response.url, f"{server}/") config = session.get(f"{server}/api/v0.2/config").json() userinfo = session.get(f"{server}/api/v0.2/userinfo").json() self.assertFalse(userinfo["userinfo"]["is_authenticated"]) self.assertIsNone(userinfo["userinfo"]["username"]) self.assertTrue(config["config"]["parameters"]["annotations"])
def app_config(data_locator, backed=False, extra_server_config={}, extra_dataset_config={}): config = AppConfig() config.update_server_config( app__flask_secret_key="secret", single_dataset__obs_names=None, single_dataset__var_names=None, adaptor__anndata_adaptor__backed=backed, single_dataset__datapath=data_locator, limits__diffexp_cellcount_max=None, limits__column_request_max=None, ) config.update_default_dataset_config( embeddings__names=["umap", "tsne", "pca"], presentation__max_categories=100, diffexp__lfc_cutoff=0.01 ) config.update_server_config(**extra_server_config) config.update_default_dataset_config(**extra_dataset_config) config.complete_config() return config
def test_auth_none(self): app_config = AppConfig() app_config.update_server_config(app__flask_secret_key="secret") app_config.update_server_config( authentication__type=None, multi_dataset__dataroot=self.dataset_dataroot) app_config.update_default_dataset_config( user_annotations__enable=False) app_config.complete_config() with test_server(app_config=app_config) as server: session = requests.Session() config = session.get( f"{server}/d/pbmc3k.cxg/api/v0.2/config").json() userinfo = session.get( f"{server}/d/pbmc3k.cxg/api/v0.2/userinfo").json() self.assertNotIn("authentication", config["config"]) self.assertIsNone(userinfo)
def test_handle_data_locator_works_for_default_types(self, mock_discover_region_name): mock_discover_region_name.return_value = None # Default config self.assertEqual(self.config.server_config.data_locator__s3__region_name, None) # hard coded config = self.get_config() self.assertEqual(config.server_config.data_locator__s3__region_name, "us-east-1") # incorrectly formatted dataroot = { "d1": {"base_url": "set1", "dataroot": "/path/to/set1_datasets/"}, "d2": {"base_url": "set2/subdir", "dataroot": "s3://shouldnt/work"}, } file_name = self.custom_app_config( dataroot=dataroot, config_file_name=self.config_file_name, data_locater_region_name="true" ) config = AppConfig() config.update_from_config_file(file_name) with self.assertRaises(ConfigurationError): config.server_config.handle_data_locator()