forked from CrayLabs/SmartSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
189 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# BSD 2-Clause License | ||
# | ||
# Copyright (c) 2021-2023, Hewlett Packard Enterprise | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# 1. Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
import os | ||
import pathlib | ||
import pytest | ||
import typing as t | ||
|
||
|
||
from smartsim._core.entrypoints.dragon import ( | ||
cleanup, | ||
main, | ||
parse_arguments, | ||
register_signal_handlers, | ||
remove_config_log, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_argv() -> t.List[str]: | ||
"""Fixture for returning valid arguments to the entrypoint""" | ||
return ["+launching_address", "mock-addr", "+interface", "mock-interface"] | ||
|
||
|
||
def test_file_removal(test_dir: str, monkeypatch: pytest.MonkeyPatch): | ||
"""Verify that the log file is removed when expected""" | ||
mock_file_name = "mocked_file_name.txt" | ||
expected_path = pathlib.Path(test_dir) / mock_file_name | ||
expected_path.touch() | ||
|
||
with monkeypatch.context() as ctx: | ||
# ensure we get outputs in the test directory | ||
ctx.setattr( | ||
"smartsim._core.entrypoints.dragon.get_log_path", lambda: str(expected_path) | ||
) | ||
|
||
remove_config_log() | ||
assert not expected_path.exists(), "Dragon config file was not removed" | ||
|
||
|
||
def test_file_removal_on_bad_path(test_dir: str, monkeypatch: pytest.MonkeyPatch): | ||
"""Verify that file removal doesn't blow up if the log file wasn't created""" | ||
mock_file_name = "mocked_file_name.txt" | ||
expected_path = pathlib.Path(test_dir) / mock_file_name | ||
|
||
with monkeypatch.context() as ctx: | ||
# ensure we get outputs in the test directory | ||
ctx.setattr( | ||
"smartsim._core.entrypoints.dragon.get_log_path", lambda: str(expected_path) | ||
) | ||
|
||
# confirm the file doesn't exist... | ||
assert not expected_path.exists(), "Dragon config file was not found" | ||
|
||
try: | ||
# ensure we don't blow up | ||
remove_config_log() | ||
except: | ||
assert False | ||
|
||
|
||
def test_dragon_failure(mock_argv: t.List[str], test_dir: str, monkeypatch: pytest.MonkeyPatch): | ||
"""Verify that the expected cleanup actions are taken when the dragon | ||
entrypoint exits""" | ||
mock_file_name = "mocked_file_name.txt" | ||
expected_path = pathlib.Path(test_dir) / mock_file_name | ||
expected_path.touch() | ||
|
||
with monkeypatch.context() as ctx: | ||
# ensure we get outputs in the test directory | ||
ctx.setattr( | ||
"smartsim._core.entrypoints.dragon.get_log_path", lambda: str(expected_path) | ||
) | ||
|
||
def raiser(args_) -> int: | ||
raise Exception("Something bad...") | ||
|
||
# we don't need to execute the entrypoint... | ||
ctx.setattr("smartsim._core.entrypoints.dragon.execute_entrypoint", raiser) | ||
|
||
return_code = main(mock_argv) | ||
|
||
# ensure our exception error code is returned | ||
assert return_code == -1 | ||
|
||
|
||
def test_dragon_main(mock_argv: t.List[str], test_dir: str, monkeypatch: pytest.MonkeyPatch): | ||
"""Verify that the expected startup & cleanup actions are taken when the dragon | ||
entrypoint exits""" | ||
mock_file_name = "mocked_file_name.txt" | ||
expected_path = pathlib.Path(test_dir) / mock_file_name | ||
expected_path.touch() | ||
|
||
with monkeypatch.context() as ctx: | ||
# ensure we get outputs in the test directory | ||
ctx.setattr( | ||
"smartsim._core.entrypoints.dragon.get_log_path", lambda: str(expected_path) | ||
) | ||
# we don't need to execute the actual entrypoint... | ||
ctx.setattr("smartsim._core.entrypoints.dragon.execute_entrypoint", lambda args_: 0) | ||
|
||
return_code = main(mock_argv) | ||
|
||
# execute_entrypoint should return 0 from our mock | ||
assert return_code == 0 | ||
# the cleanup should remove our config file | ||
assert not expected_path.exists(), "Dragon config file was not removed!" | ||
# the environment should be set as expected | ||
assert os.environ.get("PYTHONUNBUFFERED", None) == "1" |