|
| 1 | +"""Unit tests for AppAvailabilityProbe (DNS + HTTP classification).""" |
| 2 | + |
| 3 | +import socket |
| 4 | +import unittest |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +from serve_event_listener.probing import AppAvailabilityProbe, ProbeResult |
| 10 | + |
| 11 | + |
| 12 | +class TestAppAvailabilityProbe(unittest.TestCase): |
| 13 | + """Classify results based on mocked DNS and HTTP outcomes.""" |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + """Create a probe with a fake Session and short timeouts.""" |
| 17 | + self.session = MagicMock(spec=requests.Session) |
| 18 | + self.probe = AppAvailabilityProbe( |
| 19 | + self.session, |
| 20 | + verify_tls=True, |
| 21 | + timeout=(0.2, 0.5), |
| 22 | + backoff_seconds=(0.05, 0.1), |
| 23 | + ) |
| 24 | + |
| 25 | + @patch("serve_event_listener.probing.http_get") |
| 26 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 27 | + def test_notfound_when_dns_fails(self, mock_dns, mock_http_get): |
| 28 | + """Returns NotFound when DNS cannot resolve the host.""" |
| 29 | + mock_dns.side_effect = socket.gaierror() |
| 30 | + res = self.probe.probe_url("http://no-such-host.invalid/") |
| 31 | + self.assertIsInstance(res, ProbeResult) |
| 32 | + self.assertEqual(res.status, "NotFound") |
| 33 | + mock_http_get.assert_not_called() |
| 34 | + |
| 35 | + @patch("serve_event_listener.probing.http_get") |
| 36 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 37 | + def test_running_on_200(self, mock_dns, mock_http_get): |
| 38 | + """Returns Running on HTTP 200.""" |
| 39 | + mock_dns.return_value = [(None,)] |
| 40 | + mock_http_get.return_value = MagicMock(status_code=200) |
| 41 | + |
| 42 | + res = self.probe.probe_url("http://example.com/") |
| 43 | + self.assertEqual(res.status, "Running") |
| 44 | + self.assertEqual(res.port80_status, 200) |
| 45 | + |
| 46 | + # Arguments are forwarded correctly |
| 47 | + _, kwargs = mock_http_get.call_args |
| 48 | + self.assertTrue(kwargs.get("allow_redirects")) |
| 49 | + self.assertEqual(kwargs.get("timeout"), (0.2, 0.5)) |
| 50 | + |
| 51 | + @patch("serve_event_listener.probing.http_get") |
| 52 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 53 | + def test_running_on_3xx(self, mock_dns, mock_http_get): |
| 54 | + """Returns Running on HTTP 302 redirect.""" |
| 55 | + mock_dns.return_value = [(None,)] |
| 56 | + mock_http_get.return_value = MagicMock(status_code=302) |
| 57 | + res = self.probe.probe_url("http://example.com/") |
| 58 | + self.assertEqual(res.status, "Running") |
| 59 | + self.assertEqual(res.port80_status, 302) |
| 60 | + |
| 61 | + @patch("serve_event_listener.probing.http_get") |
| 62 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 63 | + def test_unknown_on_network_error(self, mock_dns, mock_http_get): |
| 64 | + """Returns Unknown when HTTP wrapper returns None (timeout/refused).""" |
| 65 | + mock_dns.return_value = [(None,)] |
| 66 | + mock_http_get.return_value = None |
| 67 | + res = self.probe.probe_url("http://example.com/") |
| 68 | + self.assertEqual(res.status, "Unknown") |
| 69 | + self.assertIsNone(res.port80_status) |
| 70 | + |
| 71 | + @patch("serve_event_listener.probing.http_get") |
| 72 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 73 | + def test_unknown_on_5xx(self, mock_dns, mock_http_get): |
| 74 | + """Returns Unknown on server 5xx.""" |
| 75 | + mock_dns.return_value = [(None,)] |
| 76 | + mock_http_get.return_value = MagicMock(status_code=503) |
| 77 | + res = self.probe.probe_url("http://example.com/") |
| 78 | + self.assertEqual(res.status, "Unknown") |
| 79 | + self.assertEqual(res.port80_status, 503) |
| 80 | + |
| 81 | + @patch("serve_event_listener.probing.http_get") |
| 82 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 83 | + def test_unknown_on_4xx(self, mock_dns, mock_http_get): |
| 84 | + """Returns Unknown on client 4xx.""" |
| 85 | + mock_dns.return_value = [(None,)] |
| 86 | + mock_http_get.return_value = MagicMock(status_code=404) |
| 87 | + res = self.probe.probe_url("http://example.com/") |
| 88 | + self.assertEqual(res.status, "Unknown") |
| 89 | + self.assertEqual(res.port80_status, 404) |
| 90 | + |
| 91 | + @patch("serve_event_listener.probing.http_get") |
| 92 | + @patch("serve_event_listener.probing.socket.getaddrinfo") |
| 93 | + def test_notfound_when_url_has_no_host(self, mock_dns, mock_http_get): |
| 94 | + """Returns NotFound when URL has no hostname.""" |
| 95 | + # urlparse(host) -> None triggers NotFound before DNS call |
| 96 | + res = self.probe.probe_url("http:///nohost") |
| 97 | + self.assertEqual(res.status, "NotFound") |
| 98 | + mock_dns.assert_not_called() |
| 99 | + mock_http_get.assert_not_called() |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + unittest.main() |
0 commit comments