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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
"""Test the Delete Indices action"""
# pylint: disable=missing-function-docstring, missing-class-docstring, line-too-long
import os
import time
import requests
from curator.helpers.getters import get_indices
from . import CuratorTestCase
from . import testvars
HOST = os.environ.get('TEST_ES_SERVER', 'http://127.0.0.1:9200')
# ' - filtertype: {0}\n'
# ' source: {1}\n'
# ' direction: {2}\n'
# ' timestring: {3}\n'
# ' unit: {4}\n'
# ' unit_count: {5}\n'
# ' field: {6}\n'
# ' stats_result: {7}\n'
# ' epoch: {8}\n')
ILM_KEYS = ['ilm-history-1-000001', 'ilm-history-1']
def exclude_ilm_history(index_list):
"""Remove any values from ILM_KEYS found in index_list"""
for val in ILM_KEYS:
if val in index_list:
index_list.remove(val)
return index_list
class TestActionFileDeleteIndices(CuratorTestCase):
def test_retention_from_name_days(self):
# Test extraction of unit_count from index name
# Create indices for 10 days with retention time of 5 days in index name
# Expected: 5 oldest indices are deleted, 5 remain
self.args['prefix'] = 'logstash_5_'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 30, '_([0-9]+)_'
),
)
self.invoke_runner()
self.assertEqual(5, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_days_ignore_failed_match(self):
# Test extraction of unit_count from index name
# Create indices for 10 days with retention time of 5 days in index name
# Create indices for 10 days with no retention time in index name
# Expected: 5 oldest indices are deleted, 5 remain - 10 indices without
# retention time are ignored and remain
self.args['prefix'] = 'logstash_5_'
self.create_indices(10)
self.args['prefix'] = 'logstash_'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m.%d\'',
'days',
30,
'_([0-9]+)_',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(15, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_days_keep_exclude_false_after_failed_match(self):
# Test extraction of unit_count from index name and confirm correct
# behavior after a failed regex match with no fallback time - see gh issue 1206
# Create indices for 30 days with retention time of 5 days in index name
#
# Create indices for 10 days with no retention time in index name
# that alphabetically sort before the 10 with retention time
#
# Create indices for 10 days with no retention time in index name
# that sort after the 10 with retention time
# Expected: 45 oldest matching indices are deleted, 5 matching indices remain
# 20 indices without retention time are ignored and remain
# overall 25 indices should remain
self.args['prefix'] = 'logstash-aanomatch-'
self.create_indices(10)
self.args['prefix'] = 'logstash-match-5-'
self.create_indices(30)
self.args['prefix'] = 'logstash-zznomatch-'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m.%d\'',
'days',
-1,
r'logstash-\w+-([0-9]+)-[0-9]{4}\.[0-9]{2}\.[0-9]{2}',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(25, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_days_failed_match_with_fallback(self):
# Test extraction of unit_count from index name
# Create indices for 10 days with retention time of 5 days in index name
# Create indices for 10 days with no retention time in index name but
# configure fallback value of 7
# Expected: 5 oldest indices are deleted, 5 remain - 7 indices without
# retention time are ignored and remain due to the fallback value
self.args['prefix'] = 'logstash_5_'
self.create_indices(10)
self.args['prefix'] = 'logstash_'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m.%d\'',
'days',
7,
'_([0-9]+)_',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(12, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_no_capture_group(self):
# Test extraction of unit_count from index name when pattern contains no
# capture group
# Create indices for 10 months with retention time of 2 months in index name
# Expected: all indices remain as the pattern cannot be used to extract a
# retention time
self.args['prefix'] = 'logstash_2_'
self.args['time_unit'] = 'months'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m\'',
'months',
-1,
'_[0-9]+_',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(10, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_illegal_regex_no_fallback(self):
# Test extraction of unit_count from index name when pattern contains an
# illegal regular expression
# Create indices for 10 months with retention time of 2 months in index name
# Expected: all indices remain as the pattern cannot be used to extract a
# retention time
self.args['prefix'] = 'logstash_2_'
self.args['time_unit'] = 'months'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m\'',
'months',
-1,
'_[0-9+_',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(10, len(exclude_ilm_history(get_indices(self.client))))
def test_retention_from_name_illegal_regex_with_fallback(self):
# Test extraction of unit_count from index name when pattern contains an
# illegal regular expression
# Create indices for 10 days with retention time of 2 days in index name
# Expected: Fallback value of 3 is used and 3 most recent indices remain
# in place
self.args['prefix'] = 'logstash_2_'
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_pattern_proto.format(
'age',
'name',
'older',
'\'%Y.%m.%d\'',
'days',
3,
'_[0-9+_',
' ',
' ',
' ',
),
)
self.invoke_runner()
self.assertEqual(3, len(exclude_ilm_history(get_indices(self.client))))
def test_name_older_than_now(self):
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 5, ' ', ' ', ' '
),
)
self.invoke_runner()
self.assertEqual(5, len(exclude_ilm_history(get_indices(self.client))))
def test_name_based_age_match_five(self):
self.create_indices(10)
self.create_index(name='foobert')
self.create_index(name='.shouldbehidden')
self.create_index(name='this_has_no_date')
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 5, ' ', ' ', ' '
),
)
self.invoke_runner()
self.assertEqual(8, len(exclude_ilm_history(get_indices(self.client))))
def test_creation_date_newer_than_epoch(self):
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age',
'creation_date',
'younger',
' ',
'seconds',
0,
' ',
' ',
int(time.time()) - 60,
),
)
self.invoke_runner()
self.assertEqual(0, len(exclude_ilm_history(get_indices(self.client))))
def test_delete_in_period(self):
# filtertype: {0}
# source: {1}
# range_from: {2}
# range_to: {3}
# timestring: {4}
# unit: {5}
# field: {6}
# stats_result: {7}
# intersect: {8}
# epoch: {9}
# week_starts_on: {10}
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_period_proto.format(
'period',
'name',
'-5',
'-1',
"'%Y.%m.%d'",
'days',
' ',
' ',
' ',
' ',
'monday',
),
)
self.invoke_runner()
self.assertEqual(0, self.result.exit_code)
self.assertEqual(5, len(exclude_ilm_history(get_indices(self.client))))
def test_delete_in_period_absolute_date(self):
delete_period_abs = (
'---\n'
'actions:\n'
' 1:\n'
' description: "Delete indices as filtered"\n'
' action: delete_indices\n'
' options:\n'
' continue_if_exception: False\n'
' disable_action: False\n'
' filters:\n'
' - filtertype: {0}\n'
' period_type: absolute\n'
' source: {1}\n'
' date_from: {2}\n'
' date_to: {3}\n'
' timestring: {4}\n'
' unit: {5}\n'
' date_from_format: {6}\n'
' date_to_format: {7}\n'
)
expected = 'index-2017.02.02'
self.create_index('index-2017.01.02')
self.create_index('index-2017.01.03')
self.create_index(expected)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
delete_period_abs.format(
'period',
'name',
'2017.01.01',
'2017.01.10',
"'%Y.%m.%d'",
'days',
"'%Y.%m.%d'",
"'%Y.%m.%d'",
),
)
self.invoke_runner()
indices = exclude_ilm_history(get_indices(self.client))
self.assertEqual(0, self.result.exit_code)
self.assertEqual(1, len(indices))
self.assertEqual(expected, indices[0])
def test_delete_in_period_intersect(self):
# filtertype: {0}
# source: {1}
# range_from: {2}
# range_to: {3}
# timestring: {4}
# unit: {5}
# field: {6}
# stats_result: {7}
# intersect: {8}
# epoch: {9}
# week_starts_on: {10}
# 2017-09-01T01:00:00 = 1504227600
# 2017-09-25T01:00:00 = 1506301200
# 2017-09-29T01:00:00 = 1506646800
idx1, idx2 = ('intersecting', 'notintersecting')
self.create_index(idx1)
self.create_index(idx2)
self.client.index(
index=idx1,
id='1',
document={'@timestamp': '2017-09-25T01:00:00Z', 'doc': 'Earliest'},
)
self.client.index(
index=idx1,
id='2',
document={'@timestamp': '2017-09-29T01:00:00Z', 'doc': 'Latest'},
)
self.client.index(
index=idx2,
id='1',
document={'@timestamp': '2017-09-01T01:00:00Z', 'doc': 'Earliest'},
)
self.client.index(
index=idx2,
id='2',
document={'@timestamp': '2017-09-29T01:00:00Z', 'doc': 'Latest'},
)
# Decorators cause this pylint error
# pylint: disable=E1123
self.client.indices.flush(index='*', force=True)
self.client.indices.refresh(index=f'{idx1},{idx2}')
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_period_proto.format(
'period',
'field_stats',
'0',
'0',
' ',
'weeks',
"'@timestamp'",
'min_value',
'true',
1506716040,
'sunday',
),
)
self.invoke_runner()
self.assertEqual(0, self.result.exit_code)
indices = exclude_ilm_history(get_indices(self.client))
self.assertEqual(1, len(indices))
self.assertEqual(idx2, indices[0])
def test_empty_list(self):
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age',
'creation_date',
'older',
' ',
'days',
90,
' ',
' ',
int(time.time()),
),
)
self.invoke_runner()
self.assertEqual(10, len(exclude_ilm_history(get_indices(self.client))))
self.assertEqual(1, self.result.exit_code)
def test_ignore_empty_list(self):
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_ignore_proto.format(
'age',
'creation_date',
'older',
' ',
'days',
90,
' ',
' ',
int(time.time()),
),
)
self.invoke_runner()
self.assertEqual(10, len(exclude_ilm_history(get_indices(self.client))))
self.assertEqual(0, self.result.exit_code)
def test_extra_options(self):
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.bad_option_proto_test.format('delete_indices'),
)
self.invoke_runner()
self.assertEqual(1, self.result.exit_code)
def test_945(self):
self.create_indices(10)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(self.args['actionfile'], testvars.test_945)
self.invoke_runner()
self.assertEqual(1, self.result.exit_code)
def test_name_epoch_zero(self):
self.create_index('epoch_zero-1970.01.01')
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 5, ' ', ' ', ' '
),
)
self.invoke_runner()
self.assertEqual(0, len(exclude_ilm_history(get_indices(self.client))))
def test_name_negative_epoch(self):
self.create_index('index-1969.12.31')
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.delete_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 5, ' ', ' ', ' '
),
)
self.invoke_runner()
self.assertEqual(0, len(exclude_ilm_history(get_indices(self.client))))
def test_allow_ilm_indices_true(self):
name = 'test'
policy = {
'policy': {
'phases': {
'hot': {
'min_age': '0ms',
'actions': {'rollover': {'max_age': '2h', 'max_docs': 4}},
}
}
}
}
url = f'{HOST}/_ilm/policy/{name}'
_ = requests.put(url, json=policy, timeout=30)
# print(r.text) # logging reminder
self.create_indices(10, ilm_policy=name)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.ilm_delete_proto.format(
'age', 'name', 'older', '\'%Y.%m.%d\'', 'days', 5, ' ', ' ', ' ', 'true'
),
)
self.invoke_runner()
self.assertEqual(5, len(exclude_ilm_history(get_indices(self.client))))
def test_allow_ilm_indices_false(self):
name = 'test'
policy = {
'policy': {
'phases': {
'hot': {
'min_age': '0ms',
'actions': {'rollover': {'max_age': '2h', 'max_docs': 4}},
}
}
}
}
url = f'{HOST}/_ilm/policy/{name}'
_ = requests.put(url, json=policy, timeout=30)
# print(r.text) # logging reminder
self.create_indices(10, ilm_policy=name)
self.write_config(self.args['configfile'], testvars.client_config.format(HOST))
self.write_config(
self.args['actionfile'],
testvars.ilm_delete_proto.format(
'age',
'name',
'older',
'\'%Y.%m.%d\'',
'days',
5,
' ',
' ',
' ',
'false',
),
)
self.invoke_runner()
self.assertEqual(10, len(exclude_ilm_history(get_indices(self.client))))
class TestCLIDeleteIndices(CuratorTestCase):
def test_name_older_than_now_cli(self):
self.create_indices(10)
args = self.get_runner_args()
args += [
'--config',
self.args['configfile'],
'delete-indices',
'--filter_list',
(
'{"filtertype":"age","source":"name","direction":"older",'
'"timestring":"%Y.%m.%d","unit":"days","unit_count":5}'
),
]
self.assertEqual(
0,
self.run_subprocess(
args, logname='TestCLIDeleteIndices.test_name_older_than_now_cli'
),
)
self.assertEqual(5, len(exclude_ilm_history(get_indices(self.client))))
|