-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_task.py
683 lines (578 loc) · 24.4 KB
/
test_task.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
"""
Tests for TaskController
"""
import os
import uuid
import tempfile
import platform
from io import open, TextIOWrapper
try:
to_unicode = unicode
except NameError:
to_unicode = str
try:
def to_bytes(val):
return bytes(val)
to_bytes("test")
except TypeError:
def to_bytes(val):
return bytes(val, "utf-8")
to_bytes("test")
from datmo.config import Config
from datmo.core.controller.project import ProjectController
from datmo.core.controller.environment.environment import EnvironmentController
from datmo.core.controller.task import TaskController
from datmo.core.entity.task import Task
from datmo.core.util.exceptions import EntityNotFound, TaskRunError, \
InvalidArgumentType, RequiredArgumentMissing, ProjectNotInitialized, \
InvalidProjectPath, TooManyArgumentsFound, DoesNotExist
from datmo.core.util.misc_functions import check_docker_inactive, pytest_docker_environment_failed_instantiation
# provide mountable tmp directory for docker
tempfile.tempdir = "/tmp" if not platform.system() == "Windows" else None
test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir())
class TestTaskController():
def setup_method(self):
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
self.environment_ids = []
def teardown_method(self):
if not check_docker_inactive(test_datmo_dir):
self.__setup()
self.environment_controller = EnvironmentController()
for env_id in list(set(self.environment_ids)):
if not self.environment_controller.delete(env_id):
raise Exception
def __setup(self):
Config().set_home(self.temp_dir)
self.project_controller = ProjectController()
self.project_controller.init("test", "test description")
self.environment_controller = EnvironmentController()
self.task_controller = TaskController()
def test_init_fail_project_not_init(self):
Config().set_home(self.temp_dir)
failed = False
try:
TaskController()
except ProjectNotInitialized:
failed = True
assert failed
def test_init_fail_invalid_path(self):
test_home = "some_random_dir"
Config().set_home(test_home)
failed = False
try:
TaskController()
except InvalidProjectPath:
failed = True
assert failed
def test_create(self):
self.__setup()
# Create task in the project
task_obj = self.task_controller.create()
assert isinstance(task_obj, Task)
assert task_obj.created_at
assert task_obj.updated_at
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_helper(self):
self.__setup()
# TODO: Try out more options (see below)
# Create environment_driver id
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
paths = [env_def_path]
environment_obj = self.environment_controller.create({"paths": paths})
self.environment_ids.append(environment_obj.id)
# Set log filepath
log_filepath = os.path.join(self.task_controller.home, "test.log")
# create volume to mount
temp_test_dirpath = os.path.join(self.temp_dir, "temp")
os.makedirs(temp_test_dirpath)
# Test option set 1
random_name = str(uuid.uuid1())
options_dict = {
"command": ["sh", "-c", "echo accuracy:0.45"],
"ports": ["8888:8888"],
"name": random_name,
"volumes": {
temp_test_dirpath: {
'bind': '/task/',
'mode': 'rw'
}
},
"mem_limit": "4g",
"detach": False,
"stdin_open": False,
"tty": False,
"api": False,
"interactive": False
}
return_code, run_id, logs = \
self.task_controller._run_helper(environment_obj.id,
options_dict, log_filepath)
assert return_code == 0
assert run_id and \
self.task_controller.environment_driver.get_container(run_id)
assert logs and \
os.path.exists(log_filepath)
self.task_controller.environment_driver.stop_remove_containers_by_term(
term=random_name)
# Test option set 2
random_name_2 = str(uuid.uuid1())
options_dict = {
"command": ["sh", "-c", "echo accuracy:0.45"],
"ports": ["8888:8888"],
"name": random_name_2,
"volumes": {
temp_test_dirpath: {
'bind': '/task/',
'mode': 'rw'
}
},
"mem_limit": "4g",
"detach": True,
"stdin_open": False,
"tty": False,
"api": True,
"interactive": False
}
return_code, run_id, logs = \
self.task_controller._run_helper(environment_obj.id,
options_dict, log_filepath)
assert return_code == 0
assert run_id and \
self.task_controller.environment_driver.get_container(run_id)
assert logs and \
os.path.exists(log_filepath)
self.task_controller.environment_driver.stop_remove_containers_by_term(
term=random_name_2)
def test_parse_logs_for_results(self):
self.__setup()
test_logs = """
this is a log
accuracy is good
accuracy : 0.94
this did not work
validation : 0.32
model_type : logistic regression
"""
result = self.task_controller._parse_logs_for_results(test_logs)
assert isinstance(result, dict)
assert result['accuracy'] == "0.94"
assert result['validation'] == "0.32"
assert result['model_type'] == "logistic regression"
test_logs = """test"""
result = self.task_controller._parse_logs_for_results(test_logs)
assert result is None
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run(self):
self.__setup()
# 0) Test failure case without command and without interactive
# 1) Test success case with default values and env def file
# 2) Test failure case if running same task (conflicting containers)
# 3) Test failure case if running same task with snapshot_dict (conflicting containers)
# 4) Test success case with snapshot_dict
# 5) Test success case with saved file during task run
# TODO: look into log filepath randomness, sometimes logs are not written
# Create task in the project
task_obj = self.task_controller.create()
# Create environment definition
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
# 0) Test option 0
failed = False
try:
self.task_controller.run(task_obj.id)
except RequiredArgumentMissing:
failed = True
assert failed
failed = False
try:
self.task_controller.run(
task_obj.id,
task_dict={
"command": None,
"interactive": False,
"ports": None
})
except RequiredArgumentMissing:
failed = True
assert failed
# Create task_dict
task_command = ["sh", "-c", "echo accuracy:0.45"]
task_dict = {"command_list": task_command}
# 1) Test option 1
updated_task_obj = self.task_controller.run(
task_obj.id, task_dict=task_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
assert isinstance(updated_task_obj, Task)
assert task_obj.id == updated_task_obj.id
assert updated_task_obj.before_snapshot_id
assert updated_task_obj.ports == None
assert updated_task_obj.interactive == False
assert updated_task_obj.task_dirpath
assert updated_task_obj.log_filepath
assert updated_task_obj.start_time
assert updated_task_obj.after_snapshot_id
assert updated_task_obj.run_id
assert updated_task_obj.logs
assert "accuracy" in updated_task_obj.logs
assert updated_task_obj.results
assert updated_task_obj.results == {"accuracy": "0.45"}
assert updated_task_obj.status == "SUCCESS"
assert updated_task_obj.end_time
assert updated_task_obj.duration
self.task_controller.stop(task_obj.id)
# 2) Test option 2
failed = False
try:
self.task_controller.run(task_obj.id)
except TaskRunError:
failed = True
assert failed
# 3) Test option 3
# Create files to add
self.project_controller.file_driver.create("dirpath1", directory=True)
self.project_controller.file_driver.create("dirpath2", directory=True)
self.project_controller.file_driver.create("filepath1")
# Snapshot dictionary
snapshot_dict = {
"paths": [
os.path.join(self.project_controller.home, "dirpath1"),
os.path.join(self.project_controller.home, "dirpath2"),
os.path.join(self.project_controller.home, "filepath1")
],
}
# Run a basic task in the project
failed = False
try:
self.task_controller.run(task_obj.id, snapshot_dict=snapshot_dict)
except TaskRunError:
failed = True
assert failed
# Test when the specific task id is already RUNNING
# Create task in the project
task_obj_1 = self.task_controller.create()
self.task_controller.dal.task.update({
"id": task_obj_1.id,
"status": "RUNNING"
})
# Create environment_driver definition
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
failed = False
try:
self.task_controller.run(task_obj_1.id, task_dict=task_dict)
except TaskRunError:
failed = True
assert failed
# 4) Test option 4
# Create a new task in the project
task_obj_2 = self.task_controller.create()
# Run another task in the project
updated_task_obj_2 = self.task_controller.run(
task_obj_2.id, task_dict=task_dict, snapshot_dict=snapshot_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj_2.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
assert isinstance(updated_task_obj_2, Task)
assert task_obj_2.id == updated_task_obj_2.id
assert updated_task_obj_2.before_snapshot_id
assert updated_task_obj_2.ports == None
assert updated_task_obj_2.interactive == False
assert updated_task_obj_2.task_dirpath
assert updated_task_obj_2.log_filepath
assert updated_task_obj_2.start_time
assert updated_task_obj_2.after_snapshot_id
assert updated_task_obj_2.run_id
assert updated_task_obj_2.logs
assert "accuracy" in updated_task_obj_2.logs
assert updated_task_obj_2.results
assert updated_task_obj_2.results == {"accuracy": "0.45"}
assert updated_task_obj_2.status == "SUCCESS"
assert updated_task_obj_2.end_time
assert updated_task_obj_2.duration
self.task_controller.stop(task_obj_2.id)
# 5) Test option 5
# Create a basic script
# (fails w/ no environment)
test_filepath = os.path.join(self.temp_dir, "script.py")
with open(test_filepath, "wb") as f:
f.write(to_bytes("import os\n"))
f.write(to_bytes("import shutil\n"))
f.write(to_bytes("print('hello')\n"))
f.write(to_bytes("print(' accuracy: 0.56 ')\n"))
f.write(
to_bytes(
"with open(os.path.join('/task', 'new_file.txt'), 'a') as f:\n"
))
f.write(to_bytes(" f.write('my test file')\n"))
# Create task in the project
task_obj_2 = self.task_controller.create()
# Create task_dict
task_command = ["python", "script.py"]
task_dict = {"command_list": task_command}
# Create environment definition
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
updated_task_obj_2 = self.task_controller.run(
task_obj_2.id, task_dict=task_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj_2.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
assert isinstance(updated_task_obj_2, Task)
assert updated_task_obj_2.before_snapshot_id
assert updated_task_obj_2.ports == None
assert updated_task_obj_2.interactive == False
assert updated_task_obj_2.task_dirpath
assert updated_task_obj_2.log_filepath
assert updated_task_obj_2.start_time
assert updated_task_obj_2.after_snapshot_id
assert updated_task_obj_2.run_id
assert updated_task_obj_2.logs
assert "accuracy" in updated_task_obj_2.logs
assert updated_task_obj_2.results
assert updated_task_obj_2.results == {"accuracy": "0.56"}
assert updated_task_obj_2.status == "SUCCESS"
assert updated_task_obj_2.end_time
assert updated_task_obj_2.duration
self.task_controller.stop(task_obj_2.id)
# test if after snapshot has the file written
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj_2.after_snapshot_id)
file_collection_obj = self.task_controller.dal.file_collection.get_by_id(
after_snapshot_obj.file_collection_id)
files_absolute_path = os.path.join(self.task_controller.home,
file_collection_obj.path)
assert os.path.isfile(os.path.join(files_absolute_path, "task.log"))
assert os.path.isfile(
os.path.join(files_absolute_path, "new_file.txt"))
def test_list(self):
self.__setup()
# Create tasks in the project
task_obj_1 = self.task_controller.create()
task_obj_2 = self.task_controller.create()
# List all tasks regardless of filters
result = self.task_controller.list()
assert len(result) == 2 and \
task_obj_1 in result and \
task_obj_2 in result
# List all tasks regardless of filters in ascending
result = self.task_controller.list(
sort_key='created_at', sort_order='ascending')
assert len(result) == 2 and \
task_obj_1 in result and \
task_obj_2 in result
assert result[0].created_at <= result[-1].created_at
# List all tasks regardless of filters in descending
result = self.task_controller.list(
sort_key='created_at', sort_order='descending')
assert len(result) == 2 and \
task_obj_1 in result and \
task_obj_2 in result
assert result[0].created_at >= result[-1].created_at
# Wrong order being passed in
failed = False
try:
_ = self.task_controller.list(
sort_key='created_at', sort_order='wrong_order')
except InvalidArgumentType:
failed = True
assert failed
# Wrong key and order being passed in
failed = False
try:
_ = self.task_controller.list(
sort_key='wrong_key', sort_order='wrong_order')
except InvalidArgumentType:
failed = True
assert failed
# wrong key and right order being passed in
expected_result = self.task_controller.list(
sort_key='created_at', sort_order='ascending')
result = self.task_controller.list(
sort_key='wrong_key', sort_order='ascending')
expected_ids = [item.id for item in expected_result]
ids = [item.id for item in result]
assert set(expected_ids) == set(ids)
# List all tasks and filter by session
result = self.task_controller.list(
session_id=self.project_controller.current_session.id)
assert len(result) == 2 and \
task_obj_1 in result and \
task_obj_2 in result
def test_get(self):
self.__setup()
# Test failure for no task
failed = False
try:
self.task_controller.get("random")
except DoesNotExist:
failed = True
assert failed
# Test success for task
task_obj = self.task_controller.create()
task_obj_returned = self.task_controller.get(task_obj.id)
assert task_obj == task_obj_returned
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_get_files(self):
self.__setup()
# Test failure case
failed = False
try:
self.task_controller.get_files("random")
except DoesNotExist:
failed = True
assert failed
# Create task in the project
task_obj = self.task_controller.create()
# Create environment definition
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
# Create file to add
self.project_controller.file_driver.create("dirpath1", directory=True)
self.project_controller.file_driver.create(
os.path.join("dirpath1", "filepath1"))
# Snapshot dictionary
snapshot_dict = {
"paths": [
os.path.join(self.project_controller.home, "dirpath1",
"filepath1")
],
}
# Create task_dict
task_command = ["sh", "-c", "echo accuracy:0.45"]
task_dict = {"command_list": task_command}
# Test the default values
updated_task_obj = self.task_controller.run(
task_obj.id, task_dict=task_dict, snapshot_dict=snapshot_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
# TODO: Test case for during run and before_snapshot run
# Get files for the task after run is complete (default)
result = self.task_controller.get_files(updated_task_obj.id)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj.after_snapshot_id)
file_collection_obj = self.task_controller.dal.file_collection.get_by_id(
after_snapshot_obj.file_collection_id)
file_names = [item.name for item in result]
assert len(result) == 2
for item in result:
assert isinstance(item, TextIOWrapper)
assert item.mode == "r"
assert os.path.join(self.task_controller.home, ".datmo", "collections",
file_collection_obj.filehash,
"task.log") in file_names
assert os.path.join(self.task_controller.home, ".datmo", "collections",
file_collection_obj.filehash,
"filepath1") in file_names
# Get files for the task after run is complete for different mode
result = self.task_controller.get_files(updated_task_obj.id, mode="a")
assert len(result) == 2
for item in result:
assert isinstance(item, TextIOWrapper)
assert item.mode == "a"
assert os.path.join(self.task_controller.home, ".datmo", "collections",
file_collection_obj.filehash,
"task.log") in file_names
assert os.path.join(self.task_controller.home, ".datmo", "collections",
file_collection_obj.filehash,
"filepath1") in file_names
self.task_controller.stop(task_obj.id)
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_delete(self):
self.__setup()
# Create tasks in the project
task_obj = self.task_controller.create()
# Delete task from the project
result = self.task_controller.delete(task_obj.id)
# Check if task retrieval throws error
thrown = False
try:
self.task_controller.dal.snapshot.get_by_id(task_obj.id)
except EntityNotFound:
thrown = True
assert result == True and \
thrown == True
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_stop_failure(self):
self.__setup()
# 1) Test required arguments not provided
# 2) Test too many arguments found
# 3) Test incorrect task id given
# 1) Test option 1
failed = False
try:
self.task_controller.stop()
except RequiredArgumentMissing:
failed = True
assert failed
# 2) Test option 2
failed = False
try:
self.task_controller.stop(task_id="test_task_id", all=True)
except TooManyArgumentsFound:
failed = True
assert failed
# 3) Test option 3
thrown = False
try:
self.task_controller.stop(task_id="incorrect_task_id")
except DoesNotExist:
thrown = True
assert thrown
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_stop_success(self):
self.__setup()
# 1) Test stop with task_id
# 2) Test stop with all given
# Create task in the project
task_obj = self.task_controller.create()
# Create environment driver definition
env_def_path = os.path.join(self.project_controller.home, "Dockerfile")
with open(env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
# Create task_dict
task_command = ["sh", "-c", "echo accuracy:0.45"]
task_dict = {"command_list": task_command}
# 1) Test option 1
updated_task_obj = self.task_controller.run(
task_obj.id, task_dict=task_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
task_id = updated_task_obj.id
result = self.task_controller.stop(task_id=task_id)
after_task_obj = self.task_controller.dal.task.get_by_id(task_id)
assert result
assert after_task_obj.status == "STOPPED"
# 2) Test option 2
task_obj_2 = self.task_controller.create()
updated_task_obj = self.task_controller.run(
task_obj_2.id, task_dict=task_dict)
after_snapshot_obj = self.task_controller.dal.snapshot.get_by_id(
updated_task_obj.after_snapshot_id)
environment_obj = self.task_controller.dal.environment.get_by_id(
after_snapshot_obj.environment_id)
self.environment_ids.append(environment_obj.id)
result = self.task_controller.stop(all=True)
all_task_objs = self.task_controller.dal.task.query({})
assert result
for task_obj in all_task_objs:
assert task_obj.status == "STOPPED"