Skip to content

Commit

Permalink
Improve a flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
tellet committed Sep 16, 2019
1 parent 58c869c commit c769e55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
5 changes: 3 additions & 2 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
27 changes: 20 additions & 7 deletions tests/suite/test_virtual_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})],
Expand All @@ -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)

0 comments on commit c769e55

Please sign in to comment.