|
| 1 | +"""Tests for the Flask monitoring backend.""" |
| 2 | +import pytest |
| 3 | +import json |
| 4 | +from unittest.mock import patch, mock_open |
| 5 | + |
| 6 | +from app import app, read_version_file |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def client(): |
| 11 | + """Create test client.""" |
| 12 | + app.config['TESTING'] = True |
| 13 | + with app.test_client() as client: |
| 14 | + yield client |
| 15 | + |
| 16 | + |
| 17 | +class TestVersionEndpoint: |
| 18 | + """Tests for the /version endpoint.""" |
| 19 | + |
| 20 | + def test_version_endpoint_returns_json(self, client): |
| 21 | + """Test that /version returns valid JSON.""" |
| 22 | + response = client.get('/version') |
| 23 | + assert response.status_code == 200 |
| 24 | + assert response.content_type == 'application/json' |
| 25 | + |
| 26 | + def test_version_endpoint_contains_version_field(self, client): |
| 27 | + """Test that /version response contains version field.""" |
| 28 | + response = client.get('/version') |
| 29 | + data = json.loads(response.data) |
| 30 | + assert 'version' in data |
| 31 | + |
| 32 | + def test_version_endpoint_contains_build_ts_field(self, client): |
| 33 | + """Test that /version response contains build_ts field.""" |
| 34 | + response = client.get('/version') |
| 35 | + data = json.loads(response.data) |
| 36 | + assert 'build_ts' in data |
| 37 | + |
| 38 | + |
| 39 | +class TestReadVersionFile: |
| 40 | + """Tests for the read_version_file function.""" |
| 41 | + |
| 42 | + def test_read_version_file_success(self): |
| 43 | + """Test reading version file successfully.""" |
| 44 | + mock_content = "1.2.3" |
| 45 | + with patch("builtins.open", mock_open(read_data=mock_content)): |
| 46 | + result = read_version_file("/VERSION") |
| 47 | + assert result == "1.2.3" |
| 48 | + |
| 49 | + def test_read_version_file_strips_whitespace(self): |
| 50 | + """Test that version file content is stripped.""" |
| 51 | + mock_content = " 1.2.3\n " |
| 52 | + with patch("builtins.open", mock_open(read_data=mock_content)): |
| 53 | + result = read_version_file("/VERSION") |
| 54 | + assert result == "1.2.3" |
| 55 | + |
| 56 | + def test_read_version_file_not_found_returns_default(self): |
| 57 | + """Test that missing file returns default value.""" |
| 58 | + with patch("builtins.open", side_effect=FileNotFoundError()): |
| 59 | + result = read_version_file("/VERSION") |
| 60 | + assert result == "unknown" |
| 61 | + |
| 62 | + def test_read_version_file_custom_default(self): |
| 63 | + """Test custom default value when file not found.""" |
| 64 | + with patch("builtins.open", side_effect=FileNotFoundError()): |
| 65 | + result = read_version_file("/VERSION", default="0.0.0") |
| 66 | + assert result == "0.0.0" |
| 67 | + |
| 68 | + |
| 69 | +class TestHealthEndpoint: |
| 70 | + """Tests for the /health endpoint.""" |
| 71 | + |
| 72 | + @patch('app.get_prometheus_client') |
| 73 | + def test_health_endpoint_healthy(self, mock_prom, client): |
| 74 | + """Test /health returns healthy when Prometheus is reachable.""" |
| 75 | + mock_prom.return_value.get_current_metric_value.return_value = [{'value': 1}] |
| 76 | + response = client.get('/health') |
| 77 | + assert response.status_code == 200 |
| 78 | + data = json.loads(response.data) |
| 79 | + assert data['status'] == 'healthy' |
| 80 | + |
| 81 | + @patch('app.get_prometheus_client') |
| 82 | + def test_health_endpoint_unhealthy(self, mock_prom, client): |
| 83 | + """Test /health returns unhealthy when Prometheus is unreachable.""" |
| 84 | + mock_prom.return_value.get_current_metric_value.side_effect = Exception("Connection failed") |
| 85 | + response = client.get('/health') |
| 86 | + assert response.status_code == 500 |
| 87 | + data = json.loads(response.data) |
| 88 | + assert data['status'] == 'unhealthy' |
0 commit comments