[test]Use pytest assume to assert (#25890)

Signed-off-by: zhuwenxing <wenxing.zhu@zilliz.com>
This commit is contained in:
zhuwenxing 2023-07-25 17:07:02 +08:00 committed by GitHub
parent fc6f7f9805
commit 7603fd2bd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 17 deletions

View File

@ -6,15 +6,17 @@ from chaos import constants
from yaml import full_load
from utils.util_log import test_log as log
from delayed_assert import expect
import pytest
def check_config(chaos_config):
if not chaos_config.get('kind', None):
if not chaos_config.get("kind", None):
raise Exception("kind must be specified")
if not chaos_config.get('spec', None):
if not chaos_config.get("spec", None):
raise Exception("spec must be specified")
if "action" not in chaos_config.get('spec', None):
if "action" not in chaos_config.get("spec", None):
raise Exception("action must be specified in spec")
if "selector" not in chaos_config.get('spec', None):
if "selector" not in chaos_config.get("spec", None):
raise Exception("selector must be specified in spec")
return True
@ -42,7 +44,7 @@ def start_monitor_threads(checkers={}):
def get_env_variable_by_name(name):
""" get env variable by name"""
"""get env variable by name"""
try:
env_var = os.environ[name]
log.debug(f"env_variable: {env_var}")
@ -58,7 +60,7 @@ def get_chaos_yamls():
if chaos_env is not None:
if os.path.isdir(chaos_env):
log.debug(f"chaos_env is a dir: {chaos_env}")
return glob.glob(chaos_env + 'chaos_*.yaml')
return glob.glob(chaos_env + "chaos_*.yaml")
elif os.path.isfile(chaos_env):
log.debug(f"chaos_env is a file: {chaos_env}")
return [chaos_env]
@ -69,12 +71,12 @@ def get_chaos_yamls():
return glob.glob(constants.TESTS_CONFIG_LOCATION + constants.ALL_CHAOS_YAMLS)
def reconnect(connections, alias='default', timeout=360):
def reconnect(connections, alias="default", timeout=360):
"""trying to connect by connection alias"""
is_connected = False
start = time.time()
end = time.time()
while not is_connected or end-start < timeout:
while not is_connected or end - start < timeout:
try:
connections.connect(alias)
is_connected = True
@ -87,17 +89,27 @@ def reconnect(connections, alias='default', timeout=360):
return connections.connect(alias)
def assert_statistic(checkers, expectations={}, succ_rate_threshold=0.95, fail_rate_threshold=0.49):
def assert_statistic(
checkers, expectations={}, succ_rate_threshold=0.95, fail_rate_threshold=0.49
):
for k in checkers.keys():
# expect succ if no expectations
succ_rate = checkers[k].succ_rate()
total = checkers[k].total()
average_time = checkers[k].average_time
if expectations.get(k, '') == constants.FAIL:
log.info(f"Expect Fail: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}")
expect(succ_rate < fail_rate_threshold or total < 2,
f"Expect Fail: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}")
if expectations.get(k, "") == constants.FAIL:
log.info(
f"Expect Fail: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}"
)
pytest.assume(
succ_rate < fail_rate_threshold or total < 2,
f"Expect Fail: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}",
)
else:
log.info(f"Expect Succ: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}")
expect(succ_rate > succ_rate_threshold and total > 2,
f"Expect Succ: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}")
log.info(
f"Expect Succ: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}"
)
pytest.assume(
succ_rate > succ_rate_threshold and total > 2,
f"Expect Succ: {str(k)} succ rate {succ_rate}, total: {total}, average time: {average_time:.4f}",
)

View File

@ -84,6 +84,7 @@ class TestOperations(TestBase):
v.pause()
for k, v in self.health_checkers.items():
v.check_result()
for k, v in self.health_checkers.items():
log.info(f"{k} rto: {v.get_rto()}")
if is_check:
assert_statistic(self.health_checkers, succ_rate_threshold=0.98)
@ -92,7 +93,7 @@ class TestOperations(TestBase):
for k, v in self.health_checkers.items():
log.info(f"{k} rto: {v.get_rto()}")
rto = v.get_rto()
assert rto < 30, f"expect 30s but get {rto}s" # rto should be less than 30s
pytest.assume(rto < 30, f"{k} rto expect 30s but get {rto}s") # rto should be less than 30s
if Op.insert in self.health_checkers:
# verify the no insert data loss