-
Notifications
You must be signed in to change notification settings - Fork 239
/
test_httpdb.py
602 lines (496 loc) · 18.2 KB
/
test_httpdb.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import codecs
import deepdiff
from collections import namedtuple
from os import environ
from pathlib import Path
from shutil import rmtree
from socket import socket
from subprocess import Popen, run, PIPE, DEVNULL
from sys import executable
from tempfile import mkdtemp
from uuid import uuid4
import pytest
from mlrun.api import schemas
import mlrun.errors
from mlrun import RunObject
from mlrun.artifacts import Artifact
from mlrun.db import RunDBError
from mlrun.db.httpdb import HTTPRunDB
from tests.conftest import wait_for_server, tests_root_directory
import mlrun.projects.project
project_dir_path = Path(__file__).absolute().parent.parent.parent
Server = namedtuple("Server", "url conn workdir")
docker_tag = "mlrun/test-api"
def free_port():
with socket() as sock:
sock.bind(("localhost", 0))
return sock.getsockname()[1]
def check_server_up(url):
health_url = f"{url}/api/healthz"
timeout = 30
if not wait_for_server(health_url, timeout):
raise RuntimeError(f"server did not start after {timeout} sec")
def create_workdir(root_dir="/tmp"):
return mkdtemp(prefix="mlrun-test-", dir=root_dir)
def start_server(workdir, env_config: dict):
port = free_port()
env = environ.copy()
env["MLRUN_httpdb__port"] = str(port)
env[
"MLRUN_httpdb__dsn"
] = f"sqlite:///{workdir}/mlrun.sqlite3?check_same_thread=false"
env["MLRUN_httpdb__logs_path"] = workdir
env.update(env_config or {})
cmd = [
executable,
"-m",
"mlrun.api.main",
]
proc = Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, cwd=project_dir_path)
url = f"http://localhost:{port}"
check_server_up(url)
return proc, url
def docker_fixture():
container_id, workdir = None, None
def create(env_config=None):
nonlocal container_id, workdir
env_config = {} if env_config is None else env_config
cmd = [
"docker",
"build",
"-f",
"dockerfiles/mlrun-api/Dockerfile",
"--tag",
docker_tag,
".",
]
run(cmd, check=True, stdout=PIPE, cwd=project_dir_path)
workdir = create_workdir(root_dir="/tmp")
cmd = [
"docker",
"run",
"--detach",
"--publish",
"8080",
# For debugging
"--volume",
f"{workdir}:/tmp",
]
env_config.setdefault("MLRUN_httpdb__logs_path", "/tmp")
for key, value in env_config.items():
cmd.extend(["--env", f"{key}={value}"])
cmd.append(docker_tag)
out = run(cmd, stdout=PIPE, check=True)
container_id = out.stdout.decode("utf-8").strip()
# retrieve container bind port + host
out = run(["docker", "port", container_id, "8080"], stdout=PIPE, check=True)
host = out.stdout.decode("utf-8").strip()
url = f"http://{host}"
print(f"api url: {url}")
check_server_up(url)
conn = HTTPRunDB(url)
conn.connect()
return Server(url, conn, workdir)
def cleanup():
if container_id:
run(["docker", "rm", "--force", container_id], stdout=DEVNULL)
if workdir:
rmtree(workdir)
return create, cleanup
def server_fixture():
process = None
workdir = None
def create(env=None):
nonlocal process, workdir
workdir = create_workdir()
process, url = start_server(workdir, env)
conn = HTTPRunDB(url)
conn.connect()
return Server(url, conn, workdir)
def cleanup():
if process:
process.terminate()
stdout = process.stdout.read()
human_readable_stdout = codecs.escape_decode(stdout)[0].decode("utf-8")
stderr = process.stderr.read()
human_readable_stderr = codecs.escape_decode(stderr)[0].decode("utf-8")
print(f"Stdout from server {human_readable_stdout}")
print(f"Stderr from server {human_readable_stderr}")
if workdir:
rmtree(workdir)
return create, cleanup
servers = [
"server",
"docker",
]
@pytest.fixture(scope="function", params=servers)
def create_server(request):
if request.param == "server":
create, cleanup = server_fixture()
else:
create, cleanup = docker_fixture()
try:
yield create
finally:
cleanup()
def test_log(create_server):
server: Server = create_server()
db = server.conn
prj, uid, body = "p19", "3920", b"log data"
db.store_run({"asd": "asd"}, uid, prj)
db.store_log(uid, prj, body)
state, data = db.get_log(uid, prj)
assert data == body, "bad log data"
def test_run(create_server):
server: Server = create_server()
db = server.conn
prj, uid = "p18", "3i920"
run_as_dict = RunObject().to_dict()
run_as_dict["metadata"].update({"algorithm": "svm", "C": 3})
db.store_run(run_as_dict, uid, prj)
data = db.read_run(uid, prj)
assert data == run_as_dict, "read_run"
new_c = 4
updates = {"metadata.C": new_c}
db.update_run(updates, uid, prj)
data = db.read_run(uid, prj)
assert data["metadata"]["C"] == new_c, "update_run"
db.del_run(uid, prj)
def test_runs(create_server):
server: Server = create_server()
db = server.conn
runs = db.list_runs()
assert not runs, "found runs in new db"
count = 7
prj = "p180"
run_as_dict = RunObject().to_dict()
for i in range(count):
uid = f"uid_{i}"
db.store_run(run_as_dict, uid, prj)
runs = db.list_runs(project=prj)
assert len(runs) == count, "bad number of runs"
db.del_runs(project=prj, state="created")
runs = db.list_runs(project=prj)
assert not runs, "found runs in after delete"
def test_artifact(create_server):
server: Server = create_server()
db = server.conn
prj, uid, key, body = "p7", "u199", "k800", "cucumber"
artifact = Artifact(key, body)
db.store_artifact(key, artifact, uid, project=prj)
# TODO: Need a run file
# db.del_artifact(key, project=prj)
def test_artifacts(create_server):
server: Server = create_server()
db = server.conn
prj, uid, key, body = "p9", "u19", "k802", "tomato"
artifact = Artifact(key, body)
db.store_artifact(key, artifact, uid, project=prj)
artifacts = db.list_artifacts(project=prj, tag="*")
assert len(artifacts) == 1, "bad number of artifacs"
db.del_artifacts(project=prj, tag="*")
artifacts = db.list_artifacts(project=prj, tag="*")
assert len(artifacts) == 0, "bad number of artifacs after del"
def test_basic_auth(create_server):
user, passwd = "bugs", "bunny"
env = {
"MLRUN_httpdb__user": user,
"MLRUN_httpdb__password": passwd,
}
server: Server = create_server(env)
db: HTTPRunDB = server.conn
with pytest.raises(RunDBError):
db.list_runs()
db.user = user
db.password = passwd
db.list_runs()
def test_bearer_auth(create_server):
token = "banana"
env = {"MLRUN_httpdb__token": token}
server: Server = create_server(env)
db: HTTPRunDB = server.conn
with pytest.raises(RunDBError):
db.list_runs()
db.token = token
db.list_runs()
def test_set_get_function(create_server):
server: Server = create_server()
db: HTTPRunDB = server.conn
func, name, proj = {"x": 1, "y": 2}, "f1", "p2"
tag = uuid4().hex
db.store_function(func, name, proj, tag=tag)
db_func = db.get_function(name, proj, tag=tag)
# db methods enriches metadata and status
del db_func["metadata"]
del db_func["status"]
assert db_func == func, "wrong func"
def test_list_functions(create_server):
server: Server = create_server()
db: HTTPRunDB = server.conn
proj = "p4"
count = 5
for i in range(count):
name = f"func{i}"
func = {"fid": i}
tag = uuid4().hex
db.store_function(func, name, proj, tag=tag)
db.store_function({}, "f2", "p7", tag=uuid4().hex)
functions = db.list_functions(project=proj)
for function in functions:
assert function["metadata"]["tag"] is not None
assert len(functions) == count, "bad list"
def test_version_compatibility_validation():
cases = [
{
"server_version": "unstable",
"client_version": "unstable",
"compatible": True,
},
{"server_version": "0.5.3", "client_version": "unstable", "compatible": True},
{"server_version": "unstable", "client_version": "0.6.1", "compatible": True},
{"server_version": "0.5.3", "client_version": "0.5.1", "compatible": True},
{"server_version": "0.6.0-rc1", "client_version": "0.6.1", "compatible": True},
{
"server_version": "0.6.0-rc1",
"client_version": "0.5.4",
"compatible": False,
},
{"server_version": "0.6.3", "client_version": "0.4.8", "compatible": False},
{"server_version": "1.0.0", "client_version": "0.5.0", "compatible": False},
]
for case in cases:
if not case["compatible"]:
with pytest.raises(mlrun.errors.MLRunIncompatibleVersionError):
HTTPRunDB._validate_version_compatibility(
case["server_version"], case["client_version"]
)
else:
HTTPRunDB._validate_version_compatibility(
case["server_version"], case["client_version"]
)
def _create_feature_set(name):
return {
"kind": "FeatureSet",
"metadata": {
"name": name,
"labels": {"owner": "saarc", "group": "dev"},
"tag": "latest",
},
"spec": {
"entities": [
{
"name": "ticker",
"value_type": "str",
"labels": {"type": "prod"},
"extra_field": 100,
}
],
"features": [
{"name": "time", "value_type": "datetime", "extra_field": "value1"},
{"name": "bid", "value_type": "float"},
{"name": "ask", "value_type": "time"},
],
},
"status": {
"state": "created",
"stats": {
"time": {
"count": "8",
"unique": "7",
"top": "2016-05-25 13:30:00.222222",
}
},
},
"some_other_field": "blabla",
}
def test_feature_sets(create_server):
server: Server = create_server()
db: HTTPRunDB = server.conn
project = "newproj"
count = 5
for i in range(count):
name = f"fs_{i}"
feature_set = _create_feature_set(name)
db.create_feature_set(feature_set, project=project, versioned=True)
# Test store_feature_set, which allows updates as well as inserts
db.store_feature_set(feature_set, name=name, project=project, versioned=True)
feature_set_update = {
"spec": {
"features": [{"name": "looks", "value_type": "str", "description": "good"}],
}
}
# additive mode means add the feature to the features-list
db.patch_feature_set(
name, feature_set_update, project, tag="latest", patch_mode="additive"
)
feature_sets = db.list_feature_sets(project=project)
assert len(feature_sets) == count, "bad list results - wrong number of members"
feature_set = db.get_feature_set(name, project)
assert len(feature_set["spec"]["features"]) == 4
# Create a feature-set that has no labels
name = "feature_set_no_labels"
feature_set_without_labels = _create_feature_set(name)
feature_set_without_labels["metadata"].pop("labels")
# Use project name in the feature-set (don't provide it to API)
feature_set_without_labels["metadata"]["project"] = project
db.store_feature_set(feature_set_without_labels)
feature_set_update = {
"spec": {"entities": [{"name": "nothing", "value_type": "bool"}]},
"metadata": {"labels": {"label1": "value1", "label2": "value2"}},
}
db.patch_feature_set(name, feature_set_update, project)
feature_set = db.get_feature_set(name, project)
assert len(feature_set["metadata"]["labels"]) == 2, "Labels didn't get updated"
features = db.list_features(project, "time")
# The feature-set with different labels also counts here
assert len(features) == count + 1
# Only count, since we modified the entity of the last feature-set - other name, no labels
entities = db.list_entities(project, "ticker")
assert len(entities) == count
entities = db.list_entities(project, labels=["type"])
assert len(entities) == count
entities = db.list_entities(project, labels=["type=prod"])
assert len(entities) == count
def _create_feature_vector(name):
return {
"kind": "FeatureVector",
"metadata": {
"name": name,
"labels": {"owner": "nobody", "group": "dev"},
"tag": "latest",
},
"spec": {
"features": ["feature_set:*", "feature_set:something", "just_a_feature"],
"description": "just a bunch of features",
},
"status": {"state": "created"},
}
def test_feature_vectors(create_server):
server: Server = create_server()
db: HTTPRunDB = server.conn
project = "newproj"
count = 5
for i in range(count):
name = f"fs_{i}"
feature_vector = _create_feature_vector(name)
db.create_feature_vector(feature_vector, project=project, versioned=True)
# Test store_feature_set, which allows updates as well as inserts
db.store_feature_vector(feature_vector, project=project)
feature_vector_update = {"spec": {"features": ["bla", "blu"]}}
# additive mode means add the feature to the features-list
db.patch_feature_vector(
name,
feature_vector_update,
project,
tag="latest",
patch_mode=schemas.PatchMode.additive,
)
feature_vectors = db.list_feature_vectors(project=project)
assert len(feature_vectors) == count, "bad list results - wrong number of members"
feature_vector = db.get_feature_vector(name, project)
assert (
len(feature_vector["spec"]["features"]) == 5
), "Features didn't get updated properly"
# Create a feature-vector that has no labels
name = "feature_vector_no_labels"
feature_vector_without_labels = _create_feature_vector(name)
feature_vector_without_labels["metadata"].pop("labels")
# Use project name in the feature-set (don't provide it to API)
feature_vector_without_labels["metadata"]["project"] = project
db.store_feature_vector(feature_vector_without_labels)
# Perform a replace (vs. additive as done earlier) - now should only have 2 features
db.patch_feature_vector(
name, feature_vector_update, project, patch_mode=schemas.PatchMode.replace
)
feature_vector = db.get_feature_vector(name, project)
assert (
len(feature_vector["spec"]["features"]) == 2
), "Features didn't get updated properly"
def test_project_file_db_roundtrip(create_server):
server: Server = create_server()
db: HTTPRunDB = server.conn
project_name = "project-name"
description = "project description"
goals = "project goals"
desired_state = mlrun.api.schemas.ProjectState.archived
params = {"param_key": "param value"}
artifact_path = "/tmp"
conda = "conda"
source = "source"
subpath = "subpath"
origin_url = "origin_url"
labels = {"key": "value"}
annotations = {"annotation-key": "annotation-value"}
project_metadata = mlrun.projects.project.ProjectMetadata(
project_name, labels=labels, annotations=annotations,
)
project_spec = mlrun.projects.project.ProjectSpec(
description,
params,
artifact_path=artifact_path,
conda=conda,
source=source,
subpath=subpath,
origin_url=origin_url,
goals=goals,
desired_state=desired_state,
)
project = mlrun.projects.project.MlrunProject(
metadata=project_metadata, spec=project_spec
)
function_name = "trainer-function"
function = mlrun.new_function(function_name, project_name)
project.set_function(function, function_name)
project.set_function("hub://describe", "describe")
workflow_name = "workflow-name"
workflow_file_path = Path(tests_root_directory) / "rundb" / "workflow.py"
project.set_workflow(workflow_name, str(workflow_file_path))
artifact_dict = {
"key": "raw-data",
"kind": "",
"iter": 0,
"tree": "latest",
"target_path": "https://raw.githubusercontent.com/mlrun/demos/master/customer-churn-prediction/WA_Fn-UseC_-Telc"
"o-Customer-Churn.csv",
"db_key": "raw-data",
}
project.artifacts = [artifact_dict]
created_project = db.create_project(project)
_assert_projects(project, created_project)
stored_project = db.store_project(project_name, project)
_assert_projects(project, stored_project)
patched_project = db.patch_project(project_name, {})
_assert_projects(project, patched_project)
get_project = db.get_project(project_name)
_assert_projects(project, get_project)
list_projects = db.list_projects()
_assert_projects(project, list_projects[0])
def _assert_projects(expected_project, project):
assert (
deepdiff.DeepDiff(
expected_project.to_dict(),
project.to_dict(),
ignore_order=True,
exclude_paths={
"root['metadata']['created']",
"root['spec']['desired_state']",
"root['status']",
},
)
== {}
)
assert expected_project.spec.desired_state == project.spec.desired_state
assert expected_project.spec.desired_state == project.status.state