File: test_api_metrics.py

package info (click to toggle)
pypuppetdb 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: python: 3,810; makefile: 127; sh: 9
file content (172 lines) | stat: -rw-r--r-- 5,842 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json

import httpretty
import pytest

import pypuppetdb


def stub_request(url, data=None, method=httpretty.GET, status=200, **kwargs):
    if data is None:
        body = "[]"
    else:
        with open(data) as d:
            body = json.load(d.read())
    return httpretty.register_uri(method, url, body=body, status=status, **kwargs)


class TestMetricsAPI:
    def test_metric_v1(self, api):
        httpretty.enable()
        httpretty.enable()
        stub_request("http://localhost:8080/metrics/v1/mbeans/test")
        api.metric("test", version="v1")
        assert httpretty.last_request().path == "/metrics/v1/mbeans/test"

    def test_metric_v1_list(self, api):
        httpretty.enable()
        httpretty.enable()
        stub_request("http://localhost:8080/metrics/v1/mbeans")
        api.metric(version="v1")
        assert httpretty.last_request().path == "/metrics/v1/mbeans"

    def test_metric_v1_version_constructor(self):
        api = pypuppetdb.api.API(metric_api_version="v1")
        httpretty.enable()
        httpretty.enable()
        stub_request("http://localhost:8080/metrics/v1/mbeans/test")
        api.metric("test")
        assert httpretty.last_request().path == "/metrics/v1/mbeans/test"

    def test_metric_v2(self, api):
        metrics_body = {
            "request": {"mbean": "test:name=Num", "type": "read"},
            "value": {"Value": 0},
            "timestamp": 0,
            "status": 200,
        }

        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://localhost:8080/metrics/v2/read/test:name=Num",
            body=json.dumps(metrics_body),
        )
        metric = api.metric("test:name=Num")
        assert httpretty.last_request().path == "/metrics/v2/read/test%3Aname%3DNum"
        assert metric["Value"] == 0
        httpretty.disable()
        httpretty.reset()

    def test_metric_v2_version_constructor(self):
        api = pypuppetdb.api.API(metric_api_version="v2")
        metrics_body = {
            "request": {"mbean": "test:name=Num", "type": "read"},
            "value": {"Value": 0},
            "timestamp": 0,
            "status": 200,
        }

        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://localhost:8080/metrics/v2/read/test:name=Num",
            body=json.dumps(metrics_body),
        )
        metric = api.metric("test:name=Num")
        assert httpretty.last_request().path == "/metrics/v2/read/test%3Aname%3DNum"
        assert metric["Value"] == 0
        httpretty.disable()
        httpretty.reset()

    def test_metric_v2_version_string(self, api):
        metrics_body = {
            "request": {"mbean": "test:name=Num", "type": "read"},
            "value": {"Value": 0},
            "timestamp": 0,
            "status": 200,
        }

        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://localhost:8080/metrics/v2/read/test:name=Num",
            body=json.dumps(metrics_body),
        )
        metric = api.metric("test:name=Num", version="v2")
        assert httpretty.last_request().path == "/metrics/v2/read/test%3Aname%3DNum"
        assert metric["Value"] == 0
        httpretty.disable()
        httpretty.reset()

    def test_metric_v2_error(self, api):
        metrics_body = {
            "request": {"mbean": "test:name=Num", "type": "read"},
            "error_type": "javax.management.InstanceNotFoundException",
            "error": "javax.management.InstanceNotFoundException : test:name=Num",
            "status": 404,
        }

        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://localhost:8080/metrics/v2/read/test:name=Num",
            body=json.dumps(metrics_body),
        )
        with pytest.raises(pypuppetdb.errors.APIError):
            api.metric("test:name=Num")
        assert httpretty.last_request().path == "/metrics/v2/read/test%3Aname%3DNum"
        httpretty.disable()
        httpretty.reset()

    def test_metric_v2_escape_special_characters(self, api):
        metrics_body = {
            "request": {"mbean": "test:name=Num", "type": "read"},
            "value": {"Value": 0},
            "timestamp": 0,
            "status": 200,
        }

        httpretty.enable()
        metric_name = 'test:special/chars!metric"name'
        metric_escaped = 'test:special!/chars!!metric!"name'
        metric_escaped_urlencoded = "test%3Aspecial%21/chars%21%21metric%21%22name"
        httpretty.register_uri(
            httpretty.GET,
            ("http://localhost:8080/metrics/v2/read/" + metric_escaped),
            body=json.dumps(metrics_body),
        )
        metric = api.metric(metric_name)
        assert httpretty.last_request().path == (
            "/metrics/v2/read/" + metric_escaped_urlencoded
        )
        assert metric["Value"] == 0
        httpretty.disable()
        httpretty.reset()

    def test_metric_v2_list(self, api):
        # test metric() (no arguments)
        metrics_body = {
            "request": {"type": "list"},
            "value": {
                "java.util.logging": {"type=Logging": {}},
            },
            "timestamp": 0,
            "status": 200,
        }

        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://localhost:8080/metrics/v2/list",
            body=json.dumps(metrics_body),
        )
        metric = api.metric()
        assert httpretty.last_request().path == "/metrics/v2/list"
        assert metric == {"java.util.logging": {"type=Logging": {}}}
        httpretty.disable()
        httpretty.reset()

    def test_metric_bad_version(self, api):
        with pytest.raises(ValueError):
            api.metric("test", version="bad")