|
1 | | -import pytest |
2 | | - |
3 | | - |
4 | | -@pytest.fixture |
5 | | -def mock_showlog_response(): |
6 | | - return """ |
7 | | - <html> |
8 | | - <body> |
9 | | - <div class='logEntry'> |
10 | | - <div class='logEntryType'>SERVICE ALERT</div> |
11 | | - <div class='logTime'>[04-29-2026 17:32:20]</div> |
12 | | - <div class='logText'>SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Connection timeout</div> |
13 | | - </div> |
| 1 | +from unittest.mock import Mock, patch |
| 2 | +import requests |
| 3 | + |
| 4 | + |
| 5 | +def test_show_logs_default_days(client, capsys): |
| 6 | + mock_response = Mock() |
| 7 | + mock_response.status_code = 200 |
| 8 | + mock_response.text = """ |
14 | 9 | <div>Service Warning[04-29-2026 17:32:20] SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Connection timeout</div> |
15 | 10 | <div>Service Ok[04-29-2026 17:30:00] SERVICE ALERT: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
16 | 11 | <div>Informational Message[04-29-2026 17:25:00] Auto-save of retention data completed successfully.</div> |
17 | 12 | <div>State Ok[04-29-2026 17:20:00] CURRENT HOST STATE: host.example.com;UP;HARD;1;PING OK</div> |
18 | | - <div>State Ok[04-29-2026 17:15:00] CURRENT SERVICE STATE: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
19 | | - </body> |
20 | | - </html> |
21 | 13 | """ |
22 | 14 |
|
23 | | - |
24 | | -def test_show_logs_default_days(client, requests_mock, capsys, mock_showlog_response): |
25 | | - requests_mock.get( |
26 | | - f"{client.showlog_url}", |
27 | | - text=mock_showlog_response, |
28 | | - status_code=200 |
29 | | - ) |
30 | | - |
31 | | - client.show_logs() |
| 15 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 16 | + client.show_logs() |
32 | 17 |
|
33 | 18 | captured = capsys.readouterr() |
34 | 19 | assert "Nagios Log Entries" in captured.out |
35 | 20 | assert "Last 1.0 day(s)" in captured.out |
36 | 21 |
|
37 | 22 |
|
38 | | -def test_show_logs_custom_days(client, requests_mock, capsys, mock_showlog_response): |
39 | | - requests_mock.get( |
40 | | - f"{client.showlog_url}", |
41 | | - text=mock_showlog_response, |
42 | | - status_code=200 |
43 | | - ) |
| 23 | +def test_show_logs_custom_days(client, capsys): |
| 24 | + mock_response = Mock() |
| 25 | + mock_response.status_code = 200 |
| 26 | + mock_response.text = """ |
| 27 | + <div>Service Ok[04-29-2026 17:30:00] SERVICE ALERT: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
| 28 | + """ |
44 | 29 |
|
45 | | - client.show_logs(days=7.0) |
| 30 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 31 | + client.show_logs(days=7.0) |
46 | 32 |
|
47 | 33 | captured = capsys.readouterr() |
48 | 34 | assert "Last 7.0 day(s)" in captured.out |
49 | 35 |
|
50 | 36 |
|
51 | | -def test_show_logs_parses_service_alerts(client, requests_mock, capsys, mock_showlog_response): |
52 | | - requests_mock.get( |
53 | | - f"{client.showlog_url}", |
54 | | - text=mock_showlog_response, |
55 | | - status_code=200 |
56 | | - ) |
| 37 | +def test_show_logs_parses_service_alerts(client, capsys): |
| 38 | + mock_response = Mock() |
| 39 | + mock_response.status_code = 200 |
| 40 | + mock_response.text = """ |
| 41 | + <div>Service Warning[04-29-2026 17:32:20] SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Connection timeout</div> |
| 42 | + """ |
57 | 43 |
|
58 | | - client.show_logs() |
| 44 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 45 | + client.show_logs() |
59 | 46 |
|
60 | 47 | captured = capsys.readouterr() |
61 | 48 | assert "SERVICE ALERT" in captured.out |
62 | 49 |
|
63 | 50 |
|
64 | | -def test_show_logs_filters_current_state(client, requests_mock, capsys, mock_showlog_response): |
65 | | - requests_mock.get( |
66 | | - f"{client.showlog_url}", |
67 | | - text=mock_showlog_response, |
68 | | - status_code=200 |
69 | | - ) |
| 51 | +def test_show_logs_filters_current_state(client, capsys): |
| 52 | + mock_response = Mock() |
| 53 | + mock_response.status_code = 200 |
| 54 | + mock_response.text = """ |
| 55 | + <div>Service Ok[04-29-2026 17:30:00] SERVICE ALERT: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
| 56 | + <div>State Ok[04-29-2026 17:20:00] CURRENT HOST STATE: host.example.com;UP;HARD;1;PING OK</div> |
| 57 | + <div>State Ok[04-29-2026 17:15:00] CURRENT SERVICE STATE: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
| 58 | + """ |
70 | 59 |
|
71 | | - client.show_logs() |
| 60 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 61 | + client.show_logs() |
72 | 62 |
|
73 | 63 | captured = capsys.readouterr() |
74 | 64 | assert "CURRENT HOST STATE" not in captured.out |
75 | 65 | assert "CURRENT SERVICE STATE" not in captured.out |
76 | 66 |
|
77 | 67 |
|
78 | | -def test_show_logs_shows_icons(client, requests_mock, capsys): |
79 | | - response_with_alerts = """ |
| 68 | +def test_show_logs_shows_icons(client, capsys): |
| 69 | + mock_response = Mock() |
| 70 | + mock_response.status_code = 200 |
| 71 | + mock_response.text = """ |
80 | 72 | <div>Service Warning[04-29-2026 17:32:20] SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Timeout</div> |
81 | 73 | <div>Service Ok[04-29-2026 17:30:00] SERVICE ALERT: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
82 | 74 | <div>Service Critical[04-29-2026 17:28:00] SERVICE ALERT: host.example.com;HTTP;CRITICAL;HARD;1;Connection refused</div> |
83 | 75 | """ |
84 | 76 |
|
85 | | - requests_mock.get( |
86 | | - f"{client.showlog_url}", |
87 | | - text=response_with_alerts, |
88 | | - status_code=200 |
89 | | - ) |
90 | | - |
91 | | - client.show_logs() |
| 77 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 78 | + client.show_logs() |
92 | 79 |
|
93 | 80 | captured = capsys.readouterr() |
94 | 81 | assert "⚠️" in captured.out or "WARNING" in captured.out |
95 | 82 | assert "✅" in captured.out or "OK" in captured.out |
96 | 83 | assert "❌" in captured.out or "CRITICAL" in captured.out |
97 | 84 |
|
98 | 85 |
|
99 | | -def test_show_logs_no_entries(client, requests_mock, capsys): |
100 | | - requests_mock.get( |
101 | | - f"{client.showlog_url}", |
102 | | - text="<html><body></body></html>", |
103 | | - status_code=200 |
104 | | - ) |
| 86 | +def test_show_logs_no_entries(client, capsys): |
| 87 | + mock_response = Mock() |
| 88 | + mock_response.status_code = 200 |
| 89 | + mock_response.text = "<html><body></body></html>" |
105 | 90 |
|
106 | | - client.show_logs() |
| 91 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 92 | + client.show_logs() |
107 | 93 |
|
108 | 94 | captured = capsys.readouterr() |
109 | 95 | assert "No log entries found" in captured.out or "No alert entries found" in captured.out |
110 | 96 |
|
111 | 97 |
|
112 | | -def test_show_logs_http_error(client, requests_mock, capsys): |
113 | | - requests_mock.get( |
114 | | - f"{client.showlog_url}", |
115 | | - status_code=500 |
116 | | - ) |
117 | | - |
118 | | - client.show_logs() |
| 98 | +def test_show_logs_http_error(client, capsys): |
| 99 | + with patch.object(client.session, 'get', side_effect=requests.exceptions.RequestException("HTTP Error")): |
| 100 | + client.show_logs() |
119 | 101 |
|
120 | 102 | captured = capsys.readouterr() |
121 | 103 | assert "Error fetching logs" in captured.out |
122 | 104 |
|
123 | 105 |
|
124 | | -def test_show_logs_timestamp_params(client, requests_mock, mock_showlog_response): |
125 | | - import datetime |
| 106 | +def test_show_logs_timestamp_params(client): |
| 107 | + mock_response = Mock() |
| 108 | + mock_response.status_code = 200 |
| 109 | + mock_response.text = """ |
| 110 | + <div>Service Ok[04-29-2026 17:30:00] SERVICE ALERT: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
| 111 | + """ |
126 | 112 |
|
127 | | - mock_request = requests_mock.get( |
128 | | - f"{client.showlog_url}", |
129 | | - text=mock_showlog_response, |
130 | | - status_code=200 |
131 | | - ) |
| 113 | + with patch.object(client.session, 'get', return_value=mock_response) as mock_get: |
| 114 | + client.show_logs(days=2.0) |
132 | 115 |
|
133 | | - client.show_logs(days=2.0) |
| 116 | + assert mock_get.called |
| 117 | + call_kwargs = mock_get.call_args[1] |
| 118 | + params = call_kwargs['params'] |
134 | 119 |
|
135 | | - assert mock_request.called |
136 | | - assert 'ts_start' in mock_request.last_request.qs |
137 | | - assert 'ts_end' in mock_request.last_request.qs |
| 120 | + assert 'ts_start' in params |
| 121 | + assert 'ts_end' in params |
138 | 122 |
|
139 | | - ts_start = int(mock_request.last_request.qs['ts_start'][0]) |
140 | | - ts_end = int(mock_request.last_request.qs['ts_end'][0]) |
| 123 | + ts_start = params['ts_start'] |
| 124 | + ts_end = params['ts_end'] |
141 | 125 |
|
142 | | - time_diff = ts_end - ts_start |
143 | | - expected_diff = 2.0 * 24 * 60 * 60 |
| 126 | + time_diff = ts_end - ts_start |
| 127 | + expected_diff = 2.0 * 24 * 60 * 60 |
144 | 128 |
|
145 | | - assert abs(time_diff - expected_diff) < 5 |
| 129 | + assert abs(time_diff - expected_diff) < 5 |
146 | 130 |
|
147 | 131 |
|
148 | | -def test_show_logs_full_flag(client, requests_mock, capsys): |
149 | | - response_with_states = """ |
| 132 | +def test_show_logs_full_flag(client, capsys): |
| 133 | + mock_response = Mock() |
| 134 | + mock_response.status_code = 200 |
| 135 | + mock_response.text = """ |
150 | 136 | <div>Service Warning[04-29-2026 17:32:20] SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Timeout</div> |
151 | 137 | <div>State Ok[04-29-2026 17:20:00] CURRENT HOST STATE: host.example.com;UP;HARD;1;PING OK</div> |
152 | 138 | <div>State Ok[04-29-2026 17:15:00] CURRENT SERVICE STATE: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
153 | 139 | """ |
154 | 140 |
|
155 | | - requests_mock.get( |
156 | | - f"{client.showlog_url}", |
157 | | - text=response_with_states, |
158 | | - status_code=200 |
159 | | - ) |
160 | | - |
161 | | - client.show_logs(full=True) |
| 141 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 142 | + client.show_logs(full=True) |
162 | 143 |
|
163 | 144 | captured = capsys.readouterr() |
164 | 145 | assert "CURRENT HOST STATE" in captured.out |
165 | 146 | assert "CURRENT SERVICE STATE" in captured.out |
166 | 147 |
|
167 | 148 |
|
168 | | -def test_show_logs_without_full_flag(client, requests_mock, capsys): |
169 | | - response_with_states = """ |
| 149 | +def test_show_logs_without_full_flag(client, capsys): |
| 150 | + mock_response = Mock() |
| 151 | + mock_response.status_code = 200 |
| 152 | + mock_response.text = """ |
170 | 153 | <div>Service Warning[04-29-2026 17:32:20] SERVICE ALERT: host.example.com;HTTP;WARNING;HARD;3;Timeout</div> |
171 | 154 | <div>State Ok[04-29-2026 17:20:00] CURRENT HOST STATE: host.example.com;UP;HARD;1;PING OK</div> |
172 | 155 | <div>State Ok[04-29-2026 17:15:00] CURRENT SERVICE STATE: host.example.com;HTTP;OK;HARD;1;HTTP OK</div> |
173 | 156 | """ |
174 | 157 |
|
175 | | - requests_mock.get( |
176 | | - f"{client.showlog_url}", |
177 | | - text=response_with_states, |
178 | | - status_code=200 |
179 | | - ) |
180 | | - |
181 | | - client.show_logs(full=False) |
| 158 | + with patch.object(client.session, 'get', return_value=mock_response): |
| 159 | + client.show_logs(full=False) |
182 | 160 |
|
183 | 161 | captured = capsys.readouterr() |
184 | 162 | assert "CURRENT HOST STATE" not in captured.out |
|
0 commit comments