Skip to content

Commit

Permalink
test elastic
Browse files Browse the repository at this point in the history
  • Loading branch information
lordgamez committed Sep 18, 2024
1 parent cd1fd88 commit fc6cb52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from utils import retry_until_not_none


class ElasticSearchChecker:
Expand All @@ -33,9 +34,12 @@ def check_elastic_field_value(self, container_name, index_name, doc_id, field_na
"curl -u elastic:password -k -XGET https://localhost:9200/" + index_name + "/_doc/" + doc_id])
return code == 0 and (field_name + '":"' + field_value) in output

@retry_until_not_none()
def elastic_generate_apikey(self, elastic_container_name):
(_, output) = self.container_communicator.execute_command(elastic_container_name, ["/bin/bash", "-c",
"curl -u elastic:password -k -XPOST https://localhost:9200/_security/api_key -H Content-Type:application/json -d'{\"name\":\"my-api-key\",\"expiration\":\"1d\",\"role_descriptors\":{\"role-a\": {\"cluster\": [\"all\"],\"index\": [{\"names\": [\"my_index\"],\"privileges\": [\"all\"]}]}}}'"])
(code, output) = self.container_communicator.execute_command(elastic_container_name, ["/bin/bash", "-c",
"curl -u elastic:password -k -XPOST https://localhost:9200/_security/api_key -H Content-Type:application/json -d'{\"name\":\"my-api-key\",\"expiration\":\"1d\",\"role_descriptors\":{\"role-a\": {\"cluster\": [\"all\"],\"index\": [{\"names\": [\"my_index\"],\"privileges\": [\"all\"]}]}}}'"])
if code != 0:
return None
output_lines = output.splitlines()
result = json.loads(output_lines[-1])
return result["encoded"]
Expand Down
17 changes: 16 additions & 1 deletion docker/test/integration/features/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def retry_check(max_tries=5, retry_interval=1):
def retry_check_func(func):
@functools.wraps(func)
def retry_wrapper(*args, **kwargs):
for i in range(max_tries):
for _ in range(max_tries):
if func(*args, **kwargs):
return True
time.sleep(retry_interval)
Expand All @@ -34,6 +34,21 @@ def retry_wrapper(*args, **kwargs):
return retry_check_func


def retry_until_not_none(max_retries=10, delay=1):
def decorator_retry(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = None
for _ in range(max_retries):
result = func(*args, **kwargs)
if result is not None:
return result
time.sleep(delay)
return result
return wrapper
return decorator_retry


def decode_escaped_str(str):
special = {"n": "\n", "v": "\v", "t": "\t", "f": "\f", "r": "\r", "a": "\a", "\\": "\\"}
escaped = False
Expand Down

0 comments on commit fc6cb52

Please sign in to comment.