Skip to content

Commit

Permalink
add two simple scenarios for the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
lvaylet committed Oct 20, 2023
1 parent 0007beb commit fd889f5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ dev =
isort
mock
pytest
pytest-bdd
pytest-cov
pylint
pytype
Expand Down
15 changes: 15 additions & 0 deletions tests/features/api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: slo-generator API
A REST-ful API to interact with the SLO Generator

Scenario: Calling the API endpoint with a GET
Given I am a public, unauthenticated user
When I call the API endpoint with a GET
Then The API is available

Scenario: Calling the API endpoint with a POST and an SLO definition
Given I am a public, unauthenticated user
When I call the API endpoint with a POST and an SLO definition
Then The API is available
And The API response is not empty
And The API response has the expected format
And The API response has the expected content
85 changes: 85 additions & 0 deletions tests/step_defs/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import pytest
import requests
from pytest_bdd import given, scenario, then, when

# The URL of an SLO Generator API running on Cloud Run with the latest version
API_CLOUD_RUN_URL: str = "https://slo-generator-ulho4vepfq-ew.a.run.app/"


class ApiHelper:
responseCode = None


@pytest.fixture
def api():
helper = ApiHelper()
return helper


@scenario("../features/api.feature", "Calling the API endpoint with a GET")
def test_call_api_with_get(api):
pass


@given("I am a public, unauthenticated user")
def public_unauthenticated_user():
pass


@when("I call the API endpoint with a GET")
def call_api_endpoint_with_get(api: ApiHelper):
response = requests.get(API_CLOUD_RUN_URL)
api.responseCode = response.status_code


@then("The API is available")
def api_is_available(api: ApiHelper):
assert api.responseCode == 200


@scenario(
"../features/api.feature",
"Calling the API endpoint with a POST and an SLO definition",
)
def test_call_api_with_post_and_slo_definition(api):
pass


@when("I call the API endpoint with a POST and an SLO definition")
def call_api_endpoint_with_post_and_slo_definition(api: ApiHelper):
with open("./samples/cloud_monitoring/slo_gae_app_availability.yaml", "rb") as f:
payload = f.read()
payload = payload.replace(b"${GAE_PROJECT_ID}", b"slo-generator-ci-a2b4")
headers = {"content-type": "application/x-www-form-urlencoded"}
response = requests.post(API_CLOUD_RUN_URL, data=payload, headers=headers)
api.responseCode = response.status_code
api.json = response.json()


@then("The API response is not empty")
def api_returns_some_data(api: ApiHelper):
assert api.json


@then("The API response has the expected format")
def api_response_has_expected_format(api: ApiHelper):
assert len(api.json) == 2 and all(
key in api.json[0]
for key in (
"alert",
"backend",
"bad_events_count",
"good_events_count",
"events_count",
"error_budget_burn_rate",
)
)


@then("The API response has the expected content")
def api_response_has_expected_content(api: ApiHelper):
assert (
api.json[0]["exporters"] == ["cloud_monitoring"]
and api.json[0]["bad_events_count"] + api.json[0]["good_events_count"]
== api.json[0]["events_count"]
)

0 comments on commit fd889f5

Please sign in to comment.