From f38c0bd8a79bee6ea56262695534c89ae51fc61c Mon Sep 17 00:00:00 2001 From: Runar Ask Johannessen Date: Thu, 10 Oct 2024 10:44:27 +0200 Subject: [PATCH] Separate test that runs ert to own file --- tests/conftest.py | 48 ++++++++++++++++++++++--- tests/test_functions.py | 73 ------------------------------------- tests/test_with_ert.py | 79 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 78 deletions(-) create mode 100644 tests/test_with_ert.py diff --git a/tests/conftest.py b/tests/conftest.py index 8d7e731..7d885e6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,6 +33,17 @@ def set_up_tmp(path): return real0, eight_datafile, config_path +@pytest.fixture(scope="function", name="ert_run_scratch_files") +def _fix_ert_run_scratch_files(tmp_path): + return set_up_tmp(tmp_path / "scratch") + + +@pytest.fixture(scope="session", name="scratch_files") +def _fix_scratch_files(tmp_path_factory): + # tmp_path_factory is a fixture provided by pytest + return set_up_tmp(tmp_path_factory.mktemp("scratch")) + + @pytest.fixture(scope="session", name="token") def _fix_token(): token = os.environ.get("ACCESS_TOKEN") @@ -68,11 +79,6 @@ def _fix_sumo(token): return SumoClient(env="dev", token=token) -@pytest.fixture(scope="session", name="scratch_files") -def _fix_scratch_files(tmp_path_factory): - return set_up_tmp(tmp_path_factory.mktemp("scratch")) - - @pytest.fixture(autouse=True, scope="function", name="set_ert_env") def _fix_ert_env(monkeypatch): monkeypatch.setenv("_ERT_REALIZATION_NUMBER", "0") @@ -108,6 +114,38 @@ def _fix_register(scratch_files, token): return sumo_uuid +@pytest.fixture(scope="function", name="ert_run_case_uuid") +def _fix_ert_run_case_uuid(ert_run_scratch_files, token): + root = ert_run_scratch_files[0].parents[1] + case_metadata_path = root / "share/metadata/fmu_case.yml" + case_metadata = yaml_load(case_metadata_path) + case_metadata["fmu"]["case"]["uuid"] = str(uuid.uuid4()) + case_metadata["tracklog"][0] = { + "datetime": datetime.now().isoformat(), + "user": { + "id": "dbs", + }, + "event": "created", + } + with open(case_metadata_path, "w", encoding="utf-8") as stream: + yaml.safe_dump(case_metadata, stream) + sumo_conn = SumoConnection(env="dev", token=token) + case = CaseOnDisk( + case_metadata_path, + sumo_conn, + verbosity="DEBUG", + ) + # Register the case in Sumo + sumo_uuid = case.register() + yield sumo_uuid + + # Teardown + try: + sumo_conn.delete(f"/objects('{sumo_uuid}')") + except HTTPStatusError: + print(f"{sumo_uuid} Already gone..") + + @pytest.fixture(scope="session", name="xtgeogrid") def _fix_xtgeogrid(): """Export egrid file to sumo diff --git a/tests/test_functions.py b/tests/test_functions.py index e9151eb..646bfae 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -1,10 +1,8 @@ """Test utility ecl2csv""" import os -from pathlib import Path from numpy.ma import allclose, allequal from shutil import copytree -from subprocess import PIPE, Popen from time import sleep from io import BytesIO import pandas as pd @@ -72,42 +70,6 @@ def check_sumo(case_uuid, tag_prefix, correct, class_type, sumo): sumo.delete(path, query) -def write_ert_config_and_run(runpath): - ert_config_path = "sim2sumo.ert" - encoding = "utf-8" - ert_full_config_path = runpath / ert_config_path - print(f"Running with path {ert_full_config_path}") - with open(ert_full_config_path, "w", encoding=encoding) as stream: - - stream.write( - f"DEFINE dev\nNUM_REALIZATIONS 1\nMAX_SUBMIT 1\nRUNPATH {runpath}\nFORWARD_MODEL SIM2SUMO" - ) - with Popen( - ["ert", "test_run", str(ert_full_config_path)], - stdout=PIPE, - stderr=PIPE, - ) as process: - stdout, stderr = process.communicate() - - print( - f"After ert run all these files where found at runpath {[item.name for item in list(Path(runpath).glob('*'))]}" - ) - if stdout: - print("stdout:", stdout.decode(encoding), sep="\n") - if stderr: - print("stderr:", stderr.decode(encoding), sep="\n") - try: - error_content = Path(runpath / "ERROR").read_text(encoding=encoding) - except FileNotFoundError: - error_content = "" - assert ( - not error_content - ), f"ERROR file found with content:\n{error_content}" - assert Path( - runpath / "OK" - ).is_file(), f"running {ert_full_config_path}, No OK file" - - def check_expected_exports(expected_exports, shared_grid, prefix): parameters = list(shared_grid.glob(f"*--{prefix.lower()}-*.roff")) meta = list(shared_grid.glob(f"*--{prefix.lower()}-*.roff.yml")) @@ -398,41 +360,6 @@ def test_convert_to_arrow(): assert isinstance(table, pa.Table), "Did not convert to table" -def test_sim2sumo_with_ert(scratch_files, case_uuid, sumo, monkeypatch): - monkeypatch.chdir(scratch_files[0]) - real0 = scratch_files[0] - write_ert_config_and_run(real0) - expected_exports = 88 - path = f"/objects('{case_uuid}')/search" - results = sumo.post( - path, - json={ - "query": { - "bool": { - "must_not": [ - { - "terms": { - "class.keyword": [ - "case", - "iteration", - "realization", - ] - } - } - ], - } - }, - "size": 0, - "track_total_hits": True, - }, - ).json() - - returned = results["hits"]["total"]["value"] - assert ( - returned == expected_exports - ), f"Supposed to upload {expected_exports}, but actual were {returned}" - - @pytest.mark.parametrize("real,nrdfiles", [(REEK_REAL0, 2), (REEK_REAL1, 5)]) def test_find_datafiles_reek(real, nrdfiles): os.chdir(real) diff --git a/tests/test_with_ert.py b/tests/test_with_ert.py new file mode 100644 index 0000000..72de670 --- /dev/null +++ b/tests/test_with_ert.py @@ -0,0 +1,79 @@ +# When ERT runs it makes changes to the files. This can cause issues for other tests if they expect certain files to exist etc. +# Tests that run ERT should therefore create their own temporary file structure, completely separate from other tests. +from pathlib import Path + +from subprocess import PIPE, Popen + + +def write_ert_config_and_run(runpath): + ert_config_path = "sim2sumo.ert" + encoding = "utf-8" + ert_full_config_path = runpath / ert_config_path + print(f"Running with path {ert_full_config_path}") + with open(ert_full_config_path, "w", encoding=encoding) as stream: + + stream.write( + f"DEFINE dev\nNUM_REALIZATIONS 1\nMAX_SUBMIT 1\nRUNPATH {runpath}\nFORWARD_MODEL SIM2SUMO" + ) + with Popen( + ["ert", "test_run", str(ert_full_config_path)], + stdout=PIPE, + stderr=PIPE, + ) as process: + stdout, stderr = process.communicate() + + print( + f"After ert run all these files where found at runpath {[item.name for item in list(Path(runpath).glob('*'))]}" + ) + if stdout: + print("stdout:", stdout.decode(encoding), sep="\n") + if stderr: + print("stderr:", stderr.decode(encoding), sep="\n") + try: + error_content = Path(runpath / "ERROR").read_text(encoding=encoding) + except FileNotFoundError: + error_content = "" + assert ( + not error_content + ), f"ERROR file found with content:\n{error_content}" + assert Path( + runpath / "OK" + ).is_file(), f"running {ert_full_config_path}, No OK file" + + +def test_sim2sumo_with_ert( + ert_run_scratch_files, ert_run_case_uuid, sumo, monkeypatch +): + monkeypatch.chdir(ert_run_scratch_files[0]) + real0 = ert_run_scratch_files[0] + # After this the files in the current directory are changed and parameters.txt no longer exists + write_ert_config_and_run(real0) + expected_exports = 88 + path = f"/objects('{ert_run_case_uuid}')/search" + results = sumo.post( + path, + json={ + "query": { + "bool": { + "must_not": [ + { + "terms": { + "class.keyword": [ + "case", + "iteration", + "realization", + ] + } + } + ], + } + }, + "size": 0, + "track_total_hits": True, + }, + ).json() + + returned = results["hits"]["total"]["value"] + assert ( + returned == expected_exports + ), f"Supposed to upload {expected_exports}, but actual were {returned}"