Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 23505d0

Browse files
committed
Add more tests for deployments
1 parent 76c5bf9 commit 23505d0

File tree

5 files changed

+134
-25
lines changed

5 files changed

+134
-25
lines changed

paperspace/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def deployments():
341341
)
342342
def create_deployment(**kwargs):
343343
del_if_value_is_none(kwargs)
344-
command = deployments_commands.CreateDeployment()
344+
command = deployments_commands.CreateDeploymentCommand()
345345
command.execute(kwargs)
346346

347347

@@ -365,7 +365,7 @@ def create_deployment(**kwargs):
365365
type=ChoiceType(DEPLOYMENT_STATES_MAP, case_sensitive=False)
366366
)
367367
def get_deployments_list(**kwargs):
368-
command = deployments_commands.ListDeployments()
368+
command = deployments_commands.ListDeploymentsCommand()
369369
command.execute(kwargs)
370370

371371

@@ -408,7 +408,7 @@ def update_deployment_model(id=None, **kwargs):
408408
required=True,
409409
)
410410
def start_deployment(id):
411-
command = deployments_commands.StartDeployment()
411+
command = deployments_commands.StartDeploymentCommand()
412412
command.execute(id)
413413

414414

@@ -419,5 +419,5 @@ def start_deployment(id):
419419
required=True,
420420
)
421421
def delete_deployment(id):
422-
command = deployments_commands.DeleteDeployment()
422+
command = deployments_commands.DeleteDeploymentCommand()
423423
command.execute(id)

paperspace/commands/deployments.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
deployments_api = client.API(config.CONFIG_HOST, headers=default_headers)
1212

1313

14-
class DeploymentCommandBase(object):
14+
class _DeploymentCommandBase(object):
1515
def __init__(self, api=deployments_api, logger_=logger):
1616
self.api = api
1717
self.logger = logger_
@@ -34,15 +34,15 @@ def _log_message(self, response, success_msg_template, error_msg):
3434
self.logger.log(error_msg)
3535

3636

37-
class CreateDeployment(DeploymentCommandBase):
37+
class CreateDeploymentCommand(_DeploymentCommandBase):
3838
def execute(self, kwargs):
3939
response = self.api.post("/deployments/createDeployment/", json=kwargs)
4040
self._log_message(response,
4141
"New deployment created with id: {id}",
4242
"Unknown error during deployment")
4343

4444

45-
class ListDeployments(DeploymentCommandBase):
45+
class ListDeploymentsCommand(_DeploymentCommandBase):
4646
def execute(self, kwargs):
4747
json_ = self._get_request_json(kwargs)
4848
response = self.api.get("/deployments/getDeploymentList/", json=json_)
@@ -98,7 +98,7 @@ def _make_deployments_list_table(deployments):
9898
return table_string
9999

100100

101-
class UpdateModelCommand(DeploymentCommandBase):
101+
class UpdateModelCommand(_DeploymentCommandBase):
102102
def execute(self, model_id, kwargs):
103103
if not kwargs:
104104
self.logger.log("No parameters to update were given. Use --help for more information.")
@@ -112,7 +112,7 @@ def execute(self, model_id, kwargs):
112112
"Unknown error occurred.")
113113

114114

115-
class StartDeployment(DeploymentCommandBase):
115+
class StartDeploymentCommand(_DeploymentCommandBase):
116116
def execute(self, model_id):
117117
json_ = {"id": model_id,
118118
"isRunning": True}
@@ -122,7 +122,7 @@ def execute(self, model_id):
122122
"Unknown error occurred.")
123123

124124

125-
class DeleteDeployment(DeploymentCommandBase):
125+
class DeleteDeploymentCommand(_DeploymentCommandBase):
126126
def execute(self, model_id):
127127
json_ = {"id": model_id,
128128
"upd": {"isDeleted": True}}

tests/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ def ok(self):
1010
return 200 <= self.status_code <= 299
1111

1212
def json(self):
13-
return self.json_data
13+
if not self.json_data:
14+
raise ValueError("No data")
15+
16+
return self.json_data

