forked from mtusi2026/mtusi2026lab4
92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
"""Операторские сценарии: две реальные реплики и переносимый запуск hooks."""
|
|
|
|
from contextlib import nullcontext
|
|
import importlib
|
|
from pathlib import Path
|
|
import sys
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from scripts import replicas
|
|
|
|
|
|
class BalancedReplicasTests(unittest.TestCase):
|
|
def check_responses(self, responses, *, times_out=False):
|
|
clock = [0.0]
|
|
current_ids = {"api-a": "current-a", "api-b": "current-b"}
|
|
iterator = iter(responses)
|
|
|
|
def reply(*args, **kwargs):
|
|
instance = next(iterator)
|
|
return nullcontext(
|
|
SimpleNamespace(status=200, headers={"X-Instance-Id": instance})
|
|
)
|
|
|
|
def sleep(seconds):
|
|
clock[0] += seconds
|
|
|
|
with (
|
|
patch.object(
|
|
replicas,
|
|
"wait_ready",
|
|
side_effect=lambda name, **kw: {"instance_id": current_ids[name]},
|
|
) as readiness,
|
|
patch.object(replicas.time, "monotonic", side_effect=lambda: clock[0]),
|
|
patch.object(replicas.time, "sleep", side_effect=sleep),
|
|
patch("urllib.request.urlopen", side_effect=reply) as requests,
|
|
):
|
|
if times_out:
|
|
with self.assertRaisesRegex(RuntimeError, "both current ready"):
|
|
replicas.wait_balanced(timeout=0.3)
|
|
else:
|
|
replicas.wait_balanced(timeout=1)
|
|
self.assertEqual(
|
|
{c.args[0] for c in readiness.call_args_list}, set(current_ids)
|
|
)
|
|
return requests.call_count
|
|
|
|
def test_one_ready_replica_never_counts_as_balanced(self):
|
|
self.assertEqual(self.check_responses(["current-a"] * 3, times_out=True), 1)
|
|
|
|
def test_waits_until_both_replicas_answer_through_lb(self):
|
|
self.assertEqual(
|
|
self.check_responses(["current-a", "current-a", "current-b"]), 3
|
|
)
|
|
|
|
def test_old_process_alias_does_not_replace_current_replica(self):
|
|
self.assertEqual(
|
|
self.check_responses(["current-a", "old-b", "current-a", "current-b"]), 4
|
|
)
|
|
|
|
|
|
class HookCommandTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
# Public helpers use sibling imports when invoked as command-line scripts.
|
|
public_tests = str(Path(__file__).resolve().parents[1] / "tests")
|
|
with patch.object(sys, "path", [public_tests, *sys.path]):
|
|
cls.resilience = importlib.import_module("resilience")
|
|
|
|
def test_python_hook_uses_current_interpreter_and_preserves_spaces(self):
|
|
script = Path("directory with spaces/restart.py")
|
|
self.assertEqual(
|
|
self.resilience.hook_command(script), [sys.executable, str(script)]
|
|
)
|
|
|
|
def test_shell_is_resolved_from_path(self):
|
|
with patch.object(self.resilience.shutil, "which", return_value="/custom/bash"):
|
|
self.assertEqual(
|
|
self.resilience.hook_command(Path("restart.sh")),
|
|
["/custom/bash", "restart.sh"],
|
|
)
|
|
|
|
def test_missing_shell_reports_failure(self):
|
|
with patch.object(self.resilience.shutil, "which", return_value=None):
|
|
with self.assertRaisesRegex(self.resilience.Failure, "Python action hook"):
|
|
self.resilience.hook_command(Path("restart.sh"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|