Skip to content

Commit

Permalink
settings: add cache configuration (bug 1870285)
Browse files Browse the repository at this point in the history
- add fallback DB backend
- add environment based redis configuration
- remove code that is no longer needed
  • Loading branch information
zzzeid committed Dec 15, 2023
1 parent e2ef945 commit 0272743
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 109 deletions.
64 changes: 0 additions & 64 deletions landoapi/cache.py

This file was deleted.

21 changes: 21 additions & 0 deletions src/lando/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@
}
}

REDIS_CONFIG = {
"HOST": os.getenv("REDIS_HOST"),
"PORT": os.getenv("REDIS_PORT"),
"USER": os.getenv("REDIS_USER"),
"PASSWORD": os.getenv("REDIS_PASSWORD"),
}

CACHES = {}

if all(REDIS_CONFIG.values()):
CACHES["default"] = {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": [
"redis://{USER}:{PASSWORD}@{HOST}:{PORT}".format(**REDIS_CONFIG),
],
}
else:
CACHES["default"] = {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "lando_cache",
}

# Password validation
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
Expand Down
22 changes: 0 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

import flask.testing
import pytest
import redis
import requests
import requests_mock
import sqlalchemy
from flask import current_app
from pytest_flask.plugin import JSONResponse

from landoapi.app import SUBSYSTEMS, construct_app, load_config
from landoapi.cache import cache
from landoapi.mocks.auth import TEST_JWKS, MockAuth0
from landoapi.phabricator import PhabricatorClient
from landoapi.projects import (
Expand Down Expand Up @@ -159,9 +157,6 @@ def docker_env_vars(versionfile, monkeypatch):
monkeypatch.setenv("BUGZILLA_URL", "asdfasdfasdfasdfasdfasdf")
monkeypatch.setenv("OIDC_IDENTIFIER", "lando-api")
monkeypatch.setenv("OIDC_DOMAIN", "lando-api.auth0.test")
# Explicitly shut off cache use for all tests. Tests can re-enable the cache
# with the redis_cache fixture.
monkeypatch.delenv("CACHE_REDIS_HOST", raising=False)
monkeypatch.delenv("CSP_REPORTING_URL", raising=False)
# Don't suppress email in tests, but point at localhost so that any
# real attempt would fail.
Expand Down Expand Up @@ -362,23 +357,6 @@ def get_client(api_key=None):
return get_client


@pytest.fixture
def redis_cache(app):
cache.init_app(
app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "redis.cache"}
)
try:
cache.clear()
except redis.exceptions.ConnectionError:
if EXTERNAL_SERVICES_SHOULD_BE_PRESENT:
raise
else:
pytest.skip("Could not connect to Redis")
yield cache
cache.clear()
cache.init_app(app, config={"CACHE_TYPE": "null", "CACHE_NO_NULL_WARNING": True})


@pytest.fixture
def celery_app(app):
"""Configure our app's Celery instance for use with the celery_worker fixture."""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dockerflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def test_dockerflow_version_matches_disk_contents(client, versionfile):


def test_heartbeat_returns_200(
client, db, phabdouble, request_mocker, redis_cache, jwks, treestatusdouble
client, db, phabdouble, request_mocker, jwks, treestatusdouble
):
assert client.get("/__heartbeat__").status_code == 200


def test_heartbeat_returns_http_502_if_phabricator_ping_returns_error(
client, request_mocker, redis_cache, jwks, treestatusdouble
client, request_mocker, jwks, treestatusdouble
):
error_json = {
"result": None,
Expand Down
21 changes: 0 additions & 21 deletions tests/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

from unittest.mock import Mock

import redis
from sqlalchemy.exc import SQLAlchemyError

from landoapi.auth import auth0_subsystem
from landoapi.cache import cache_subsystem
from landoapi.phabricator import PhabricatorAPIException, phabricator_subsystem
from landoapi.storage import db_subsystem

Expand Down Expand Up @@ -37,25 +35,6 @@ def raises(*args, **kwargs):
assert phabricator_subsystem.healthy() is not True


def test_cache_healthy(redis_cache):
assert cache_subsystem.healthy() is True


def test_cache_unhealthy_configuration():
assert cache_subsystem.healthy() is not True


def test_cache_unhealthy_service(redis_cache, monkeypatch):
mock_cache = Mock(redis_cache)
mock_cache.cache._read_client.ping.side_effect = redis.TimeoutError
monkeypatch.setattr("landoapi.cache.cache", mock_cache)
monkeypatch.setattr("landoapi.cache.RedisCache", type(mock_cache.cache))

health = cache_subsystem.healthy()
assert health is not True
assert health.startswith("RedisError:")


def test_auth0_healthy(app, jwks):
assert auth0_subsystem.healthy() is True

Expand Down

0 comments on commit 0272743

Please sign in to comment.