From 9a08f4e2fa8af6db211a334384b2fd958d6f19fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Therese=20Natter=C3=B8y?= <61694854+tnatt@users.noreply.github.com> Date: Wed, 8 May 2024 10:43:54 +0200 Subject: [PATCH] TST: Fix inproper environment framework (#647) --- tests/conftest.py | 15 +++++ .../test_objectdataprovider_class.py | 13 ++-- .../test_prerealization_surfaces.py | 65 ++++++++++++------- tests/test_units/test_rms_context.py | 6 +- 4 files changed, 66 insertions(+), 33 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2a092caf2..2fcc80dde 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,6 +74,21 @@ def _fmu_run1_env_variables(monkeypatch, usepath="", case_only=False): logger.debug("Setting env %s as %s", key, env_value) +def remove_ert_env(monkeypatch): + for key in ERTRUN_ENV_FULLRUN: + monkeypatch.delenv(key, raising=False) + + +def set_ert_env_forward(monkeypatch): + for key, val in ERTRUN_ENV_FORWARD.items(): + monkeypatch.setenv(key, val) + + +def set_ert_env_prehook(monkeypatch): + for key, val in ERTRUN_ENV_PREHOOK.items(): + monkeypatch.setenv(key, val) + + @pytest.fixture(scope="function") def fmurun(tmp_path_factory, monkeypatch, rootpath): """A tmp folder structure for testing; here a new fmurun without case metadata.""" diff --git a/tests/test_units/test_objectdataprovider_class.py b/tests/test_units/test_objectdataprovider_class.py index 8d1c58277..099b82261 100644 --- a/tests/test_units/test_objectdataprovider_class.py +++ b/tests/test_units/test_objectdataprovider_class.py @@ -14,6 +14,7 @@ ) from fmu.dataio.providers.objectdata._xtgeo import RegularSurfaceDataProvider +from ..conftest import remove_ert_env, set_ert_env_prehook from ..utils import inside_rms @@ -128,7 +129,7 @@ def test_objectdata_provider_factory_raises_on_unknown(edataobj1): def test_regsurf_preprocessed_observation( - fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf + fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch ): """Test generating pre-realization surfaces that comes to share/preprocessed. @@ -150,7 +151,7 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf): ) return edata, edata.export(regsurf) - def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): + def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath): """Run FMU workflow, using the preprocessed data as case data. When re-using metadata, the input object to dataio shall not be a XTGeo or @@ -161,9 +162,9 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): But it requires that valid metadata for that file is found. The rule for merging is currently defaulted to "preprocessed". """ - os.chdir(fmurun_w_casemetadata) + os.chdir(fmurun_prehook) - casepath = fmurun_w_casemetadata.parent.parent + casepath = fmurun_prehook edata = dataio.ExportData( config=rmsglobalconfig, fmu_context="case", @@ -174,6 +175,8 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): return edata.generate_metadata(surfacepath) # run two stage process + remove_ert_env(monkeypatch) edata, mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf) - case_meta = _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf) + set_ert_env_prehook(monkeypatch) + case_meta = _run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf) assert edata._metadata["data"] == case_meta["data"] diff --git a/tests/test_units/test_prerealization_surfaces.py b/tests/test_units/test_prerealization_surfaces.py index 67020c4cd..233e08eca 100644 --- a/tests/test_units/test_prerealization_surfaces.py +++ b/tests/test_units/test_prerealization_surfaces.py @@ -17,23 +17,25 @@ import pytest from fmu.dataio import _utils as utils +from ..conftest import remove_ert_env, set_ert_env_prehook from ..utils import inside_rms logger = logging.getLogger(__name__) -def test_regsurf_case_observation(fmurun_w_casemetadata, rmsglobalconfig, regsurf): +def test_regsurf_case_observation(fmurun_prehook, rmsglobalconfig, regsurf): """Test generating pre-realization surfaces that comes right to case. Notice the difference between this use-case and the 'preprocessed' example later! """ - logger.info("Active folder is %s", fmurun_w_casemetadata) + logger.info("Active folder is %s", fmurun_prehook) - os.chdir(fmurun_w_casemetadata) + os.chdir(fmurun_prehook) edata = dataio.ExportData( config=rmsglobalconfig, # read from global config fmu_context="case", + casepath=fmurun_prehook, name="mymap", content="depth", is_observation=True, @@ -50,7 +52,7 @@ def test_regsurf_case_observation(fmurun_w_casemetadata, rmsglobalconfig, regsur def test_regsurf_preprocessed_observation( - fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf + fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch ): """Test generating pre-realization surfaces that comes to share/preprocessed. @@ -84,7 +86,7 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf): return edata.export(regsurf) - def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): + def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath): """Run FMU workflow, using the preprocessed data as case data. When re-using metadata, the input object to dataio shall not be a XTGeo or @@ -95,10 +97,10 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): But it requires that valid metadata for that file is found. The rule for merging is currently defaulted to "preprocessed". """ - os.chdir(fmurun_w_casemetadata) - logger.info("Active folder is %s", fmurun_w_casemetadata) + os.chdir(fmurun_prehook) + logger.info("Active folder is %s", fmurun_prehook) - casepath = fmurun_w_casemetadata.parent.parent + casepath = fmurun_prehook edata = dataio.ExportData( config=rmsglobalconfig, # read from global config @@ -132,8 +134,10 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): ).exists() # run two stage process + remove_ert_env(monkeypatch) mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf) - _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf) + set_ert_env_prehook(monkeypatch) + _run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf) logger.info("Preprocessed surface is %s", mysurf) @@ -156,7 +160,7 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): ], ) def test_regsurf_preprocessed_obs_vary_name_tagname( - fmurun_w_casemetadata, + fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, @@ -166,6 +170,7 @@ def test_regsurf_preprocessed_obs_vary_name_tagname( tagname_merge, exproot1, exproot2, + monkeypatch, ): """Check that current name and/or tagname are propegated or updated.""" @@ -203,7 +208,7 @@ def _export_data_from_rms( return edata.export(regsurf) def _run_case_fmu( - fmurun_w_casemetadata, + fmurun_prehook, rmsglobalconfig, surfacepath, name_merge, @@ -212,13 +217,14 @@ def _run_case_fmu( ): """Run FMU workflow, using the preprocessed data on a subfolder.""" - os.chdir(fmurun_w_casemetadata) - logger.info("Active folder is %s", fmurun_w_casemetadata) + os.chdir(fmurun_prehook) + logger.info("Active folder is %s", fmurun_prehook) edata = dataio.ExportData( config=rmsglobalconfig, # read from global config fmu_context="case", content="depth", + casepath=fmurun_prehook, is_observation=True, name=name_merge, tagname=tagname_merge, @@ -229,11 +235,13 @@ def _run_case_fmu( metadata = edata.generate_metadata(surfacepath) assert metadata["file"]["relative_path"] == f"{prefix}/{exproot2}--{dates}.gri" + remove_ert_env(monkeypatch) mysurf = _export_data_from_rms( rmssetup, rmsglobalconfig, regsurf, name_pre, tagname_pre, exproot1 ) + set_ert_env_prehook(monkeypatch) _run_case_fmu( - fmurun_w_casemetadata, + fmurun_prehook, rmsglobalconfig, mysurf, name_merge, @@ -243,7 +251,7 @@ def _run_case_fmu( def test_regsurf_preprocessed_observation_subfolder( - fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf + fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch ): """As previous test, but with data using subfolder option. @@ -279,15 +287,16 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf): return edata.export(regsurf) - def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath, subf=None): + def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath, subf=None): """Run FMU workflow, using the preprocessed data on a subfolder.""" - os.chdir(fmurun_w_casemetadata) - logger.info("Active folder is %s", fmurun_w_casemetadata) + os.chdir(fmurun_prehook) + logger.info("Active folder is %s", fmurun_prehook) edata = dataio.ExportData( config=rmsglobalconfig, # read from global config fmu_context="case", + casepath=fmurun_prehook, content="depth", name="pre_v3", is_observation=True, @@ -307,9 +316,12 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath, subf=None assert "merged" in metadata["tracklog"][-1]["event"] # run two stage process + remove_ert_env(monkeypatch) mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf) - _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf) - _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, mysurf, subf="xxxx") + + set_ert_env_prehook(monkeypatch) + _run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf) + _run_case_fmu(fmurun_prehook, rmsglobalconfig, mysurf, subf="xxxx") @inside_rms @@ -356,7 +368,7 @@ def test_preprocessed_with_rel_forcefolder_ok(rmssetup, rmsglobalconfig, regsurf def test_access_settings_retained( - fmurun_w_casemetadata, rmssetup, rmsglobalconfig, regsurf + fmurun_prehook, rmssetup, rmsglobalconfig, regsurf, monkeypatch ): """Test that access level put on pre-processed data are retained when the metadata is being completed during later FMU run. @@ -388,15 +400,16 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf): return edata.export(regsurf) - def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): + def _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath): """Run FMU workflow, test that access is retained from preprocessed.""" - os.chdir(fmurun_w_casemetadata) - logger.info("Active folder is %s", fmurun_w_casemetadata) + os.chdir(fmurun_prehook) + logger.info("Active folder is %s", fmurun_prehook) edata = dataio.ExportData( config=rmsglobalconfig, fmu_context="case", + casepath=fmurun_prehook, content="depth", name="MyName", ) @@ -406,5 +419,7 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath): assert metadata["access"]["classification"] == "restricted" # run two stage process + remove_ert_env(monkeypatch) surfacepath = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf) - _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath) + set_ert_env_prehook(monkeypatch) + _run_case_fmu(fmurun_prehook, rmsglobalconfig, surfacepath) diff --git a/tests/test_units/test_rms_context.py b/tests/test_units/test_rms_context.py index e3c59a03c..6133cffd1 100644 --- a/tests/test_units/test_rms_context.py +++ b/tests/test_units/test_rms_context.py @@ -13,6 +13,7 @@ import fmu.dataio.readers as readers import pandas as pd import pytest +from fmu.dataio._definitions import FmuContext from fmu.dataio._utils import prettyprint_dict from fmu.dataio.dataio import ValidationError @@ -373,7 +374,7 @@ def test_cube_export_file_set_name_as_observation_forcefolder( def test_cube_export_as_case(rmssetup, rmsglobalconfig, cube): """Export the cube to file with correct metadata and name, is_observation. - In addition, try fmu_conext=case + In addition try fmu_context=case; when inside rms this should be reset to "non-fmu" """ logger.info("Active folder is %s", rmssetup) os.chdir(rmssetup) @@ -390,7 +391,7 @@ def test_cube_export_as_case(rmssetup, rmsglobalconfig, cube): is_observation=True, ) logger.info("Output %s", output) - + assert edata.fmu_context == FmuContext.NON_FMU assert str(output) == str( (edata._rootpath / "share/observations/cubes/mycube.segy").resolve() ) @@ -503,7 +504,6 @@ def test_cube_export_as_observation_forcefolder_w_subfolder_case( cube, name="MyCube", is_observation=True, - fmu_context="case", forcefolder="seismic/xxx", ) logger.info("Output after force is %s", output)