conductor(checkpoint): Checkpoint end of Phase 2
This commit is contained in:
@@ -4,7 +4,9 @@ import cluster_aggregator
|
||||
|
||||
@patch("consul_client.get_cluster_services")
|
||||
@patch("litefs_client.get_node_status")
|
||||
def test_aggregate_cluster_status(mock_litefs, mock_consul):
|
||||
@patch("nomad_client.get_allocation_id")
|
||||
@patch("nomad_client.get_allocation_logs")
|
||||
def test_aggregate_cluster_status(mock_nomad_logs, mock_nomad_id, mock_litefs, mock_consul):
|
||||
"""Test aggregating Consul and LiteFS data."""
|
||||
# Mock Consul data
|
||||
mock_consul.return_value = [
|
||||
@@ -19,6 +21,7 @@ def test_aggregate_cluster_status(mock_litefs, mock_consul):
|
||||
return {"is_primary": False, "uptime": 50, "advertise_url": "url2", "replication_lag": 10}
|
||||
|
||||
mock_litefs.side_effect = litefs_side_effect
|
||||
mock_nomad_id.return_value = None
|
||||
|
||||
cluster_data = cluster_aggregator.get_cluster_status("http://consul:8500")
|
||||
|
||||
@@ -35,12 +38,17 @@ def test_aggregate_cluster_status(mock_litefs, mock_consul):
|
||||
|
||||
@patch("consul_client.get_cluster_services")
|
||||
@patch("litefs_client.get_node_status")
|
||||
def test_aggregate_cluster_status_unhealthy(mock_litefs, mock_consul):
|
||||
@patch("nomad_client.get_allocation_id")
|
||||
@patch("nomad_client.get_allocation_logs")
|
||||
def test_aggregate_cluster_status_unhealthy(mock_nomad_logs, mock_nomad_id, mock_litefs, mock_consul):
|
||||
"""Test health calculation when nodes are critical."""
|
||||
mock_consul.return_value = [
|
||||
{"node": "node1", "address": "1.1.1.1", "role": "primary", "status": "critical"}
|
||||
]
|
||||
mock_litefs.return_value = {"is_primary": True, "uptime": 100}
|
||||
mock_nomad_id.return_value = "alloc1"
|
||||
mock_nomad_logs.return_value = "error logs"
|
||||
|
||||
cluster_data = cluster_aggregator.get_cluster_status("http://consul:8500")
|
||||
assert cluster_data["health"] == "Unhealthy"
|
||||
assert cluster_data["nodes"][0]["nomad_logs"] == "error logs"
|
||||
|
||||
@@ -12,7 +12,8 @@ def test_arg_parsing_default():
|
||||
|
||||
def test_arg_parsing_custom():
|
||||
"""Test that custom arguments are parsed correctly."""
|
||||
with patch.object(sys, 'argv', ['cli.py', '--consul-url', 'http://custom:8500', '--no-color']):
|
||||
with patch.object(sys, 'argv', ['cli.py', '--consul-url', 'http://custom:8500', '--no-color', '--restart', 'node1']):
|
||||
args = cli.parse_args()
|
||||
assert args.consul_url == 'http://custom:8500'
|
||||
assert args.no_color is True
|
||||
assert args.restart == 'node1'
|
||||
|
||||
58
scripts/cluster_status/tests/test_nomad_client.py
Normal file
58
scripts/cluster_status/tests/test_nomad_client.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import nomad_client
|
||||
import subprocess
|
||||
|
||||
@patch("subprocess.run")
|
||||
@patch("nomad_client.get_node_map")
|
||||
def test_get_allocation_id(mock_node_map, mock_run):
|
||||
"""Test getting allocation ID for a node."""
|
||||
mock_node_map.return_value = {"node_id1": "node1"}
|
||||
|
||||
# Mock 'nomad job status navidrome-litefs' output
|
||||
mock_job_status = MagicMock()
|
||||
mock_job_status.stdout = """
|
||||
Allocations
|
||||
ID Node ID Task Group Version Desired Status Created Modified
|
||||
abc12345 node_id1 navidrome 1 run running 1h ago 1h ago
|
||||
"""
|
||||
|
||||
# Mock 'nomad alloc status abc12345' output
|
||||
mock_alloc_status = MagicMock()
|
||||
mock_alloc_status.stdout = "ID = abc12345-full-id"
|
||||
|
||||
mock_run.side_effect = [mock_job_status, mock_alloc_status]
|
||||
|
||||
alloc_id = nomad_client.get_allocation_id("node1", "navidrome-litefs")
|
||||
assert alloc_id == "abc12345-full-id"
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_get_logs(mock_run):
|
||||
"""Test fetching logs for an allocation."""
|
||||
mock_stderr = "Error: database is locked\nSome other error"
|
||||
m = MagicMock()
|
||||
m.stdout = mock_stderr
|
||||
m.return_code = 0
|
||||
mock_run.return_value = m
|
||||
|
||||
logs = nomad_client.get_allocation_logs("abc12345", tail=20)
|
||||
assert "database is locked" in logs
|
||||
# It should have tried with -task navidrome first
|
||||
mock_run.assert_any_call(
|
||||
["nomad", "alloc", "logs", "-stderr", "-task", "navidrome", "-n", "20", "abc12345"],
|
||||
capture_output=True, text=True, check=True
|
||||
)
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_restart_allocation(mock_run):
|
||||
"""Test restarting an allocation."""
|
||||
m = MagicMock()
|
||||
m.return_code = 0
|
||||
mock_run.return_value = m
|
||||
|
||||
success = nomad_client.restart_allocation("abc12345")
|
||||
assert success is True
|
||||
mock_run.assert_called_with(
|
||||
["nomad", "alloc", "restart", "abc12345"],
|
||||
capture_output=True, text=True, check=True
|
||||
)
|
||||
Reference in New Issue
Block a user