File: test_exports.py

package info (click to toggle)
django-prometheus 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 568 kB
  • sloc: python: 1,776; sh: 5; makefile: 3
file content (29 lines) | stat: -rw-r--r-- 1,106 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python
import socket
from unittest.mock import ANY, MagicMock, call, patch

from django_prometheus.exports import SetupPrometheusEndpointOnPortRange


@patch("django_prometheus.exports.HTTPServer")
def test_port_range_available(httpserver_mock):
    """Test port range setup with an available port."""
    httpserver_mock.side_effect = [socket.error, MagicMock()]
    port_range = [8000, 8001]
    port_chosen = SetupPrometheusEndpointOnPortRange(port_range)
    assert port_chosen in port_range

    expected_calls = [call(("", 8000), ANY), call(("", 8001), ANY)]
    assert httpserver_mock.mock_calls == expected_calls


@patch("django_prometheus.exports.HTTPServer")
def test_port_range_unavailable(httpserver_mock):
    """Test port range setup with no available ports."""
    httpserver_mock.side_effect = [socket.error, socket.error]
    port_range = [8000, 8001]
    port_chosen = SetupPrometheusEndpointOnPortRange(port_range)

    expected_calls = [call(("", 8000), ANY), call(("", 8001), ANY)]
    assert httpserver_mock.mock_calls == expected_calls
    assert port_chosen is None