Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Dec 6, 2024
1 parent 7b4f84a commit 07c6de1
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 59 deletions.
12 changes: 7 additions & 5 deletions src/everest/bin/everest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ async def run_everest(options):
print("Waiting for server ...")
wait_for_server(options.config.output_dir, timeout=600)
print("Everest server found!")

await start_experiment(server_context=ServerConfig.get_server_context(options.config.output_dir),
config=options.config,
debug=options.debug)


start_experiment(
server_context=ServerConfig.get_server_context(options.config.output_dir),
config=options.config,
debug=options.debug,
)

run_detached_monitor(
server_context=ServerConfig.get_server_context(options.config.output_dir),
optimization_output_dir=options.config.optimization_output_dir,
Expand Down
8 changes: 6 additions & 2 deletions src/everest/detached/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
# everest.log file instead


async def start_experiment(server_context: Tuple[str, str, Tuple[str, str]], config: EverestConfig, debug: bool = False) -> None:
def start_experiment(
server_context: Tuple[str, str, Tuple[str, str]],
config: EverestConfig,
debug: bool = False,
) -> None:
try:
url, cert, auth = server_context
start_endpoint = "/".join([url, START_ENDPOINT])
Expand All @@ -65,7 +69,7 @@ async def start_experiment(server_context: Tuple[str, str, Tuple[str, str]], con
response.raise_for_status()
except:
raise ValueError("Failed to start experiment") from None


async def start_server(config: EverestConfig, debug: bool = False) -> Driver:
"""
Expand Down
5 changes: 5 additions & 0 deletions src/everest/detached/jobs/everest_server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def __init__(self, everest_config: EverestConfig):
STOP_ENDPOINT: False,
}

self.runner: Optional[ExperimentRunner] = None

self.everest_config = everest_config
self.output_dir = everest_config.output_dir
self.optimization_output_dir = everest_config.optimization_output_dir
Expand Down Expand Up @@ -309,6 +311,9 @@ def get_exit_code(
) -> JSONResponse:
self._log(request)
self._check_user(credentials)

if not self.runner:
return JSONResponse(jsonable_encoder({}))
return JSONResponse(
jsonable_encoder(
self.runner.get_exit_code() if self.runner.get_exit_code() else {}
Expand Down
9 changes: 4 additions & 5 deletions src/everest/detached/jobs/everserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
OPT_FAILURE_REALIZATIONS,
SHARED_DATA_ENDPOINT,
SIM_PROGRESS_ENDPOINT,
START_ENDPOINT,
STOP_ENDPOINT,
)
from everest.util import get_azure_logging_handler, makedirs_if_needed, version_info
Expand Down Expand Up @@ -180,16 +179,16 @@ def main():

update_everserver_status(status_path, ServerStatus.running)

#response = requests.post(
# response = requests.post(
# url + "/" + START_ENDPOINT, verify=cert, auth=auth, proxies=PROXY
#)
# )

is_done = False
while not is_done:
response = requests.get(
url + "/" + EXIT_CODE_ENDPOINT, verify=cert, auth=auth, proxies=PROXY
)
exit_code = ExitCode.model_validate_json(response.text)
exit_code = ExitCode.model_validate_json(response.body)
if exit_code.exit_code or exit_code.message:
is_done = True
else:
Expand All @@ -206,7 +205,7 @@ def main():
response = requests.get(
url + "/" + SHARED_DATA_ENDPOINT, verify=cert, auth=auth, proxies=PROXY
)
if json_body := json.loads(response.text):
if json_body := json.loads(response.body):
shared_data = json_body

status, message = _get_optimization_status(exit_code.exit_code, shared_data)
Expand Down
17 changes: 17 additions & 0 deletions tests/everest/entry_points/test_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def run_detached_monitor_mock(status=ServerStatus.completed, error=None, **kwarg
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@patch("everest.bin.everest_script.start_experiment")
def test_everest_entry_debug(
mock_start_experiment,
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
Expand All @@ -94,6 +96,7 @@ def test_everest_entry_debug(
wait_for_server_mock.assert_called_once()
start_monitor_mock.assert_called_once()
everserver_status_mock.assert_called()
mock_start_experiment.assert_called()

# the config file itself is dumped at DEBUG level
assert '"controls"' in logstream
Expand All @@ -109,7 +112,9 @@ def test_everest_entry_debug(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@patch("everest.bin.everest_script.start_experiment")
def test_everest_entry(
mock_start_experiment,
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
Expand All @@ -122,6 +127,7 @@ def test_everest_entry(
wait_for_server_mock.assert_called_once()
start_monitor_mock.assert_called_once()
everserver_status_mock.assert_called()
mock_start_experiment.assert_called()


@patch("everest.bin.everest_script.server_is_running", return_value=False)
Expand All @@ -132,7 +138,9 @@ def test_everest_entry(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.completed, "message": None},
)
@patch("everest.bin.everest_script.start_experiment")
def test_everest_entry_detached_already_run(
mock_start_experiment,
everserver_status_mock,
start_server_mock,
wait_for_server_mock,
Expand All @@ -151,6 +159,7 @@ def test_everest_entry_detached_already_run(
server_is_running_mock.assert_called_once()
everserver_status_mock.assert_called()
everserver_status_mock.reset_mock()
mock_start_experiment.assert_not_called()

# stopping the server has no effect
kill_entry([CONFIG_FILE_MINIMAL])
Expand Down Expand Up @@ -297,7 +306,9 @@ def test_everest_entry_monitor_no_run(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@patch("everest.bin.everest_script.start_experiment")
def test_everest_entry_show_all_jobs(
mock_start_experiment,
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
Expand Down Expand Up @@ -331,7 +342,9 @@ def test_everest_entry_show_all_jobs(
"everest.bin.everest_script.everserver_status",
return_value={"status": ServerStatus.never_run, "message": None},
)
@patch("everest.bin.everest_script.start_experiment")
def test_everest_entry_no_show_all_jobs(
mock_start_experiment,
everserver_status_mock,
get_opt_status_mock,
get_server_context_mock,
Expand Down Expand Up @@ -430,7 +443,9 @@ def test_monitor_entry_no_show_all_jobs(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@patch("everest.bin.everest_script.start_experiment")
def test_exception_raised_when_server_run_fails(
mock_start_experiment,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
Expand Down Expand Up @@ -462,7 +477,9 @@ def test_exception_raised_when_server_run_fails_monitor(
)
@patch("everest.bin.everest_script.wait_for_server")
@patch("everest.bin.everest_script.start_server")
@patch("everest.bin.everest_script.start_experiment")
def test_complete_status_for_normal_run(
mock_start_experiment,
start_server_mock,
wait_for_server_mock,
start_monitor_mock,
Expand Down
Loading

0 comments on commit 07c6de1

Please sign in to comment.