summaryrefslogtreecommitdiff
path: root/tests/unit/test_prometheus.py
blob: 1f264839913f92dbc3ed8f2e8b69814404a774c8 (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
# Copyright 2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import requests

from tests.base import ZuulTestCase


class BaseTestPrometheus(ZuulTestCase):
    config_file = 'zuul-prometheus.conf'
    tenant_config_file = 'config/single-tenant/main.yaml'

    def get_path(self, path):
        return requests.get(
            "http://localhost:%d%s" % (
                self.scheds.first.sched.monitoring_server.port,
                path))

    def get_metrics(self, path=''):
        metrics = {}
        r = self.get_path(path)
        for line in r.text.split('\n'):
            if not line or line.startswith("#"):
                continue
            try:
                key, value = line.split()
            except ValueError:
                continue
            metrics[key] = value
        return metrics


class TestPrometheus(BaseTestPrometheus):
    def test_prometheus_process_metrics(self):
        metrics = self.get_metrics()
        self.assertIn("process_resident_memory_bytes", metrics)
        self.assertIn("process_open_fds", metrics)
        metrics = self.get_metrics('/metrics')
        self.assertIn("process_resident_memory_bytes", metrics)
        self.assertIn("process_open_fds", metrics)

    def test_health(self):
        r = self.get_path('/health/live')
        self.assertEqual(r.status_code, 200)
        r = self.get_path('/health/ready')
        self.assertEqual(r.status_code, 200)
        r = self.get_path('/health/status')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, 'RUNNING')
        r = self.get_path('/dne')
        self.assertEqual(r.status_code, 404)

        self.scheds.first.sched.component_info.state = \
            self.scheds.first.sched.component_info.INITIALIZING

        r = self.get_path('/health/live')
        self.assertEqual(r.status_code, 200)
        r = self.get_path('/health/ready')
        self.assertEqual(r.status_code, 503)
        r = self.get_path('/health/status')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, 'INITIALIZING')