File: test_restore.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 (214 lines) | stat: -rw-r--r-- 7,739 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
"""Test snapshot restore functionality"""

# pylint: disable=missing-function-docstring, missing-class-docstring, line-too-long
import os
import time
from curator.helpers.getters import get_indices, get_snapshot
from . import CuratorTestCase
from . import testvars

HOST = os.environ.get('TEST_ES_SERVER', 'http://127.0.0.1:9200')

# '      repository: {0}\n'
# '      name: {1}\n'
# '      indices: {2}\n'
# '      include_aliases: {3}\n'
# '      ignore_unavailable: {4}\n'
# '      include_global_state: {5}\n'
# '      partial: {6}\n'
# '      rename_pattern: {7}\n'
# '      rename_replacement: {8}\n'
# '      extra_settings: {9}\n'
# '      wait_for_completion: {10}\n'
# '      skip_repo_fs_check: {11}\n'
# '      timeout_override: {12}\n'
# '      wait_interval: {13}\n'
# '      max_wait: {14}\n'


class TestActionFileRestore(CuratorTestCase):
    """Test file-based configuration restore operations"""

    def test_restore(self):
        """Test restore action"""
        indices = []
        for i in range(1, 4):
            self.add_docs(f'my_index{i}')
            indices.append(f'my_index{i}')
        snap_name = 'snapshot1'
        self.create_snapshot(snap_name, ','.join(indices))
        snapshot = get_snapshot(self.client, self.args['repository'], '*')
        self.assertEqual(1, len(snapshot['snapshots']))
        assert 1 == len(snapshot['snapshots'])
        self.client.indices.delete(index=','.join(indices))
        self.assertEqual([], get_indices(self.client))
        assert not get_indices(self.client)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.restore_snapshot_proto.format(
                self.args['repository'],
                snap_name,
                indices,
                False,
                False,
                True,
                False,
                ' ',
                ' ',
                ' ',
                True,
                False,
                301,
                1,
                3,
            ),
        )
        self.invoke_runner()
        restored_indices = sorted(get_indices(self.client))
        self.assertEqual(indices, restored_indices)
        assert indices == restored_indices
        # The test runs so fast that it tries to execute the cleanup step
        # and delete the repository before Elasticsearch is actually ready
        time.sleep(0.5)

    def test_restore_with_rename(self):
        """Test restore action with renaming enabled"""
        indices = []
        for i in range(1, 4):
            self.add_docs(f'my_index{i}')
            indices.append(f'my_index{i}')
        snap_name = 'snapshot1'
        self.create_snapshot(snap_name, ','.join(indices))
        snapshot = get_snapshot(self.client, self.args['repository'], '*')
        time.sleep(1)
        self.assertEqual(1, len(snapshot['snapshots']))
        assert 1 == len(snapshot['snapshots'])
        self.client.indices.delete(index=','.join(indices))
        self.assertEqual([], get_indices(self.client))
        assert not get_indices(self.client)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.restore_snapshot_proto.format(
                self.args['repository'],
                snap_name,
                indices,
                False,
                False,
                True,
                False,
                'my_index(.+)',
                'new_index$1',
                ' ',
                True,
                False,
                301,
                1,
                3,
            ),
        )
        self.invoke_runner()
        time.sleep(1)
        restored_indices = sorted(get_indices(self.client))
        self.assertEqual(['new_index1', 'new_index2', 'new_index3'], restored_indices)
        assert ['new_index1', 'new_index2', 'new_index3'] == restored_indices
        # The test runs so fast that it tries to execute the cleanup step
        # and delete the repository before Elasticsearch is actually ready
        time.sleep(1)

    def test_restore_wildcard(self):
        """Test restore with wildcard"""
        indices = []
        my_indices = []
        wildcard = ['my_*']
        for i in range(1, 4):
            for prefix in ['my_', 'not_my_']:
                self.add_docs(f'{prefix}index{i}')
                indices.append(f'{prefix}index{i}')
                if prefix == 'my_':
                    my_indices.append(f'{prefix}index{i}')
        snap_name = 'snapshot1'
        self.create_snapshot(snap_name, ','.join(indices))
        snapshot = get_snapshot(self.client, self.args['repository'], '*')
        self.assertEqual(1, len(snapshot['snapshots']))
        assert 1 == len(snapshot['snapshots'])
        self.client.indices.delete(index=','.join(indices))
        self.assertEqual([], get_indices(self.client))
        assert not get_indices(self.client)
        self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
        self.write_config(
            self.args['actionfile'],
            testvars.restore_snapshot_proto.format(
                self.args['repository'],
                snap_name,
                wildcard,
                False,
                False,
                True,
                False,
                ' ',
                ' ',
                ' ',
                True,
                False,
                301,
                1,
                3,
            ),
        )
        self.invoke_runner()
        restored_indices = sorted(get_indices(self.client))
        self.assertEqual(my_indices, restored_indices)
        assert my_indices == restored_indices
        # The test runs so fast that it tries to execute the cleanup step
        # and delete the repository before Elasticsearch is actually ready
        time.sleep(0.5)


class TestCLIRestore(CuratorTestCase):
    def test_restore(self):
        indices = []
        for i in range(1, 4):
            self.add_docs(f'my_index{i}')
            indices.append(f'my_index{i}')
        snap_name = 'snapshot1'
        self.create_snapshot(snap_name, ','.join(indices))
        snapshot = get_snapshot(self.client, self.args['repository'], '*')
        self.assertEqual(1, len(snapshot['snapshots']))
        assert 1 == len(snapshot['snapshots'])
        self.client.indices.delete(index=f'{",".join(indices)}')
        self.assertEqual([], get_indices(self.client))
        assert not get_indices(self.client)
        args = self.get_runner_args()
        args += [
            '--config',
            self.args['configfile'],
            'restore',
            '--repository',
            self.args['repository'],
            '--name',
            snap_name,
            '--index',
            indices[0],
            '--index',
            indices[1],
            '--index',
            indices[2],
            '--wait_interval',
            '1',
            '--max_wait',
            '3',
            '--filter_list',
            '{"filtertype":"none"}',
        ]
        # self.assertEqual(
        #     0, self.run_subprocess(args, logname='TestCLIRestore.test_restore')
        # )
        assert 0 == self.run_subprocess(args, logname='TestCLIRestore.test_restore')
        restored_indices = sorted(get_indices(self.client))
        self.assertEqual(indices, restored_indices)
        assert indices == restored_indices
        # The test runs so fast that it tries to execute the cleanup step
        # and delete the repository before Elasticsearch is actually ready
        time.sleep(0.5)