1+ from os import getenv
2+ from time import sleep
13from typing import List
24
35import pytest
4- from aiohttp import ClientResponse
5- from api_test_utils import poll_until
6- from api_test_utils .api_session_client import APISessionClient
7- from api_test_utils .api_test_session_config import APITestSessionConfig
8- from api_test_utils import env
6+ import requests
97
108
119def dict_path (raw , path : List [str ]):
@@ -24,114 +22,115 @@ def dict_path(raw, path: List[str]):
2422
2523@pytest .mark .e2e
2624@pytest .mark .smoketest
27- @pytest .mark .asyncio
28- async def test_wait_for_ping (api_client : APISessionClient , api_test_config : APITestSessionConfig ):
25+ def test_wait_for_ping (nhsd_apim_proxy_url ):
26+ retries = 0
27+ resp = requests .get (f"{ nhsd_apim_proxy_url } /_ping" , timeout = 30 )
28+ deployed_commit_id = resp .json ().get ("commitId" )
2929
30- async def _is_complete (resp : ClientResponse ):
30+ while deployed_commit_id != getenv ("SOURCE_COMMIT_ID" ) and retries <= 30 :
31+ resp = requests .get (f"{ nhsd_apim_proxy_url } /_ping" , timeout = 30 )
3132
32- if resp .status != 200 :
33- return False
34- body = await resp .json ()
35- return body .get ("commitId" ) == api_test_config .commit_id
33+ if resp .status_code != 200 :
34+ pytest .fail (f"Status code { resp .status_code } , expecting 200" )
3635
37- await poll_until (
38- make_request = lambda : api_client .get ('_ping' ),
39- until = _is_complete ,
40- timeout = 120
41- )
36+ deployed_commit_id = resp .json ().get ("commitId" )
37+ retries += 1
38+ sleep (1 )
4239
40+ if retries >= 30 :
41+ pytest .fail ("Timeout Error - max retries" )
4342
44- @pytest .mark .e2e
45- @pytest .mark .smoketest
46- @pytest .mark .asyncio
47- async def test_check_status_is_secured (api_client : APISessionClient ):
48-
49- async with api_client .get ("_status" , allow_retries = True ) as resp :
50- assert resp .status == 401
43+ assert deployed_commit_id == getenv ("SOURCE_COMMIT_ID" )
5144
5245
5346@pytest .mark .e2e
5447@pytest .mark .smoketest
55- @pytest .mark .asyncio
56- async def test_wait_for_status (
57- api_client : APISessionClient , api_test_config : APITestSessionConfig
58- ):
59- async def is_deployed (resp : ClientResponse ):
60- if resp .status != 200 :
61- return False
62- body = await resp .json ()
63-
64- if body .get ("commitId" ) != api_test_config .commit_id :
65- return False
66-
67- backend = dict_path (body , ["checks" , "healthcheck" , "outcome" , "version" ])
68-
69- if type (backend ) != dict :
70- return False
71-
72- return backend .get ("commitId" ) == api_test_config .commit_id
73-
74- await poll_until (
75- make_request = lambda : api_client .get (
76- "_status" , headers = {"apikey" : env .status_endpoint_api_key ()}
77- ),
78- until = is_deployed ,
79- timeout = 120 ,
80- )
81-
82-
83- @pytest .mark .e2e
84- @pytest .mark .asyncio
85- async def test_api_status_with_service_header_another_service (api_client : APISessionClient ):
86-
87- r = await api_client .get (
88- "_status" , allow_retries = True , max_retries = 5 ,
89- headers = {
90- 'x-apim-service' : 'async-slowapp' ,
91- 'apikey' : env .status_endpoint_api_key ()
92- }
93- )
94- assert r .status == 200 , (r .status , r .reason , (await r .text ())[:2000 ])
95- body = await r .json ()
96-
97- service = dict_path (body , ["checks" , "healthcheck" , "outcome" , "service" ])
98-
99- assert service == 'sync-wrap'
100-
101-
102- @pytest .mark .e2e
103- @pytest .mark .asyncio
104- async def test_api_status_with_service_header (api_client : APISessionClient ):
105-
106- r = await api_client .get (
107- "_status" , allow_retries = True , max_retries = 5 ,
108- headers = {
109- 'x-apim-service' : 'sync-wrap' ,
110- 'apikey' : env .status_endpoint_api_key ()
111- }
112- )
113- assert r .status == 200 , (r .status , r .reason , (await r .text ())[:2000 ])
114- body = await r .json ()
115-
116- service = dict_path (body , ["checks" , "healthcheck" , "outcome" , "service" ])
117-
118- assert service == 'sync-wrap'
119-
120-
121- @pytest .mark .e2e
122- @pytest .mark .asyncio
123- async def test_api_slowapp_slower_than_sync_wait (api_client : APISessionClient ):
124-
125- r = await api_client .get (
126- "async-slowapp/slow?delay=5" , allow_retries = True , max_retries = 5 , headers = {'x-sync-wait' : '0.25' }
127- )
128- assert r .status == 504 , (r .status , r .reason , (await r .text ())[:2000 ])
129-
130-
131- @pytest .mark .e2e
132- @pytest .mark .asyncio
133- async def test_api_slowapp_responds_test_final_status (api_client : APISessionClient ):
134-
135- r = await api_client .get ("async-slowapp/slow?final_status=418&complete_in=0.5" , allow_retries = True , max_retries = 5 )
136- assert r .status == 418 , (r .status , r .reason , (await r .text ())[:2000 ])
137- assert r .reason == "I'm a Teapot"
48+ def test_check_status_is_secured (nhsd_apim_proxy_url ):
49+ resp = requests .get (f"{ nhsd_apim_proxy_url } /_status" )
50+ assert resp .status_code == 401
51+
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"
0 commit comments