Skip to content

Commit

Permalink
Refactor everest tmpdir fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Oct 9, 2024
1 parent b505d33 commit b6a7f62
Show file tree
Hide file tree
Showing 42 changed files with 417 additions and 636 deletions.
3 changes: 0 additions & 3 deletions tests/everest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .utils import tmp

__all__ = ["tmp"]
41 changes: 41 additions & 0 deletions tests/everest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from everest.config.control_config import ControlConfig
from tests.everest.utils import relpath


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -82,3 +83,43 @@ def control_config(
config = deepcopy(control_data_no_variables)
config["variables"] = request.param
return ControlConfig.model_validate(config)


@pytest.fixture
def copy_math_func_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("..", "..", "test-data", "everest", "math_func")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_mocked_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data", "mocked_test_case")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_template_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("test_data", "templating")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def copy_egg_test_data_to_tmp(tmp_path, monkeypatch):
path = relpath("..", "..", "test-data", "everest", "egg")
shutil.copytree(path, tmp_path, dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)


@pytest.fixture
def change_to_tmpdir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
11 changes: 5 additions & 6 deletions tests/everest/entry_points/test_config_branch_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from everest.config import EverestConfig
from everest.config_file_loader import load_yaml
from everest.config_keys import ConfigKeys as CK
from tests.everest.utils import relpath, tmpdir
from tests.everest.utils import relpath

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE = "config_advanced.yml"
CACHED_SEBA_FOLDER = relpath("test_data", "cached_results_config_advanced")

Expand All @@ -22,8 +21,7 @@
new_callable=PropertyMock,
return_value=CACHED_SEBA_FOLDER,
)
@tmpdir(CONFIG_PATH)
def test_config_branch_entry(get_opt_output_dir_mock):
def test_config_branch_entry(get_opt_output_dir_mock, copy_math_func_test_data_to_tmp):
new_config_file_name = "new_restart_config.yml"
batch_id = 1

Expand Down Expand Up @@ -65,8 +63,9 @@ def test_config_branch_entry(get_opt_output_dir_mock):
new_callable=PropertyMock,
return_value=CACHED_SEBA_FOLDER,
)
@tmpdir(CONFIG_PATH)
def test_config_branch_preserves_config_section_order(get_opt_output_dir_mock):
def test_config_branch_preserves_config_section_order(
get_opt_output_dir_mock, copy_math_func_test_data_to_tmp
):
new_config_file_name = "new_restart_config.yml"
batch_id = 1

Expand Down
43 changes: 22 additions & 21 deletions tests/everest/entry_points/test_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
from everest.jobs import shell_commands
from everest.simulator import JOB_SUCCESS
from ieverest.bin.ieverest_script import ieverest_entry
from tests.everest.utils import capture_streams, relpath, tmpdir
from tests.everest.utils import capture_streams

CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func")
CONFIG_FILE_MINIMAL = "config_minimal.yml"


Expand Down Expand Up @@ -80,13 +79,13 @@ def run_detached_monitor_mock(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_debug(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
caplog,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with --debug"""
with caplog.at_level(logging.DEBUG):
Expand All @@ -111,12 +110,12 @@ def test_everest_entry_debug(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest in detached mode"""
everest_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -134,13 +133,13 @@ def test_everest_entry(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_already_run(
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, when an optimization has already run"""
# optimization already run, notify the user
Expand Down Expand Up @@ -171,11 +170,11 @@ def test_everest_entry_detached_already_run(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_already_run_monitor(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, when an optimization has already run"""
# optimization already run, notify the user
Expand All @@ -199,7 +198,6 @@ def test_everest_entry_detached_already_run_monitor(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_running(
everserver_status_mock,
wait_for_server_to_stop_mock,
Expand All @@ -209,6 +207,7 @@ def test_everest_entry_detached_running(
start_monitor_mock,
server_is_running_mock_kill_script,
server_is_running_mock_everest_script,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running"""
# can't start a new run if one is already running
Expand Down Expand Up @@ -248,11 +247,11 @@ def test_everest_entry_detached_running(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_detached_running_monitor(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running, monitoring"""
# Attach to a running optimization.
Expand All @@ -269,11 +268,11 @@ def test_everest_entry_detached_running_monitor(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_monitor_no_run(
everserver_status_mock,
start_monitor_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test everest detached, optimization is running, monitoring"""
# Attach to a running optimization.
Expand All @@ -300,7 +299,6 @@ def test_everest_entry_monitor_no_run(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
Expand All @@ -309,6 +307,7 @@ def test_everest_entry_show_all_jobs(
start_server_mock,
wait_for_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with --show-all-jobs"""

Expand All @@ -335,7 +334,6 @@ def test_everest_entry_show_all_jobs(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_everest_entry_no_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
Expand All @@ -344,6 +342,7 @@ def test_everest_entry_no_show_all_jobs(
start_server_mock,
wait_for_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest without --show-all-jobs"""

Expand Down Expand Up @@ -372,13 +371,13 @@ def test_everest_entry_no_show_all_jobs(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_monitor_entry_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
query_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest with and without --show-all-jobs"""

Expand All @@ -404,13 +403,13 @@ def test_monitor_entry_show_all_jobs(
"everest.bin.monitor_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@tmpdir(CONFIG_PATH)
def test_monitor_entry_no_show_all_jobs(
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
query_server_mock,
server_is_running_mock,
copy_math_func_test_data_to_tmp,
):
"""Test running everest without --show-all-jobs"""

Expand All @@ -436,9 +435,11 @@ def test_monitor_entry_no_show_all_jobs(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@tmpdir(CONFIG_PATH)
def test_exception_raised_when_server_run_fails(
start_server_mock, wait_for_server_mock, start_monitor_mock
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
with pytest.raises(SystemExit, match="Reality was ripped to shreds!"):
everest_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -453,9 +454,8 @@ def test_exception_raised_when_server_run_fails(
error="Reality was ripped to shreds!",
),
)
@tmpdir(CONFIG_PATH)
def test_exception_raised_when_server_run_fails_monitor(
start_monitor_mock, server_is_running_mock
start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp
):
with pytest.raises(SystemExit, match="Reality was ripped to shreds!"):
monitor_entry([CONFIG_FILE_MINIMAL])
Expand All @@ -467,9 +467,11 @@ def test_exception_raised_when_server_run_fails_monitor(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@tmpdir(CONFIG_PATH)
def test_complete_status_for_normal_run(
start_server_mock, wait_for_server_mock, start_monitor_mock
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
copy_math_func_test_data_to_tmp,
):
everest_entry([CONFIG_FILE_MINIMAL])
config = EverestConfig.load_file(CONFIG_FILE_MINIMAL)
Expand All @@ -486,9 +488,8 @@ def test_complete_status_for_normal_run(
"everest.bin.monitor_script.run_detached_monitor",
side_effect=run_detached_monitor_mock,
)
@tmpdir(CONFIG_PATH)
def test_complete_status_for_normal_run_monitor(
start_monitor_mock, server_is_running_mock
start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp
):
monitor_entry([CONFIG_FILE_MINIMAL])
config = EverestConfig.load_file(CONFIG_FILE_MINIMAL)
Expand Down
Loading

0 comments on commit b6a7f62

Please sign in to comment.