]>
Commit | Line | Data |
---|---|---|
116b2602 | 1 | #!/usr/bin/env python |
13291274 | 2 | import os |
116b2602 RG |
3 | import requests |
4 | import subprocess | |
13291274 | 5 | import unittest |
116b2602 RG |
6 | from dnsdisttests import DNSDistTest |
7 | ||
13291274 | 8 | @unittest.skipIf('SKIP_PROMETHEUS_TESTS' in os.environ, 'Prometheus tests are disabled') |
116b2602 RG |
9 | class TestPrometheus(DNSDistTest): |
10 | ||
11 | _webTimeout = 2.0 | |
12 | _webServerPort = 8083 | |
13 | _webServerBasicAuthPassword = 'secret' | |
14 | _webServerAPIKey = 'apisecret' | |
15 | _config_params = ['_testServerPort', '_webServerPort', '_webServerBasicAuthPassword', '_webServerAPIKey'] | |
16 | _config_template = """ | |
17 | newServer{address="127.0.0.1:%s"} | |
18 | webserver("127.0.0.1:%s", "%s", "%s") | |
19 | """ | |
20 | ||
21 | def checkPrometheusContentBasic(self, content): | |
22 | for line in content.splitlines(): | |
23 | if line.startswith('# HELP'): | |
24 | tokens = line.split(' ') | |
25 | self.assertGreaterEqual(len(tokens), 4) | |
26 | elif line.startswith('# TYPE'): | |
27 | tokens = line.split(' ') | |
28 | self.assertEquals(len(tokens), 4) | |
29 | self.assertIn(tokens[3], ['counter', 'gauge', 'histogram']) | |
30 | elif not line.startswith('#'): | |
31 | tokens = line.split(' ') | |
32 | self.assertEquals(len(tokens), 2) | |
33 | if not line.startswith('dnsdist_'): | |
34 | raise AssertionError('Expecting prometheus metric to be prefixed by \'dnsdist_\', got: "%s"' % (line)) | |
35 | ||
36 | def checkPrometheusContentPromtool(self, content): | |
37 | output = None | |
38 | try: | |
39 | testcmd = ['promtool', 'check', 'metrics'] | |
40 | process = subprocess.Popen(testcmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) | |
41 | output = process.communicate(input=content) | |
42 | except subprocess.CalledProcessError as exc: | |
43 | raise AssertionError('%s failed (%d): %s' % (testcmd, process.returncode, process.output)) | |
44 | ||
45 | # commented out because promtool returns 3 because of the "_total" suffix warnings | |
46 | #if process.returncode != 0: | |
47 | # raise AssertionError('%s failed (%d): %s' % (testcmd, process.returncode, output)) | |
48 | ||
49 | for line in output[0].splitlines(): | |
50 | if line.endswith(b"should have \"_total\" suffix"): | |
51 | continue | |
52 | raise AssertionError('%s returned an unexpected output. Faulty line is "%s", complete content is "%s"' % (testcmd, line, output)) | |
53 | ||
54 | def testMetrics(self): | |
55 | """ | |
56 | Prometheus: Retrieve metrics | |
57 | """ | |
58 | url = 'http://127.0.0.1:' + str(self._webServerPort) + '/metrics' | |
59 | r = requests.get(url, auth=('whatever', self._webServerBasicAuthPassword), timeout=self._webTimeout) | |
60 | self.assertTrue(r) | |
61 | self.assertEquals(r.status_code, 200) | |
62 | self.checkPrometheusContentBasic(r.text) | |
63 | self.checkPrometheusContentPromtool(r.content) |