Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PROMETHEUS_ENABLED = True

# Settings required for tests
SECRET_KEY = "test"
DATABASES = {
"default": dj_database_url.parse(
env(
Expand All @@ -30,6 +31,7 @@
"task_processor",
]
MIDDLEWARE = [
"django.middleware.common.CommonMiddleware",
"common.gunicorn.middleware.RouteLoggerMiddleware",
]
LOG_FORMAT = "json"
Expand Down
8 changes: 4 additions & 4 deletions src/common/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from common.core import views

urlpatterns = [
path("version", views.version_info),
path("health/liveness", views.liveness),
path("health/readiness", include("health_check.urls", namespace="health")),
path("version/", views.version_info),
path("health/liveness/", views.liveness),
path("health/readiness/", include("health_check.urls", namespace="health")),
re_path(r"^health", include("health_check.urls", namespace="health-deprecated")),
# Aptible health checks must be on /healthcheck and cannot redirect
# see https://www.aptible.com/docs/core-concepts/apps/connecting-to-apps/app-endpoints/https-endpoints/health-checks
re_path(r"^healthcheck", include("health_check.urls", namespace="health-aptible")),
]

if settings.PROMETHEUS_ENABLED:
urlpatterns += [path("metrics", views.metrics)]
urlpatterns += [path("metrics/", views.metrics)]
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
import os
from importlib import reload

import prometheus_client
import prometheus_client.values
import pytest

pytest_plugins = "flagsmith-test-tools"


@pytest.fixture(scope="session")
def prometheus_multiproc_dir(tmp_path_factory: pytest.TempPathFactory) -> str:
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir_path = str(
tmp_path_factory.mktemp("prometheus_multiproc")
)
reload(prometheus_client.values)
return prometheus_multiproc_dir_path


@pytest.fixture(scope="session")
def test_metric(prometheus_multiproc_dir: str) -> prometheus_client.Counter:
return prometheus_client.Counter(
"pytest_tests_run_total",
"Total number of tests run by pytest",
["test_name"],
)
Empty file added tests/integration/__init__.py
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions tests/integration/core/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import prometheus_client
from rest_framework.test import APIClient


def test_liveness_probe__return_expected(
client: APIClient,
) -> None:
response = client.get("/health/liveness/")
assert response.status_code == 200
assert response.json() == {"status": "ok"}


def test_metrics__return_expected(
test_metric: prometheus_client.Counter,
client: APIClient,
) -> None:
# Arrange
test_metric.labels(test_name="test_metrics__return_expected").inc()

# Act
response = client.get("/metrics", follow=True)

# Assert
assert response.status_code == 200
assert response.content == (
"\n".join(
[
"# HELP pytest_tests_run_total Total number of tests run by pytest",
"# TYPE pytest_tests_run_total counter",
'pytest_tests_run_total{test_name="test_metrics__return_expected"} 1.0',
"",
]
).encode()
)
11 changes: 4 additions & 7 deletions tests/unit/common/test_tools/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
from common.test_tools import AssertMetricFixture
from common.test_tools.plugin import assert_metric_impl

test_metric = prometheus_client.Counter(
"pytest_tests_run_total",
"Total number of tests run by pytest",
["test_name"],
)


def test_assert_metrics__asserts_expected(
assert_metric: AssertMetricFixture,
test_metric: prometheus_client.Counter,
) -> None:
# Given
test_metric.labels(test_name="test_assert_metrics__asserts_expected").inc()
Expand All @@ -25,7 +20,9 @@ def test_assert_metrics__asserts_expected(
)


def test_assert_metrics__registry_reset_expected() -> None:
def test_assert_metrics__registry_reset_expected(
test_metric: prometheus_client.Counter,
) -> None:
# Given
test_metric.labels(test_name="test_assert_metrics__registry_reset_expected").inc()

Expand Down