summaryrefslogtreecommitdiffstats
path: root/tests/functional/heketi/test_heketi_metrics.py
blob: 65a8c2ec169e1f1110a38ef380bc5c5b005d9d99 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from openshiftstoragelibs.baseclass import BaseClass
from openshiftstoragelibs.heketi_ops import (
    get_heketi_metrics,
    heketi_cluster_info,
    heketi_cluster_list,
    heketi_topology_info,
    heketi_volume_create,
    heketi_volume_delete,
    heketi_volume_list,
)
from openshiftstoragelibs import heketi_version
from openshiftstoragelibs.openshift_ops import (
    get_pod_name_from_dc,
    scale_dc_pod_amount_and_wait,
    wait_for_pod_be_ready,
)


class TestHeketiMetrics(BaseClass):

    def setUp(self):
        super(TestHeketiMetrics, self).setUp()
        self.node = self.ocp_master_node[0]
        version = heketi_version.get_heketi_version(self.heketi_client_node)
        if version < '6.0.0-14':
            self.skipTest("heketi-client package %s does not support heketi "
                          "metrics functionality" % version.v_str)

    def verify_heketi_metrics_with_topology_info(self):
        topology = heketi_topology_info(
            self.heketi_client_node, self.heketi_server_url, json=True)

        metrics = get_heketi_metrics(
            self.heketi_client_node, self.heketi_server_url)

        self.assertTrue(topology)
        self.assertIn('clusters', list(topology.keys()))
        self.assertGreater(len(topology['clusters']), 0)

        self.assertTrue(metrics)
        self.assertGreater(len(metrics.keys()), 0)

        self.assertEqual(
            len(topology['clusters']), metrics['heketi_cluster_count'])

        for cluster in topology['clusters']:
            self.assertIn('nodes', list(cluster.keys()))
            self.assertGreater(len(cluster['nodes']), 0)

            cluster_id = cluster['id']

            cluster_ids = ([obj['cluster']
                           for obj in metrics['heketi_nodes_count']])
            self.assertIn(cluster_id, cluster_ids)
            for node_count in metrics['heketi_nodes_count']:
                if node_count['cluster'] == cluster_id:
                    self.assertEqual(
                        len(cluster['nodes']), node_count['value'])

            cluster_ids = ([obj['cluster']
                           for obj in metrics['heketi_volumes_count']])
            self.assertIn(cluster_id, cluster_ids)
            for vol_count in metrics['heketi_volumes_count']:
                if vol_count['cluster'] == cluster_id:
                    self.assertEqual(
                        len(cluster['volumes']), vol_count['value'])

            for node in cluster['nodes']:
                self.assertIn('devices', list(node.keys()))
                self.assertGreater(len(node['devices']), 0)

                hostname = node['hostnames']['manage'][0]

                cluster_ids = ([obj['cluster']
                               for obj in metrics['heketi_device_count']])
                self.assertIn(cluster_id, cluster_ids)
                hostnames = ([obj['hostname']
                             for obj in metrics['heketi_device_count']])
                self.assertIn(hostname, hostnames)
                for device_count in metrics['heketi_device_count']:
                    if (device_count['cluster'] == cluster_id
                            and device_count['hostname'] == hostname):
                        self.assertEqual(
                            len(node['devices']), device_count['value'])

                for device in node['devices']:
                    device_name = device['name']
                    device_size_t = device['storage']['total']
                    device_free_t = device['storage']['free']
                    device_used_t = device['storage']['used']

                    cluster_ids = ([obj['cluster']
                                   for obj in
                                   metrics['heketi_device_brick_count']])
                    self.assertIn(cluster_id, cluster_ids)
                    hostnames = ([obj['hostname']
                                 for obj in
                                 metrics['heketi_device_brick_count']])
                    self.assertIn(hostname, hostnames)
                    devices = ([obj['device']
                               for obj in
                               metrics['heketi_device_brick_count']])
                    self.assertIn(device_name, devices)
                    for brick_count in metrics['heketi_device_brick_count']:
                        if (brick_count['cluster'] == cluster_id
                                and brick_count['hostname'] == hostname
                                and brick_count['device'] == device_name):
                            self.assertEqual(
                                len(device['bricks']), brick_count['value'])

                    cluster_ids = ([obj['cluster']
                                   for obj in metrics['heketi_device_size']])
                    self.assertIn(cluster_id, cluster_ids)
                    hostnames = ([obj['hostname']
                                 for obj in metrics['heketi_device_size']])
                    self.assertIn(hostname, hostnames)
                    devices = ([obj['device']
                               for obj in metrics['heketi_device_size']])
                    self.assertIn(device_name, devices)
                    for device_size in metrics['heketi_device_size']:
                        if (device_size['cluster'] == cluster_id
                                and device_size['hostname'] == hostname
                                and device_size['device'] == device_name):
                            self.assertEqual(
                                device_size_t, device_size['value'])

                    cluster_ids = ([obj['cluster']
                                   for obj in metrics['heketi_device_free']])
                    self.assertIn(cluster_id, cluster_ids)
                    hostnames = ([obj['hostname']
                                 for obj in metrics['heketi_device_free']])
                    self.assertIn(hostname, hostnames)
                    devices = ([obj['device']
                               for obj in metrics['heketi_device_free']])
                    self.assertIn(device_name, devices)
                    for device_free in metrics['heketi_device_free']:
                        if (device_free['cluster'] == cluster_id
                                and device_free['hostname'] == hostname
                                and device_free['device'] == device_name):
                            self.assertEqual(
                                device_free_t, device_free['value'])

                    cluster_ids = ([obj['cluster']
                                   for obj in metrics['heketi_device_used']])
                    self.assertIn(cluster_id, cluster_ids)
                    hostnames = ([obj['hostname']
                                 for obj in metrics['heketi_device_used']])
                    self.assertIn(hostname, hostnames)
                    devices = ([obj['device']
                               for obj in metrics['heketi_device_used']])
                    self.assertIn(device_name, devices)
                    for device_used in metrics['heketi_device_used']:
                        if (device_used['cluster'] == cluster_id
                                and device_used['hostname'] == hostname
                                and device_used['device'] == device_name):
                            self.assertEqual(
                                device_used_t, device_used['value'])

    def verify_volume_count(self):
        metrics = get_heketi_metrics(
            self.heketi_client_node,
            self.heketi_server_url)
        self.assertTrue(metrics['heketi_volumes_count'])

        for vol_count in metrics['heketi_volumes_count']:
            self.assertTrue(vol_count['cluster'])
            cluster_info = heketi_cluster_info(
                self.heketi_client_node,
                self.heketi_server_url,
                vol_count['cluster'], json=True)
            self.assertEqual(vol_count['value'], len(cluster_info['volumes']))

    def test_heketi_metrics_with_topology_info(self):
        """Validate heketi metrics generation"""
        self.verify_heketi_metrics_with_topology_info()

    def test_heketi_metrics_heketipod_failure(self):
        """Validate heketi metrics after heketi pod failure"""
        scale_dc_pod_amount_and_wait(
            self.ocp_master_node[0], self.heketi_dc_name, pod_amount=0)
        self.addCleanup(
            scale_dc_pod_amount_and_wait, self.ocp_master_node[0],
            self.heketi_dc_name, pod_amount=1)

        # verify that metrics is not accessable when heketi pod is down
        with self.assertRaises(AssertionError):
            get_heketi_metrics(
                self.heketi_client_node,
                self.heketi_server_url,
                prometheus_format=True)

        scale_dc_pod_amount_and_wait(
            self.ocp_master_node[0], self.heketi_dc_name, pod_amount=1)

        pod_name = get_pod_name_from_dc(
            self.ocp_master_node[0], self.heketi_dc_name, self.heketi_dc_name)
        wait_for_pod_be_ready(self.ocp_master_node[0], pod_name, wait_step=5)

        for i in range(3):
            vol = heketi_volume_create(
                self.heketi_client_node,
                self.heketi_server_url, 1, json=True)

            self.assertTrue(vol)

            self.addCleanup(
                heketi_volume_delete,
                self.heketi_client_node,
                self.heketi_server_url,
                vol['id'],
                raise_on_error=False)

            vol_list = heketi_volume_list(
                self.heketi_client_node,
                self.heketi_server_url)

            self.assertIn(vol['id'], vol_list)

        self.verify_heketi_metrics_with_topology_info()

    def test_heketi_metrics_validating_vol_count_on_vol_creation(self):
        """Validate heketi metrics VolumeCount after volume creation"""

        for i in range(3):
            # Create volume
            vol = heketi_volume_create(
                self.heketi_client_node,
                self.heketi_server_url, 1, json=True)
            self.assertTrue(vol)
            self.addCleanup(
                heketi_volume_delete,
                self.heketi_client_node,
                self.heketi_server_url,
                vol['id'],
                raise_on_error=False)

            vol_list = heketi_volume_list(
                self.heketi_client_node,
                self.heketi_server_url)

            self.assertIn(vol['id'], vol_list)

        self.verify_volume_count()

    def test_heketi_metrics_validating_vol_count_on_vol_deletion(self):
        """Validate heketi metrics VolumeCount after volume deletion"""

        vol_list = []

        for i in range(3):
            # Create volume
            vol = heketi_volume_create(
                self.heketi_client_node,
                self.heketi_server_url, 1, json=True)

            self.assertTrue(vol)

            self.addCleanup(
                heketi_volume_delete,
                self.heketi_client_node,
                self.heketi_server_url,
                vol['id'],
                raise_on_error=False)

            volume_list = heketi_volume_list(
                self.heketi_client_node,
                self.heketi_server_url)

            self.assertIn(vol['id'], volume_list)
            vol_list.append(vol)

        for vol in vol_list:
            # delete volume
            heketi_volume_delete(
                self.heketi_client_node,
                self.heketi_server_url,
                vol['id'])
            volume_list = heketi_volume_list(
                self.heketi_client_node,
                self.heketi_server_url)
            self.assertNotIn(vol['id'], volume_list)
            self.verify_volume_count()

    def test_heketi_metrics_validating_cluster_count(self):
        """Validate 'cluster count' in heketi metrics"""
        cluster_list = heketi_cluster_list(
            self.heketi_client_node, self.heketi_server_url, json=True)

        self.assertTrue(cluster_list)
        self.assertTrue(cluster_list.get('clusters'))

        metrics = get_heketi_metrics(
            self.heketi_client_node, self.heketi_server_url)

        self.assertTrue(metrics)
        self.assertTrue(metrics.get('heketi_cluster_count'))

        self.assertEqual(
            len(cluster_list['clusters']), metrics['heketi_cluster_count'])

    def test_heketi_metrics_validating_existing_node_count(self):
        """Validate existing 'node count' in heketi metrics"""
        metrics = get_heketi_metrics(
            self.heketi_client_node, self.heketi_server_url)

        self.assertTrue(metrics)
        self.assertTrue(metrics.get('heketi_nodes_count'))

        for cluster in metrics['heketi_nodes_count']:
            cluster_info = heketi_cluster_info(
                self.heketi_client_node, self.heketi_server_url,
                cluster['cluster'], json=True)

            self.assertTrue(cluster_info)
            self.assertTrue(cluster_info.get('nodes'))

            self.assertEqual(len(cluster_info['nodes']), cluster['value'])