Skip to content

Commit

Permalink
adding cli snapshot create from task
Browse files Browse the repository at this point in the history
  • Loading branch information
shabazpatel committed May 7, 2018
1 parent 2dca7f0 commit 8fc89c7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 43 deletions.
61 changes: 38 additions & 23 deletions datmo/cli/command/snapshot.py
Expand Up @@ -18,37 +18,49 @@ def __init__(self, home, cli_helper, parser):
def create(self, **kwargs):
self.cli_helper.echo(__("info", "cli.snapshot.create"))

snapshot_dict = {"visible": True}
task_id = kwargs.get("task_id", None)
# creating snapshot with task id if it exists
if task_id is not None:
message = kwargs.get("message", None)
# Create a new core snapshot object
snapshot_task_obj = self.snapshot_controller.create_from_task(
message, task_id)
return snapshot_task_obj.id
else:
# creating snapshot without task id
snapshot_dict = {"visible": True}

# Code
mutually_exclusive_args = ["code_id", "commit_id"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)
# Code
mutually_exclusive_args = ["code_id", "commit_id"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)

# Environment
mutually_exclusive_args = ["environment_id", "environment_def_path"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)
# Environment
mutually_exclusive_args = [
"environment_id", "environment_def_path"
]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)

# File
mutually_exclusive_args = ["file_collection_id", "filepaths"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)
# File
mutually_exclusive_args = ["file_collection_id", "filepaths"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)

# Config
mutually_exclusive_args = ["config_filepath", "config_filename"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)
# Config
mutually_exclusive_args = ["config_filepath", "config_filename"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)

# Stats
mutually_exclusive_args = ["stats_filepath", "stats_filename"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)
# Stats
mutually_exclusive_args = ["stats_filepath", "stats_filename"]
mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict)

optional_args = ["session_id", "task_id", "message", "label"]
optional_args = ["session_id", "message", "label"]

for arg in optional_args:
if arg in kwargs and kwargs[arg] is not None:
snapshot_dict[arg] = kwargs[arg]
for arg in optional_args:
if arg in kwargs and kwargs[arg] is not None:
snapshot_dict[arg] = kwargs[arg]

snapshot_obj = self.snapshot_controller.create(snapshot_dict)
snapshot_obj = self.snapshot_controller.create(snapshot_dict)

return snapshot_obj.id
return snapshot_obj.id

