summaryrefslogtreecommitdiff
path: root/tests/accel-pppd/general/test_metrics.py
blob: 6344d3d048d626af4d3a7b9212d7dbd9355f0c42 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import http.client
import json

import pytest


PROM_PORT = 9099


def _config(fmt):
    return f"""
    [modules]
    metrics

    [core]
    log-error=/dev/stderr

    [log]
    log-emerg=/dev/stderr
    level=1

    [cli]
    tcp=127.0.0.1:2001

    [metrics]
    address=127.0.0.1:{PROM_PORT}
    format={fmt}
    """


def _request(path, method="GET"):
    conn = http.client.HTTPConnection("127.0.0.1", PROM_PORT, timeout=5)
    try:
        conn.request(method, path)
        resp = conn.getresponse()
        body = resp.read().decode("utf-8", "replace")
        headers = {k.lower(): v for k, v in resp.getheaders()}
        return resp.status, headers, body
    finally:
        conn.close()


class TestPrometheus:
    @pytest.fixture()
    def accel_pppd_config(self):
        return _config("prometheus")

    def test_metrics_prometheus(self, accel_pppd_instance):
        assert accel_pppd_instance

        status, headers, body = _request("/metrics")

        assert status == 200
        assert "text/plain" in headers.get("content-type", "")
        assert "accel_ppp_build_info{version=" in body
        assert "# TYPE accel_ppp_uptime_seconds gauge" in body
        assert 'accel_ppp_sessions{state="active"}' in body

    def test_metrics_404_unknown_path(self, accel_pppd_instance):
        assert accel_pppd_instance

        status, _, _ = _request("/nope")

        assert status == 404

    def test_metrics_405_non_get(self, accel_pppd_instance):
        assert accel_pppd_instance

        status, _, _ = _request("/metrics", method="POST")

        assert status == 405


class TestJson:
    @pytest.fixture()
    def accel_pppd_config(self):
        return _config("json")

    def test_metrics_json(self, accel_pppd_instance):
        assert accel_pppd_instance

        status, headers, body = _request("/metrics")

        assert status == 200
        assert headers.get("content-type") == "application/json"

        doc = json.loads(body)
        assert "build" in doc and "version" in doc["build"]
        assert "uptime_seconds" in doc
        assert "active" in doc["sessions"]
        assert "threads" in doc["core"]