File: test_helpers_waiters.py

package info (click to toggle)
elasticsearch-curator 8.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,716 kB
  • sloc: python: 17,838; makefile: 159; sh: 156
file content (412 lines) | stat: -rw-r--r-- 12,937 bytes parent folder | download
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""Unit tests for utils"""

from unittest import TestCase
from unittest.mock import Mock
import pytest
from curator.exceptions import (
    ActionTimeout,
    ConfigurationError,
    CuratorException,
    MissingArgument,
)
from curator.helpers.waiters import (
    health_check,
    restore_check,
    snapshot_check,
    task_check,
    wait_for_it,
)

FAKE_FAIL = Exception('Simulated Failure')


class TestHealthCheck(TestCase):
    """TestHealthCheck

    Test helpers.waiters.health_check functionality
    """

    # pylint: disable=line-too-long
    CLUSTER_HEALTH = {
        "cluster_name": "unit_test",
        "status": "green",
        "timed_out": False,
        "number_of_nodes": 7,
        "number_of_data_nodes": 3,
        "active_primary_shards": 235,
        "active_shards": 471,
        "relocating_shards": 0,
        "initializing_shards": 0,
        "unassigned_shards": 0,
        "delayed_unassigned_shards": 0,
        "number_of_pending_tasks": 0,
        "task_max_waiting_in_queue_millis": 0,
        "active_shards_percent_as_number": 100,
    }

    def test_no_kwargs(self):
        """test_no_kwargs

        Should raise a ``MissingArgument`` exception when no keyword args are passed.
        """
        client = Mock()
        with pytest.raises(
            MissingArgument, match=r'Must provide at least one keyword argument'
        ):
            health_check(client)

    def test_key_value_match(self):
        """test_key_value_match

        Should return ``True`` when matching keyword args are passed.
        """
        client = Mock()
        client.cluster.health.return_value = self.CLUSTER_HEALTH
        assert health_check(client, status='green')

    def test_key_value_no_match(self):
        """test_key_value_no_match

        Should return ``False`` when matching keyword args are passed, but no matches
        are found.
        """
        client = Mock()
        client.cluster.health.return_value = self.CLUSTER_HEALTH
        assert not health_check(client, status='red')

    def test_key_not_found(self):
        """test_key_not_found

        Should raise ``ConfigurationError`` when keyword args are passed, but keys
        match.
        """
        client = Mock()
        client.cluster.health.return_value = self.CLUSTER_HEALTH
        with pytest.raises(ConfigurationError, match=r'not in cluster health output'):
            health_check(client, foo='bar')


class TestRestoreCheck(TestCase):
    """TestRestoreCheck

    Test helpers.waiters.restore_check functionality
    """

    SNAP_NAME = 'snap_name'
    NAMED_INDICES = ["index-2015.01.01", "index-2015.02.01"]

    def test_fail_to_get_recovery(self):
        """test_fail_to_get_recovery

        Should raise ``CuratorException`` when an upstream Exception is encountered
        """
        client = Mock()
        client.indices.recovery.side_effect = FAKE_FAIL
        with pytest.raises(
            CuratorException, match=r'Unable to obtain recovery information'
        ):
            restore_check(client, [])

    def test_incomplete_recovery(self):
        """test_incomplete_recovery

        Should return ``False`` when recovery is incomplete
        """
        client = Mock()
        # :pylint disable=line-too-long
        client.indices.recovery.return_value = {
            'index-2015.01.01': {'shards': [{'stage': 'INDEX'}]},
            'index-2015.02.01': {'shards': [{'stage': 'INDEX'}]},
        }
        assert not restore_check(client, self.NAMED_INDICES)

    def test_completed_recovery(self):
        """test_completed_recovery

        Should return ``True`` when recovery is complete
        """
        client = Mock()
        # :pylint disable=line-too-long
        client.indices.recovery.return_value = {
            'index-2015.01.01': {'shards': [{'stage': 'DONE'}]},
            'index-2015.02.01': {'shards': [{'stage': 'DONE'}]},
        }
        assert restore_check(client, self.NAMED_INDICES)

    def test_empty_recovery(self):
        """test_empty_recovery

        Should return ``False`` when an empty response comes back
        """
        client = Mock()
        client.indices.recovery.return_value = {}
        assert not restore_check(client, self.NAMED_INDICES)


class TestSnapshotCheck(TestCase):
    """TestSnapshotCheck

    Test helpers.waiters.snapshot_check functionality
    """

    # :pylint disable=line-too-long
    SNAP_NAME = 'snap_name'
    NAMED_INDICES = ["index-2015.01.01", "index-2015.02.01"]

    def test_fail_to_get_snapshot(self):
        """test_fail_to_get_snapshot

        Should raise ``CuratorException`` when another upstream Exception occurs.
        """
        client = Mock()
        client.snapshot.get.side_effect = FAKE_FAIL
        self.assertRaises(CuratorException, snapshot_check, client)

    def test_in_progress(self):
        """test_in_progress

        Should return ``False`` when state is ``IN_PROGRESS``.
        """
        client = Mock()
        test_val = {
            'snapshots': [
                {
                    'state': 'IN_PROGRESS',
                    'snapshot': self.SNAP_NAME,
                    'indices': self.NAMED_INDICES,
                }
            ]
        }
        client.snapshot.get.return_value = test_val
        assert not snapshot_check(client, repository='foo', snapshot=self.SNAP_NAME)

    def test_success(self):
        """test_success

        Should return ``True`` when state is ``SUCCESS``.
        """
        client = Mock()
        test_val = {
            'snapshots': [
                {
                    'state': 'SUCCESS',
                    'snapshot': self.SNAP_NAME,
                    'indices': self.NAMED_INDICES,
                }
            ]
        }
        client.snapshot.get.return_value = test_val
        assert snapshot_check(client, repository='foo', snapshot=self.SNAP_NAME)

    def test_partial(self):
        """test_partial

        Should return ``True`` when state is ``PARTIAL``.
        """
        client = Mock()
        test_val = {
            'snapshots': [
                {
                    'state': 'PARTIAL',
                    'snapshot': self.SNAP_NAME,
                    'indices': self.NAMED_INDICES,
                }
            ]
        }
        client.snapshot.get.return_value = test_val
        assert snapshot_check(client, repository='foo', snapshot=self.SNAP_NAME)

    def test_failed(self):
        """test_failed

        Should return ``True`` when state is ``FAILED``.
        """
        client = Mock()
        test_val = {
            'snapshots': [
                {
                    'state': 'FAILED',
                    'snapshot': self.SNAP_NAME,
                    'indices': self.NAMED_INDICES,
                }
            ]
        }
        client.snapshot.get.return_value = test_val
        assert snapshot_check(client, repository='foo', snapshot=self.SNAP_NAME)

    def test_other(self):
        """test_other

        Should return ``True`` when state is anything other than ``IN_PROGRESS`` or the
        above.
        """
        client = Mock()
        test_val = {
            'snapshots': [
                {
                    'state': 'SOMETHINGELSE',
                    'snapshot': self.SNAP_NAME,
                    'indices': self.NAMED_INDICES,
                }
            ]
        }
        client.snapshot.get.return_value = test_val
        assert snapshot_check(client, repository='foo', snapshot=self.SNAP_NAME)


class TestTaskCheck(TestCase):
    """TestTaskCheck

    Test helpers.waiters.task_check functionality
    """

    # pylint: disable=line-too-long
    PROTO_TASK = {
        'node': 'I0ekFjMhSPCQz7FUs1zJOg',
        'description': 'UNIT TEST',
        'running_time_in_nanos': 1637039537721,
        'action': 'indices:data/write/reindex',
        'id': 54510686,
        'start_time_in_millis': 1489695981997,
    }
    GENERIC_TASK = {'task': 'I0ekFjMhSPCQz7FUs1zJOg:54510686'}

    def test_bad_task_id(self):
        """test_bad_task_id

        Should raise ``CuratorException`` if a bad value for ``task_id`` is passed
        """
        client = Mock()
        client.tasks.get.side_effect = FAKE_FAIL
        with pytest.raises(
            CuratorException, match=r'Unable to obtain task information for task'
        ):
            task_check(client, 'foo')

    def test_incomplete_task(self):
        """test_incomplete_task

        Should return ``False`` if task is incomplete
        """
        client = Mock()
        test_task = {
            'completed': False,
            'task': self.PROTO_TASK,
            'response': {'failures': []},
        }
        client.tasks.get.return_value = test_task
        assert not task_check(client, task_id=self.GENERIC_TASK['task'])

    def test_complete_task(self):
        """test_complete_task

        Should return ``True`` if task is complete
        """
        client = Mock()
        test_task = {
            'completed': True,
            'task': self.PROTO_TASK,
            'response': {'failures': []},
        }
        client.tasks.get.return_value = test_task
        assert task_check(client, task_id=self.GENERIC_TASK['task'])


class TestWaitForIt(TestCase):
    """TestWaitForIt

    Test helpers.waiters.wait_for_it functionality
    """

    # pylint: disable=line-too-long
    def test_bad_action(self):
        """test_bad_action

        Should raise a ``ConfigurationError`` exception if ``action`` is invalid
        """
        client = Mock()
        # self.assertRaises(ConfigurationError, wait_for_it, client, 'foo')
        with pytest.raises(ConfigurationError, match=r'"action" must be one of'):
            wait_for_it(client, 'foo')

    def test_reindex_action_no_task_id(self):
        """test_reindex_action_no_task_id

        Should raise a ``MissingArgument`` exception if ``task_id`` is missing for
        ``reindex``
        """
        client = Mock()
        # self.assertRaises(MissingArgument, wait_for_it, client, 'reindex')
        with pytest.raises(MissingArgument, match=r'A task_id must accompany "action"'):
            wait_for_it(client, 'reindex')

    def test_snapshot_action_no_snapshot(self):
        """test_snapshot_action_no_snapshot

        Should raise a ``MissingArgument`` exception if ``snapshot`` is missing for
        ``snapshot``
        """
        client = Mock()
        # self.assertRaises(MissingArgument, wait_for_it, client,
        #   'snapshot', repository='foo')
        with pytest.raises(
            MissingArgument, match=r'A snapshot and repository must accompany "action"'
        ):
            wait_for_it(client, 'snapshot', repository='foo')

    def test_snapshot_action_no_repository(self):
        """test_snapshot_action_no_repository

        Should raise a ``MissingArgument`` exception if ``repository`` is missing for
        ``snapshot``
        """
        client = Mock()
        # self.assertRaises(MissingArgument, wait_for_it, client,
        #   'snapshot', snapshot='foo')
        with pytest.raises(
            MissingArgument, match=r'A snapshot and repository must accompany "action"'
        ):
            wait_for_it(client, 'snapshot', snapshot='foo')

    def test_restore_action_no_indexlist(self):
        """test_restore_action_no_indexlist

        Should raise a ``MissingArgument`` exception if ``index_list`` is missing for
        ``restore``
        """
        client = Mock()
        # self.assertRaises(MissingArgument, wait_for_it, client, 'restore')
        with pytest.raises(
            MissingArgument, match=r'An index_list must accompany "action"'
        ):
            wait_for_it(client, 'restore')

    def test_reindex_action_bad_task_id(self):
        """test_reindex_action_bad_task_id

        Should raise a ``CuratorException`` exception if there's a bad task_id

        This is kind of a fake fail, even in the code.
        """
        client = Mock()
        client.tasks.get.return_value = {'a': 'b'}
        client.tasks.get.side_effect = FAKE_FAIL
        # self.assertRaises(CuratorException, wait_for_it,
        #       client, 'reindex', task_id='foo')
        with pytest.raises(CuratorException, match=r'Unable to find task_id'):
            wait_for_it(client, 'reindex', task_id='foo')

    def test_reached_max_wait(self):
        """test_reached_max_wait

        Should raise a ``ActionTimeout`` exception if we've waited past the defined
        timeout period
        """
        client = Mock()
        client.cluster.health.return_value = {'status': 'red'}
        # self.assertRaises(ActionTimeout, wait_for_it, client, 'replicas',
        # wait_interval=1, max_wait=1)
        with pytest.raises(
            ActionTimeout, match=r'failed to complete in the max_wait period'
        ):
            wait_for_it(client, 'replicas', wait_interval=1, max_wait=1)