Skip to content

Commit 5271f8d

Browse files
APM-6136 re-write python tests using apim pytest library
1 parent f56015a commit 5271f8d

3 files changed

Lines changed: 65 additions & 105 deletions

File tree

e2e/tests/api_tests.py

Lines changed: 64 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -49,88 +49,67 @@ def test_check_status_is_secured(nhsd_apim_proxy_url):
4949
resp = requests.get(f"{nhsd_apim_proxy_url}/_status")
5050
assert resp.status_code == 401
5151

52-
# @pytest.mark.e2e
53-
# @pytest.mark.smoketest
54-
# @pytest.mark.asyncio
55-
# async def test_wait_for_status(
56-
# api_client: APISessionClient, api_test_config: APITestSessionConfig
57-
# ):
58-
# async def is_deployed(resp: ClientResponse):
59-
# if resp.status != 200:
60-
# return False
61-
# body = await resp.json()
62-
#
63-
# if body.get("commitId") != api_test_config.commit_id:
64-
# return False
65-
#
66-
# backend = dict_path(body, ["checks", "healthcheck", "outcome", "version"])
67-
#
68-
# if type(backend) != dict:
69-
# return False
70-
#
71-
# return backend.get("commitId") == api_test_config.commit_id
72-
#
73-
# await poll_until(
74-
# make_request=lambda: api_client.get(
75-
# "_status", headers={"apikey": env.status_endpoint_api_key()}
76-
# ),
77-
# until=is_deployed,
78-
# timeout=120,
79-
# )
80-
#
81-
#
82-
# @pytest.mark.e2e
83-
# @pytest.mark.asyncio
84-
# async def test_api_status_with_service_header_another_service(api_client: APISessionClient):
85-
#
86-
# r = await api_client.get(
87-
# "_status", allow_retries=True, max_retries=5,
88-
# headers={
89-
# 'x-apim-service': 'async-slowapp',
90-
# 'apikey': env.status_endpoint_api_key()
91-
# }
92-
# )
93-
# assert r.status == 200, (r.status, r.reason, (await r.text())[:2000])
94-
# body = await r.json()
95-
#
96-
# service = dict_path(body, ["checks", "healthcheck", "outcome", "service"])
97-
#
98-
# assert service == 'sync-wrap'
99-
#
100-
#
101-
# @pytest.mark.e2e
102-
# @pytest.mark.asyncio
103-
# async def test_api_status_with_service_header(api_client: APISessionClient):
104-
#
105-
# r = await api_client.get(
106-
# "_status", allow_retries=True, max_retries=5,
107-
# headers={
108-
# 'x-apim-service': 'sync-wrap',
109-
# 'apikey': env.status_endpoint_api_key()
110-
# }
111-
# )
112-
# assert r.status == 200, (r.status, r.reason, (await r.text())[:2000])
113-
# body = await r.json()
114-
#
115-
# service = dict_path(body, ["checks", "healthcheck", "outcome", "service"])
116-
#
117-
# assert service == 'sync-wrap'
118-
#
119-
#
120-
# @pytest.mark.e2e
121-
# @pytest.mark.asyncio
122-
# async def test_api_slowapp_slower_than_sync_wait(api_client: APISessionClient):
123-
#
124-
# r = await api_client.get(
125-
# "async-slowapp/slow?delay=5", allow_retries=True, max_retries=5, headers={'x-sync-wait': '0.25'}
126-
# )
127-
# assert r.status == 504, (r.status, r.reason, (await r.text())[:2000])
128-
#
129-
#
130-
# @pytest.mark.e2e
131-
# @pytest.mark.asyncio
132-
# async def test_api_slowapp_responds_test_final_status(api_client: APISessionClient):
133-
#
134-
# r = await api_client.get("async-slowapp/slow?final_status=418&complete_in=0.5", allow_retries=True, max_retries=5)
135-
# assert r.status == 418, (r.status, r.reason, (await r.text())[:2000])
136-
# assert r.reason == "I'm a Teapot"
52+
53+
@pytest.mark.e2e
54+
@pytest.mark.smoketest
55+
@pytest.mark.parametrize("service_header", ["", "async-slowapp", "sync-wrap"])
56+
def test_wait_for_status(nhsd_apim_proxy_url, status_endpoint_auth_headers, service_header):
57+
def _container_not_ready(resp: requests.Response):
58+
"""
59+
Requests to ECS containers which are still starting up return with a
60+
HTTP 503 (service unavailable).
61+
"""
62+
return resp.json().get("checks", {}) \
63+
.get("healthcheck", {}) \
64+
.get("responseCode") == 503
65+
66+
retries = 0
67+
headers = status_endpoint_auth_headers
68+
if service_header:
69+
headers["x-apim-service"] = service_header
70+
resp = requests.get(
71+
f"{nhsd_apim_proxy_url}/_status", headers=headers, timeout=30
72+
)
73+
74+
if resp.status_code != 200:
75+
# Status should always be 200 we don't need to wait for it
76+
pytest.fail(f"Status code {resp.status_code}, expecting 200")
77+
if not resp.json().get("version"):
78+
pytest.fail("version not found")
79+
80+
deployed_commit_id = resp.json().get("commitId")
81+
82+
while deployed_commit_id != getenv("SOURCE_COMMIT_ID") or _container_not_ready(resp) and retries <= 45:
83+
resp = requests.get(
84+
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers, timeout=30
85+
)
86+
87+
deployed_commit_id = resp.json().get("commitId")
88+
retries += 1
89+
sleep(1)
90+
91+
if retries >= 45:
92+
pytest.fail("Timeout Error - max retries")
93+
94+
body = resp.json()
95+
assert body.get("status") == "pass"
96+
97+
service = dict_path(body, ["checks", "healthcheck", "outcome", "service"])
98+
assert service == 'sync-wrap'
99+
100+
101+
@pytest.mark.e2e
102+
def test_api_slowapp_slower_than_sync_wait(nhsd_apim_proxy_url):
103+
resp = requests.get(
104+
f"{nhsd_apim_proxy_url}/async-slowapp/slow?delay=5", headers={'x-sync-wait': '0.25'}, timeout=45
105+
)
106+
assert resp.status_code == 504, (resp.status_code, resp.reason, resp.text[:2000])
107+
108+
109+
@pytest.mark.e2e
110+
def test_api_slowapp_responds_test_final_status(nhsd_apim_proxy_url):
111+
resp = requests.get(
112+
f"{nhsd_apim_proxy_url}/async-slowapp/slow?final_status=418&complete_in=0.5", timeout=30
113+
)
114+
assert resp.status_code == 418, (resp.status_code, resp.reason, resp.text[:2000])
115+
assert resp.reason == "I'm a Teapot"

poetry.lock

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ requests = "^2.32.3"
2828

2929
[tool.poetry.group.dev.dependencies]
3030
pytest = "^8.3.5"
31-
pytest-asyncio = "^0.26.0"
3231
coverage = "^7.8.0"
3332
flake8 = "^7.2.0"
3433
black = "^25.1.0"

0 commit comments

Comments
 (0)