|
3 | 3 | # 2.0; you may not use this file except in compliance with the Elastic License |
4 | 4 | # 2.0. |
5 | 5 |
|
| 6 | +import time |
6 | 7 | import unittest |
7 | 8 |
|
8 | 9 | from elasticsearch import BadRequestError |
| 10 | +from elasticsearch import ConnectionError as ESConnectionError |
9 | 11 |
|
10 | 12 | from detection_rules.misc import get_default_config, get_elasticsearch_client, get_kibana_client, getdefault |
11 | 13 | from detection_rules.rule_validators import ESQLValidator |
@@ -47,15 +49,28 @@ def test_esql_rules(self): |
47 | 49 |
|
48 | 50 | failed_count = 0 |
49 | 51 | fail_list = [] |
| 52 | + max_retries = 3 |
50 | 53 | for r in esql_rules: |
51 | 54 | print() |
52 | | - try: |
53 | | - validator = ESQLValidator(r.contents.data.query) |
54 | | - validator.remote_validate_rule(kibana_client, elastic_client, r.contents, verbosity) |
55 | | - except (ValueError, BadRequestError) as e: |
56 | | - print(f"FAILURE: {e}") |
57 | | - fail_list.append(f"FAILURE: {e}") |
58 | | - failed_count += 1 |
| 55 | + retry_count = 0 |
| 56 | + while retry_count < max_retries: |
| 57 | + try: |
| 58 | + validator = ESQLValidator(r.contents.data.query) |
| 59 | + validator.remote_validate_rule(kibana_client, elastic_client, r.contents, verbosity) |
| 60 | + break |
| 61 | + except (ValueError, BadRequestError) as e: |
| 62 | + print(f"FAILURE: {e}") |
| 63 | + fail_list.append(f"FAILURE: {e}") |
| 64 | + failed_count += 1 |
| 65 | + break |
| 66 | + except ESConnectionError as e: |
| 67 | + retry_count += 1 |
| 68 | + print(f"Connection error: {e}. Retrying {retry_count}/{max_retries}...") |
| 69 | + time.sleep(30) |
| 70 | + if retry_count == max_retries: |
| 71 | + print(f"FAILURE: {e} after {max_retries} retries") |
| 72 | + fail_list.append(f"FAILURE: {e} after {max_retries} retries") |
| 73 | + failed_count += 1 |
59 | 74 |
|
60 | 75 | print(f"Total rules: {len(esql_rules)}") |
61 | 76 | print(f"Failed rules: {failed_count}") |
|
0 commit comments