Skip to content

Commit

Permalink
Able to run a few checks in cli hook mode: test_edr_landingpage, test…
Browse files Browse the repository at this point in the history
…_edr_conformance
  • Loading branch information
Lars Falk-Petersen committed Sep 27, 2024
1 parent de42e30 commit bcccc8a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
10 changes: 8 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
"request": "launch",
"program": "venv/bin/st",
"console": "integratedTerminal",
"args": ["run", "--experimental=openapi-3.1", "https://edrisobaric.k8s.met.no/api"],
"args": ["run",
// "--show-trace",
"--experimental=openapi-3.1",
"--checks", "all",
"--workers", "2",
// "--include-path-regex", "'/collections$'",
"--hypothesis-suppress-health-check=too_slow",
"https://edrisobaric.k8s.met.no/api"],
"env": {"SCHEMATHESIS_HOOKS": "sedr.hooks"}

}
]
}
8 changes: 7 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

# export PYTHONPATH=$(pwd)
export SCHEMATHESIS_HOOKS=sedr.hooks
st run --experimental=openapi-3.1 https://edrisobaric.k8s.met.no/api
st run \
--experimental=openapi-3.1 \
--checks all \
--workers 8 \
--include-path-regex '/collections$' \
--hypothesis-suppress-health-check=too_slow \
https://edrisobaric.k8s.met.no/api
6 changes: 3 additions & 3 deletions sedr/edreq11.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""EDR requirements."""

import util
import sedr.util as util

conformance_urls = [
"http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/core",
Expand Down Expand Up @@ -44,7 +44,7 @@ def requirementA11_1(jsondata: str) -> tuple[bool, str]:

for url in jsondata:
if url in openapi_conformance_urls:
if (
if util.args and (
util.args.openapi_version == "3.1"
and "oas31" in url
or util.args.openapi_version == "3.0"
Expand All @@ -53,7 +53,7 @@ def requirementA11_1(jsondata: str) -> tuple[bool, str]:
return True, url
return (
False,
f"OpenAPI version {util.args.openapi_version} and version in conformance {url} doesn't match. See <{spec_url}> for more info.",
f"OpenAPI version {util.args.openapi_version if util.args else "unknown"} and version in conformance {url} doesn't match. See <{spec_url}> for more info.",
)

return (
Expand Down
101 changes: 88 additions & 13 deletions sedr/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,95 @@
from schemathesis.runner import events
from typing import List
import pprint
import json
import sedr.util as util
import sedr.edreq11 as edreq


class SimpleHandler(EventHandler):
def handle_event(self, context, event):
# pprint.pp(event)
if isinstance(event, events.Finished):
print("Done!")
pprint.pp(event)
@schemathesis.hook
def after_call(context, case, response):
"""Hook runs after any call to the API."""
if response.request:
# Log calls with status
# print(
# f"after_call {'OK' if response.ok else 'ERR'} "
# + f"{response.request.path_url} {response.text[0:150]}"
# )

if response.request.path_url == "/":
# test_edr_landingpage
"""Test that the landing page contains required elements."""
spec_ref = "https://docs.ogc.org/is/19-072/19-072.html#_7c772474-7037-41c9-88ca-5c7e95235389"
try:
landingpage_json = response.json()
landing, landing_message = util.parse_landing_json(landingpage_json)
if not landing:
raise AssertionError(
f"Landing page is missing required elements. See <{spec_ref}> for more info. {landing_message}"
)

@schemathesis.hook
def after_init_cli_run_handlers(
context: HookContext,
handlers: List[EventHandler],
execution_context: ExecutionContext,
) -> None:
handlers[:] = [SimpleHandler()]
print("Landingpage %s tested OK", response.url)
except json.decoder.JSONDecodeError as e:
print(
f"Landing page is not valid JSON, other formats are not tested yet., {e}"
)


if response.request.path_url == "/conformance":
# def test_edr_conformance(case):
"""Test /conformance endpoint."""
conformance_json = response.json()

if "conformsTo" not in conformance_json:
spec_ref = "https://docs.ogc.org/is/19-072/19-072.html#_4129e3d3-9428-4e91-9bfc-645405ed2369"
raise AssertionError(
f"Conformance page /conformance does not contain a conformsTo attribute. See {spec_ref} for more info."
)

resolves, resolves_message = util.test_conformance_links(
jsondata=conformance_json["conformsTo"]
)
if not resolves:
raise AssertionError(resolves_message)

requirementA2_2_A5, requirementA2_2_A5_message = edreq.requirementA2_2_A5(
jsondata=conformance_json["conformsTo"]
)
if not requirementA2_2_A5:
raise AssertionError(requirementA2_2_A5_message)

requirementA2_2_A7, requirementA2_2_A7_message = edreq.requirementA2_2_A7(
response.raw.version
)
if not requirementA2_2_A7:
raise AssertionError(requirementA2_2_A7_message)

requirementA11_1, requirementA11_1_message = edreq.requirementA11_1(
jsondata=conformance_json["conformsTo"]
)
if not requirementA11_1:
raise AssertionError(requirementA11_1_message)

print("Conformance %s tested OK", response.url)


# class SimpleHandler(EventHandler):
# def handle_event(self, context, event):
# print(event.__class__)

# if isinstance(event, events.AfterProbing):
# print("AfterProbing event:")
# pprint.pp(event)

# if isinstance(event, events.Finished):
# print("Finished event:")
# pprint.pp(event)


# @schemathesis.hook
# def after_init_cli_run_handlers(
# context: HookContext,
# handlers: List[EventHandler],
# execution_context: ExecutionContext,
# ) -> None:
# handlers[:] = [SimpleHandler()]

0 comments on commit bcccc8a

Please sign in to comment.