def delete(self, **kwargs):
self.cli_helper.echo(__("info", "cli.snapshot.delete"))
Expand Down Expand Up @@ -89,7 +101,10 @@ def ls(self, **kwargs):
]
t = prettytable.PrettyTable(header_list)
snapshot_objs = self.snapshot_controller.list(
session_id=session_id, visible=True)
session_id=session_id,
visible=True,
sort_key='created_at',
sort_order='descending')
for snapshot_obj in snapshot_objs:
t.add_row([
snapshot_obj.id,
Expand Down
66 changes: 46 additions & 20 deletions datmo/cli/command/tests/test_snapshot.py
Expand Up @@ -24,6 +24,7 @@
from datmo.cli.parser import parser
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.snapshot import SnapshotCommand
from datmo.cli.command.task import TaskCommand
from datmo.core.util.exceptions import (ProjectNotInitializedException,
MutuallyExclusiveArguments)

Expand Down Expand Up @@ -94,6 +95,16 @@ def test_datmo_snapshot_create(self):
test_stats_filepath = self.config_filepath
test_filepaths = [self.filepath, self.filepath_2]

# try single filepath
self.snapshot.parse([
"snapshot", "create", "--message", test_message, "--task-id",
test_task_id
])

# test for desired side effects
assert self.snapshot.args.message == test_message
assert self.snapshot.args.task_id == test_task_id

# try single filepath
self.snapshot.parse([
"snapshot",
Expand All @@ -104,8 +115,6 @@ def test_datmo_snapshot_create(self):
test_label,
"--session-id",
test_session_id,
"--task-id",
test_task_id,
"--code-id",
test_code_id,
"--environment-def-path",
Expand All @@ -122,7 +131,6 @@ def test_datmo_snapshot_create(self):
assert self.snapshot.args.message == test_message
assert self.snapshot.args.label == test_label
assert self.snapshot.args.session_id == test_session_id
assert self.snapshot.args.task_id == test_task_id
assert self.snapshot.args.code_id == test_code_id
assert self.snapshot.args.environment_def_path == test_environment_def_path
assert self.snapshot.args.config_filepath == test_config_filepath
Expand All @@ -132,18 +140,17 @@ def test_datmo_snapshot_create(self):
# test multiple filepaths
self.snapshot.parse([
"snapshot", "create", "--message", test_message, "--label",
test_label, "--session-id", test_session_id, "--task-id",
test_task_id, "--code-id", test_code_id, "--environment-def-path",
test_environment_def_path, "--config-filepath",
test_config_filepath, "--stats-filepath", test_stats_filepath,
"--filepaths", test_filepaths[0], "--filepaths", test_filepaths[1]
test_label, "--session-id", test_session_id, "--code-id",
test_code_id, "--environment-def-path", test_environment_def_path,
"--config-filepath", test_config_filepath, "--stats-filepath",
test_stats_filepath, "--filepaths", test_filepaths[0],
"--filepaths", test_filepaths[1]
])

# test for desired side effects
assert self.snapshot.args.message == test_message
assert self.snapshot.args.label == test_label
assert self.snapshot.args.session_id == test_session_id
assert self.snapshot.args.task_id == test_task_id
assert self.snapshot.args.code_id == test_code_id
assert self.snapshot.args.environment_def_path == test_environment_def_path
assert self.snapshot.args.config_filepath == test_config_filepath
Expand All @@ -153,6 +160,34 @@ def test_datmo_snapshot_create(self):
snapshot_id_1 = self.snapshot.execute()
assert snapshot_id_1

def test_datmo_snapshot_create_from_task(self):
self.__set_variables()
test_message = "this is a test message"

# create task
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.task = TaskCommand(self.temp_dir, self.cli_helper, parser)
self.task.parse(
["task", "run", "--env-def", test_dockerfile, test_command])

# test proper execution of task run command
task_obj = self.task.execute()

task_id = task_obj.id

# test multiple filepaths
self.snapshot.parse([
"snapshot", "create", "--message", test_message, "--task-id",
task_id
])

# test for desired side effects
assert self.snapshot.args.message == test_message

snapshot_id = self.snapshot.execute()
assert snapshot_id

def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
self.__set_variables()
test_message = "this is a test message"
Expand All @@ -175,9 +210,8 @@ def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
try:
self.snapshot.parse([
"snapshot", "create", "--message", test_message, "--label",
test_label, "--session-id", test_session_id, "--task-id",
test_task_id, "--code-id", test_code_id, "--commit-id",
test_commit_id
test_label, "--session-id", test_session_id, "--code-id",
test_code_id, "--commit-id", test_commit_id
])
_ = self.snapshot.execute()
except MutuallyExclusiveArguments:
Expand All @@ -196,8 +230,6 @@ def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
test_label,
"--session-id",
test_session_id,
"--task-id",
test_task_id,
"--environment-id",
test_environment_id,
"--environment-def-path",
Expand All @@ -220,8 +252,6 @@ def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
test_label,
"--session-id",
test_session_id,
"--task-id",
test_task_id,
"--file-collection-id",
test_file_collection_id,
"--filepaths",
Expand All @@ -244,8 +274,6 @@ def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
test_label,
"--session-id",
test_session_id,
"--task-id",
test_task_id,
"--config-filename",
test_config_filename,
"--config-filepath",
Expand All @@ -268,8 +296,6 @@ def test_datmo_snapshot_create_fail_mutually_exclusive_args(self):
test_label,
"--session-id",
test_session_id,
"--task-id",
test_task_id,
"--stats-filename",
test_stats_filename,
"--stats-filepath",
Expand Down

0 comments on commit 8fc89c7

Please sign in to comment.