Commit 55fb9ec2 authored by Mark Swillus's avatar Mark Swillus
Browse files

Add url and change to POST

parent 47484cbc
Pipeline #5972 passed with stage
in 37 seconds
......@@ -69,7 +69,7 @@ async def all_url_checks(check: Union[http.CheckUrl, dns.CheckDomain]) -> list[d
return results
@app.get("/check/prometheus")
async def prometheus_checks() -> None:
@app.post("/check/prometheus")
async def prometheus_checks(prometheusUrl: prometheus.PrometheusUrl) -> None:
checker = retrieve_check("prometheus")
return await checker()
return await checker(prometheusUrl)
async def check_prometheus() -> None:
import httpx
import pydantic
from httpx import AsyncClient
from isitup.checks import CheckErrorException
class PrometheusUrl(pydantic.BaseModel):
prometheusUrl: pydantic.AnyHttpUrl = pydantic.Field(
..., title="Prometheus Url", description="Prometheus server to query"
)
class Config:
schema_extra = {"example": {"PrometheusUrl": "https://prometheus.example.com",}}
async def check_prometheus(prometheusUrl: PrometheusUrl) -> None:
healthy = await prometheus_is_healthy(prometheusUrl.prometheusUrl)
if not healthy:
raise CheckErrorException(status_code=502, detail="Prometheus is not reachable")
return 1
def get_client():
return AsyncClient()
async def prometheus_is_healthy(url: str) -> bool:
healthcheck_url = url + "/-/healthy"
ready_url = url + "/-/ready"
async with get_client() as client:
response = await client.get(healthcheck_url)
if response.status_code == 200 and b"Prometheus is Healthy." in response.read():
response = await client.get(ready_url)
if (
response.status_code == 200
and b"Prometheus is Ready." in response.read()
):
return True
return False
def register(register_callback: callable) -> None:
register_callback(check_prometheus, "prometheus")
......@@ -3,7 +3,9 @@ black==20.8b1
asgi_lifespan
pytest==6.2.1
pytest-asyncio
pytest-httpserver
pytest-mock
pytest-raises
pre-commit
pytest-watch
pytest-cov
......
import re
from unittest import mock
import pytest
from httpx import AsyncClient
from pytest_httpserver import HTTPServer
from pytest_mock import MockerFixture
from starlette.status import (
HTTP_200_OK,
......@@ -10,6 +12,8 @@ from starlette.status import (
)
import isitup
from isitup.checks import CheckErrorException
from isitup.prometheus import PrometheusUrl, check_prometheus, prometheus_is_healthy
class TestHttpChecks:
......@@ -59,10 +63,20 @@ class TestDnsChecks:
class TestPrometheusChecks:
@pytest.mark.asyncio
async def test_check_prometheus_is_reachable(
self, api: AsyncClient, base_url: str
async def test_check_prometheus_requires_post(
self,
api: AsyncClient,
httpserver: HTTPServer,
mocker: MockerFixture,
base_url: str,
) -> None:
res = await api.get("/check/prometheus/")
assert res.status_code == 405
mock_function = mock.AsyncMock(return_value=True)
mocker.patch("isitup.prometheus.prometheus_is_healthy", mock_function)
res = await api.post(
"/check/prometheus/", json={"prometheusUrl": "https://ignored.net"}
)
assert res.status_code == 200
@pytest.mark.asyncio
......@@ -71,5 +85,34 @@ class TestPrometheusChecks:
) -> None:
mock_function = mock.AsyncMock()
mocker.patch("isitup.prometheus.check_prometheus", mock_function)
res = await api.get("/check/prometheus/")
res = await api.post("/check/prometheus/")
mock_function.call_count == 1
@pytest.mark.asyncio
async def test_prometheus_check_function_executes_healthcheck(
self, api: AsyncClient, httpserver: HTTPServer, base_url: str
) -> None:
health = await prometheus_is_healthy(httpserver.url_for("")) # returns 500
assert health == False
httpserver.expect_request("/-/healthy").respond_with_data(
"Prometheus is Healthy."
)
httpserver.expect_oneshot_request("/-/ready").respond_with_data(
"Prometheus is Ready."
)
health = await prometheus_is_healthy(httpserver.url_for(""))
assert health == True
health = await prometheus_is_healthy(
httpserver.url_for("")
) # ready returns 500 now
assert health == False
@pytest.mark.asyncio
@pytest.mark.raises(exception=CheckErrorException)
async def test_check_prometheus_raises_exception_when_prometheus_not_healthy(
self, api: AsyncClient, mocker: MockerFixture, base_url: str
) -> None:
mocker.patch("isitup.prometheus.prometheus_is_healthy", return_value=False)
ignored = PrometheusUrl
ignored.prometheusUrl = "ignored"
await check_prometheus(ignored)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment