Skip to content

Commit ef98424

Browse files
authored
Add state machine tests (kubeflow#1271)
* Add state machine tests Signed-off-by: fege <[email protected]> * adjust if to test on the open PR Signed-off-by: fege <[email protected]> * revert if and remove debugging prints Signed-off-by: fege <[email protected]> * revert if and remove debugging prints Signed-off-by: fege <[email protected]> * Remove db-cleanup Signed-off-by: fege <[email protected]> --------- Signed-off-by: fege <[email protected]>
1 parent 185c389 commit ef98424

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

clients/python/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ test-e2e-run:
5757

5858
.PHONY: test-e2e-cleanup
5959
test-e2e-cleanup:
60+
@echo "Cleaning up database..."
61+
cd ../../ && ./scripts/cleanup.sh
6062
@echo "Cleaning up port-forward processes..."
6163
@if [ -f .port-forwards.pid ]; then \
6264
kill $$(cat .port-forwards.pid) || true; \

clients/python/tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
from collections.abc import Generator
1313
from contextlib import asynccontextmanager
1414
from pathlib import Path
15+
from typing import Any
1516
from unittest.mock import Mock, patch
1617
from urllib.parse import urlparse
1718

1819
import pytest
1920
import requests
2021
import schemathesis
2122
import uvloop
23+
from schemathesis import Case, Response
24+
from schemathesis.generation.stateful.state_machine import APIStateMachine
2225
from schemathesis.specs.openapi.schemas import BaseOpenAPISchema
2326

2427
from model_registry import ModelRegistry
@@ -385,3 +388,36 @@ def _artifact_resource(auth_headers: dict, payload: dict) -> Generator[str, None
385388
except Exception as e:
386389
print(f"Failed to delete artifact {artifact_id}: {e}")
387390
return _artifact_resource
391+
392+
@pytest.fixture
393+
def auth_headers(setup_env_user_token):
394+
"""Provides authorization headers for API requests."""
395+
return {
396+
"Content-Type": "application/json",
397+
"Authorization": f"Bearer {setup_env_user_token}"
398+
}
399+
400+
@pytest.fixture
401+
def state_machine(generated_schema: BaseOpenAPISchema, auth_headers: str) -> APIStateMachine:
402+
BaseAPIWorkflow = generated_schema.as_state_machine()
403+
404+
class APIWorkflow(BaseAPIWorkflow): # type: ignore
405+
headers: dict[str, str]
406+
407+
def setup(self) -> None:
408+
print("Cleaning up database")
409+
subprocess.run(
410+
["../../scripts/cleanup.sh"],
411+
capture_output=True,
412+
check=True
413+
)
414+
self.headers = auth_headers
415+
416+
def before_call(self, case: Case) -> None:
417+
print(f"Checking: {case.method} {case.path}")
418+
def get_call_kwargs(self, case: Case) -> dict[str, Any]:
419+
return {"verify": False, "headers": self.headers}
420+
421+
def after_call(self, response: Response, case: Case) -> None:
422+
print(f"{case.method} {case.path} -> {response.status_code},")
423+
return APIWorkflow

clients/python/tests/test_api.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
schema = schemathesis.pytest.from_fixture("generated_schema")
1313

1414

15-
@pytest.fixture
16-
def auth_headers(setup_env_user_token):
17-
"""Provides authorization headers for API requests."""
18-
return {
19-
"Content-Type": "application/json",
20-
"Authorization": f"Bearer {setup_env_user_token}"
21-
}
22-
2315
schema = (
2416
schema
2517
.exclude(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from hypothesis import HealthCheck, settings
3+
4+
5+
@pytest.mark.fuzz
6+
class TestRestAPIStateful:
7+
def test_mr_api_stateful(self, state_machine):
8+
"""Launches stateful tests against the Model Registry API endpoints defined in its openAPI yaml spec file"""
9+
state_machine.run(settings=settings(
10+
max_examples=20,
11+
deadline=10000, #10 seconds
12+
suppress_health_check=[
13+
HealthCheck.filter_too_much,
14+
HealthCheck.too_slow,
15+
HealthCheck.data_too_large,
16+
],
17+
))

0 commit comments

Comments
 (0)