-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconftest.py
320 lines (246 loc) · 8.99 KB
/
conftest.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import pytest
import os
import logging
from lume_services import config
from lume_services.docker.files import DOCKER_COMPOSE
from lume_services.services.models.db import ModelDBConfig
from lume_services.services.results.mongodb import MongodbResultsDBConfig
from lume_services.services.scheduling.backends.server import (
PrefectAgentConfig,
PrefectConfig,
PrefectServerConfig,
)
from lume_services.services.files.filesystems import (
LocalFilesystem,
MountedFilesystem,
)
def pytest_addoption(parser):
parser.addini("backend", default="docker", help="Backend interface to use")
parser.addini(
"prefect_backend", default="server", help="Prefect backend: server or cloud"
)
parser.addini("mysql_host", default="127.0.0.1", help="MySQL host")
parser.addini("mysql_port", default=3306, help="MySQL port")
parser.addini("mysql_user", default="root", help="MySQL user")
parser.addini("mysql_password", default="root", help="MySQL password")
parser.addini("mysql_database", default="model_db", help="Model database name")
parser.addini("mysql_poolsize", default=1, help="MySQL client poolsize")
parser.addini("mongodb_host", default="127.0.0.1", help="MySQL host")
parser.addini("mongodb_port", default=27017, help="MySQL port")
parser.addini("mongodb_user", default="root", help="MySQL user")
parser.addini("mongodb_password", default="password", help="MySQL password")
parser.addini(name="mongodb_database", help="Mysql database name", default="test")
# prefect
parser.addini(name="server_tag", help="Prefect server image tag", default="latest")
parser.addini(
name="server_host_port", help="Prefect server apollo api port", default=4200
)
parser.addini(
name="server_host",
help="Prefect server apollo host IP",
default="http://localhost",
)
parser.addini(
name="agent_host", help="Prefect agent host", default="http://localhost"
)
parser.addini(
name="agent_host_port", help="Prefect agent port for comms", default=5000
)
@pytest.fixture(scope="session", autouse=True)
def rootdir(request):
rootdir = request.config.rootpath
os.environ["LUME_SERVICES_ROOTDIR"] = str(rootdir)
return rootdir
@pytest.fixture(scope="session", autouse=True)
def lume_backend(request):
lume_backend = request.config.getini("backend")
os.environ["LUME_BACKEND"] = lume_backend
return lume_backend
@pytest.fixture(scope="session", autouse=True)
def mysql_host(request):
host = request.config.getini("mysql_host")
os.environ["LUME_MODEL_DB__HOST"] = host
return host
@pytest.fixture(scope="session", autouse=True)
def mysql_user(request):
user = request.config.getini("mysql_user")
os.environ["LUME_MODEL_DB__USER"] = user
return user
@pytest.fixture(scope="session", autouse=True)
def mysql_password(request):
password = request.config.getini("mysql_password")
os.environ["LUME_MODEL_DB__PASSWORD"] = password
return password
@pytest.fixture(scope="session", autouse=True)
def mysql_port(request):
port = request.config.getini("mysql_port")
os.environ["LUME_MODEL_DB__PORT"] = port
return int(port)
@pytest.fixture(scope="session", autouse=True)
def mysql_database(request):
database = request.config.getini("mysql_database")
os.environ["LUME_MODEL_DB__DATABASE"] = database
return database
@pytest.fixture(scope="session", autouse=True)
def mysql_pool_size(request):
pool_size = request.config.getini("mysql_poolsize")
os.environ["LUME_MODEL_DB__CONNECTION__POOL_SIZE"] = pool_size
return int(pool_size)
@pytest.fixture(scope="session", autouse=True)
def base_mysql_uri(mysql_user, mysql_password, mysql_host, mysql_port):
return f"mysql+pymysql://{mysql_user}:{mysql_password}@{mysql_host}:{mysql_port}"
@pytest.fixture(scope="session")
def server_tag(request):
tag = request.config.getini("server_tag")
os.environ["LUME_PREFECT__SERVER__TAG"] = tag
return tag
@pytest.fixture(scope="session")
def server_host_port(request):
port = request.config.getini("server_host_port")
os.environ["LUME_PREFECT__SERVER__HOST_PORT"] = port
return port
@pytest.fixture(scope="session")
def server_host(request):
port = request.config.getini("server_host")
os.environ["LUME_PREFECT__SERVER__HOST"] = port
return port
@pytest.fixture(scope="session")
def agent_host(request):
host = request.config.getini("agent_host")
os.environ["LUME_PREFECT__AGENT__HOST"] = host
return host
@pytest.fixture(scope="session")
def agent_host_port(request):
port = request.config.getini("agent_host_port")
os.environ["LUME_PREFECT__AGENT__HOST_PORT"] = port
return port
@pytest.fixture(scope="session")
def prefect_backend(request):
os.environ["LUME_PREFECT__BACKEND"] = "server"
return "server"
## mongodb
@pytest.fixture(scope="session", autouse=True)
def mongodb_host(request):
mongodb_host = request.config.getini("mongodb_host")
os.environ["LUME_RESULTS_DB__HOST"] = mongodb_host
return mongodb_host
@pytest.fixture(scope="session", autouse=True)
def mongodb_port(request):
mongodb_port = request.config.getini("mongodb_port")
os.environ["LUME_RESULTS_DB__PORT"] = mongodb_port
return int(mongodb_port)
@pytest.fixture(scope="session", autouse=True)
def mongodb_user(request):
mongodb_user = request.config.getini("mongodb_user")
os.environ["LUME_RESULTS_DB__USERNAME"] = mongodb_user
return mongodb_user
@pytest.fixture(scope="session", autouse=True)
def mongodb_password(request):
mongodb_password = request.config.getini("mongodb_password")
os.environ["LUME_RESULTS_DB__PASSWORD"] = mongodb_password
return mongodb_password
@pytest.fixture(scope="session", autouse=True)
def mongodb_database(request):
database = request.config.getini("mongodb_database")
os.environ["LUME_RESULTS_DB__DATABASE"] = database
return database
# Scheduling
# Filesystem
@pytest.fixture(scope="session")
def mount_path(tmp_path_factory):
return str(tmp_path_factory.mktemp("mounted_dir"))
@pytest.fixture(scope="session", autouse=True)
def local_filesystem():
return LocalFilesystem()
@pytest.fixture(scope="session", autouse=True)
def mounted_filesystem(mount_path):
os.environ["LUME_MOUNTED_FILESYSTEM__IDENTIFIER"] = "mounted"
os.environ["LUME_MOUNTED_FILESYSTEM__MOUNT_PATH"] = mount_path
os.environ["LUME_MOUNTED_FILESYSTEM__MOUNT_ALIAS"] = "/User/my_user/data"
os.environ["LUME_MOUNTED_FILESYSTEM__MOUNT_TYPE"] = "DirectoryOrCreate"
return MountedFilesystem(
mount_path=mount_path,
mount_alias="/User/my_user/data",
identifier="mounted",
mount_type="DirectoryOrCreate",
)
# Docker
@pytest.fixture(scope="session", autouse=True)
def prefect_docker_tag():
os.environ["LUME_PREFECT__IMAGE"] = "lume_services:pytest"
return "lume_services:pytest"
@pytest.fixture(scope="session", autouse=True)
def dockerfile(rootdir):
return f"{rootdir}/Dockerfile"
@pytest.fixture(scope="session", autouse=True)
def prefect_debug():
os.environ["LUME_PREFECT__DEBUG"] = "true"
return "true"
## ENVIRONMENT VARIABLES:
# @pytest.fixture(autouse=True)
# def mock_settings_env_vars():
# with mock.patch.dict(os.environ, {"LUME_BACKEND": "DOCKER"}):
# yield
# Full configuration
@pytest.fixture(scope="session", autouse=True)
def lume_services_settings(
mysql_host,
mysql_port,
mysql_user,
mysql_password,
mysql_database,
mongodb_host,
mongodb_port,
mongodb_user,
mongodb_password,
mongodb_database,
server_host_port,
server_host,
server_tag,
agent_host,
agent_host_port,
prefect_backend,
lume_backend,
mounted_filesystem,
prefect_debug,
prefect_docker_tag,
):
model_db_config = ModelDBConfig(
host=mysql_host,
port=mysql_port,
user=mysql_user,
password=mysql_password,
database=mysql_database,
)
results_db_config = MongodbResultsDBConfig(
port=mongodb_port,
host=mongodb_host,
username=mongodb_user,
database=mongodb_database,
password=mongodb_password,
)
prefect_config = PrefectConfig(
server=PrefectServerConfig(
host=server_host, host_port=server_host_port, tag=server_tag
),
agent=PrefectAgentConfig(host=agent_host, host_port=agent_host_port),
backend=prefect_backend,
default_image=prefect_docker_tag,
debug=prefect_debug,
)
settings = config.LUMEServicesSettings(
model_db=model_db_config,
results_db=results_db_config,
prefect=prefect_config,
backend=lume_backend,
mounted_filesystem=mounted_filesystem,
)
return settings
# Now setup all fixtures
pytest_plugins = [
"lume_services.tests.fixtures.docker",
"lume_services.tests.fixtures.services.files",
"lume_services.tests.fixtures.services.models",
"lume_services.tests.fixtures.services.results",
"lume_services.tests.fixtures.services.scheduling",
]