|
| 1 | +import json |
1 | 2 | import pytest |
2 | 3 | import responses # https://github.com/getsentry/responses |
3 | 4 |
|
4 | 5 | from verda.constants import Actions, ErrorCodes, Locations |
5 | 6 | from verda.exceptions import APIException |
6 | | -from verda.instances import Instance, InstancesService |
| 7 | +from verda.instances import Instance, InstancesService, OSVolume |
7 | 8 |
|
8 | 9 | INVALID_REQUEST = ErrorCodes.INVALID_REQUEST |
9 | 10 | INVALID_REQUEST_MESSAGE = 'Your existence is invalid' |
@@ -266,6 +267,31 @@ def test_create_spot_instance_successful(self, instances_service, endpoint): |
266 | 267 | assert responses.assert_call_count(endpoint, 1) is True |
267 | 268 | assert responses.assert_call_count(url, 1) is True |
268 | 269 |
|
| 270 | + def test_create_spot_instance(self, instances_service, endpoint): |
| 271 | + # arrange |
| 272 | + responses.add(responses.POST, endpoint, body=INSTANCE_ID, status=200) |
| 273 | + url = endpoint + '/' + INSTANCE_ID |
| 274 | + responses.add(responses.GET, url, json=PAYLOAD[0], status=200) |
| 275 | + |
| 276 | + SPOT_INSTANCE_OS_VOLUME = OSVolume(name='spot-instance-os-volume', size=50, on_spot_discontinue='delete_permanently') |
| 277 | + |
| 278 | + # act |
| 279 | + instances_service.create( |
| 280 | + instance_type=INSTANCE_TYPE, |
| 281 | + image=INSTANCE_IMAGE, |
| 282 | + ssh_key_ids=[SSH_KEY_ID], |
| 283 | + hostname=INSTANCE_HOSTNAME, |
| 284 | + description=INSTANCE_DESCRIPTION, |
| 285 | + os_volume=SPOT_INSTANCE_OS_VOLUME, |
| 286 | + ) |
| 287 | + |
| 288 | + # assert |
| 289 | + request_body = responses.calls[0].request.body.decode('utf-8') |
| 290 | + body = json.loads(request_body) |
| 291 | + assert body['os_volume']['name'] == SPOT_INSTANCE_OS_VOLUME.name |
| 292 | + assert body['os_volume']['size'] == SPOT_INSTANCE_OS_VOLUME.size |
| 293 | + assert body['os_volume']['on_spot_discontinue'] == 'delete_permanently' |
| 294 | + |
269 | 295 | def test_create_instance_attached_os_volume_successful(self, instances_service, endpoint): |
270 | 296 | # arrange - add response mock |
271 | 297 | # create instance |
@@ -340,6 +366,28 @@ def test_action_successful(self, instances_service, endpoint): |
340 | 366 | assert result is None |
341 | 367 | assert responses.assert_call_count(url, 1) is True |
342 | 368 |
|
| 369 | + def test_action_with_delete_permanently_sends_payload(self, instances_service, endpoint): |
| 370 | + # arrange |
| 371 | + url = endpoint |
| 372 | + responses.add(responses.PUT, url, status=202) |
| 373 | + volume_ids = [OS_VOLUME_ID] |
| 374 | + |
| 375 | + # act |
| 376 | + instances_service.action( |
| 377 | + id_list=[INSTANCE_ID], |
| 378 | + action=Actions.DELETE, |
| 379 | + volume_ids=volume_ids, |
| 380 | + delete_permanently=True, |
| 381 | + ) |
| 382 | + |
| 383 | + # assert |
| 384 | + request_body = responses.calls[0].request.body.decode('utf-8') |
| 385 | + body = json.loads(request_body) |
| 386 | + assert body['id'] == [INSTANCE_ID] |
| 387 | + assert body['action'] == Actions.DELETE |
| 388 | + assert body['volume_ids'] == volume_ids |
| 389 | + assert body['delete_permanently'] is True |
| 390 | + |
343 | 391 | def test_action_failed(self, instances_service, endpoint): |
344 | 392 | # arrange - add response mock |
345 | 393 | url = endpoint |
|
0 commit comments