summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/logging_check_test.py
blob: 59c7032142bc519cabc408d776bce6878f45c359 (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
import pytest
import json

from openshift_checks.logging.logging import LoggingCheck, MissingComponentPods, CouldNotUseOc

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


def canned_loggingcheck(exec_oc=None, execute_module=None):
    """Create a LoggingCheck object with canned exec_oc method"""
    check = LoggingCheck(execute_module)
    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_es_pod = {
    "metadata": {
        "labels": {"component": "es", "deploymentconfig": "logging-es"},
        "name": "logging-es",
    },
    "status": {
        "conditions": [{"status": "True", "type": "Ready"}],
        "containerStatuses": [{"ready": True}],
        "podIP": "10.10.10.10",
    },
    "_test_master_name_str": "name logging-es",
}

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"}],
    }
}

plain_kibana_pod_no_containerstatus = {
    "metadata": {
        "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
        "name": "logging-kibana-1",
    },
    "status": {
        "conditions": [{"status": "True", "type": "Ready"}],
    }
}

fluentd_pod_node1 = {
    "metadata": {
        "labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
        "name": "logging-fluentd-1",
    },
    "spec": {"host": "node1", "nodeName": "node1"},
    "status": {
        "containerStatuses": [{"ready": True}],
        "conditions": [{"status": "True", "type": "Ready"}],
    }
}

plain_curator_pod = {
    "metadata": {
        "labels": {"component": "curator", "deploymentconfig": "logging-curator"},
        "name": "logging-curator-1",
    },
    "status": {
        "containerStatuses": [{"ready": True}],
        "conditions": [{"status": "True", "type": "Ready"}],
        "podIP": "10.10.10.10",
    }
}


@pytest.mark.parametrize('problem, expect', [
    ("[Errno 2] No such file or directory", "supposed to be a master"),
    ("Permission denied", "Unexpected error using `oc`"),
])
def test_oc_failure(problem, expect):
    def execute_module(module_name, *_):
        if module_name == "ocutil":
            return dict(failed=True, result=problem)
        return dict(changed=False)

    check = LoggingCheck(execute_module, task_vars_config_base)

    with pytest.raises(CouldNotUseOc) as excinfo:
        check.exec_oc('get foo', [])
    assert expect in str(excinfo)


groups_with_first_master = dict(oo_first_master=['this-host'])
groups_not_a_master = dict(oo_first_master=['other-host'], oo_masters=['other-host'])


@pytest.mark.parametrize('groups, logging_deployed, is_active', [
    (groups_with_first_master, True, True),
    (groups_with_first_master, False, False),
    (groups_not_a_master, True, False),
    (groups_not_a_master, True, False),
])
def test_is_active(groups, logging_deployed, is_active):
    task_vars = dict(
        ansible_host='this-host',
        groups=groups,
        openshift_hosted_logging_deploy=logging_deployed,
    )

    assert LoggingCheck(None, task_vars).is_active() == is_active


@pytest.mark.parametrize('pod_output, expect_pods', [
    (
        json.dumps({'items': [plain_es_pod]}),
        [plain_es_pod],
    ),
])
def test_get_pods_for_component(pod_output, expect_pods):
    check = canned_loggingcheck(lambda *_: pod_output)
    pods = check.get_pods_for_component("es")
    assert pods == expect_pods


@pytest.mark.parametrize('exec_oc_output, expect_error', [
    (
        'No resources found.',
        MissingComponentPods,
    ),
    (
        '{"items": null}',
        MissingComponentPods,
    ),
])
def test_get_pods_for_component_fail(exec_oc_output, expect_error):
    check = canned_loggingcheck(lambda *_: exec_oc_output)
    with pytest.raises(expect_error):
        check.get_pods_for_component("es")


@pytest.mark.parametrize('name, pods, expected_pods', [
    (
        'test single pod found, scheduled, but no containerStatuses field',
        [plain_kibana_pod_no_containerstatus],
        [plain_kibana_pod_no_containerstatus],
    ),
    (
        'set of pods has at least one pod with containerStatuses (scheduled); should still fail',
        [plain_kibana_pod_no_containerstatus, plain_kibana_pod],
        [plain_kibana_pod_no_containerstatus],
    ),

], ids=lambda argvals: argvals[0])
def test_get_not_running_pods_no_container_status(name, pods, expected_pods):
    check = canned_loggingcheck(lambda *_: '')
    result = check.not_running_pods(pods)

    assert result == expected_pods