-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for reactive.runner_manager
- Loading branch information
Showing
5 changed files
with
198 additions
and
19 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
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
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,164 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
import random | ||
import subprocess | ||
from pathlib import Path | ||
from subprocess import CompletedProcess | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from charm_state import ReactiveConfig | ||
from reactive.runner_manager import ( | ||
PS_COMMAND_LINE_LIST, | ||
PYTHON_BIN, | ||
REACTIVE_RUNNER_SCRIPT_FILE, | ||
ReactiveRunnerError, | ||
ReactiveRunnerManager, | ||
) | ||
from utilities import secure_run_subprocess | ||
|
||
|
||
@pytest.fixture(name="log_file_path", autouse=True) | ||
def log_file_path_fixture(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: | ||
"""Return the path to the log file.""" | ||
log_file_path = tmp_path / "log.txt" | ||
monkeypatch.setattr("reactive.runner_manager.REACTIVE_RUNNER_LOG_FILE", log_file_path) | ||
monkeypatch.setattr("shutil.chown", lambda *args, **kwargs: None) | ||
return log_file_path | ||
|
||
|
||
@pytest.fixture(name="secure_run_subprocess_mock") | ||
def secure_run_subprocess_mock_fixture(monkeypatch: pytest.MonkeyPatch) -> MagicMock: | ||
"""Mock the ps command.""" | ||
secure_run_subprocess_mock = MagicMock(spec=secure_run_subprocess) | ||
monkeypatch.setattr( | ||
"reactive.runner_manager.secure_run_subprocess", secure_run_subprocess_mock | ||
) | ||
return secure_run_subprocess_mock | ||
|
||
|
||
@pytest.fixture(name="subprocess_popen_mock") | ||
def subprocess_popen_mock_fixture(monkeypatch: pytest.MonkeyPatch) -> MagicMock: | ||
"""Mock the subprocess.Popen function.""" | ||
subprocess_popen_mock = MagicMock( | ||
spec=subprocess.Popen, | ||
return_value=MagicMock(pid=1234, returncode=random.choice([None, 0])), | ||
) | ||
monkeypatch.setattr("subprocess.Popen", subprocess_popen_mock) | ||
return subprocess_popen_mock | ||
|
||
|
||
def test_reconcile_spawns_runners( | ||
secure_run_subprocess_mock: MagicMock, subprocess_popen_mock: MagicMock, log_file_path: Path | ||
): | ||
""" | ||
arrange: Mock that two reactive runner processes are active. | ||
act: Call reconcile with a quantity of 5. | ||
assert: Three runners are spawned. Log file is setup. | ||
""" | ||
reactive_config = ReactiveConfig(mq_uri="http://example.com") | ||
_arrange_reactive_processes(secure_run_subprocess_mock, count=2) | ||
|
||
manager = ReactiveRunnerManager(reactive_config, "test-queue") | ||
delta = manager.reconcile(5) | ||
|
||
assert delta == 3 | ||
assert subprocess_popen_mock.call_count == 3 | ||
assert log_file_path.exists() | ||
|
||
|
||
def _arrange_reactive_processes(secure_run_subprocess_mock: MagicMock, count: int): | ||
"""Mock reactive runner processes are active. | ||
Args: | ||
secure_run_subprocess_mock: The mock to use for the ps command. | ||
count: The number of processes | ||
""" | ||
process_cmds = "\n".join([f"{PYTHON_BIN} {REACTIVE_RUNNER_SCRIPT_FILE}" for _ in range(count)]) | ||
secure_run_subprocess_mock.return_value = CompletedProcess( | ||
args=PS_COMMAND_LINE_LIST, | ||
returncode=0, | ||
stdout=f"CMD\n{process_cmds}".encode("utf-8"), | ||
stderr=b"", | ||
) | ||
|
||
|
||
def test_reconcile_does_not_spawn_runners( | ||
secure_run_subprocess_mock: MagicMock, subprocess_popen_mock: MagicMock | ||
): | ||
""" | ||
arrange: Mock that two reactive runner processes are active. | ||
act: Call reconcile with a quantity of 2. | ||
assert: No runners are spawned. | ||
""" | ||
reactive_config = ReactiveConfig(mq_uri="http://example.com") | ||
_arrange_reactive_processes(secure_run_subprocess_mock, count=2) | ||
|
||
manager = ReactiveRunnerManager(reactive_config, "test-queue") | ||
delta = manager.reconcile(2) | ||
|
||
assert delta == 0 | ||
assert subprocess_popen_mock.call_count == 0 | ||
|
||
|
||
def test_reconcile_does_not_spawn_runners_for_too_many_processes( | ||
secure_run_subprocess_mock: MagicMock, subprocess_popen_mock: MagicMock | ||
): | ||
""" | ||
arrange: Mock that two reactive runner processes are active. | ||
act: Call reconcile with a quantity of 1. | ||
assert: No runners are spawned and delta is 0. | ||
""" | ||
reactive_config = ReactiveConfig(mq_uri="http://example.com") | ||
_arrange_reactive_processes(secure_run_subprocess_mock, count=2) | ||
manager = ReactiveRunnerManager(reactive_config, "test-queue") | ||
delta = manager.reconcile(1) | ||
|
||
assert delta == 0 | ||
assert subprocess_popen_mock.call_count == 0 | ||
|
||
|
||
def test_reconcile_raises_reactive_runner_error_on_ps_failure( | ||
secure_run_subprocess_mock: MagicMock, | ||
): | ||
""" | ||
arrange: Mock that the ps command fails. | ||
act: Call reconcile with a quantity of 1. | ||
assert: A ReactiveRunnerError is raised. | ||
""" | ||
secure_run_subprocess_mock.return_value = CompletedProcess( | ||
args=PS_COMMAND_LINE_LIST, | ||
returncode=1, | ||
stdout=b"", | ||
stderr=b"error", | ||
) | ||
|
||
reactive_config = ReactiveConfig(mq_uri="http://example.com") | ||
manager = ReactiveRunnerManager(reactive_config, "test-queue") | ||
with pytest.raises(ReactiveRunnerError) as err: | ||
manager.reconcile(1) | ||
|
||
assert "Failed to get list of processes" in str(err.value) | ||
|
||
|
||
def test_reconcile_spawn_runner_failed( | ||
secure_run_subprocess_mock: MagicMock, subprocess_popen_mock: MagicMock | ||
): | ||
""" | ||
arrange: Mock that one reactive runner spawn fails. | ||
act: Call reconcile with a quantity of 3. | ||
assert: The delta is 2. | ||
""" | ||
subprocess_popen_mock.side_effect = [ | ||
MagicMock(returncode=0), | ||
MagicMock(return_code=1), | ||
MagicMock(returncode=0), | ||
] | ||
_arrange_reactive_processes(secure_run_subprocess_mock, count=0) | ||
|
||
reactive_config = ReactiveConfig(mq_uri="http://example.com") | ||
manager = ReactiveRunnerManager(reactive_config, "test-queue") | ||
delta = manager.reconcile(3) | ||
|
||
assert delta == 2 |