11DEFAULT_MASTER_PORTS = (16379, 16389)
12DEFAULT_SENTINEL_PORT = 26379
13DEFAULT_SLAVE_PORTS = (16380, 16390, 16381)
14DEFAULT_CLUSTER_PORTS = (17380, 17381, 17382, 17383, 17384, 17385)
15DEFAULT_CLUSTER_REPLICAS = 1
16DEFAULT_STANDALONE_PORT = 7000
18SERVICE_SCRIPT_PATH = pathlib.Path(__file__).parent.joinpath(
19 'scripts/service-redis',
21CLUSTER_SERVICE_SCRIPT_PATH = pathlib.Path(__file__).parent.joinpath(
22 'scripts/service-cluster-redis',
24STANDALONE_SERVICE_SCRIPT_PATH = pathlib.Path(__file__).parent.joinpath(
25 'scripts/service-standalone-redis',
39 master_ports: tuple[int, ...]
41 slave_ports: tuple[int, ...]
44 if len(self.master_ports) != len(DEFAULT_MASTER_PORTS):
46 f
'Need exactly {len(DEFAULT_MASTER_PORTS)} masters!',
48 if len(self.slave_ports) != len(DEFAULT_SLAVE_PORTS):
50 f
'Need exactly {len(DEFAULT_SLAVE_PORTS)} slaves!',
56 cluster_ports: tuple[int, ...]
60 if len(self.
cluster_ports) % (self.cluster_replicas + 1) != 0:
62 f
'Number of nodes does not match number of replicas ({self.cluster_replicas})',
64 min_masters = (self.cluster_replicas + 1) * 3
67 f
'Need at least {min_masters} cluster nodes!',
76def get_service_settings():
79 master_ports=utils.getenv_ints(
80 key=
'TESTSUITE_REDIS_MASTER_PORTS',
81 default=DEFAULT_MASTER_PORTS,
83 sentinel_port=utils.getenv_int(
84 key=
'TESTSUITE_REDIS_SENTINEL_PORT',
85 default=DEFAULT_SENTINEL_PORT,
87 slave_ports=utils.getenv_ints(
88 key=
'TESTSUITE_REDIS_SLAVE_PORTS',
89 default=DEFAULT_SLAVE_PORTS,
94def get_cluster_service_settings():
97 cluster_ports=utils.getenv_ints(
98 key=
'TESTSUITE_REDIS_CLUSTER_PORTS',
99 default=DEFAULT_CLUSTER_PORTS,
101 cluster_replicas=utils.getenv_int(
102 key=
'TESTSUITE_REDIS_CLUSTER_REPLICAS',
103 default=DEFAULT_CLUSTER_REPLICAS,
108def get_standalone_service_settings():
110 host=_get_hostname(),
111 port=utils.getenv_int(
112 key=
'TESTSUITE_REDIS_STANDALONE_PORT',
113 default=DEFAULT_STANDALONE_PORT,
118def create_redis_service(
121 settings: ServiceSettings |
None =
None,
125 settings = get_service_settings()
126 configs_dir = pathlib.Path(working_dir).joinpath(
'configs')
128 settings.sentinel_port,
129 *settings.master_ports,
130 *settings.slave_ports,
134 configs_dir.mkdir(parents=
True, exist_ok=
True)
136 genredis.generate_redis_configs(
137 output_path=configs_dir,
139 master0_port=settings.master_ports[0],
140 master1_port=settings.master_ports[1],
141 slave0_port=settings.slave_ports[0],
142 slave1_port=settings.slave_ports[1],
143 slave2_port=settings.slave_ports[2],
144 sentinel_port=settings.sentinel_port,
148 service_name=service_name,
149 script_path=str(SERVICE_SCRIPT_PATH),
150 working_dir=working_dir,
152 'REDIS_TMPDIR': working_dir,
153 'REDIS_CONFIGS_DIR': str(configs_dir),
154 'REDIS_SENTINEL_PORT': str(settings.sentinel_port),
155 'REDIS_SENTINEL_HOST': settings.host,
158 check_host=settings.host,
159 check_ports=check_ports,
160 prestart_hook=prestart_hook,
164def create_cluster_redis_service(
167 settings: ClusterServiceSettings |
None =
None,
171 settings = get_cluster_service_settings()
172 configs_dir = pathlib.Path(working_dir).joinpath(
'configs')
174 *settings.cluster_ports,
178 configs_dir.mkdir(parents=
True, exist_ok=
True)
180 genredis.generate_cluster_redis_configs(
181 output_path=configs_dir,
183 cluster_ports=settings.cluster_ports,
187 service_name=service_name,
188 script_path=str(CLUSTER_SERVICE_SCRIPT_PATH),
189 working_dir=working_dir,
191 'REDIS_TMPDIR': working_dir,
192 'REDIS_CONFIGS_DIR': str(configs_dir),
193 'REDIS_HOST': settings.host,
194 'REDIS_CLUSTER_PORTS':
' '.join(
195 [str(port)
for port
in settings.cluster_ports]
197 'REDIS_CLUSTER_REPLICAS': str(settings.cluster_replicas),
200 check_host=settings.host,
201 check_ports=check_ports,
202 prestart_hook=prestart_hook,
206def create_standalone_redis_service(
209 settings: StandaloneServiceSettings |
None =
None,
213 settings = get_standalone_service_settings()
214 configs_dir = pathlib.Path(working_dir).joinpath(
'configs')
217 configs_dir.mkdir(parents=
True, exist_ok=
True)
218 genredis.generate_standalone_redis_config(
219 output_path=configs_dir,
225 service_name=service_name,
226 script_path=str(STANDALONE_SERVICE_SCRIPT_PATH),
227 working_dir=working_dir,
229 'REDIS_CONFIGS_DIR': str(configs_dir),
232 check_host=settings.host,
233 check_ports=[settings.port],
234 prestart_hook=prestart_hook,
239 hostname =
'localhost'
240 for var
in (
'TESTSUITE_REDIS_HOSTNAME',
'HOSTNAME'):
241 if var
in os.environ:
242 hostname = os.environ[var]
244 return _resolve_hostname(hostname)
247def _resolve_hostname(hostname: str) -> str:
248 for family
in socket.AF_INET6, socket.AF_INET:
250 result = socket.getaddrinfo(
254 type=socket.SOCK_STREAM,
259 return result[0][4][0]
260 warnings.warn(f
'Failed to resolve hostname {hostname}')