Debian Codegraph @

tests/conftest.py

raw
1 import os
2 import subprocess
3 import warnings
4 from pathlib import Path
5
6 import pytest
7 from xprocess import ProcessStarter
8
9
10 def under_uwsgi():
11 try:
12 import uwsgi # noqa: F401
13 except ImportError:
14 return False
15 else:
16 return True
17
18
19 @pytest.hookimpl()
20 def pytest_sessionfinish(session, exitstatus):
21 if under_uwsgi():
22 try:
23 script_path = Path(os.environ["TMPDIR"], "return_pytest_exit_code.py")
24 except KeyError:
25 warnings.warn(
26 "Pytest could not find tox 'TMPDIR' in the environment,"
27 " make sure the variable is set in the project tox"
28 " config if you are running under tox.",
29 stacklevel=2,
30 )
31 else:
32 with open(script_path, mode="w") as f:
33 f.write(f"import sys; sys.exit({exitstatus})")
34
35
36 @pytest.fixture(scope="class")
37 def redis_server(xprocess):
38 package_name = "redis"
39 pytest.importorskip(
40 modname=package_name, reason=f"could not find python package {package_name}"
41 )
42
43 if os.environ.get("CI", "false") == "true":
44 yield
45 return
46
47 class Starter(ProcessStarter):
48 pattern = "[Rr]eady to accept connections"
49 args = ["redis-server", "--port 6360"]
50
51 def startup_check(self):
52 out = subprocess.run(
53 ["redis-cli", "-p", "6360", "ping"], stdout=subprocess.PIPE
54 )
55 return out.stdout == b"PONG\n"
56
57 xprocess.ensure(package_name, Starter)
58 yield
59 xprocess.getinfo(package_name).terminate()
60
61
62 @pytest.fixture(scope="class")
63 def memcached_server(xprocess):
64 package_name = "pylibmc"
65 pytest.importorskip(
66 modname=package_name, reason=f"could not find python package {package_name}"
67 )
68
69 if os.environ.get("CI", "false") == "true":
70 yield
71 return
72
73 class Starter(ProcessStarter):
74 pattern = "server listening"
75 args = ["memcached", "-vv", "-p", "11212"]
76
77 def startup_check(self):
78 out = subprocess.run(["memcached", "-p", "11212"], stderr=subprocess.PIPE)
79 return b"Address already" in out.stderr
80
81 xprocess.ensure(package_name, Starter)
82 yield
83 xprocess.getinfo(package_name).terminate()
84
85
86 @pytest.fixture(scope="class")
87 def valkey_server(xprocess):
88 package_name = "valkey"
89 pytest.importorskip(
90 modname=package_name, reason=f"could not find python package {package_name}"
91 )
92
93 if os.environ.get("CI", "false") == "true":
94 yield
95 return
96
97 class Starter(ProcessStarter):
98 pattern = "[Rr]eady to accept connections"
99 args = ["valkey-server", "--port 6370"]
100
101 def startup_check(self):
102 out = subprocess.run(
103 ["valkey-cli", "-p", "6370", "ping"], stdout=subprocess.PIPE
104 )
105 return out.stdout == b"PONG\n"
106
107 xprocess.ensure(package_name, Starter)
108 yield
109 xprocess.getinfo(package_name).terminate()
110
111
112 @pytest.fixture(
113 params=[None, "test-key-prefix:"], ids=["no_key_prefix", "with_key_prefix"]
114 )
115 def key_prefix(request):
116 """Runs dependent tests once without a key_prefix and once with one set,
117 so backends that support key_prefix are exercised in both configurations.
118 """
119 return request.param
120
121
122 class TestData:
123 """This class centralizes all data samples used in tests"""
124
125 sample_numbers = [0, 10, 1024000, 9, 5000000000000, 99, 738, 2000000]
126 sample_pairs = {
127 "128": False,
128 "beef": True,
129 "crevettes": {},
130 "1024": "spam",
131 "bacon": "eggs",
132 "sausage": 2048,
133 "3072": [],
134 "brandy": [{}, "fried eggs"],
135 "lobster": ["baked beans", [512]],
136 "4096": {"sauce": [], 256: "truffle"},
137 }