File: test_stats_plugin_collector.py

package info (click to toggle)
prometheus-trafficserver-exporter 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 348 kB
  • sloc: python: 457; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (3)
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
import os

import prometheus_client
from mock import Mock, patch

from trafficserver_exporter.collector import StatsPluginCollector
from trafficserver_exporter.trafficserver_exporter import PKG_METRICS_FILE


def test_metric_output(stats_json):
    # With some existing stats data
    fname, stats = stats_json
    registry = prometheus_client.registry.CollectorRegistry()
    # And the collector setup with the default metrics config
    collector = StatsPluginCollector("http://ats", PKG_METRICS_FILE)
    registry.register(collector)
    # With a mocked response that returns the stats data
    with patch.object(collector, "session") as mock_session:
        mock_response = Mock()
        mock_response.json.return_value = stats
        # If I generate metric output with that collector
        output = prometheus_client.generate_latest(registry).decode("utf-8")

    # I expect a proper HTTP request with the session
    mock_session.get.assert_called_once_with("http://ats", verify=True)

    if os.getenv("WRITE_METRIC_DATA"):
        with open("./tests/output/{}.prom".format(fname), "w") as f:
            f.write(output)

    # If I compare the output to a known valid set
    with open("./tests/output/{}.prom".format(fname), "r") as f:
        valid_lines = {l.strip() for l in f.readlines()}

    for line in output.split("\n"):
        if line and not any(
            (
                # With some variable metrics removed
                line.startswith("trafficserver_scrape_duration_seconds "),
                # And comments
                line.startswith("#"),
            )
        ):
            # I expect each metric line to match
            assert line in valid_lines