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

from openshift_checks.logging.fluentd import Fluentd, OpenShiftCheckExceptionList, OpenShiftCheckException


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))


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"}],
    }
}
fluentd_pod_node2_down = {
    "metadata": {
        "labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
        "name": "logging-fluentd-2",
    },
    "spec": {"host": "node2", "nodeName": "node2"},
    "status": {
        "containerStatuses": [{"ready": False}],
        "conditions": [{"status": "False", "type": "Ready"}],
    }
}
fluentd_node1 = {
    "metadata": {
        "labels": {"logging-infra-fluentd": "true", "kubernetes.io/hostname": "node1"},
        "name": "node1",
    },
    "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.1"}]},
}
fluentd_node2 = {
    "metadata": {
        "labels": {"logging-infra-fluentd": "true", "kubernetes.io/hostname": "hostname"},
        "name": "node2",
    },
    "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.2"}]},
}
fluentd_node3_unlabeled = {
    "metadata": {
        "labels": {"kubernetes.io/hostname": "hostname"},
        "name": "node3",
    },
    "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.3"}]},
}


def test_get_fluentd_pods():
    check = Fluentd()
    check.exec_oc = lambda *_: json.dumps(dict(items=[fluentd_node1]))
    check.get_pods_for_component = lambda *_: [fluentd_pod_node1]
    assert not check.run()


@pytest.mark.parametrize('pods, nodes, expect_error', [
    (
        [],
        [],
        'NoNodesDefined',
    ),
    (
        [],
        [fluentd_node3_unlabeled],
        'NoNodesLabeled',
    ),
    (
        [],
        [fluentd_node1, fluentd_node3_unlabeled],
        'NodesUnlabeled',
    ),
    (
        [],
        [fluentd_node2],
        'MissingFluentdPod',
    ),
    (
        [fluentd_pod_node1, fluentd_pod_node1],
        [fluentd_node1],
        'TooManyFluentdPods',
    ),
    (
        [fluentd_pod_node2_down],
        [fluentd_node2],
        'FluentdNotRunning',
    ),
])
def test_get_fluentd_pods_errors(pods, nodes, expect_error):
    check = Fluentd()
    check.exec_oc = lambda *_: json.dumps(dict(items=nodes))

    with pytest.raises(OpenShiftCheckException) as excinfo:
        check.check_fluentd(pods)
    if isinstance(excinfo.value, OpenShiftCheckExceptionList):
        assert_error_in_list(expect_error, excinfo.value)
    else:
        assert expect_error == excinfo.value.name


def test_bad_oc_node_list():
    check = Fluentd()
    check.exec_oc = lambda *_: "this isn't even json"
    with pytest.raises(OpenShiftCheckException) as excinfo:
        check.get_nodes_by_name()
    assert 'BadOcNodeList' == excinfo.value.name