summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/docker_storage_test.py
blob: 73c43338372d5b9ccc7e2ca143c176f7814c148d (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import pytest
import json


from openshift_checks.docker_storage import DockerStorage, OpenShiftCheckException


@pytest.mark.parametrize('is_containerized,is_active', [
    (False, False),
    (True, True),
])
def test_is_active(is_containerized, is_active):
    task_vars = dict(
        openshift=dict(common=dict(is_containerized=is_containerized)),
    )
    assert DockerStorage.is_active(task_vars=task_vars) == is_active


@pytest.mark.parametrize('stdout,message,failed,extra_words', [
    (None, "", True, ["no thinpool usage data"]),
    ("", "", False, ["Invalid JSON value returned by lvs command"]),
    (None, "invalid response", True, ["invalid response"]),
    ("invalid", "invalid response", False, ["Invalid JSON value"]),
])
def test_get_lvs_data_with_failed_response(stdout, message, failed, extra_words):
    def execute_module(module_name, args, tmp=None, task_vars=None):
        if module_name != "command":
            return {
                "changed": False,
            }

        response = {
            "stdout": stdout,
            "msg": message,
            "failed": failed,
        }

        if stdout is None:
            response.pop("stdout")

        return response

    task_vars = dict(
        max_thinpool_data_usage_percent=90.0
    )

    check = DockerStorage(execute_module=execute_module)
    with pytest.raises(OpenShiftCheckException) as excinfo:
        check.run(tmp=None, task_vars=task_vars)

    for word in extra_words:
        assert word in str(excinfo.value)


@pytest.mark.parametrize('limit_percent,failed,extra_words', [
    ("90.0", False, []),
    (80.0, False, []),
    ("invalid percent", True, ["Unable to convert", "to float", "invalid percent"]),
    ("90%", True, ["Unable to convert", "to float", "90%"]),
])
def test_invalid_value_for_thinpool_usage_limit(limit_percent, failed, extra_words):
    def execute_module(module_name, args, tmp=None, task_vars=None):
        if module_name != "command":
            return {
                "changed": False,
            }

        return {
            "stdout": json.dumps({
                "report": [
                    {
                        "lv": [
                            {"lv_name": "docker-pool", "vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                             "pool_lv": "", "origin": "", "data_percent": "58.96", "metadata_percent": "4.77",
                             "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""},
                        ]
                    }
                ]
            }),
            "failed": False,
        }

    task_vars = dict(
        max_thinpool_data_usage_percent=limit_percent
    )

    check = DockerStorage(execute_module=execute_module).run(tmp=None, task_vars=task_vars)

    if failed:
        assert check["failed"]

        for word in extra_words:
            assert word in check["msg"]
    else:
        assert not check.get("failed", False)


def test_get_lvs_data_with_valid_response():
    def execute_module(module_name, args, tmp=None, task_vars=None):
        if module_name != "command":
            return {
                "changed": False,
            }

        return {
            "stdout": json.dumps({
                "report": [
                    {
                        "lv": [
                            {"lv_name": "docker-pool", "vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                             "pool_lv": "", "origin": "", "data_percent": "58.96", "metadata_percent": "4.77",
                             "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""}
                        ]
                    }
                ]
            })
        }

    task_vars = dict(
        max_thinpool_data_usage_percent="90"
    )

    check = DockerStorage(execute_module=execute_module).run(tmp=None, task_vars=task_vars)
    assert not check.get("failed", False)


@pytest.mark.parametrize('response,extra_words', [
    (
        {
            "report": [{}],
        },
        ["no thinpool usage data"],
    ),
    (
        {
            "report": [
                {
                    "lv": [
                        {"vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                         "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""}
                    ]
                }
            ],
        },
        ["no thinpool usage data"],
    ),
    (
        {
            "report": [
                {
                    "lv": [],
                }
            ],
        },
        ["no thinpool usage data"],
    ),
    (
        {
            "report": [
                {
                    "lv": [
                        {"lv_name": "docker-pool", "vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                         "pool_lv": "", "origin": "", "data_percent": "58.96",
                         "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""}
                    ]
                }
            ],
        },
        ["no thinpool usage data"],
    ),
])
def test_get_lvs_data_with_incomplete_response(response, extra_words):
    def execute_module(module_name, args, tmp=None, task_vars=None):
        if module_name != "command":
            return {
                "changed": False,
            }

        return {
            "stdout": json.dumps(response)
        }

    task_vars = dict(
        max_thinpool_data_usage_percent=90.0
    )

    check = DockerStorage(execute_module=execute_module)
    with pytest.raises(OpenShiftCheckException) as excinfo:
        check.run(tmp=None, task_vars=task_vars)

    assert "no thinpool usage data" in str(excinfo.value)


@pytest.mark.parametrize('response,extra_words', [
    (
        {
            "report": [
                {
                    "lv": [
                        {"lv_name": "docker-pool", "vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                         "pool_lv": "", "origin": "", "data_percent": "100.0", "metadata_percent": "90.0",
                         "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""}
                    ]
                }
            ],
        },
        ["thinpool data usage above maximum threshold"],
    ),
    (
        {
            "report": [
                {
                    "lv": [
                        {"lv_name": "docker-pool", "vg_name": "docker", "lv_attr": "twi-aot---", "lv_size": "6.95g",
                         "pool_lv": "", "origin": "", "data_percent": "10.0", "metadata_percent": "91.0",
                         "move_pv": "", "mirror_log": "", "copy_percent": "", "convert_lv": ""}
                    ]
                }
            ],
        },
        ["thinpool metadata usage above maximum threshold"],
    ),
])
def test_get_lvs_data_with_high_thinpool_usage(response, extra_words):
    def execute_module(module_name, args, tmp=None, task_vars=None):
        if module_name != "command":
            return {
                "changed": False,
            }

        return {
            "stdout": json.dumps(response),
        }

    task_vars = dict(
        max_thinpool_data_usage_percent="90"
    )

    check = DockerStorage(execute_module=execute_module).run(tmp=None, task_vars=task_vars)

    assert check["failed"]
    for word in extra_words:
        assert word in check["msg"]