Skip to content

Commit 9824206

Browse files
APM-6127 initial changes to refactor tests
1 parent cd01b4d commit 9824206

12 files changed

Lines changed: 985 additions & 1382 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
2+
23
bin/
34
dist/
45
build/
@@ -12,5 +13,6 @@ __pycache__/
1213
.envrc
1314
.idea/
1415
.venv/
16+
.direnv/
1517

1618
smoketest-report.xml

Makefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ release: clean publish build-proxy
4545
mkdir -p dist
4646
for f in $(_dist_include); do cp -r $$f dist; done
4747

48+
TEST_CMD := @APIGEE_ACCESS_TOKEN=$(APIGEE_ACCESS_TOKEN) \
49+
poetry run pytest -v \
50+
--color=yes \
51+
--api-name=splunk-monitor \
52+
--proxy-name=$(PROXY_NAME) \
53+
-s
54+
4855
test:
49-
# this target should be used for local unit tests .. runs as part of the build pipeline
50-
make --no-print-directory -C test
56+
$(TEST_CMD) \
57+
--junitxml=test-report.xml \
5158

5259
smoketest:
53-
# this target is for end to end smoketests this would be run 'post deploy' to verify an environment is working
54-
poetry run pytest -v --junitxml=smoketest-report.xml -s -m smoketest
60+
$(TEST_CMD) \
61+
--junitxml=smoketest-report.xml \
62+
-m smoketest

azure/templates/run-smoke-tests.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ steps:
55
displayName: Setup pytests
66
77
- bash: |
8-
export RELEASE_RELEASEID=$(Build.BuildId)
9-
export SOURCE_COMMIT_ID=$(Build.SourceVersion)
10-
export APIGEE_ENVIRONMENT="$(ENVIRONMENT)"
11-
export SERVICE_BASE_PATH="$(SERVICE_BASE_PATH)"
12-
export STATUS_ENDPOINT_API_KEY="$(status-endpoint-api-key)"
8+
export PROXY_NAME="$(FULLY_QUALIFIED_SERVICE_NAME)"
9+
export APIGEE_ACCESS_TOKEN="$(secret.AccessToken)"
10+
export SOURCE_COMMIT_ID="$(Build.SourceVersion)"
1311
1412
make smoketest
1513
workingDirectory: $(Pipeline.Workspace)/s/$(SERVICE_NAME)/$(SERVICE_ARTIFACT_NAME)

poetry.lock

Lines changed: 874 additions & 1193 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package-mode = false
1515

1616
[tool.poetry.dependencies]
1717
python = "^3.9"
18+
pytest-nhsd-apim = "^4.0.1"
19+
requests = "^2.32.3"
1820

1921
[tool.poetry.group.dev.dependencies]
2022
flake8 = "^7.1.2"
@@ -26,10 +28,9 @@ docopt = "^0.6.2"
2628
jsonpath-rw = "^1.4.0"
2729
semver = "^3.0.2"
2830
gitpython = "^3.1.34"
29-
pytest = "^6.1.2"
31+
pytest = "^8.2.0"
3032
coverage = "^5.5"
3133
aiohttp = "^3.10.11"
3234
pytest-asyncio = "^0.14.0"
33-
api-test-utils = {url = "https://github.com/NHSDigital/apim-test-utils/releases/download/v1.1.43-alpha/api_test_utils-1.1.43a0-py3-none-any.whl"}
3435

3536
[tool.poetry.scripts]

tests/__init__.py

Whitespace-only changes.

tests/api_tests.py

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,31 @@
1-
from functools import partial
1+
from os import getenv
22

33
import pytest
4-
from aiohttp import ClientResponse
5-
from api_test_utils.api_session_client import APISessionClient
6-
from api_test_utils.api_test_session_config import APITestSessionConfig
7-
from api_test_utils import poll_until, env
8-
9-
10-
async def _is_deployed(resp: ClientResponse, api_test_config: APITestSessionConfig) -> bool:
11-
12-
if resp.status != 200:
13-
return False
14-
body = await resp.json()
15-
16-
return body.get("commitId") == api_test_config.commit_id
17-
18-
19-
async def is_401(resp: ClientResponse) -> bool:
20-
return resp.status == 401
21-
22-
23-
async def is_200(resp: ClientResponse) -> bool:
24-
return resp.status == 200
25-
26-
27-
@pytest.mark.e2e
28-
@pytest.mark.smoketest
29-
def test_output_test_config(api_test_config: APITestSessionConfig):
30-
print(api_test_config)
4+
import requests
315

326

337
@pytest.mark.e2e
348
@pytest.mark.smoketest
359
@pytest.mark.asyncio
36-
async def test_wait_for_ping(api_client: APISessionClient, api_test_config: APITestSessionConfig):
37-
"""
38-
test for _ping .. this uses poll_until to wait until the correct SOURCE_COMMIT_ID ( from env var )
39-
is available
40-
"""
41-
42-
is_deployed = partial(_is_deployed, api_test_config=api_test_config)
43-
44-
await poll_until(
45-
make_request=lambda: api_client.get('_ping'),
46-
until=is_deployed,
47-
timeout=120
48-
)
49-
50-
51-
@pytest.mark.e2e
52-
@pytest.mark.smoketest
53-
@pytest.mark.asyncio
54-
@pytest.mark.skip(reason="There is no _status endpoint configured on this proxy.")
55-
async def test_check_status_is_secured(api_client: APISessionClient):
56-
57-
await poll_until(
58-
make_request=lambda: api_client.get('_status'),
59-
until=is_401,
60-
timeout=120
61-
)
62-
63-
64-
@pytest.mark.e2e
65-
@pytest.mark.smoketest
66-
@pytest.mark.asyncio
67-
@pytest.mark.skip(reason="There is no _status endpoint configured on this proxy.")
68-
async def test_wait_for_status(api_client: APISessionClient, api_test_config: APITestSessionConfig):
69-
10+
async def test_wait_for_ping(nhsd_apim_proxy_url):
7011
"""
71-
test for _status .. this uses poll_until to wait until the correct SOURCE_COMMIT_ID ( from env var )
12+
test for _ping, this waits until the correct SOURCE_COMMIT_ID ( from env var )
7213
is available
7314
"""
74-
await poll_until(
75-
make_request=lambda: api_client.get('_status', headers={'apikey': env.status_endpoint_api_key()}),
76-
until=is_200,
77-
timeout=120
78-
)
15+
retries = 0
16+
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
17+
deployed_commit_id = resp.json().get("commitId")
18+
19+
while (deployed_commit_id != getenv('SOURCE_COMMIT_ID')
20+
and retries <= 30
21+
and resp.status_code == 200):
22+
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
23+
deployed_commit_id = resp.json().get("commitId")
24+
retries += 1
25+
26+
if resp.status_code != 200:
27+
pytest.fail(f"Status code {resp.status_code}, expecting 200")
28+
elif retries >= 30:
29+
pytest.fail("Timeout Error - max retries")
30+
31+
assert deployed_commit_id == getenv('SOURCE_COMMIT_ID')

tests/configuration/__init__.py

Whitespace-only changes.

tests/configuration/config.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/configuration/environment.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)