def _service_check(args: Any) -> None: if args.service_name == "storage": try: Storage.connect(timeout=args.timeout) return # No exception ocurred, success! except TimeoutError: sys.exit("ERROR: Ert storage not found!") raise SystemExit(f"{args.service_name} not implemented")
def test_cli_local_test_run(tmpdir): with chdir(tmpdir): with Storage.start_server(): args = ["ert3", "init", "--example", "polynomial"] with patch.object(sys, "argv", args): ert3.console._console._main() os.chdir("polynomial") # Error testing is performed in this context to reduce # test time as a full storage server is involved. with pytest.raises(ert.exceptions.ExperimentError): with patch.object( sys, "argv", ["ert3", "run", "sensitivity", "--local-test-run"] ): ert3.console._console._main() with patch.object( sys, "argv", ["ert3", "run", "evaluation", "--local-test-run"] ): ert3.console._console._main() experiments = [ experiment for experiment in ert.storage.get_experiment_names( workspace_name="polynomial" ) if experiment.startswith("evaluation-") ] assert len(experiments) == 1 run_id = experiments[0].split("-")[1] assert pathlib.Path(f"local-test-run-{run_id}/output.json").exists()
def test_check_service(tmpdir, monkeypatch): with tmpdir.as_cwd(): with patch.object( sys, "argv", ["ert3", "service", "check", "storage", "--timeout", "10"]): with Storage.start_server(): ert3.console._console._main()
def start_ert_server(mode: str): if mode in ("api", "vis") or not FeatureToggling.is_enabled("new-storage"): yield return with Storage.start_server(): yield
def run_ert_storage(args): kwargs = {"res_config": args.config} if args.database_url is not None: kwargs["database_url"] = args.database_url with Storage.start_server(**kwargs) as server: server.wait()
def test_integration(tmp_path, monkeypatch): """Actually start the server, wait for it to be online and do a health check""" monkeypatch.chdir(tmp_path) # Note: Sqlite needs at least 4-5 seconds to spin up even on # an unloaded M1-based Mac using local disk. On the CI-server # we have less control of available resources, so set timeout- # value large to allow time for sqlite to get ready with Storage.start_server(timeout=120) as server: resp = requests.get(f"{server.fetch_url()}/healthcheck", auth=server.fetch_auth()) assert "ALL OK!" in resp.json() with Storage.session() as session: session.get("/healthcheck") assert not (tmp_path / "storage_server.json").exists()
def test_cli_local_test_run_specific_realization(tmpdir): with chdir(tmpdir): with Storage.start_server(): args = ["ert3", "init", "--example", "polynomial"] with patch.object(sys, "argv", args): ert3.console._console._main() os.chdir("polynomial") with pytest.raises(ert.exceptions.ExperimentError): with patch.object( sys, "argv", ["ert3", "run", "evaluation", "--realization", "2"] ): ert3.console._console._main() poly_size = yaml.safe_load( pathlib.Path("experiments/evaluation/ensemble.yml").read_text( encoding="utf-8" ) )["size"] not_existing_realization = poly_size # zero-indexed realizations with pytest.raises( ert.exceptions.ConfigValidationError, match="Realization out of ensemble bounds", ): assert not_existing_realization > poly_size - 1 with patch.object( sys, "argv", [ "ert3", "run", "evaluation", "--local-test-run", "--realization", str(not_existing_realization), ], ): ert3.console._console._main() with patch.object( sys, "argv", ["ert3", "run", "evaluation", "--local-test-run", "--realization", "2"], ): ert3.console._console._main() experiments = [ experiment for experiment in ert.storage.get_experiment_names( workspace_name="polynomial" ) if experiment.startswith("evaluation-") ] assert len(experiments) == 1 run_id = experiments[0].split("-")[1] assert pathlib.Path(f"local-test-run-{run_id}/output.json").exists()
def run_webviz_ert(args): kwargs = {"res_config": args.config} if args.database_url is not None: kwargs["database_url"] = args.database_url with Storage.connect_or_start_server(**kwargs) as storage: storage.wait_until_ready() with WebvizErt.start_server() as webviz_ert: webviz_ert.wait()
def _get_from_server(url, headers={}, status_code=200) -> requests.Response: session = Storage.session() resp = session.get( url, headers=headers, ) if resp.status_code != status_code: logger.error(f"Failed to fetch from {url}. Response: {resp.text}") return resp
def workspace_integration(tmpdir): from ert_shared.services import Storage workspace_dir = pathlib.Path(tmpdir / "polynomial") workspace_dir.mkdir() with chdir(workspace_dir): with Storage.start_server(): workspace_obj = ert3.workspace.initialize(workspace_dir) ert.storage.init(workspace_name=workspace_obj.name) yield workspace_obj
def test_integration_timeout(tmp_path, monkeypatch): """Try to start the server but give it too small time to get ready and expect a timeout""" monkeypatch.chdir(tmp_path) with pytest.raises(TimeoutError): # Note timeout-value here in context of note above with Storage.start_server(timeout=0.01) as server: requests.get(f"{server.fetch_url()}/healthcheck", auth=server.fetch_auth()) assert not (tmp_path / "storage_server.json").exists()
def _put_to_server( path: str, headers: Optional[Dict[Any, Any]] = None, status_code: int = 200, **kwargs: Any, ) -> httpx.Response: if not headers: headers = {} with Storage.session() as session: resp = session.put(path, headers=headers, timeout=60, **kwargs) if resp.status_code != status_code: logger.error("Failed to put to %s. Response: %s", path, resp.text) return resp
def _delete_on_server( path: str, headers: Optional[Dict[Any, Any]] = None, status_code: int = 200 ) -> httpx.Response: if not headers: headers = {} with Storage.session() as session: resp = session.delete( path, headers=headers, timeout=60, ) if resp.status_code != status_code: logger.error("Failed to delete %s. Response: %s", path, resp.text) return resp
def run_webviz_ert(args): kwargs = {"res_config": args.config} if args.database_url is not None: kwargs["database_url"] = args.database_url with Storage.connect_or_start_server(**kwargs) as storage: storage.wait_until_ready() print(""" ----------------------------------------------------------- Starting up Webviz-ERT. This might take more than a minute. ----------------------------------------------------------- """) with WebvizErt.start_server() as webviz_ert: webviz_ert.wait()
def _post_to_server(url, data=None, params=None, json=None, headers={}, status_code=200) -> requests.Response: session = Storage.session() resp = session.post( url, headers=headers, params=params, data=data, json=json, ) if resp.status_code != status_code: logger.error(f"Failed to post to {url}. Response: {resp.text}") return resp
def _put(url: str, headers: Dict[str, Any], **kwargs: Any) -> httpx.Response: with Storage.session() as session: return session.put(url=url, headers=headers, timeout=60, **kwargs)
def _get(url: str, headers: Dict[str, Any]) -> httpx.Response: with Storage.session() as session: return session.get(url, headers=headers, timeout=60)
def get_info(): from ert_shared.services import Storage with Storage.start_server() as service: yield service.fetch_url(), service.fetch_auth()[1]
def get_info(project_id: Optional[os.PathLike] = None): client = Storage.connect(project=project_id) return { "baseurl": client.fetch_url(), "auth": client.fetch_auth(), }