From c769e55b4bf3d58108231c38e1857868810addeb Mon Sep 17 00:00:00 2001 From: tellet Date: Tue, 10 Sep 2019 15:31:27 +0100 Subject: [PATCH] Improve a flaky test --- tests/suite/resources_utils.py | 5 +++-- tests/suite/test_virtual_server.py | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/suite/resources_utils.py b/tests/suite/resources_utils.py index fb2550f2f7..75f32980aa 100644 --- a/tests/suite/resources_utils.py +++ b/tests/suite/resources_utils.py @@ -963,9 +963,10 @@ def ensure_response_from_backend(req_url, host) -> None: :param host: :return: """ - for _ in range(5): + for _ in range(10): resp = requests.get(req_url, headers={"host": host}, verify=False) if resp.status_code != 502: + print(f"At last after {_ * 2} seconds got 200. Continue...") return time.sleep(2) - pytest.fail(f"Keep getting 502 from {req_url} after 10 seconds. Exiting...") + pytest.fail(f"Keep getting 502 from {req_url} after 20 seconds. Exiting...") diff --git a/tests/suite/test_virtual_server.py b/tests/suite/test_virtual_server.py index 182fd5f3fb..a8f588f293 100644 --- a/tests/suite/test_virtual_server.py +++ b/tests/suite/test_virtual_server.py @@ -181,6 +181,24 @@ def test_responses_after_crd_removal_on_the_fly(self, kube_apis, crd_ingress_con assert resp.status_code == 200 +def wait_and_assert_status_code(code, req_url, host) -> None: + """ + Wait for a specific response status code. + + :param code: status_code + :param req_url: request url + :param host: request headers if any + :return: + """ + counter = 0 + resp = requests.get(req_url, headers={"host": host}) + while not resp.status_code == code and counter <= 4: + wait_before_test(1) + counter = counter + 1 + resp = requests.get(req_url, headers={"host": host}) + assert resp.status_code == code, f"After a few seconds the status_code is not {code}" + + @pytest.mark.parametrize('crd_ingress_controller, virtual_server_setup', [({"type": "rbac-without-vs", "extra_args": [f"-enable-custom-resources"]}, {"example": "virtual-server", "app_type": "simple"})], @@ -197,10 +215,5 @@ def test_responses_after_rbac_misconfiguration(self, kube_apis, crd_ingress_cont print("Step 2: configure RBAC and check") patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml") - wait_before_test(1) - resp = requests.get(virtual_server_setup.backend_1_url, - headers={"host": virtual_server_setup.vs_host}) - assert resp.status_code == 200 - resp = requests.get(virtual_server_setup.backend_2_url, - headers={"host": virtual_server_setup.vs_host}) - assert resp.status_code == 200 + wait_and_assert_status_code(200, virtual_server_setup.backend_1_url, virtual_server_setup.vs_host) + wait_and_assert_status_code(200, virtual_server_setup.backend_2_url, virtual_server_setup.vs_host)