summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/test/disk_availability_test.py
blob: 0c111a46df4aaf56f409eaba184255baa72b4dba (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
import pytest

from openshift_checks.disk_availability import DiskAvailability, OpenShiftCheckException


@pytest.mark.parametrize('group_names,is_containerized,is_active', [
    (['masters'], False, True),
    # ensure check is skipped on containerized installs
    (['masters'], True, False),
    (['nodes'], False, True),
    (['etcd'], False, True),
    (['masters', 'nodes'], False, True),
    (['masters', 'etcd'], False, True),
    ([], False, False),
    (['lb'], False, False),
    (['nfs'], False, False),
])
def test_is_active(group_names, is_containerized, is_active):
    task_vars = dict(
        group_names=group_names,
        openshift=dict(common=dict(is_containerized=is_containerized)),
    )
    assert DiskAvailability.is_active(task_vars=task_vars) == is_active


@pytest.mark.parametrize('ansible_mounts,extra_words', [
    ([], ['none']),  # empty ansible_mounts
    ([{'mount': '/mnt'}], ['/mnt']),  # missing relevant mount paths
    ([{'mount': '/var'}], ['/var']),  # missing size_available
])
def test_cannot_determine_available_disk(ansible_mounts, extra_words):
    task_vars = dict(
        group_names=['masters'],
        ansible_mounts=ansible_mounts,
    )
    check = DiskAvailability(execute_module=fake_execute_module)

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

    for word in 'determine disk availability'.split() + extra_words:
        assert word 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,
    )

    check = DiskAvailability(execute_module=fake_execute_module)
    result = check.run(tmp=None, task_vars=task_vars)

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


@pytest.mark.parametrize('group_names,configured_min,ansible_mounts,extra_words', [
    (
        ['masters'],
        0,
        [{
            'mount': '/',
            'size_available': 1,
        }],
        ['0.0 GB'],
    ),
    (
        ['masters'],
        100,  # set a higher threshold
        [{
            'mount': '/',
            'size_available': 50 * 10**9,  # would normally be enough...
        }],
        ['100.0 GB'],
    ),
    (
        ['nodes'],
        0,
        [{
            'mount': '/',
            'size_available': 1 * 10**9,
        }],
        ['1.0 GB'],
    ),
    (
        ['etcd'],
        0,
        [{
            'mount': '/',
            'size_available': 1,
        }],
        ['0.0 GB'],
    ),
    (
        ['nodes', 'masters'],
        0,
        [{
            'mount': '/',
            # enough space for a node, not enough for a master
            'size_available': 15 * 10**9 + 1,
        }],
        ['15.0 GB'],
    ),
    (
        ['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'],
    ),
])
def test_fails_with_insufficient_disk_space(group_names, configured_min, ansible_mounts, extra_words):
    task_vars = dict(
        group_names=group_names,
        openshift_check_min_host_disk_gb=configured_min,
        ansible_mounts=ansible_mounts,
    )

    check = DiskAvailability(execute_module=fake_execute_module)
    result = check.run(tmp=None, task_vars=task_vars)

    assert result['failed']
    for word in 'below recommended'.split() + extra_words:
        assert word in result['msg']


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