tests/example_responses.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@
861861
"deploymentType": "Tensorflow Serving on K8s",
862862
"modelId": "mosu30xm7q8vb0p",
863863
"modelUrl": "s3://ps-projects-development/prmr22ve0/ehla1kvbwzaco/model/",
864-
"name": "keton",
864+
"name": "some_name",
865865
"tag": "some_tag",
866866
"params": None,
867867
"code": "some_code",
@@ -908,7 +908,7 @@
908908
"deploymentType": "Tensorflow Serving on K8s",
909909
"modelId": "mosu30xm7q8vb0p",
910910
"modelUrl": "s3://ps-projects-development/prmr22ve0/ehla1kvbwzaco/model/",
911-
"name": "keton",
911+
"name": "some_name",
912912
"tag": None,
913913
"params": None,
914914
"code": None,
@@ -955,7 +955,7 @@
955955
"deploymentType": "Tensorflow Serving on K8s",
956956
"modelId": "mosu30xm7q8vb0p",
957957
"modelUrl": "s3://ps-projects-development/prmr22ve0/ehla1kvbwzaco/model/",
958-
"name": "keton",
958+
"name": "some_name",
959959
"tag": None,
960960
"params": None,
961961
"code": None,
@@ -1002,7 +1002,7 @@
10021002
"deploymentType": "Tensorflow Serving on K8s",
10031003
"modelId": "mosu30xm7q8vb0p",
10041004
"modelUrl": "s3://ps-projects-development/prmr22ve0/ehla1kvbwzaco/model/",
1005-
"name": "keton",
1005+
"name": "some_name",
10061006
"tag": None,
10071007
"params": None,
10081008
"code": None,
@@ -1049,7 +1049,7 @@
10491049
"deploymentType": "Tensorflow Serving on K8s",
10501050
"modelId": "mosu30xm7q8vb0p",
10511051
"modelUrl": "s3://ps-projects-development/prmr22ve0/ehla1kvbwzaco/model/",
1052-
"name": "keton",
1052+
"name": "some_name",
10531053
"tag": None,
10541054
"params": None,
10551055
"code": None,

