Skip to content

Commit

Permalink
Move duplicate patching in test_everserver.py to mock_server fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSava committed Dec 19, 2024
1 parent 68309bb commit a159fa7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 74 deletions.
16 changes: 16 additions & 0 deletions tests/everest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from copy import deepcopy
from pathlib import Path
from textwrap import dedent
from unittest.mock import MagicMock

import pytest
import yaml
Expand All @@ -14,6 +15,7 @@
from ert.run_models.everest_run_model import EverestRunModel
from everest.config import EverestConfig
from everest.config.control_config import ControlConfig
from everest.detached.jobs import everserver
from tests.everest.utils import relpath


Expand Down Expand Up @@ -211,3 +213,17 @@ def min_config():
config_path: .
""")
)


@pytest.fixture()
def mock_server(monkeypatch):
monkeypatch.setattr(everserver, "_configure_loggers", MagicMock())
monkeypatch.setattr(everserver, "_generate_authentication", MagicMock())
monkeypatch.setattr(
everserver, "_generate_certificate", lambda *args: (None, None, None)
)
monkeypatch.setattr(everserver, "_find_open_port", lambda *args, **kwargs: 42)
monkeypatch.setattr(everserver, "_write_hostfile", MagicMock())
monkeypatch.setattr(everserver, "_everserver_thread", MagicMock())
monkeypatch.setattr(everserver, "export_to_csv", MagicMock())
monkeypatch.setattr(everserver, "export_with_progress", MagicMock())
79 changes: 5 additions & 74 deletions tests/everest/test_everserver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
import ssl
import stat
from functools import partial
from pathlib import Path
from unittest.mock import patch
Expand Down Expand Up @@ -103,21 +102,6 @@ def test_everserver_status_failure(_1, copy_math_func_test_data_to_tmp):


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
return_value=(None, None, None),
)
@patch(
"everest.detached.jobs.everserver._find_open_port",
return_value=42,
)
@patch(
"everest.detached.jobs.everserver._write_hostfile",
side_effect=partial(check_status, status=ServerStatus.starting),
)
@patch("everest.detached.jobs.everserver._everserver_thread")
@patch(
"ert.run_models.everest_run_model.EverestRunModel.run_experiment",
autospec=True,
Expand All @@ -126,13 +110,8 @@ def test_everserver_status_failure(_1, copy_math_func_test_data_to_tmp):
status=ServerStatus.running,
),
)
@patch(
"everest.detached.jobs.everserver.check_for_errors",
return_value=([], False),
)
@patch("everest.detached.jobs.everserver.export_to_csv")
def test_everserver_status_running_complete(
_1, _2, _3, _4, _5, _6, _7, _8, _9, copy_math_func_test_data_to_tmp
_1, mock_server, copy_math_func_test_data_to_tmp
):
config_file = "config_minimal.yml"
config = EverestConfig.load_file(config_file)
Expand All @@ -146,18 +125,6 @@ def test_everserver_status_running_complete(


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
return_value=(None, None, None),
)
@patch(
"everest.detached.jobs.everserver._find_open_port",
return_value=42,
)
@patch("everest.detached.jobs.everserver._write_hostfile")
@patch("everest.detached.jobs.everserver._everserver_thread")
@patch(
"ert.run_models.everest_run_model.EverestRunModel.run_experiment",
autospec=True,
Expand All @@ -182,7 +149,7 @@ def test_everserver_status_running_complete(
),
)
def test_everserver_status_failed_job(
_1, _2, _3, _4, _5, _6, _7, _8, copy_math_func_test_data_to_tmp
_1, _2, mock_server, copy_math_func_test_data_to_tmp
):
config_file = "config_minimal.yml"
config = EverestConfig.load_file(config_file)
Expand All @@ -200,18 +167,6 @@ def test_everserver_status_failed_job(


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
return_value=(None, None, None),
)
@patch(
"everest.detached.jobs.everserver._find_open_port",
return_value=42,
)
@patch("everest.detached.jobs.everserver._write_hostfile")
@patch("everest.detached.jobs.everserver._everserver_thread")
@patch(
"ert.run_models.everest_run_model.EverestRunModel.run_experiment",
autospec=True,
Expand All @@ -224,7 +179,7 @@ def test_everserver_status_failed_job(
side_effect=partial(set_shared_status, progress=[]),
)
def test_everserver_status_exception(
_1, _2, _3, _4, _5, _6, _7, _8, copy_math_func_test_data_to_tmp
_1, _2, mock_server, copy_math_func_test_data_to_tmp
):
config_file = "config_minimal.yml"
config = EverestConfig.load_file(config_file)
Expand All @@ -240,24 +195,12 @@ def test_everserver_status_exception(


@patch("sys.argv", ["name", "--config-file", "config_one_batch.yml"])
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
return_value=(None, None, None),
)
@patch(
"everest.detached.jobs.everserver._find_open_port",
return_value=42,
)
@patch("everest.detached.jobs.everserver._write_hostfile")
@patch("everest.detached.jobs.everserver._everserver_thread")
@patch(
"everest.detached.jobs.everserver._sim_monitor",
side_effect=partial(set_shared_status, progress=[]),
)
def test_everserver_status_max_batch_num(
_1, _2, _3, _4, _5, _6, _7, copy_math_func_test_data_to_tmp
_1, mock_server, copy_math_func_test_data_to_tmp
):
config_file = "config_one_batch.yml"
config = EverestConfig.load_file(config_file)
Expand All @@ -277,20 +220,8 @@ def test_everserver_status_max_batch_num(


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
return_value=(None, None, None),
)
@patch(
"everest.detached.jobs.everserver._find_open_port",
return_value=42,
)
@patch("everest.detached.jobs.everserver._write_hostfile")
@patch("everest.detached.jobs.everserver._everserver_thread")
def test_everserver_status_contains_max_runtime_failure(
_1, _2, _3, _4, _5, _6, change_to_tmpdir, min_config
mock_server, change_to_tmpdir, min_config
):
config_file = "config_minimal.yml"

Expand Down

0 comments on commit a159fa7

Please sign in to comment.