summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/disk_availability_test.py
blob: 9ae679b79a0bd4d55f3fedadce6c7ee060dde09b (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
244
245
246
247
248
249
import pytest

from openshift_checks.disk_availability import DiskAvailability, OpenShiftCheckException


@pytest.mark.parametrize('group_names,is_active', [
    (['masters'], True),
    (['nodes'], True),
    (['etcd'], True),
    (['masters', 'nodes'], True),
    (['masters', 'etcd'], True),
    ([], False),
    (['lb'], False),
    (['nfs'], False),
])
def test_is_active(group_names, is_active):
    task_vars = dict(
        group_names=group_names,
    )
    assert DiskAvailability(None, task_vars).is_active() == is_active


@pytest.mark.parametrize('desc, ansible_mounts, expect_chunks', [
    (
        'empty ansible_mounts',
        [],
        ['determine mount point', 'none'],
    ),
    (
        'missing relevant mount paths',
        [{'mount': '/mnt'}],
        ['determine mount point', '/mnt'],
    ),
    (
        'missing size_available',
        [{'mount': '/var'}, {'mount': '/usr'}, {'mount': '/tmp'}],
        ['missing', 'size_available'],
    ),
])
def test_cannot_determine_available_disk(desc, ansible_mounts, expect_chunks):
    task_vars = dict(
        group_names=['masters'],
        ansible_mounts=ansible_mounts,
    )

    with pytest.raises(OpenShiftCheckException) as excinfo:
        DiskAvailability(fake_execute_module, task_vars).run()

    for chunk in expect_chunks:
        assert chunk in str(excinfo.value)


@pytest.mark.parametrize('group_names,configured_min,ansible_mounts', [
    (
        ['masters'],
        0,
        [{
            'mount': '/',
            'size_available': 40 * 10**9 + 1,
        }],
    ),
    (
        ['nodes'],
        0,
        [{
            'mount': '/',
            'size_available': 15 * 10**9 + 1,
        }],
    ),
    (
        ['etcd'],
        0,
        [{
            'mount': '/',
            'size_available': 20 * 10**9 + 1,
        }],
    ),
    (
        ['etcd'],
        1,  # configure lower threshold
        [{
            'mount': '/',
            'size_available': 1 * 10**9 + 1,  # way smaller than recommended
        }],
    ),
    (
        ['etcd'],
        0,
        [{
            # not enough space on / ...
            'mount': '/',
            'size_available': 2 * 10**9,
        }, {
            # ... but enough on /var
            'mount': '/var',
            'size_available': 20 * 10**9 + 1,
        }],
    ),
])
def test_succeeds_with_recommended_disk_space(group_names, configured_min, ansible_mounts):
    task_vars = dict(
        group_names=group_names,
        openshift_check_min_host_disk_gb=configured_min,
        ansible_mounts=ansible_mounts,
    )

    result = DiskAvailability(fake_execute_module, task_vars).run()

    assert not result.get('failed', False)


@pytest.mark.parametrize('name,group_names,configured_min,ansible_mounts,expect_chunks', [
    (
        'test with no space available',
        ['masters'],
        0,
        [{
            'mount': '/',
            'size_available': 1,
        }],
        ['0.0 GB'],
    ),
    (
        'test with a higher configured required value',
        ['masters'],
        100,  # set a higher threshold
        [{
            'mount': '/',
            'size_available': 50 * 10**9,  # would normally be enough...
        }],
        ['100.0 GB'],
    ),
    (
        'test with 1GB available, but "0" GB space requirement',
        ['nodes'],
        0,
        [{
            'mount': '/',
            'size_available': 1 * 10**9,
        }],
        ['1.0 GB'],
    ),
    (
        'test with no space available, but "0" GB space requirement',
        ['etcd'],
        0,
        [{
            'mount': '/',
            'size_available': 1,
        }],
        ['0.0 GB'],
    ),
    (
        'test with enough space for a node, but not for a master',
        ['nodes', 'masters'],
        0,
        [{
            'mount': '/',
            'size_available': 15 * 10**9 + 1,
        }],
        ['15.0 GB'],
    ),
    (
        'test failure with enough space on "/", but not enough on "/var"',
        ['etcd'],
        0,
        [{
            # enough space on / ...
            'mount': '/',
            'size_available': 20 * 10**9 + 1,
        }, {
            # .. but not enough on /var
            'mount': '/var',
            'size_available': 0,
        }],
        ['0.0 GB'],
    ),
], ids=lambda argval: argval[0])
def test_fails_with_insufficient_disk_space(name, group_names, configured_min, ansible_mounts, expect_chunks):
    task_vars = dict(
        group_names=group_names,
        openshift_check_min_host_disk_gb=configured_min,
        ansible_mounts=ansible_mounts,
    )

    check = DiskAvailability(fake_execute_module, task_vars)
    check.run()

    assert check.failures
    for chunk in 'below recommended'.split() + expect_chunks:
        assert chunk in str(check.failures[0])


@pytest.mark.parametrize('name,group_names,context,ansible_mounts,failed,extra_words', [
    (
        'test without enough space for master under "upgrade" context',
        ['nodes', 'masters'],
        "upgrade",
        [{
            'mount': '/',
            'size_available': 1 * 10**9 + 1,
            'size_total': 21 * 10**9 + 1,
        }],
        True,
        ["1.0 GB"],
    ),
    (
        'test with enough space for master under "upgrade" context',
        ['nodes', 'masters'],
        "upgrade",
        [{
            'mount': '/',
            'size_available': 10 * 10**9 + 1,
            'size_total': 21 * 10**9 + 1,
        }],
        False,
        [],
    ),
    (
        'test with not enough space for master, and non-upgrade context',
        ['nodes', 'masters'],
        "health",
        [{
            'mount': '/',
            # not enough space for a master,
            # "health" context should not lower requirement
            'size_available': 20 * 10**9 + 1,
        }],
        True,
        ["20.0 GB", "below minimum"],
    ),
], ids=lambda argval: argval[0])
def test_min_required_space_changes_with_upgrade_context(name, group_names, context, ansible_mounts, failed, extra_words):
    task_vars = dict(
        r_openshift_health_checker_playbook_context=context,
        group_names=group_names,
        ansible_mounts=ansible_mounts,
    )

    check = DiskAvailability(fake_execute_module, task_vars)
    check.run()

    assert bool(check.failures) == failed
    for word in extra_words:
        assert word in str(check.failures[0])


def fake_execute_module(*args):
    raise AssertionError('this function should not be called')