Skip to content

Commit

Permalink
Merge pull request #106 from datmo/test-interactive
Browse files Browse the repository at this point in the history
adding test for interactive in dockerenv driver and environment
  • Loading branch information
asampat3090 committed May 10, 2018
2 parents 5370a94 + 385a7e0 commit 797136b
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 30 deletions.
113 changes: 86 additions & 27 deletions datmo/core/controller/environment/driver/tests/test_dockerenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tempfile
import platform
import uuid
import timeout_decorator
from io import open
try:
to_unicode = unicode
Expand Down Expand Up @@ -223,33 +224,6 @@ def test_run(self):
# teardown
self.docker_environment_manager.stop(run_id, force=True)

# TODO: Adding test with stdin_open and tty as True for `interactive` argument and terminate inside container
# # keeping stdin_open, tty as True
# run_options = {
# "command": [],
# "ports": ["8888:9999", "5000:5001"],
# "name": str(uuid.uuid1()),
# "volumes": {
# self.docker_environment_manager.filepath: {
# 'bind': '/home/',
# 'mode': 'rw'
# }
# },
# "detach": True,
# "stdin_open": True,
# "tty": True,
# "api": False
# }
# return_code, run_id, logs = \
# self.docker_environment_manager.run(image_name, run_options, log_filepath)
#
# assert return_code == 0
# assert run_id
# assert not logs
#
# # teardown
# self.docker_environment_manager.stop(run_id, force=True)

# Test default values for run options
run_options = {"command": ["sh", "-c", "echo yo"]}
return_code, run_id, logs = \
Expand All @@ -259,6 +233,91 @@ def test_run(self):
assert logs
# teardown container
self.docker_environment_manager.stop(run_id, force=True)

# teardown image
self.docker_environment_manager.remove(image_name, force=True)

def test_interactive_run(self):
# keeping stdin_open, tty as True
# build image
image_name = str(uuid.uuid1())
path = os.path.join(self.docker_environment_manager.filepath,
"Dockerfile")
random_text = str(uuid.uuid1())
with open(path, "w") as f:
f.write(to_unicode("FROM datmo/xgboost:cpu" + "\n"))
f.write(to_unicode(str("RUN echo " + random_text)))
log_filepath = os.path.join(self.docker_environment_manager.filepath,
"test.log")
self.docker_environment_manager.build(image_name, path)

@timeout_decorator.timeout(10, use_signals=False)
def timed_run(container_name, timed_run_result):
run_options = {
"command": [],
"ports": ["8888:9999", "5000:5001"],
"name": container_name,
"volumes": {
self.docker_environment_manager.filepath: {
'bind': '/home/',
'mode': 'rw'
}
},
"detach": True,
"stdin_open": True,
"tty": True,
"api": False
}

self.docker_environment_manager.run(image_name, run_options,
log_filepath)
return timed_run_result

container_name = str(uuid.uuid1())
timed_run_result = False
try:
timed_run_result = timed_run(container_name, timed_run_result)
except timeout_decorator.timeout_decorator.TimeoutError:
timed_run_result = True

assert timed_run_result

# teardown container
self.docker_environment_manager.stop(container_name, force=True)

@timeout_decorator.timeout(10, use_signals=False)
def timed_run(container_name, timed_run_result):
run_options = {
"command": ["jupyter", "notebook"],
"ports": ["8888:8888"],
"name": container_name,
"volumes": {
self.docker_environment_manager.filepath: {
'bind': '/home/',
'mode': 'rw'
}
},
"detach": True,
"stdin_open": False,
"tty": False,
"api": False
}

self.docker_environment_manager.run(image_name, run_options,
log_filepath)
return timed_run_result

container_name = str(uuid.uuid1())
timed_run_result = False
try:
timed_run_result = timed_run(container_name, timed_run_result)
except timeout_decorator.timeout_decorator.TimeoutError:
timed_run_result = True
assert timed_run_result

# teardown container
self.docker_environment_manager.stop(container_name, force=True)

# teardown image
self.docker_environment_manager.remove(image_name, force=True)

