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

from openshift_checks.memory_availability import MemoryAvailability


@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 MemoryAvailability(None, task_vars).is_active() == is_active


@pytest.mark.parametrize('group_names,configured_min,ansible_memtotal_mb', [
    (
        ['masters'],
        0,
        17200,
    ),
    (
        ['nodes'],
        0,
        8200,
    ),
    (
        ['nodes'],
        1,  # configure lower threshold
        2000,  # too low for recommended but not for configured
    ),
    (
        ['nodes'],
        2,  # configure threshold where adjustment pushes it over
        1900,
    ),
    (
        ['etcd'],
        0,
        8200,
    ),
    (
        ['masters', 'nodes'],
        0,
        17000,
    ),
])
def test_succeeds_with_recommended_memory(group_names, configured_min, ansible_memtotal_mb):
    task_vars = dict(
        group_names=group_names,
        openshift_check_min_host_memory_gb=configured_min,
        ansible_memtotal_mb=ansible_memtotal_mb,
    )

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

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


@pytest.mark.parametrize('group_names,configured_min,ansible_memtotal_mb,extra_words', [
    (
        ['masters'],
        0,
        0,
        ['0.0 GiB'],
    ),
    (
        ['nodes'],
        0,
        100,
        ['0.1 GiB'],
    ),
    (
        ['nodes'],
        24,  # configure higher threshold
        20 * 1024,  # enough to meet recommended but not configured
        ['20.0 GiB'],
    ),
    (
        ['nodes'],
        24,  # configure higher threshold
        22 * 1024,  # not enough for adjustment to push over threshold
        ['22.0 GiB'],
    ),
    (
        ['etcd'],
        0,
        6 * 1024,
        ['6.0 GiB'],
    ),
    (
        ['etcd', 'masters'],
        0,
        9 * 1024,  # enough memory for etcd, not enough for a master
        ['9.0 GiB'],
    ),
    (
        ['nodes', 'masters'],
        0,
        # enough memory for a node, not enough for a master
        11 * 1024,
        ['11.0 GiB'],
    ),
])
def test_fails_with_insufficient_memory(group_names, configured_min, ansible_memtotal_mb, extra_words):
    task_vars = dict(
        group_names=group_names,
        openshift_check_min_host_memory_gb=configured_min,
        ansible_memtotal_mb=ansible_memtotal_mb,
    )

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

    assert result.get('failed', False)
    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')