1313
1414from unittest import mock
1515
16+ from openstack .block_storage .v2 import volume as _volume
17+ from openstack import exceptions as sdk_exceptions
18+ from openstack .test import fakes as sdk_fakes
1619from osc_lib .cli import format_columns
1720from osc_lib import exceptions
1821from osc_lib import utils
@@ -46,12 +49,6 @@ def setUp(self):
4649 self .consistencygroups_mock = self .volume_client .consistencygroups
4750 self .consistencygroups_mock .reset_mock ()
4851
49- def setup_volumes_mock (self , count ):
50- volumes = volume_fakes .create_volumes (count = count )
51-
52- self .volumes_mock .get = volume_fakes .get_volumes (volumes , 0 )
53- return volumes
54-
5552
5653class TestVolumeCreate (TestVolume ):
5754 project = identity_fakes .FakeProject .create_one_project ()
@@ -662,37 +659,37 @@ def test_volume_create_hints(self):
662659 self .assertCountEqual (self .datalist , data )
663660
664661
665- class TestVolumeDelete (TestVolume ):
662+ class TestVolumeDelete (volume_fakes . TestVolume ):
666663 def setUp (self ):
667664 super ().setUp ()
668665
669- self .volumes_mock .delete .return_value = None
666+ self .volumes = list (sdk_fakes .generate_fake_resources (_volume .Volume ))
667+ self .volume_sdk_client .find_volume .side_effect = self .volumes
668+ self .volume_sdk_client .delete_volume .return_value = None
670669
671- # Get the command object to mock
672670 self .cmd = volume .DeleteVolume (self .app , None )
673671
674672 def test_volume_delete_one_volume (self ):
675- volumes = self .setup_volumes_mock (count = 1 )
676-
677- arglist = [volumes [0 ].id ]
673+ arglist = [self .volumes [0 ].id ]
678674 verifylist = [
679675 ("force" , False ),
680676 ("purge" , False ),
681- ("volumes" , [volumes [0 ].id ]),
677+ ("volumes" , [self . volumes [0 ].id ]),
682678 ]
683679 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
684680
685681 result = self .cmd .take_action (parsed_args )
682+ self .assertIsNone (result )
686683
687- self .volumes_mock .delete .assert_called_once_with (
688- volumes [0 ].id , cascade = False
684+ self .volume_sdk_client .find_volume .assert_called_once_with (
685+ self .volumes [0 ].id , ignore_missing = False
686+ )
687+ self .volume_sdk_client .delete_volume .assert_called_once_with (
688+ self .volumes [0 ].id , cascade = False , force = False
689689 )
690- self .assertIsNone (result )
691690
692691 def test_volume_delete_multi_volumes (self ):
693- volumes = self .setup_volumes_mock (count = 3 )
694-
695- arglist = [v .id for v in volumes ]
692+ arglist = [v .id for v in self .volumes ]
696693 verifylist = [
697694 ('force' , False ),
698695 ('purge' , False ),
@@ -701,83 +698,95 @@ def test_volume_delete_multi_volumes(self):
701698 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
702699
703700 result = self .cmd .take_action (parsed_args )
704-
705- calls = [mock .call (v .id , cascade = False ) for v in volumes ]
706- self .volumes_mock .delete .assert_has_calls (calls )
707701 self .assertIsNone (result )
708702
703+ self .volume_sdk_client .find_volume .assert_has_calls (
704+ [mock .call (v .id , ignore_missing = False ) for v in self .volumes ]
705+ )
706+ self .volume_sdk_client .delete_volume .assert_has_calls (
707+ [mock .call (v .id , cascade = False , force = False ) for v in self .volumes ]
708+ )
709+
709710 def test_volume_delete_multi_volumes_with_exception (self ):
710- volumes = self .setup_volumes_mock (count = 2 )
711+ self .volume_sdk_client .find_volume .side_effect = [
712+ self .volumes [0 ],
713+ sdk_exceptions .NotFoundException (),
714+ ]
711715
712716 arglist = [
713- volumes [0 ].id ,
717+ self . volumes [0 ].id ,
714718 'unexist_volume' ,
715719 ]
716720 verifylist = [
717721 ('force' , False ),
718722 ('purge' , False ),
719- ('volumes' , arglist ),
723+ ('volumes' , [ self . volumes [ 0 ]. id , 'unexist_volume' ] ),
720724 ]
721725 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
722726
723- find_mock_result = [volumes [0 ], exceptions .CommandError ]
724- with mock .patch .object (
725- utils , 'find_resource' , side_effect = find_mock_result
726- ) as find_mock :
727- try :
728- self .cmd .take_action (parsed_args )
729- self .fail ('CommandError should be raised.' )
730- except exceptions .CommandError as e :
731- self .assertEqual ('1 of 2 volumes failed to delete.' , str (e ))
732-
733- find_mock .assert_any_call (self .volumes_mock , volumes [0 ].id )
734- find_mock .assert_any_call (self .volumes_mock , 'unexist_volume' )
735-
736- self .assertEqual (2 , find_mock .call_count )
737- self .volumes_mock .delete .assert_called_once_with (
738- volumes [0 ].id , cascade = False
739- )
727+ exc = self .assertRaises (
728+ exceptions .CommandError ,
729+ self .cmd .take_action ,
730+ parsed_args ,
731+ )
732+ self .assertEqual ('1 of 2 volumes failed to delete.' , str (exc ))
740733
741- def test_volume_delete_with_purge (self ):
742- volumes = self .setup_volumes_mock (count = 1 )
734+ self .volume_sdk_client .find_volume .assert_has_calls (
735+ [
736+ mock .call (self .volumes [0 ].id , ignore_missing = False ),
737+ mock .call ('unexist_volume' , ignore_missing = False ),
738+ ]
739+ )
740+ self .volume_sdk_client .delete_volume .assert_has_calls (
741+ [
742+ mock .call (self .volumes [0 ].id , cascade = False , force = False ),
743+ ]
744+ )
743745
746+ def test_volume_delete_with_purge (self ):
744747 arglist = [
745748 '--purge' ,
746- volumes [0 ].id ,
749+ self . volumes [0 ].id ,
747750 ]
748751 verifylist = [
749752 ('force' , False ),
750753 ('purge' , True ),
751- ('volumes' , [volumes [0 ].id ]),
754+ ('volumes' , [self . volumes [0 ].id ]),
752755 ]
753756 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
754757
755758 result = self .cmd .take_action (parsed_args )
759+ self .assertIsNone (result )
756760
757- self .volumes_mock .delete .assert_called_once_with (
758- volumes [0 ].id , cascade = True
761+ self .volume_sdk_client .find_volume .assert_called_once_with (
762+ self .volumes [0 ].id , ignore_missing = False
763+ )
764+ self .volume_sdk_client .delete_volume .assert_called_once_with (
765+ self .volumes [0 ].id , cascade = True , force = False
759766 )
760- self .assertIsNone (result )
761767
762768 def test_volume_delete_with_force (self ):
763- volumes = self .setup_volumes_mock (count = 1 )
764-
765769 arglist = [
766770 '--force' ,
767- volumes [0 ].id ,
771+ self . volumes [0 ].id ,
768772 ]
769773 verifylist = [
770774 ('force' , True ),
771775 ('purge' , False ),
772- ('volumes' , [volumes [0 ].id ]),
776+ ('volumes' , [self . volumes [0 ].id ]),
773777 ]
774778 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
775779
776780 result = self .cmd .take_action (parsed_args )
777-
778- self .volumes_mock .force_delete .assert_called_once_with (volumes [0 ].id )
779781 self .assertIsNone (result )
780782
783+ self .volume_sdk_client .find_volume .assert_called_once_with (
784+ self .volumes [0 ].id , ignore_missing = False
785+ )
786+ self .volume_sdk_client .delete_volume .assert_called_once_with (
787+ self .volumes [0 ].id , cascade = False , force = True
788+ )
789+
781790
782791class TestVolumeList (TestVolume ):
783792 project = identity_fakes .FakeProject .create_one_project ()
0 commit comments