-
Notifications
You must be signed in to change notification settings - Fork 879
/
Copy pathapi_utils.py
executable file
·465 lines (395 loc) · 16.5 KB
/
api_utils.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
import glob
import os
import shutil
import sys
REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
sys.path.append(REPO_ROOT)
from ts_scripts import tsutils as ts
TEST_DIR = os.path.join("test")
MODEL_STORE_DIR = os.path.join("model_store")
# Contains separate collection and respecive tests for both kserve and torchserve execution
### Torchserve
ARTIFACTS_MANAGEMENT_DIR = os.path.join("artifacts", "management")
ARTIFACTS_INFERENCE_DIR = os.path.join("artifacts", "inference")
ARTIFACTS_WORKFLOW_MANAGEMENT_DIR = os.path.join("artifacts", "workflow_management")
ARTIFACTS_WORKFLOW_INFERENCE_DIR = os.path.join("artifacts", "workflow_inference")
ARTIFACTS_EXPLANATION_DIR = os.path.join("artifacts", "explanation")
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR = os.path.join(
"artifacts", "increased_timeout_inference"
)
ARTIFACTS_HTTPS_DIR = os.path.join("artifacts", "https")
TS_CONSOLE_LOG_FILE = os.path.join("ts_console.log")
TS_CONFIG_FILE_HTTPS = os.path.join("resources", "config.properties")
POSTMAN_ENV_FILE = os.path.join("postman", "environment.json")
POSTMAN_INFERENCE_DATA_FILE = os.path.join("postman", "inference_data.json")
POSTMAN_LARGE_MODEL_INFERENCE_DATA_FILE = os.path.join(
"postman", "large_model_inference_data.json"
)
POSTMAN_EXPLANATION_DATA_FILE = os.path.join("postman", "explanation_data.json")
POSTMAN_MANAGEMENT_DATA_FILE = os.path.join("postman", "management_data.json")
POSTMAN_WORKFLOW_DATA_FILE = os.path.join("postman", "workflow_data.json")
POSTMAN_WORKFLOW_INFERENCE_DATA_FILE = os.path.join(
"postman", "workflow_inference_data.json"
)
POSTMAN_INCRSD_TIMEOUT_INFERENCE_DATA_FILE = os.path.join(
"postman", "increased_timeout_inference.json"
)
# only one management collection for both KServe and torchserve
POSTMAN_COLLECTION_MANAGEMENT = os.path.join(
"postman", "management_api_test_collection.json"
)
POSTMAN_COLLECTION_INFERENCE = os.path.join(
"postman", "inference_api_test_collection.json"
)
POSTMAN_COLLECTION_WORKFLOW = os.path.join(
"postman", "workflow_api_test_collection.json"
)
POSTMAN_COLLECTION_WORKFLOW_INFERENCE = os.path.join(
"postman", "workflow_inference_collection.json"
)
POSTMAN_COLLECTION_EXPLANATION = os.path.join(
"postman", "explanation_api_test_collection.json"
)
POSTMAN_COLLECTION_HTTPS = os.path.join("postman", "https_test_collection.json")
### KServe v1 protocol
ARTIFACTS_MANAGEMENT_DIR_KF = os.path.join("artifacts", "management_kf")
ARTIFACTS_INFERENCE_DIR_KF = os.path.join("artifacts", "inference_kf")
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR_KF = os.path.join(
"artifacts", "increased_timeout_inference_kf"
)
ARTIFACTS_HTTPS_DIR_KF = os.path.join("artifacts", "https_kf")
TS_CONFIG_FILE_HTTPS_KF = os.path.join("resources", "config_kf.properties")
POSTMAN_INFERENCE_DATA_FILE_KF = os.path.join("postman", "kf_inference_data.json")
POSTMAN_INCRSD_TIMEOUT_INFERENCE_DATA_FILE_KF = os.path.join(
"postman", "increased_timeout_inference.json"
)
POSTMAN_COLLECTION_INFERENCE_KF = os.path.join("postman", "kf_api_test_collection.json")
POSTMAN_COLLECTION_HTTPS_KF = os.path.join("postman", "kf_https_test_collection.json")
### KServe v2 protocol
ARTIFACTS_INFERENCE_DIR_KFV2 = os.path.join("artifacts", "inference_kfv2")
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR_KFV2 = os.path.join(
"artifacts", "increased_timeout_inference_kfv2"
)
ARTIFACTS_HTTPS_DIR_KFV2 = os.path.join("artifacts", "https_kfv2")
TS_CONFIG_FILE_HTTPS_KFV2 = os.path.join("resources", "config_kfv2.properties")
POSTMAN_INFERENCE_DATA_FILE_KFV2 = os.path.join("postman", "kfv2_inference_data.json")
POSTMAN_COLLECTION_INFERENCE_KFV2 = os.path.join(
"postman", "kfv2_api_test_collection.json"
)
POSTMAN_COLLECTION_HTTPS_KFV2 = os.path.join(
"postman", "kfv2_https_test_collection.json"
)
REPORT_FILE = os.path.join("report.html")
def cleanup_model_store():
# rm -rf $MODEL_STORE_DIR / *
for f in glob.glob(os.path.join(MODEL_STORE_DIR, "*")):
os.remove(f)
def move_logs(log_file, artifact_dir):
logs_dir = os.path.join("logs")
os.rename(log_file, os.path.join(logs_dir, log_file)) # mv file logs/
os.rename(logs_dir, os.path.join(artifact_dir, logs_dir)) # mv logs/ dir
def trigger_management_tests():
"""Return exit code of newman execution of management collection"""
config_file = open("config.properties", "w")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_MANAGEMENT} -d {POSTMAN_MANAGEMENT_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_MANAGEMENT_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_MANAGEMENT_DIR)
cleanup_model_store()
return EXIT_CODE
def trigger_inference_tests():
"""Return exit code of newman execution of inference collection"""
config_file = open("config.properties", "w")
config_file.write("metrics_mode=prometheus\n")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_INFERENCE} -d {POSTMAN_INFERENCE_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_INFERENCE_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_INFERENCE_DIR)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_workflow_tests():
"""Return exit code of newman execution of workflow collection"""
config_file = open("config.properties", "w")
config_file.write("disable_token_authorization=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
workflow_store=MODEL_STORE_DIR,
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_WORKFLOW} -d {POSTMAN_WORKFLOW_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_WORKFLOW_MANAGEMENT_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_WORKFLOW_MANAGEMENT_DIR)
cleanup_model_store()
return EXIT_CODE
def trigger_workflow_inference_tests():
"""Return exit code of newman execution of workflow inference collection"""
config_file = open("config.properties", "w")
config_file.write("disable_token_authorization=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
workflow_store=MODEL_STORE_DIR,
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_WORKFLOW_INFERENCE} -d {POSTMAN_WORKFLOW_INFERENCE_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_WORKFLOW_INFERENCE_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_WORKFLOW_INFERENCE_DIR)
cleanup_model_store()
return EXIT_CODE
def trigger_explanation_tests():
"""Return exit code of newman execution of inference collection"""
config_file = open("config.properties", "w")
config_file.write("metrics_mode=prometheus\n")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_EXPLANATION} -d {POSTMAN_EXPLANATION_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_INFERENCE_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_EXPLANATION_DIR)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_incr_timeout_inference_tests():
"""Return exit code of newman execution of increased timeout inference collection"""
# Configuration with increased timeout
config_file = open("config.properties", "w")
config_file.write("default_response_timeout=300\n")
config_file.write("metrics_mode=prometheus\n")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_INFERENCE} -d {POSTMAN_INCRSD_TIMEOUT_INFERENCE_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_https_tests():
"""Return exit code of newman execution of https collection"""
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file=TS_CONFIG_FILE_HTTPS,
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run --insecure -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_HTTPS} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_HTTPS_DIR}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_HTTPS_DIR)
cleanup_model_store()
return EXIT_CODE
## KServe tests starts here
def trigger_management_tests_kf():
"""Return exit code of newman execution of management collection"""
config_file = open("config.properties", "w")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true\n")
config_file.write("service_envelope=kserve")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_MANAGEMENT} -d {POSTMAN_MANAGEMENT_DATA_FILE} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_MANAGEMENT_DIR_KF}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_MANAGEMENT_DIR_KF)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_inference_tests_kf():
"""Return exit code of newman execution of inference collection"""
config_file = open("config.properties", "w")
config_file.write("service_envelope=kserve\n")
config_file.write("metrics_mode=prometheus\n")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true\n")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_INFERENCE_KF} -d {POSTMAN_INFERENCE_DATA_FILE_KF} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_INFERENCE_DIR_KF}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_INFERENCE_DIR_KF)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_https_tests_kf():
"""Return exit code of newman execution of https collection"""
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file=TS_CONFIG_FILE_HTTPS_KF,
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run --insecure -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_HTTPS_KF} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_HTTPS_DIR_KF}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_HTTPS_DIR_KF)
cleanup_model_store()
return EXIT_CODE
def trigger_inference_tests_kfv2():
"""Return exit code of newman execution of inference collection"""
config_file = open("config.properties", "w")
config_file.write("service_envelope=kservev2\n")
config_file.write("metrics_mode=prometheus\n")
config_file.write("disable_token_authorization=true\n")
config_file.write("enable_model_api=true\n")
config_file.close()
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file="config.properties",
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_INFERENCE_KFV2} -d {POSTMAN_INFERENCE_DATA_FILE_KFV2} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_INFERENCE_DIR_KFV2}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_INFERENCE_DIR_KFV2)
cleanup_model_store()
os.remove("config.properties")
return EXIT_CODE
def trigger_https_tests_kfv2():
"""Return exit code of newman execution of https collection"""
ts.start_torchserve(
ncs=True,
model_store=MODEL_STORE_DIR,
config_file=TS_CONFIG_FILE_HTTPS_KFV2,
log_file=TS_CONSOLE_LOG_FILE,
)
EXIT_CODE = os.system(
f"newman run --insecure -e {POSTMAN_ENV_FILE} {POSTMAN_COLLECTION_HTTPS_KFV2} -r cli,htmlextra --reporter-htmlextra-export {ARTIFACTS_HTTPS_DIR_KFV2}/{REPORT_FILE} --verbose"
)
ts.stop_torchserve()
move_logs(TS_CONSOLE_LOG_FILE, ARTIFACTS_HTTPS_DIR_KFV2)
cleanup_model_store()
return EXIT_CODE
def trigger_all():
exit_code1 = trigger_management_tests()
exit_code2 = trigger_inference_tests()
exit_code3 = trigger_incr_timeout_inference_tests()
exit_code4 = trigger_https_tests()
exit_code5 = trigger_management_tests_kf()
exit_code6 = trigger_inference_tests_kf()
exit_code7 = trigger_https_tests_kf()
exit_code8 = trigger_inference_tests_kfv2()
exit_code9 = trigger_https_tests_kfv2()
exit_code10 = trigger_explanation_tests()
# Skipping as these tests are flaky
# exit_code11 = trigger_workflow_tests()
# exit_code12 = trigger_workflow_inference_tests()
return (
1
if any(
code != 0
for code in [
exit_code1,
exit_code2,
exit_code3,
exit_code4,
exit_code5,
exit_code6,
exit_code7,
exit_code8,
exit_code9,
exit_code10,
]
)
else 0
)
def test_api(collection):
os.chdir(TEST_DIR)
ALL_DIRS = [
MODEL_STORE_DIR,
ARTIFACTS_MANAGEMENT_DIR,
ARTIFACTS_INFERENCE_DIR,
ARTIFACTS_EXPLANATION_DIR,
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR,
ARTIFACTS_HTTPS_DIR,
ARTIFACTS_MANAGEMENT_DIR_KF,
ARTIFACTS_INFERENCE_DIR_KF,
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR_KF,
ARTIFACTS_HTTPS_DIR_KF,
ARTIFACTS_INFERENCE_DIR_KFV2,
ARTIFACTS_INCRSD_TIMEOUT_INFERENCE_DIR_KFV2,
ARTIFACTS_HTTPS_DIR_KFV2,
ARTIFACTS_WORKFLOW_MANAGEMENT_DIR,
ARTIFACTS_WORKFLOW_INFERENCE_DIR,
]
for DIR in ALL_DIRS:
shutil.rmtree(DIR, True)
os.makedirs(DIR, exist_ok=True)
switcher = {
"management": trigger_management_tests,
"management_kf": trigger_management_tests_kf,
"inference": trigger_inference_tests,
"inference_kf": trigger_inference_tests_kf,
"inference_kfv2": trigger_inference_tests_kfv2,
"explanation": trigger_explanation_tests,
"increased_timeout_inference": trigger_incr_timeout_inference_tests,
"https": trigger_https_tests,
"https_kf": trigger_https_tests_kf,
"https_kfv2": trigger_https_tests_kfv2,
"all": trigger_all,
}
exit_code = switcher[collection]()
os.chdir(REPO_ROOT)
if exit_code != 0:
sys.exit("## Newman API Tests Failed !")