Expand Down
98 changes: 96 additions & 2 deletions datmo/core/controller/environment/tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Tests for EnvironmentController
"""
import os
import uuid
import random
import string
import tempfile
import platform
import timeout_decorator
from io import open
try:
to_unicode = unicode
Expand Down Expand Up @@ -278,7 +280,6 @@ def test_build(self):
def test_run(self):
# 1) Test run simple command with simple Dockerfile
# 2) Test run script, with autogenerated definition
# TODO: Adding test with stdin_open and tty as True for `interactive` argument and terminate inside container
self.project.init("test5", "test description")

# 1) Test option 1
Expand All @@ -297,7 +298,7 @@ def test_run(self):
"ports": ["8888:8888"],
"name": random_name,
"volumes": None,
"detach": False,
"detach": True,
"stdin_open": False,
"tty": False,
"api": False
Expand Down Expand Up @@ -371,6 +372,99 @@ def test_run(self):
# teardown
self.environment.delete(environment_obj.id)

def test_interactive_run(self):
# 1) Test run interactive terminal in environment
# 2) Test run jupyter notebook in environment
# Create environment definition
self.project.init("test6", "test description")

definition_filepath = os.path.join(self.environment.home, "Dockerfile")
with open(definition_filepath, "w") as f:
f.write(to_unicode(str("FROM datmo/xgboost:cpu")))

input_dict = {
"definition_filepath": definition_filepath,
}

# Create environment in the project
environment_obj = self.environment.create(input_dict)
# 1) Test option 1
@timeout_decorator.timeout(10, use_signals=False)
def timed_run(container_name, timed_run):
run_options = {
"command": [],
"ports": ["8888:8888"],
"name": container_name,
"volumes": None,
"detach": True,
"stdin_open": True,
"tty": True,
"api": False
}

log_filepath = os.path.join(self.project.home, "task.log")

# Build environment in the project
_ = self.environment.build(environment_obj.id)

# Run environment in the project
self.environment.run(environment_obj.id, run_options, log_filepath)

return timed_run

container_name = str(uuid.uuid1())
timed_run_result = False
try:
timed_run_result = timed_run(container_name, timed_run_result)
except timeout_decorator.timeout_decorator.TimeoutError:
timed_run_result = True

assert timed_run_result

# teardown
self.environment.delete(environment_obj.id)

# 2) Test option 2
environment_obj = self.environment.create(input_dict)

@timeout_decorator.timeout(10, use_signals=False)
def timed_run(container_name, timed_run):
run_options = {
"command": ["jupyter", "notebook"],
"ports": ["8888:8888"],
"name": container_name,
"volumes": None,
"detach": True,
"stdin_open": False,
"tty": False,
"api": False
}

log_filepath = os.path.join(self.project.home, "task.log")

# Build environment in the project
_ = self.environment.build(environment_obj.id)

# Run environment in the project
self.environment.run(environment_obj.id, run_options, log_filepath)

return timed_run

container_name = str(uuid.uuid1())
timed_run_result = False
try:
timed_run_result = timed_run(container_name, timed_run_result)
except timeout_decorator.timeout_decorator.TimeoutError:
timed_run_result = True

assert timed_run_result

# Stop the running environment
# self.environment.stop(container_name)

# teardown
self.environment.delete(environment_obj.id)

def test_list(self):
self.project.init("test4", "test description")

Expand Down
2 changes: 2 additions & 0 deletions datmo/core/controller/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Tests for TaskController
"""
import os
import time
import random
import string
import tempfile
import platform
import timeout_decorator
from io import open, TextIOWrapper
try:
to_unicode = unicode
Expand Down
3 changes: 3 additions & 0 deletions datmo/core/storage/local/tests/test_dal_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ def test_update_task(self):
updated_task_input_dict['id'] = task.id
updated_task_input_dict['command'] = "task_new"
updated_task_input_dict['ports'] = ["9000:9000"]
updated_task_input_dict['interactive'] = True
updated_task = self.dal.task.update(updated_task_input_dict)

assert task.id == updated_task.id
assert task.updated_at < updated_task.updated_at
assert updated_task.command == updated_task_input_dict['command']
assert updated_task.ports == updated_task_input_dict['ports']
assert updated_task.interactive == updated_task_input_dict[
'interactive']

def test_delete_task(self):
task = self.dal.task.create(Task(self.task_input_dict))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"rsfile>=2.1", "humanfriendly>=3.6.1", "python-slugify>=1.2.4",
"giturlparse.py>=0.0.5", "blitzdb>=0.2.12", "kids.cache>=0.0.7",
"pymongo>=3.6.0", "checksumdir>=1.1.4", "semver>=2.7.8",
"pipreqs>=0.4.9", "backports.ssl-match-hostname>=3.5.0.1"
"pipreqs>=0.4.9", "backports.ssl-match-hostname>=3.5.0.1", "timeout-decorator==0.4.0"
],
tests_require=["pytest==3.0.4"],
entry_points={'console_scripts': ['datmo = datmo.cli.main:main']},
Expand Down

0 comments on commit 797136b

Please sign in to comment.