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
13 changes: 9 additions & 4 deletions src/common/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from functools import lru_cache
from itertools import cycle
from typing import (
TYPE_CHECKING,
Iterator,
Literal,
NotRequired,
Expand All @@ -17,19 +18,23 @@

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser
from django.db import connections
from django.db.models import Manager, Model
from django.db.utils import OperationalError

from common.core import ReplicaReadStrategy

if TYPE_CHECKING:
from django.contrib.auth.models import AbstractBaseUser
from django.db.models.base import Model
from django.db.models.manager import Manager


logger = logging.getLogger(__name__)

UNKNOWN = "unknown"
VERSIONS_INFO_FILE_LOCATION = ".versions.json"

ManagerType = TypeVar("ManagerType", bound=Manager[Model])
ManagerType = TypeVar("ManagerType", bound="Manager[Model]")

ReplicaNamePrefix = Literal["replica_", "cross_region_replica_"]
_replica_sequential_names_by_prefix: dict[ReplicaNamePrefix, Iterator[str]] = {}
Expand Down Expand Up @@ -102,7 +107,7 @@ def get_version_info() -> VersionInfo:
version_json["image_tag"] = manifest_versions["."]

if not _is_saas:
user_objects: Manager[AbstractBaseUser] = getattr(get_user_model(), "objects")
user_objects: "Manager[AbstractBaseUser]" = getattr(get_user_model(), "objects")

version_json["self_hosted_data"] = {
"has_users": user_objects.exists(),
Expand Down
25 changes: 21 additions & 4 deletions tests/integration/core/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
from pathlib import Path

import django
Expand Down Expand Up @@ -110,9 +111,11 @@ def test_main__healthcheck_http__server_invalid_response__runs_expected(
main(argv)


def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected() -> None:
def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Given
os.environ.pop("PROMETHEUS_MULTIPROC_DIR_KEEP", None)
monkeypatch.delenv("PROMETHEUS_MULTIPROC_DIR_KEEP", raising=False)

# When
main(["flagsmith"])
Expand All @@ -122,14 +125,28 @@ def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected() -> No


def test_main__prometheus_multiproc_remove_dir_on_exit_true__expected(
monkeypatch: pytest.MonkeyPatch,
fs: FakeFilesystem,
) -> None:
# Given
os.environ.pop("PROMETHEUS_MULTIPROC_DIR", None)
os.environ["PROMETHEUS_MULTIPROC_DIR_KEEP"] = "true"
monkeypatch.delenv("PROMETHEUS_MULTIPROC_DIR")
monkeypatch.setenv("PROMETHEUS_MULTIPROC_DIR_KEEP", "true")

# When
main(["flagsmith"])

# Then
assert Path(os.environ["PROMETHEUS_MULTIPROC_DIR"]).exists()


def test_main__no_django_configured__expected_0(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Given
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")

# When
output = subprocess.run("flagsmith")

# Then
assert output.returncode == 0