]>
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' | |
2c0392a5 | 14 | _webServerBasicAuthPasswordHashed = '$scrypt$ln=10,p=1,r=8$6DKLnvUYEeXWh3JNOd3iwg==$kSrhdHaRbZ7R74q3lGBqO1xetgxRxhmWzYJ2Qvfm7JM=' |
116b2602 | 15 | _webServerAPIKey = 'apisecret' |
2c0392a5 | 16 | _webServerAPIKeyHashed = '$scrypt$ln=10,p=1,r=8$9v8JxDfzQVyTpBkTbkUqYg==$bDQzAOHeK1G9UvTPypNhrX48w974ZXbFPtRKS34+aso=' |
412f99ef | 17 | _config_params = ['_testServerPort', '_webServerPort', '_webServerBasicAuthPasswordHashed', '_webServerAPIKeyHashed'] |
116b2602 RG |
18 | _config_template = """ |
19 | newServer{address="127.0.0.1:%s"} | |
fa7e8b5d | 20 | webserver("127.0.0.1:%s") |
cfe95ada | 21 | setWebserverConfig({password="%s", apiKey="%s"}) |
116b2602 RG |
22 | """ |
23 | ||
24 | def checkPrometheusContentBasic(self, content): | |
25 | for line in content.splitlines(): | |
26 | if line.startswith('# HELP'): | |
27 | tokens = line.split(' ') | |
28 | self.assertGreaterEqual(len(tokens), 4) | |
29 | elif line.startswith('# TYPE'): | |
30 | tokens = line.split(' ') | |
4bfebc93 | 31 | self.assertEqual(len(tokens), 4) |
116b2602 RG |
32 | self.assertIn(tokens[3], ['counter', 'gauge', 'histogram']) |
33 | elif not line.startswith('#'): | |
34 | tokens = line.split(' ') | |
4bfebc93 | 35 | self.assertEqual(len(tokens), 2) |
116b2602 RG |
36 | if not line.startswith('dnsdist_'): |
37 | raise AssertionError('Expecting prometheus metric to be prefixed by \'dnsdist_\', got: "%s"' % (line)) | |
38 | ||
39 | def checkPrometheusContentPromtool(self, content): | |
40 | output = None | |
41 | try: | |
42 | testcmd = ['promtool', 'check', 'metrics'] | |
43 | process = subprocess.Popen(testcmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) | |
44 | output = process.communicate(input=content) | |
45 | except subprocess.CalledProcessError as exc: | |
46 | raise AssertionError('%s failed (%d): %s' % (testcmd, process.returncode, process.output)) | |
47 | ||
48 | # commented out because promtool returns 3 because of the "_total" suffix warnings | |
49 | #if process.returncode != 0: | |
50 | # raise AssertionError('%s failed (%d): %s' % (testcmd, process.returncode, output)) | |
51 | ||
52 | for line in output[0].splitlines(): | |
53 | if line.endswith(b"should have \"_total\" suffix"): | |
54 | continue | |
55 | raise AssertionError('%s returned an unexpected output. Faulty line is "%s", complete content is "%s"' % (testcmd, line, output)) | |
56 | ||
57 | def testMetrics(self): | |
58 | """ | |
59 | Prometheus: Retrieve metrics | |
60 | """ | |
61 | url = 'http://127.0.0.1:' + str(self._webServerPort) + '/metrics' | |
62 | r = requests.get(url, auth=('whatever', self._webServerBasicAuthPassword), timeout=self._webTimeout) | |
63 | self.assertTrue(r) | |
4bfebc93 | 64 | self.assertEqual(r.status_code, 200) |
116b2602 RG |
65 | self.checkPrometheusContentBasic(r.text) |
66 | self.checkPrometheusContentPromtool(r.content) |