Skip to content

Commit

Permalink
Add tests for unreadable files
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-krasontovitsch committed Mar 1, 2023
1 parent 8ca1b41 commit 1167e30
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/ert/_c_wrappers/enkf/enkf_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,15 @@ def sample_prior(
grid_file = self.ensembleConfig().grid_file
assert grid_file is not None
grid = xtgeo.grid_from_file(grid_file)
props = xtgeo.gridproperty_from_file(
init_file, name=parameter, grid=grid
)
try:
props = xtgeo.gridproperty_from_file(
init_file, name=parameter, grid=grid
)
except PermissionError as err:
context_message = (
f"Failed to open init file for parameter {parameter!r}"
)
raise RuntimeError(context_message) from err

data = props.values1d.data
field_config = config_node.getFieldModelConfig()
Expand Down
78 changes: 76 additions & 2 deletions tests/unit_tests/cli/test_integration_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest

import ert.shared
from ert import LibresFacade
from ert import LibresFacade, ensemble_evaluator
from ert.__main__ import ert_parser
from ert._c_wrappers.config.config_parser import ConfigValidationError
from ert._c_wrappers.enkf import EnKFMain, ErtConfig
Expand All @@ -21,7 +21,7 @@
ITERATIVE_ENSEMBLE_SMOOTHER_MODE,
TEST_RUN_MODE,
)
from ert.cli.main import run_cli
from ert.cli.main import ErtCliError, run_cli
from ert.shared.feature_toggling import FeatureToggling
from ert.storage import open_storage

Expand Down Expand Up @@ -414,3 +414,77 @@ def test_config_parser_fails_gracefully_on_unreadable_config_file(copy_case, cap
f"could not open file `{gen_kw_parameter_file_abs_path}` for parsing"
in caplog.text
)


def test_field_init_file_not_readable(copy_case, monkeypatch):
monkeypatch.setattr(
ensemble_evaluator._wait_for_evaluator, "WAIT_FOR_EVALUATOR_TIMEOUT", 5
)
copy_case("snake_oil_field")
config_file_name = "snake_oil_field.ert"
field_file_rel_path = "fields/permx0.grdecl"
os.chmod(field_file_rel_path, 0x0)

try:
run_ert_test_run(config_file_name)
except ErtCliError as err:
assert "Failed to open init file for parameter 'PERMX'" in str(err)


def test_surface_init_fails_during_forward_model_callback(copy_case):
copy_case("snake_oil_field")
config_file_name = "snake_oil_surface.ert"
parameter_name = "TOP"
with open(config_file_name, mode="r+", encoding="utf-8") as config_file_handler:
content_lines = config_file_handler.read().splitlines()
index_line_with_surface_top = [
index
for index, line in enumerate(content_lines)
if line.startswith(f"SURFACE {parameter_name}")
][0]
line_with_surface_top = content_lines[index_line_with_surface_top]
breaking_line_with_surface_top = line_with_surface_top + " FORWARD_INIT:True"
content_lines[index_line_with_surface_top] = breaking_line_with_surface_top
config_file_handler.seek(0)
config_file_handler.write("\n".join(content_lines))

try:
run_ert_test_run(config_file_name)
except ErtCliError as err:
assert f"Failed to initialize parameter {parameter_name!r}" in str(err)


def test_unopenable_observation_config_fails_gracefully(copy_case):
copy_case("snake_oil_field")
config_file_name = "snake_oil_field.ert"
with open(config_file_name, mode="r", encoding="utf-8") as config_file_handler:
content_lines = config_file_handler.read().splitlines()
index_line_with_observation_config = [
index
for index, line in enumerate(content_lines)
if line.startswith("OBS_CONFIG")
][0]
line_with_observation_config = content_lines[index_line_with_observation_config]
observation_config_rel_path = line_with_observation_config.split(" ")[1]
observation_config_abs_path = os.path.join(os.getcwd(), observation_config_rel_path)
os.chmod(observation_config_abs_path, 0x0)

try:
run_ert_test_run(config_file_name)
except RuntimeError as err:
assert (
"Do not have permission to open observation config file "
f"{observation_config_abs_path!r}" in str(err)
)


def run_ert_test_run(config_file: str) -> None:
parser = ArgumentParser(prog="test_run")
parsed = ert_parser(
parser,
[
TEST_RUN_MODE,
config_file,
],
)
run_cli(parsed)

0 comments on commit 1167e30

Please sign in to comment.