aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: ec5073cf9af044e51ea2256620a90e3ab02f0f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations

import subprocess
from collections.abc import Generator
from random import randint
from typing import TYPE_CHECKING

import pytest
import redis
from generalresearch.redis_helper import RedisConfig

if TYPE_CHECKING:
    from jb.settings import Settings


pytest_plugins = [
    "tests.fixtures.http",
]


# --- Settings ---


@pytest.fixture(scope="session")
def settings() -> Settings:
    from jb.settings import Settings as JBSettings

    return JBSettings()


# --- Redis ---


@pytest.fixture(scope="session")
def redis_config_db() -> str:
    # need to update 'databases' in /etc/redis/redis.conf
    #   or this won't work and you'll have no indication why ...
    return str(randint(99, 1_023))


@pytest.fixture(scope="session")
def redis_config(settings: Settings, redis_config_db: str) -> Generator[RedisConfig]:
    assert "unittest" in str(settings.testing_redis) or "127.0.0.1" in str(
        settings.testing_redis
    )

    uri = f"redis://{settings.testing_redis}/{redis_config_db}"

    res = subprocess.run(
        ["redis-cli", "-u", uri, "SET", "jenkins_lock", "1", "NX", "EX", "3600"],
        check=True,
        text=True,
        capture_output=True,
    )

    if res.stdout.strip() != "OK":
        raise ValueError("Redis already locked... aborting.")

    yield RedisConfig(
        dsn=uri,
        decode_responses=True,
        socket_timeout=settings.redis_timeout,
        socket_connect_timeout=settings.redis_timeout,
    )

    r = redis.from_url(uri)
    r.flushdb()