From 7603fd2bd4c3f7f308a35aadef805c33ee8ed667 Mon Sep 17 00:00:00 2001 From: zhuwenxing Date: Tue, 25 Jul 2023 17:07:02 +0800 Subject: [PATCH] [test]Use pytest assume to assert (#25890) Signed-off-by: zhuwenxing --- tests/python_client/chaos/chaos_commons.py | 44 ++++++++++++------- ...le_request_operation_for_rolling_update.py | 3 +- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/tests/python_client/chaos/chaos_commons.py b/tests/python_client/chaos/chaos_commons.py index 39a2e2f05d..8970630ef6 100644 --- a/tests/python_client/chaos/chaos_commons.py +++ b/tests/python_client/chaos/chaos_commons.py @@ -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}") \ No newline at end of file + 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}", + ) diff --git a/tests/python_client/chaos/testcases/test_single_request_operation_for_rolling_update.py b/tests/python_client/chaos/testcases/test_single_request_operation_for_rolling_update.py index 5884e2ab1b..6927c265a8 100644 --- a/tests/python_client/chaos/testcases/test_single_request_operation_for_rolling_update.py +++ b/tests/python_client/chaos/testcases/test_single_request_operation_for_rolling_update.py @@ -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