2929HDD_VOL_SIZE = 100
3030HDD_VOL_CREATED_AT = "2021-06-02T12:56:49.582Z"
3131
32+ RANDOM_VOL_ID = '07d864ee-ba86-451e-85b3-34ef551bd4a2'
33+ RANDOM_VOL2_ID = '72c5c082-7fe7-4d13-bd9e-f529c97d63b3'
34+
3235NVME_VOLUME = {
3336 "id" : NVME_VOL_ID ,
3437 "status" : NVME_VOL_STATUS ,
@@ -69,7 +72,23 @@ def volumes_service(self, http_client):
6972 def endpoint (self , http_client ):
7073 return http_client ._base_url + "/volumes"
7174
72- def test_get_instances (self , volumes_service , endpoint ):
75+ def test_initialize_a_volume (self ):
76+ volume = Volume (RANDOM_VOL_ID , VolumeStatus .DETACHED , HDD_VOL_NAME , HDD_VOL_SIZE ,
77+ HDD , False , HDD_VOL_CREATED_AT )
78+
79+ assert volume .id == RANDOM_VOL_ID
80+ assert volume .status == VolumeStatus .DETACHED
81+ assert volume .instance_id == None
82+ assert volume .name == HDD_VOL_NAME
83+ assert volume .size == HDD_VOL_SIZE
84+ assert volume .type == HDD
85+ assert volume .location == FIN1
86+ assert volume .is_os_volume == False
87+ assert volume .created_at == HDD_VOL_CREATED_AT
88+ assert volume .target == None
89+ assert volume .ssh_key_ids == []
90+
91+ def test_get_volumes (self , volumes_service , endpoint ):
7392 # arrange - add response mock
7493 responses .add (
7594 responses .GET ,
@@ -219,7 +238,8 @@ def test_create_volume_successful(self, volumes_service, endpoint):
219238 )
220239
221240 # act
222- volume = volumes_service .create (VolumeTypes .NVMe , NVME_VOL_NAME , NVME_VOL_SIZE )
241+ volume = volumes_service .create (
242+ VolumeTypes .NVMe , NVME_VOL_NAME , NVME_VOL_SIZE )
223243
224244 # assert
225245 assert volume .id == NVME_VOL_ID
@@ -236,7 +256,8 @@ def test_create_volume_failed(self, volumes_service, endpoint):
236256
237257 # act
238258 with pytest .raises (APIException ) as excinfo :
239- volumes_service .create (VolumeTypes .NVMe , NVME_VOL_NAME , 100000000000000000000000 )
259+ volumes_service .create (
260+ VolumeTypes .NVMe , NVME_VOL_NAME , 100000000000000000000000 )
240261
241262 # assert
242263 assert excinfo .value .code == INVALID_REQUEST
@@ -481,3 +502,139 @@ def test_delete_volume_failed(self, volumes_service, endpoint):
481502 assert excinfo .value .code == INVALID_REQUEST
482503 assert excinfo .value .message == INVALID_REQUEST_MESSAGE
483504 assert responses .assert_call_count (endpoint , 1 ) is True
505+
506+ def test_clone_volume_with_input_name_successful (self , volumes_service , endpoint ):
507+ # arrange
508+ CLONED_VOLUME_NAME = "cloned-volume"
509+
510+ # mock response for cloning the volume
511+ responses .add (
512+ responses .PUT ,
513+ endpoint ,
514+ status = 202 ,
515+ json = [RANDOM_VOL_ID ],
516+ match = [
517+ responses .json_params_matcher ({
518+ "id" : NVME_VOL_ID ,
519+ "action" : VolumeActions .CLONE ,
520+ "name" : CLONED_VOLUME_NAME ,
521+ "type" : None
522+ })
523+ ]
524+ )
525+
526+ # mock object for the cloned volume
527+ CLONED_VOL_GET_MOCK = NVME_VOLUME
528+ CLONED_VOL_GET_MOCK ['id' ] = RANDOM_VOL_ID
529+ CLONED_VOL_GET_MOCK ['name' ] = CLONED_VOLUME_NAME
530+ CLONED_VOL_GET_MOCK ['status' ] = VolumeStatus .CLONING
531+
532+ # mock response for getting the cloned volume
533+ responses .add (
534+ responses .GET ,
535+ endpoint + "/" + RANDOM_VOL_ID ,
536+ status = 200 ,
537+ json = CLONED_VOL_GET_MOCK ,
538+ )
539+
540+ # act
541+ cloned_volume = volumes_service .clone (NVME_VOL_ID , CLONED_VOLUME_NAME )
542+
543+ # assert
544+ assert responses .assert_call_count (endpoint , 1 ) is True
545+ assert cloned_volume .name == CLONED_VOLUME_NAME
546+
547+ def test_clone_volume_without_input_name_successful (self , volumes_service : VolumesService , endpoint ):
548+ # arrange
549+ CLONED_VOLUME_NAME = "CLONE-" + NVME_VOL_NAME
550+
551+ # mock response for cloning the volume
552+ responses .add (
553+ responses .PUT ,
554+ endpoint ,
555+ status = 202 ,
556+ json = [RANDOM_VOL_ID ],
557+ match = [
558+ responses .json_params_matcher ({
559+ "id" : NVME_VOL_ID ,
560+ "action" : VolumeActions .CLONE ,
561+ "name" : None ,
562+ "type" : None
563+ })
564+ ]
565+ )
566+
567+ # mock object for the cloned volume
568+ CLONED_VOL_GET_MOCK = NVME_VOLUME
569+ CLONED_VOL_GET_MOCK ['id' ] = RANDOM_VOL_ID
570+ CLONED_VOL_GET_MOCK ['name' ] = CLONED_VOLUME_NAME
571+ CLONED_VOL_GET_MOCK ['status' ] = VolumeStatus .CLONING
572+
573+ # mock response for getting the cloned volume
574+ responses .add (
575+ responses .GET ,
576+ endpoint + "/" + RANDOM_VOL_ID ,
577+ status = 200 ,
578+ json = CLONED_VOL_GET_MOCK ,
579+ )
580+
581+ # act
582+ cloned_volume = volumes_service .clone (NVME_VOL_ID )
583+
584+ # assert
585+ assert responses .assert_call_count (endpoint , 1 ) is True
586+ assert cloned_volume .name == CLONED_VOLUME_NAME
587+
588+ def test_clone_two_volumes_successful (self , volumes_service : VolumesService , endpoint ):
589+ # arrange
590+ CLONED_VOL1_NAME = "CLONE-" + NVME_VOL_NAME
591+ CLONED_VOL2_NAME = "CLONE-" + HDD_VOL_NAME
592+
593+ # mock response for cloning the volumes
594+ responses .add (
595+ responses .PUT ,
596+ endpoint ,
597+ status = 202 ,
598+ json = [RANDOM_VOL_ID , RANDOM_VOL2_ID ],
599+ match = [
600+ responses .json_params_matcher ({
601+ "id" : [NVME_VOL_ID , HDD_VOL_ID ],
602+ "action" : VolumeActions .CLONE ,
603+ "name" : None ,
604+ "type" : None
605+ })
606+ ]
607+ )
608+
609+ # mock object for the cloned volumes
610+ CLONED_VOL1_GET_MOCK = NVME_VOLUME
611+ CLONED_VOL1_GET_MOCK ['id' ] = RANDOM_VOL_ID
612+ CLONED_VOL1_GET_MOCK ['name' ] = CLONED_VOL1_NAME
613+ CLONED_VOL1_GET_MOCK ['status' ] = VolumeStatus .CLONING
614+
615+ CLONED_VOL2_GET_MOCK = HDD_VOLUME
616+ CLONED_VOL2_GET_MOCK ['id' ] = RANDOM_VOL2_ID
617+ CLONED_VOL2_GET_MOCK ['name' ] = CLONED_VOL2_NAME
618+ CLONED_VOL2_GET_MOCK ['status' ] = VolumeStatus .CLONING
619+
620+ # mock response for getting the cloned volumes
621+ responses .add (
622+ responses .GET ,
623+ endpoint + "/" + RANDOM_VOL_ID ,
624+ status = 200 ,
625+ json = CLONED_VOL1_GET_MOCK ,
626+ )
627+ responses .add (
628+ responses .GET ,
629+ endpoint + "/" + RANDOM_VOL2_ID ,
630+ status = 200 ,
631+ json = CLONED_VOL2_GET_MOCK ,
632+ )
633+
634+ # act
635+ cloned_volume = volumes_service .clone ([NVME_VOL_ID , HDD_VOL_ID ])
636+
637+ # assert
638+ assert responses .assert_call_count (endpoint , 1 ) is True
639+ assert cloned_volume [0 ].name == CLONED_VOL1_NAME
640+ assert cloned_volume [1 ].name == CLONED_VOL2_NAME
0 commit comments