Skip to content

Commit

Permalink
Separate test that runs ert to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
equinor-ruaj committed Oct 10, 2024
1 parent 8a77c97 commit dc22cb0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 78 deletions.
48 changes: 43 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -108,6 +114,38 @@ def _fix_register(scratch_files, token):
return sumo_uuid


@pytest.fixture(scope="session", 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
Expand Down
73 changes: 0 additions & 73 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <SUMO_ENV> 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"))
Expand Down Expand Up @@ -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)
Expand Down
79 changes: 79 additions & 0 deletions tests/test_with_ert.py
Original file line number Diff line number Diff line change
@@ -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 <SUMO_ENV> 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}"

0 comments on commit dc22cb0

Please sign in to comment.