tests/functional/test_deployments.py

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ class TestDeploymentsList(object):
7171
LIST_WITH_FILTER_REQUEST_JSON = {"filter": {"where": {"and": [{"state": "Stopped"}]}}}
7272
LIST_WITH_FILTER_RESPONSE_JSON_WHEN_NO_DEPLOYMENTS_FOUND = {"deploymentList": [], "total": 17, "displayTotal": 0,
7373
"runningTotal": 0}
74-
DETAILS_STDOUT = """+-------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
75-
| Name | ID | Endpoint | Api Type | Deployment Type |
76-
+-------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
77-
| keton | dev61ity7lx232 | https://development-services.paperspace.io/model-serving/dev61ity7lx232:predict | some_api_type | Tensorflow Serving on K8s |
78-
| keton | desanw1jptk7woh | https://development-services.paperspace.io/model-serving/desanw1jptk7woh:predict | REST | Tensorflow Serving on K8s |
79-
| keton | desfnnrqt1v633v | https://development-services.paperspace.io/model-serving/desfnnrqt1v633v:predict | REST | Tensorflow Serving on K8s |
80-
| keton | desdyn55d2e02su | https://development-services.paperspace.io/model-serving/desdyn55d2e02su:predict | REST | Tensorflow Serving on K8s |
81-
| keton | des3tmqa3s627o9 | https://development-services.paperspace.io/model-serving/des3tmqa3s627o9:predict | REST | Tensorflow Serving on K8s |
82-
+-------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
74+
DETAILS_STDOUT = """+-----------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
75+
| Name | ID | Endpoint | Api Type | Deployment Type |
76+
+-----------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
77+
| some_name | dev61ity7lx232 | https://development-services.paperspace.io/model-serving/dev61ity7lx232:predict | some_api_type | Tensorflow Serving on K8s |
78+
| some_name | desanw1jptk7woh | https://development-services.paperspace.io/model-serving/desanw1jptk7woh:predict | REST | Tensorflow Serving on K8s |
79+
| some_name | desfnnrqt1v633v | https://development-services.paperspace.io/model-serving/desfnnrqt1v633v:predict | REST | Tensorflow Serving on K8s |
80+
| some_name | desdyn55d2e02su | https://development-services.paperspace.io/model-serving/desdyn55d2e02su:predict | REST | Tensorflow Serving on K8s |
81+
| some_name | des3tmqa3s627o9 | https://development-services.paperspace.io/model-serving/des3tmqa3s627o9:predict | REST | Tensorflow Serving on K8s |
82+
+-----------+-----------------+----------------------------------------------------------------------------------+---------------+---------------------------+
8383
"""
8484

8585
@mock.patch("paperspace.cli.deployments_commands.client.requests.get")
@@ -127,3 +127,109 @@ def test_should_send_get_request_and_print_list_of_deployments_filtered_with_sta
127127
result = runner.invoke(cli.cli, self.COMMAND_WITH_FILTER_WITH_STATE)
128128

129129
assert result.output == "No deployments found\n"
130+
131+
132+
class TestDeploymentsUpdate(object):
133+
URL = "https://api.paperspace.io/deployments/updateDeployment/"
134+
COMMAND = [
135+
"deployments", "update",
136+
"--id", "some_id",
137+
"--name", "new_name",
138+
]
139+
BASIC_OPTIONS_REQUEST_JSON = {"upd": {"name": u"new_name"}, "id": u"some_id"}
140+
141+
RESPONSE_JSON_200 = {"upd": {"name": u"asd"}, "id": u"dennaw0wzbvvg3"}
142+
EXPECTED_STDOUT = "Deployment model updated.\n"
143+
144+
RESPONSE_JSON_400 = {"error": {"name": "Error", "status": 400, "message": "Unable to access deployment"}}
145+
EXPECTED_STDOUT_WITH_WRONG_ID = "Unable to access deployment\n"
146+
147+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
148+
def test_should_send_proper_data_and_print_message_when_update_deployment(self, post_patched):
149+
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, "fake content")
150+
151+
runner = CliRunner()
152+
result = runner.invoke(cli.cli, self.COMMAND)
153+
154+
post_patched.assert_called_once_with(self.URL,
155+
headers=EXPECTED_HEADERS,
156+
json=self.BASIC_OPTIONS_REQUEST_JSON,
157+
params=None)
158+
assert result.output == self.EXPECTED_STDOUT
159+
assert result.exit_code == 0
160+
161+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
162+
def test_should_send_proper_data_and_print_message_when_update_deployment_used_with_wrong_id(self, post_patched):
163+
post_patched.return_value = MockResponse(self.RESPONSE_JSON_400, 400, "fake content")
164+
165+
runner = CliRunner()
166+
result = runner.invoke(cli.cli, self.COMMAND)
167+
168+
post_patched.assert_called_once_with(self.URL,
169+
headers=EXPECTED_HEADERS,
170+
json=self.BASIC_OPTIONS_REQUEST_JSON,
171+
params=None)
172+
assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_ID
173+
assert result.exit_code == 0
174+
175+
176+
class TestStartDeployment(object):
177+
URL = "https://api.paperspace.io/deployments/updateDeployment/"
178+
COMMAND = ["deployments", "start",
179+
"--id", "some_id"]
180+
REQUEST_JSON = {"isRunning": True, "id": u"some_id"}
181+
EXPECTED_STDOUT = "Deployment started\n"
182+
183+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
184+
def test_should_send_proper_data_and_print_message_when_deployments_start_was_used(self, post_patched):
185+
post_patched.return_value = MockResponse(None, 204, "fake content")
186+
187+
runner = CliRunner()
188+
result = runner.invoke(cli.cli, self.COMMAND)
189+
190+
post_patched.assert_called_once_with(self.URL,
191+
headers=EXPECTED_HEADERS,
192+
json=self.REQUEST_JSON,
193+
params=None)
194+
assert result.output == self.EXPECTED_STDOUT
195+
assert result.exit_code == 0
196+
197+
198+
class TestDeleteDeployment(object):
199+
URL = "https://api.paperspace.io/deployments/updateDeployment/"
200+
COMMAND = ["deployments", "delete",
201+
"--id", "some_id"]
202+
203+
REQUEST_JSON = {"upd": {"isDeleted": True}, "id": u"some_id"}
204+
EXPECTED_STDOUT = "Deployment deleted.\n"
205+
206+
RESPONSE_JSON_400 = {"error": {"name": "Error", "status": 400, "message": "Unable to access deployment"}}
207+
EXPECTED_STDOUT_WITH_WRONG_ID = "Unable to access deployment\n"
208+
209+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
210+
def test_should_send_proper_data_and_print_message_when_deployments_delete_was_used(self, post_patched):
211+
post_patched.return_value = MockResponse(None, 204, "fake content")
212+
213+
runner = CliRunner()
214+
result = runner.invoke(cli.cli, self.COMMAND)
215+
216+
post_patched.assert_called_once_with(self.URL,
217+
headers=EXPECTED_HEADERS,
218+
json=self.REQUEST_JSON,
219+
params=None)
220+
assert result.output == self.EXPECTED_STDOUT
221+
assert result.exit_code == 0
222+
223+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
224+
def test_should_send_proper_data_and_print_message_when_deployments_delete_used_with_wrong_id(self, post_patched):
225+
post_patched.return_value = MockResponse(self.RESPONSE_JSON_400, 400, "fake content")
226+
227+
runner = CliRunner()
228+
result = runner.invoke(cli.cli, self.COMMAND)
229+
230+
post_patched.assert_called_once_with(self.URL,
231+
headers=EXPECTED_HEADERS,
232+
json=self.REQUEST_JSON,
233+
params=None)
234+
assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_ID
235+
assert result.exit_code == 0

0 commit comments

Comments
 (0)