summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/elasticsearch_test.py
blob: 09bacd9ac0b16f5dd3ed1ac1283a84df956a4c5d (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
import pytest
import json

from openshift_checks.logging.elasticsearch import Elasticsearch, OpenShiftCheckExceptionList


task_vars_config_base = dict(openshift=dict(common=dict(config_base='/etc/origin')))


def canned_elasticsearch(task_vars=None, exec_oc=None):
    """Create an Elasticsearch check object with stubbed exec_oc method"""
    check = Elasticsearch(None, task_vars or {})
    if exec_oc:
        check.exec_oc = exec_oc
    return check


def assert_error_in_list(expect_err, errorlist):
    assert any(err.name == expect_err for err in errorlist), "{} in {}".format(str(expect_err), str(errorlist))


def pods_by_name(pods):
    return {pod['metadata']['name']: pod for pod in pods}


plain_es_pod = {
    "metadata": {
        "labels": {"component": "es", "deploymentconfig": "logging-es"},
        "name": "logging-es",
    },
    "spec": {},
    "status": {
        "conditions": [{"status": "True", "type": "Ready"}],
        "containerStatuses": [{"ready": True}],
        "podIP": "10.10.10.10",
    },
    "_test_master_name_str": "name logging-es",
}

split_es_pod = {
    "metadata": {
        "labels": {"component": "es", "deploymentconfig": "logging-es-2"},
        "name": "logging-es-2",
    },
    "spec": {},
    "status": {
        "conditions": [{"status": "True", "type": "Ready"}],
        "containerStatuses": [{"ready": True}],
        "podIP": "10.10.10.10",
    },
    "_test_master_name_str": "name logging-es-2",
}

unready_es_pod = {
    "metadata": {
        "labels": {"component": "es", "deploymentconfig": "logging-es-3"},
        "name": "logging-es-3",
    },
    "spec": {},
    "status": {
        "conditions": [{"status": "False", "type": "Ready"}],
        "containerStatuses": [{"ready": False}],
        "podIP": "10.10.10.10",
    },
    "_test_master_name_str": "BAD_NAME_RESPONSE",
}


def test_check_elasticsearch():
    with pytest.raises(OpenShiftCheckExceptionList) as excinfo:
        canned_elasticsearch().check_elasticsearch([])
    assert_error_in_list('NoRunningPods', excinfo.value)

    # canned oc responses to match so all the checks pass
    def exec_oc(cmd, args):
        if '_cat/master' in cmd:
            return 'name logging-es'
        elif '/_nodes' in cmd:
            return json.dumps(es_node_list)
        elif '_cluster/health' in cmd:
            return '{"status": "green"}'
        elif ' df ' in cmd:
            return 'IUse% Use%\n 3%  4%\n'
        else:
            raise Exception(cmd)

    check = canned_elasticsearch({}, exec_oc)
    check.get_pods_for_component = lambda *_: [plain_es_pod]
    assert {} == check.run()


def test_check_running_es_pods():
    pods, errors = Elasticsearch().running_elasticsearch_pods([plain_es_pod, unready_es_pod])
    assert plain_es_pod in pods
    assert_error_in_list('PodNotRunning', errors)


def test_check_elasticsearch_masters():
    pods = [plain_es_pod]
    check = canned_elasticsearch(task_vars_config_base, lambda *_: plain_es_pod['_test_master_name_str'])
    assert not check.check_elasticsearch_masters(pods_by_name(pods))


@pytest.mark.parametrize('pods, expect_error', [
    (
        [],
        'NoMasterFound',
    ),
    (
        [unready_es_pod],
        'NoMasterName',
    ),
    (
        [plain_es_pod, split_es_pod],
        'SplitBrainMasters',
    ),
])
def test_check_elasticsearch_masters_error(pods, expect_error):
    test_pods = list(pods)
    check = canned_elasticsearch(task_vars_config_base, lambda *_: test_pods.pop(0)['_test_master_name_str'])
    assert_error_in_list(expect_error, check.check_elasticsearch_masters(pods_by_name(pods)))


es_node_list = {
    'nodes': {
        'random-es-name': {
            'host': 'logging-es',
        }}}


def test_check_elasticsearch_node_list():
    check = canned_elasticsearch(task_vars_config_base, lambda *_: json.dumps(es_node_list))
    assert not check.check_elasticsearch_node_list(pods_by_name([plain_es_pod]))


@pytest.mark.parametrize('pods, node_list, expect_error', [
    (
        [],
        {},
        'MissingComponentPods',
    ),
    (
        [plain_es_pod],
        {},  # empty list of nodes triggers KeyError
        'MissingNodeList',
    ),
    (
        [split_es_pod],
        es_node_list,
        'EsPodNodeMismatch',
    ),
])
def test_check_elasticsearch_node_list_errors(pods, node_list, expect_error):
    check = canned_elasticsearch(task_vars_config_base, lambda cmd, args: json.dumps(node_list))
    assert_error_in_list(expect_error, check.check_elasticsearch_node_list(pods_by_name(pods)))


def test_check_elasticsearch_cluster_health():
    test_health_data = [{"status": "green"}]
    check = canned_elasticsearch(exec_oc=lambda *_: json.dumps(test_health_data.pop(0)))
    assert not check.check_es_cluster_health(pods_by_name([plain_es_pod]))


@pytest.mark.parametrize('pods, health_data, expect_error', [
    (
        [plain_es_pod],
        [{"no-status": "should bomb"}],
        'BadEsResponse',
    ),
    (
        [plain_es_pod, split_es_pod],
        [{"status": "green"}, {"status": "red"}],
        'EsClusterHealthRed',
    ),
])
def test_check_elasticsearch_cluster_health_errors(pods, health_data, expect_error):
    test_health_data = list(health_data)
    check = canned_elasticsearch(exec_oc=lambda *_: json.dumps(test_health_data.pop(0)))
    assert_error_in_list(expect_error, check.check_es_cluster_health(pods_by_name(pods)))


def test_check_elasticsearch_diskspace():
    check = canned_elasticsearch(exec_oc=lambda *_: 'IUse% Use%\n 3%  4%\n')
    assert not check.check_elasticsearch_diskspace(pods_by_name([plain_es_pod]))


@pytest.mark.parametrize('disk_data, expect_error', [
    (
        'df: /elasticsearch/persistent: No such file or directory\n',
        'BadDfResponse',
    ),
    (
        'IUse% Use%\n 95%  40%\n',
        'InodeUsageTooHigh',
    ),
    (
        'IUse% Use%\n 3%  94%\n',
        'DiskUsageTooHigh',
    ),
])
def test_check_elasticsearch_diskspace_errors(disk_data, expect_error):
    check = canned_elasticsearch(exec_oc=lambda *_: disk_data)
    assert_error_in_list(expect_error, check.check_elasticsearch_diskspace(pods_by_name([plain_es_pod])))