Skip to content
Snippets Groups Projects
Verified Commit 4637b2ed authored by Mario Oenning's avatar Mario Oenning
Browse files

fix(tests): Fix test case for Prometheus metrics


Disable prometheus multiprocess mode in tests to avoid global state:
Depending on the workers which are processing a testfile,
we might run into race issues where tests might influence each other.

We also need to make sure to clear any previously collected values
in case the same worker/process is executing different tests which
evaluate prometheus values.

Signed-off-by: default avatarmoson <moson@archlinux.org>
parent 027dfbd9
No related branches found
No related tags found
No related merge requests found
from aurweb import prometheus
def clear_metrics():
prometheus.PACKAGES.clear()
prometheus.REQUESTS.clear()
prometheus.SEARCH_REQUESTS.clear()
prometheus.USERS.clear()
......@@ -43,6 +43,7 @@ from multiprocessing import Lock
import py
import pytest
from prometheus_client import values
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
from sqlalchemy.engine.base import Engine
......@@ -54,12 +55,16 @@ import aurweb.db
from aurweb import aur_logging, initdb, testing
from aurweb.testing.email import Email
from aurweb.testing.git import GitRepository
from aurweb.testing.prometheus import clear_metrics
logger = aur_logging.get_logger(__name__)
# Synchronization lock for database setup.
setup_lock = Lock()
# Disable prometheus multiprocess mode for tests
values.ValueClass = values.MutexValue
def test_engine() -> Engine:
"""
......@@ -206,3 +211,13 @@ def email_test() -> None:
that we set them up to be used via aurweb.testing.email.Email.
"""
setup_email()
@pytest.fixture
def prometheus_test():
"""
Prometheus test fixture
Removes any existing values from our metrics
"""
clear_metrics()
......@@ -8,7 +8,7 @@ from aurweb.models.user import User
@pytest.fixture(autouse=True)
def setup(db_test):
def setup(db_test, prometheus_test):
return
......
import pytest
from prometheus_client import REGISTRY, generate_latest
from aurweb import cache, db, prometheus, time
from aurweb import cache, db, time
from aurweb.models import Package, PackageBase, PackageRequest
from aurweb.models.account_type import PACKAGE_MAINTAINER_ID, USER_ID
from aurweb.models.package_request import (
......@@ -16,7 +16,7 @@ from aurweb.statistics import Statistics, update_prometheus_metrics
@pytest.fixture(autouse=True)
def setup(db_test):
def setup(db_test, prometheus_test):
return
......@@ -140,11 +140,6 @@ def test_get_count_change(stats: Statistics, test_data):
def test_update_prometheus_metrics(test_data):
# Make sure any previous data is cleared
prometheus.USERS.clear()
prometheus.PACKAGES.clear()
prometheus.REQUESTS.clear()
metrics = str(generate_latest(REGISTRY))
assert "aur_users{" not in metrics
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment