Skip to content

Commit

Permalink
Move remaining c_wrappers test to match src
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Aug 17, 2023
1 parent f8b5fdc commit 966c3ac
Show file tree
Hide file tree
Showing 39 changed files with 156 additions and 180 deletions.
1 change: 0 additions & 1 deletion tests/unit_tests/c_wrappers/conftest.py

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
14 changes: 0 additions & 14 deletions tests/unit_tests/c_wrappers/res/enkf/conftest.py

This file was deleted.

Empty file.
Empty file.
164 changes: 0 additions & 164 deletions tests/unit_tests/c_wrappers/test_integration_config.py

This file was deleted.

94 changes: 94 additions & 0 deletions tests/unit_tests/cli/test_integration_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import fileinput
import json
import logging
import os
import shutil
Expand Down Expand Up @@ -798,3 +799,96 @@ def test_failing_job_cli_error_message():
assert substring in f"{error}"
else:
pytest.fail(msg="Expected run cli to raise ErtCliError!")


@pytest.fixture
def setenv_config(tmp_path):
config = tmp_path / "test.ert"

# Given that environment variables are set in the config
config.write_text(
"""
NUM_REALIZATIONS 1
SETENV FIRST first:$PATH
SETENV SECOND $MYVAR
SETENV MYVAR foo
SETENV THIRD TheThirdValue
SETENV FOURTH fourth:$MYVAR
INSTALL_JOB ECHO ECHO.txt
FORWARD_MODEL ECHO
""",
encoding="utf-8",
)
run_script = tmp_path / "run.py"
run_script.write_text(
"#!/usr/bin/env python3\n"
"import os\n"
'print(os.environ["FIRST"])\n'
'print(os.environ["SECOND"])\n'
'print(os.environ["THIRD"])\n'
'print(os.environ["FOURTH"])\n',
encoding="utf-8",
)
os.chmod(run_script, 0o755)

(tmp_path / "ECHO.txt").write_text(
dedent(
"""
EXECUTABLE run.py
"""
)
)
return config


expected_vars = {
"FIRST": "first:$PATH",
"SECOND": "$MYVAR",
"MYVAR": "foo",
"THIRD": "TheThirdValue",
"FOURTH": "fourth:$MYVAR",
}


def test_that_setenv_config_is_parsed_correctly(setenv_config):
config = ErtConfig.from_file(str(setenv_config))
# then res config should read the SETENV as is
assert config.env_vars == expected_vars


def test_that_setenv_sets_environment_variables_in_jobs(setenv_config):
# When running the jobs
parser = ArgumentParser(prog="test_main")
parsed = ert_parser(
parser,
[
TEST_RUN_MODE,
str(setenv_config),
"--port-range",
"1024-65535",
],
)

run_cli(parsed)

# Then the environment variables are put into jobs.json
with open("simulations/realization-0/iter-0/jobs.json", encoding="utf-8") as f:
data = json.load(f)
global_env = data.get("global_environment")
assert global_env == expected_vars

path = os.environ["PATH"]

# and then job_dispatch should expand the variables on the compute side
with open("simulations/realization-0/iter-0/ECHO.stdout.0", encoding="utf-8") as f:
lines = f.readlines()
assert len(lines) == 4
# the compute-nodes path is the same since it's running locally,
# so we can test that we can prepend to it
assert lines[0].strip() == f"first:{path}"
# MYVAR is not set in the compyte node yet, so it should not be expanded
assert lines[1].strip() == "$MYVAR"
# THIRD is just a simple value
assert lines[2].strip() == "TheThirdValue"
# now MYVAR now set, so should be expanded inside the value of FOURTH
assert lines[3].strip() == "fourth:foo"
13 changes: 12 additions & 1 deletion tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This conftest still exists so that tests files can import ert_utils
import logging
import os
import sys

import pytest

from ert.enkf_main import EnKFMain
from ert.ensemble_evaluator.config import EvaluatorServerConfig


Expand Down Expand Up @@ -47,3 +47,14 @@ def ensure_bin_in_path():
path = os.environ["PATH"]
exec_path = os.path.dirname(sys.executable)
os.environ["PATH"] = exec_path + ":" + path


@pytest.fixture()
def snake_oil_field_example(setup_case):
return EnKFMain(setup_case("snake_oil_field", "snake_oil_field.ert"))


@pytest.fixture
def prior_ensemble(storage):
experiment_id = storage.create_experiment()
return storage.create_ensemble(experiment_id, name="prior", ensemble_size=100)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ert.enkf_main import EnKFMain
from ert.run_context import RunContext
from ert.runpaths import Runpaths
from ert.storage import StorageAccessor


@pytest.mark.usefixtures("use_tmpdir")
Expand Down Expand Up @@ -404,3 +405,52 @@ def test_assert_export(prior_ensemble):
runpath_list_file.read_text("utf-8")
== f"000 {os.getcwd()}/simulations/realization-0/iter-0 a_name_0 000\n"
)


def _create_runpath(enkf_main: EnKFMain, storage: StorageAccessor) -> RunContext:
"""
Instantiate an ERT runpath. This will create the parameter coefficients.
"""
run_context = enkf_main.ensemble_context(
storage.create_ensemble(
storage.create_experiment(),
name="prior",
ensemble_size=enkf_main.getEnsembleSize(),
),
[True] * enkf_main.getEnsembleSize(),
iteration=0,
)
enkf_main.createRunPath(run_context)
return run_context


@pytest.mark.parametrize(
"append,numcpu",
[
("", 1), # Default is 1
("NUM_CPU 2\n", 2),
("DATA_FILE DATA\n", 8), # Data file dictates NUM_CPU with PARALLEL
("NUM_CPU 3\nDATA_FILE DATA\n", 3), # Explicit NUM_CPU supersedes PARALLEL
],
)
def test_num_cpu_subst(monkeypatch, tmp_path, append, numcpu, storage):
"""
Make sure that <NUM_CPU> is substituted to the correct values
"""
monkeypatch.chdir(tmp_path)

(tmp_path / "test.ert").write_text(
"JOBNAME test_%d\n"
"NUM_REALIZATIONS 1\n"
"INSTALL_JOB dump DUMP\n"
"FORWARD_MODEL dump\n" + append
)
(tmp_path / "DATA").write_text("PARALLEL 8 /")
(tmp_path / "DUMP").write_text("EXECUTABLE echo\nARGLIST <NUM_CPU>\n")

config = ErtConfig.from_file(str(tmp_path / "test.ert"))
enkf_main = EnKFMain(config)
_create_runpath(enkf_main, storage)

with open("simulations/realization-0/iter-0/jobs.json", encoding="utf-8") as f:
assert f'"argList": ["{numcpu}"]' in f.read()
File renamed without changes.
File renamed without changes.

0 comments on commit 966c3ac

Please sign in to comment.