File: test_reindex.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 (414 lines) | stat: -rw-r--r-- 16,950 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
413
414
"""Test reindex action functionality"""

# pylint: disable=C0115, C0116, invalid-name
import os
from unittest.case import SkipTest
import pytest
from dotmap import DotMap  # type: ignore
from es_client.builder import Builder
from curator.helpers.getters import get_indices
from . import CuratorTestCase
from . import testvars

UNDEF = 'undefined'
HOST = os.environ.get('TEST_ES_SERVER', 'http://127.0.0.1:9200')
RHOST = os.environ.get('REMOTE_ES_SERVER', UNDEF)
WAIT_INTERVAL = 1
MAX_WAIT = 3


class TestActionFileReindex(CuratorTestCase):
    """Test file-based reindex operations"""

    def test_reindex_manual(self):
        """Test that manual reindex results in proper count of documents"""
        source = 'my_source'
        dest = 'my_dest'
        expected = 3
        self.create_index(source)
        self.add_docs(source)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, source, dest),
        )
        self.invoke_runner()
        assert expected == self.client.count(index=dest)['count']

    def test_reindex_selected(self):
        """Reindex selected indices"""
        source = 'my_source'
        dest = 'my_dest'
        expected = 3
        self.create_index(source)
        self.add_docs(source)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, 'REINDEX_SELECTION', dest),
        )
        self.invoke_runner()
        assert expected == self.client.count(index=dest)['count']

    def test_reindex_empty_list(self):
        """
        This test raises <class 'curator.exceptions.FailedExecution'>:
        Reindex failed. The index or alias identified by "my_dest" was not found.
        The source is never created, so the dest is also not able to be created
        This means that we expect an empty list.
        """
        source = 'my_source'
        dest = 'my_dest'
        expected = []
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, source, dest),
        )
        self.invoke_runner()
        assert expected == get_indices(self.client)

    def test_reindex_selected_many_to_one(self):
        """Test reindexing many indices to one destination"""
        source1 = 'my_source1'
        source2 = 'my_source2'
        dest = 'my_dest'
        expected = 6
        self.create_index(source1)
        self.add_docs(source1)
        self.create_index(source2)
        for i in ["4", "5", "6"]:
            self.client.create(
                index=source2, id=i, document={"doc" + i: 'TEST DOCUMENT'}
            )
            # Decorators make this pylint exception necessary
            # pylint: disable=E1123
            self.client.indices.flush(index=source2, force=True)
            self.client.indices.refresh(index=source2)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, 'REINDEX_SELECTION', dest),
        )
        self.invoke_runner()
        self.client.indices.refresh(index=dest)
        assert expected == self.client.count(index=dest)['count']

    def test_reindex_selected_empty_list_fail(self):
        """Ensure an empty list results in an exit code 1"""
        source1 = 'my_source1'
        source2 = 'my_source2'
        dest = 'my_dest'
        self.create_index(source1)
        self.add_docs(source1)
        self.create_index(source2)
        for i in ["4", "5", "6"]:
            self.client.create(
                index=source2, id=i, document={"doc" + i: 'TEST DOCUMENT'}
            )
            # Decorators make this pylint exception necessary
            # pylint: disable=E1123
            self.client.indices.flush(index=source2, force=True)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex_empty_list.format('false', WAIT_INTERVAL, MAX_WAIT, dest),
        )
        self.invoke_runner()
        assert 1 == self.result.exit_code

    def test_reindex_selected_empty_list_pass(self):
        """Ensure an empty list results in an exit code 0"""
        source1 = 'my_source1'
        source2 = 'my_source2'
        dest = 'my_dest'
        self.create_index(source1)
        self.add_docs(source1)
        self.create_index(source2)
        for i in ["4", "5", "6"]:
            self.client.create(
                index=source2, id=i, document={"doc" + i: 'TEST DOCUMENT'}
            )
            # Decorators make this pylint exception necessary
            # pylint: disable=E1123
            self.client.indices.flush(index=source2, force=True)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex_empty_list.format('true', WAIT_INTERVAL, MAX_WAIT, dest),
        )
        self.invoke_runner()
        assert 0 == self.result.exit_code

    @pytest.mark.skipif((RHOST == UNDEF), reason='REMOTE_ES_SERVER is not defined')
    def test_reindex_from_remote(self):
        """Test remote reindex functionality"""
        diff_wait = 6
        source1 = 'my_source1'
        source2 = 'my_source2'
        prefix = 'my_'
        dest = 'my_dest'
        expected = 6
        # Build remote client
        try:
            remote_args = DotMap({'hosts': RHOST})
            remote_config = {'elasticsearch': {'client': remote_args.toDict()}}
            builder = Builder(configdict=remote_config)
            builder.version_min = (5, 0, 0)
            builder.connect()
            rclient = builder.client
            rclient.info()
        except Exception as exc:
            raise SkipTest(f'Unable to connect to host at {RHOST}: {exc}') from exc
        # Build indices remotely.
        counter = 0
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        for rindex in [source1, source2]:
            rclient.indices.create(index=rindex)
            for i in range(0, 3):
                rclient.create(
                    index=rindex,
                    id=str(counter + 1),
                    document={"doc" + str(i): 'TEST DOCUMENT'},
                )
                counter += 1
                # Decorators make this pylint exception necessary
                # pylint: disable=E1123
                rclient.indices.flush(index=rindex, force=True)
                rclient.indices.refresh(index=rindex)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.remote_reindex.format(
                WAIT_INTERVAL, diff_wait, RHOST, 'REINDEX_SELECTION', dest, prefix
            ),
        )
        self.invoke_runner()
        # Do our own cleanup here.
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        assert expected == self.client.count(index=dest)['count']

    @pytest.mark.skipif((RHOST == UNDEF), reason='REMOTE_ES_SERVER is not defined')
    def test_reindex_migrate_from_remote(self):
        """Test remote reindex migration"""
        source1 = 'my_source1'
        source2 = 'my_source2'
        prefix = 'my_'
        dest = 'MIGRATION'
        expected = 3
        # Build remote client
        try:
            remote_args = DotMap({'hosts': RHOST})
            remote_config = {'elasticsearch': {'client': remote_args.toDict()}}
            builder = Builder(configdict=remote_config)
            builder.version_min = (5, 0, 0)
            builder.connect()
            rclient = builder.client
            rclient.info()
        except Exception as exc:
            raise SkipTest(f'Unable to connect to host at {RHOST}: {exc}') from exc
        # Build indices remotely.
        counter = 0
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        for rindex in [source1, source2]:
            rclient.indices.create(index=rindex)
            for i in range(0, 3):
                rclient.create(
                    index=rindex,
                    id=str(counter + 1),
                    document={"doc" + str(i): 'TEST DOCUMENT'},
                )
                counter += 1
                # Decorators make this pylint exception necessary
                # pylint: disable=E1123
                rclient.indices.flush(index=rindex, force=True)
                rclient.indices.refresh(index=rindex)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.remote_reindex.format(
                WAIT_INTERVAL, MAX_WAIT, RHOST, 'REINDEX_SELECTION', dest, prefix
            ),
        )
        self.invoke_runner()
        # Do our own cleanup here.
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        # And now the neat trick of verifying that the reindex worked to both
        # indices, and they preserved their names
        assert expected == self.client.count(index=source1)['count']
        assert expected == self.client.count(index=source2)['count']

    @pytest.mark.skipif((RHOST == UNDEF), reason='REMOTE_ES_SERVER is not defined')
    def test_reindex_migrate_from_remote_with_pre_suf_fixes(self):
        """Ensure migrate from remote with prefixes and suffixes works"""
        source1 = 'my_source1'
        source2 = 'my_source2'
        prefix = 'my_'
        dest = 'MIGRATION'
        expected = 3
        mpfx = 'pre-'
        msfx = '-fix'
        # Build remote client
        try:
            remote_args = DotMap({'hosts': RHOST})
            remote_config = {'elasticsearch': {'client': remote_args.toDict()}}
            builder = Builder(configdict=remote_config)
            builder.version_min = (5, 0, 0)
            builder.connect()
            rclient = builder.client
            rclient.info()
        except Exception as exc:
            raise SkipTest(f'Unable to connect to host at {RHOST}: {exc}') from exc
        # Build indices remotely.
        counter = 0
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        for rindex in [source1, source2]:
            rclient.indices.create(index=rindex)
            for i in range(0, 3):
                rclient.create(
                    index=rindex,
                    id=str(counter + 1),
                    document={"doc" + str(i): 'TEST DOCUMENT'},
                )
                counter += 1
                # Decorators make this pylint exception necessary
                # pylint: disable=E1123
                rclient.indices.flush(index=rindex, force=True)
                rclient.indices.refresh(index=rindex)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.migration_reindex.format(
                WAIT_INTERVAL,
                MAX_WAIT,
                mpfx,
                msfx,
                RHOST,
                'REINDEX_SELECTION',
                dest,
                prefix,
            ),
        )
        self.invoke_runner()
        # Do our own cleanup here.
        rclient.indices.delete(index=f'{source1},{source2}')
        # And now the neat trick of verifying that the reindex worked to both
        # indices, and they preserved their names
        assert expected == self.client.count(index=f'{mpfx}{source1}{msfx}')['count']

    def test_reindex_from_remote_no_connection(self):
        """Ensure that the inability to connect to the remote cluster fails"""
        dest = 'my_dest'
        bad_remote = 'http://127.0.0.1:9601'
        expected = 1
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.remote_reindex.format(
                WAIT_INTERVAL, MAX_WAIT, bad_remote, 'REINDEX_SELECTION', dest, 'my_'
            ),
        )
        self.invoke_runner()
        assert expected == self.result.exit_code

    @pytest.mark.skipif((RHOST == UNDEF), reason='REMOTE_ES_SERVER is not defined')
    def test_reindex_from_remote_no_indices(self):
        """
        Test that attempting to reindex remotely with an empty list exits with a fail
        """
        source1 = 'wrong1'
        source2 = 'wrong2'
        prefix = 'my_'
        dest = 'my_dest'
        expected = 1
        # Build remote client
        try:
            remote_args = DotMap({'hosts': RHOST})
            remote_config = {'elasticsearch': {'client': remote_args.toDict()}}
            builder = Builder(configdict=remote_config)
            builder.version_min = (5, 0, 0)
            builder.connect()
            rclient = builder.client
            rclient.info()
        except Exception as exc:
            raise SkipTest(f'Unable to connect to host at {RHOST}: {exc}') from exc
        # Build indices remotely.
        counter = 0
        # Force remove my_source1 and my_source2 to prevent false positives
        rclient.indices.delete(
            index=f"{'my_source1'},{'my_source2'}", ignore_unavailable=True
        )
        rclient.indices.delete(index=f'{source1},{source2}', ignore_unavailable=True)
        for rindex in [source1, source2]:
            rclient.indices.create(index=rindex)
            for i in range(0, 3):
                rclient.create(
                    index=rindex,
                    id=str(counter + 1),
                    document={"doc" + str(i): 'TEST DOCUMENT'},
                )
                counter += 1
                rclient.indices.flush(index=rindex, force=True)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.remote_reindex.format(
                WAIT_INTERVAL, MAX_WAIT, f'{RHOST}', 'REINDEX_SELECTION', dest, prefix
            ),
        )
        self.invoke_runner()
        # Do our own cleanup here.
        rclient.indices.delete(index=f'{source1},{source2}')
        assert expected == self.result.exit_code

    def test_reindex_into_alias(self):
        """Ensure that reindexing into an alias works as expected"""
        source = 'my_source'
        dest = 'my_dest'
        expected = 3
        alias_dict = {dest: {}}
        self.client.indices.create(index='dummy', aliases=alias_dict)
        self.add_docs(source)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, source, dest),
        )
        self.invoke_runner()
        assert expected == self.client.count(index=dest)['count']

    def test_reindex_manual_date_math(self):
        """Ensure date math is functional with reindex calls"""
        source = '<source-{now/d}>'
        dest = '<target-{now/d}>'
        expected = 3

        self.create_index(source)
        self.add_docs(source)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, source, dest),
        )
        self.invoke_runner()
        assert expected == self.client.count(index=dest)['count']

    def test_reindex_bad_mapping(self):
        """This test addresses GitHub issue #1260"""
        source = 'my_source'
        dest = 'my_dest'
        expected = 1
        settings = {"number_of_shards": 1, "number_of_replicas": 0}
        mappings1 = {"properties": {"doc1": {"type": "keyword"}}}
        mappings2 = {"properties": {"doc1": {"type": "integer"}}}
        self.client.indices.create(index=source, settings=settings, mappings=mappings1)
        self.add_docs(source)
        # Create the dest index with a different mapping.
        self.client.indices.create(index=dest, settings=settings, mappings=mappings2)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.reindex.format(WAIT_INTERVAL, MAX_WAIT, source, dest),
        )
        self.invoke_runner()
        assert expected == self.result.exit_code