summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/kibana_test.py
blob: 19140a1b6cfa5dd149ee25e9d4c5ab313d7448ec (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import pytest
import json

try:
    import urllib2
    from urllib2 import HTTPError, URLError
except ImportError:
    from urllib.error import HTTPError, URLError
    import urllib.request as urllib2

from openshift_checks.logging.kibana import Kibana


def canned_kibana(exec_oc=None):
    """Create a Kibana check object with canned exec_oc method"""
    check = Kibana("dummy")  # fails if a module is actually invoked
    if exec_oc:
        check._exec_oc = exec_oc
    return check


def assert_error(error, expect_error):
    if expect_error:
        assert error
        assert expect_error in error
    else:
        assert not error


plain_kibana_pod = {
    "metadata": {
        "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
        "name": "logging-kibana-1",
    },
    "status": {
        "containerStatuses": [{"ready": True}, {"ready": True}],
        "conditions": [{"status": "True", "type": "Ready"}],
    }
}
not_running_kibana_pod = {
    "metadata": {
        "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
        "name": "logging-kibana-2",
    },
    "status": {
        "containerStatuses": [{"ready": True}, {"ready": False}],
        "conditions": [{"status": "True", "type": "Ready"}],
    }
}


@pytest.mark.parametrize('pods, expect_error', [
    (
        [],
        "There are no Kibana pods deployed",
    ),
    (
        [plain_kibana_pod],
        None,
    ),
    (
        [not_running_kibana_pod],
        "No Kibana pod is in a running state",
    ),
    (
        [plain_kibana_pod, not_running_kibana_pod],
        "The following Kibana pods are not currently in a running state",
    ),
])
def test_check_kibana(pods, expect_error):
    check = canned_kibana()
    error = check.check_kibana(pods)
    assert_error(error, expect_error)


@pytest.mark.parametrize('route, expect_url, expect_error', [
    (
        None,
        None,
        'no_route_exists',
    ),

    # test route with no ingress
    (
        {
            "metadata": {
                "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
                "name": "logging-kibana",
            },
            "status": {
                "ingress": [],
            },
            "spec": {
                "host": "hostname",
            }
        },
        None,
        'route_not_accepted',
    ),

    # test route with no host
    (
        {
            "metadata": {
                "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
                "name": "logging-kibana",
            },
            "status": {
                "ingress": [{
                    "status": True,
                }],
            },
            "spec": {},
        },
        None,
        'route_missing_host',
    ),

    # test route that looks fine
    (
        {
            "metadata": {
                "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
                "name": "logging-kibana",
            },
            "status": {
                "ingress": [{
                    "status": True,
                }],
            },
            "spec": {
                "host": "hostname",
            },
        },
        "https://hostname/",
        None,
    ),
])
def test_get_kibana_url(route, expect_url, expect_error):
    check = canned_kibana(lambda cmd, args, task_vars: json.dumps(route) if route else "")

    url, error = check._get_kibana_url({})
    if expect_url:
        assert url == expect_url
    else:
        assert not url
    if expect_error:
        assert error == expect_error
    else:
        assert not error


@pytest.mark.parametrize('exec_result, expect', [
    (
        'urlopen error [Errno 111] Connection refused',
        'at least one router routing to it?',
    ),
    (
        'urlopen error [Errno -2] Name or service not known',
        'DNS configured for the Kibana hostname?',
    ),
    (
        'Status code was not [302]: HTTP Error 500: Server error',
        'did not return the correct status code',
    ),
    (
        'bork bork bork',
        'bork bork bork',  # should pass through
    ),
])
def test_verify_url_internal_failure(exec_result, expect):
    check = Kibana(execute_module=lambda module_name, args, task_vars: dict(failed=True, msg=exec_result))
    check._get_kibana_url = lambda task_vars: ('url', None)

    error = check._check_kibana_route({})
    assert_error(error, expect)


@pytest.mark.parametrize('lib_result, expect', [
    (
        HTTPError('url', 500, "it broke", hdrs=None, fp=None),
        'it broke',
    ),
    (
        URLError('it broke'),
        'it broke',
    ),
    (
        302,
        'returned the wrong error code',
    ),
    (
        200,
        None,
    ),
])
def test_verify_url_external_failure(lib_result, expect, monkeypatch):

    class _http_return:

        def __init__(self, code):
            self.code = code

        def getcode(self):
            return self.code

    def urlopen(url, context):
        if type(lib_result) is int:
            return _http_return(lib_result)
        raise lib_result
    monkeypatch.setattr(urllib2, 'urlopen', urlopen)

    check = canned_kibana()
    check._get_kibana_url = lambda task_vars: ('url', None)
    check._verify_url_internal = lambda url, task_vars: None

    error = check._check_kibana_route({})
    assert_error(error, expect)