Skip to content

Commit

Permalink
epmanager tests DRY refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khk-globus committed Oct 17, 2024
1 parent 258497b commit 637854c
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions compute_endpoint/tests/unit/test_endpointmanager_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@


_MOCK_BASE = "globus_compute_endpoint.endpoint.endpoint_manager."
_GOOD_EC = 87 # SPoA for "good/happy-path" exit code

_mock_rootuser_rec = pwd.struct_passwd(
("root", "", 0, 0, "Mock Root User", "/mock_root", "/bin/false")
Expand Down Expand Up @@ -1544,7 +1545,7 @@ def test_environment_default_path(mocker, successful_exec_from_mocked_root):
expected_pybindir = pathlib.Path(sys.executable).parent
expected_order = ("/usr/local/bin", "/usr/bin", "/bin", str(expected_pybindir))

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
a, k = mock_os.execvpe.call_args
env = k["env"]
assert "PATH" in env, "Path always set, with default if nothing else available"
Expand All @@ -1561,7 +1562,7 @@ def test_loads_user_environment(successful_exec_from_mocked_root, randomstring):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
a, k = mock_os.execvpe.call_args
env = k["env"]
assert sentinel_key in env
Expand All @@ -1578,7 +1579,7 @@ def test_handles_invalid_user_environment_file_gracefully(
env_path.write_text("\nalkdhj: g\nkladhj - asdf -asd f")
with pytest.raises(SystemExit) as pyexc:
em._event_loop()
assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
a, k = mock_warn.call_args_list[0]
assert "Failed to parse user environment variables" in a[0]
assert env_path in a, "Expected pointer to problem file in warning"
Expand All @@ -1595,7 +1596,7 @@ def test_environment_default_path_set_if_not_specified(
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
a, k = mock_os.execvpe.call_args
env = k["env"]
assert "PATH" in env, "Expected PATH is always set"
Expand All @@ -1609,7 +1610,7 @@ def test_warns_if_environment_file_not_found(successful_exec_from_mocked_root, c
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
assert f"No user environment variable file found at {conf_path}" in caplog.text


Expand All @@ -1621,7 +1622,7 @@ def test_warns_if_environment_file_empty(successful_exec_from_mocked_root, caplo
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
assert f"User environment variable file at {conf_path} is empty" in caplog.text


Expand Down Expand Up @@ -1668,7 +1669,7 @@ def test_start_endpoint_children_die_with_parent(successful_exec_from_mocked_roo
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
a, k = mock_os.execvpe.call_args
assert a[0] == "globus-compute-endpoint", "Sanity check"
assert k["args"][0] == a[0], "Expect transparency for admin"
Expand All @@ -1680,7 +1681,7 @@ def test_start_endpoint_children_have_own_session(successful_exec_from_mocked_ro
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
assert mock_os.setsid.called


Expand All @@ -1689,7 +1690,7 @@ def test_start_endpoint_privileges_dropped(successful_exec_from_mocked_root):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

expected_user = _mock_localuser_rec.pw_name
expected_gid = _mock_localuser_rec.pw_gid
Expand All @@ -1714,7 +1715,7 @@ def test_start_endpoint_logs_to_std(mocker, successful_exec_from_mocked_root):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

log_config = mock_logging.config.dictConfig.call_args[0][0]
handlers = log_config["handlers"]
Expand Down Expand Up @@ -1854,7 +1855,7 @@ def test_default_to_secure_umask(successful_exec_from_mocked_root):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

assert mock_os.umask.called
umask = mock_os.umask.call_args[0][0]
Expand All @@ -1866,7 +1867,7 @@ def test_start_from_user_dir(successful_exec_from_mocked_root):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

udir = mock_os.chdir.call_args[0][0]
expected_udir = _mock_localuser_rec.pw_dir
Expand All @@ -1878,7 +1879,7 @@ def test_all_files_closed(successful_exec_from_mocked_root):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

_soft_no, hard_no = resource.getrlimit(resource.RLIMIT_NOFILE)
assert mock_os.closerange.called
Expand Down Expand Up @@ -1911,7 +1912,7 @@ def test_pipe_size_limit(mocker, mock_log, successful_exec_from_mocked_root, con
em._event_loop()

if is_valid:
assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"
else:
assert pyexc.value.code < 87
assert f"{stdin_data_size} bytes" in mock_log.error.call_args[0][0]
Expand All @@ -1928,7 +1929,7 @@ def _remove_user_config_template(*args, **kwargs):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"


def test_redirect_stdstreams_to_user_log(
Expand All @@ -1949,7 +1950,7 @@ def test_redirect_stdstreams_to_user_log(
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

a, k = next((a, k) for a, k in mock_os.open.call_args_list if a[0] == ep_log)
assert a[1] == exp_flags, "Expect replacement stdout/stderr: append, wronly, sync"
Expand Down Expand Up @@ -1977,7 +1978,7 @@ def duped_first_check(*a, **k):
with pytest.raises(SystemExit) as pyexc:
em._event_loop()

assert pyexc.value.code == 87, "Q&D: verify we exec'ed, based on '+= 1'"
assert pyexc.value.code == _GOOD_EC, "Q&D: verify we exec'ed, based on '+= 1'"

assert mock_print.called is debug, "Expect only written if `debug: true` set"
if debug:
Expand Down

0 comments on commit 637854c

Please sign in to comment.