1414
1515from unittest .mock import call
1616
17+ from openstack .block_storage .v2 import consistency_group as _consistency_group
18+ from openstack .block_storage .v2 import (
19+ consistency_group_snapshot as _cg_snapshot ,
20+ )
21+ from openstack .test import fakes as sdk_fakes
22+ from osc_lib import exceptions
23+
1724from openstackclient .tests .unit .volume .v2 import fakes as volume_fakes
1825from openstackclient .volume .v2 import consistency_group_snapshot
1926
2027
21- class TestConsistencyGroupSnapshot (volume_fakes .TestVolume ):
22- def setUp (self ):
23- super ().setUp ()
24-
25- # Get a shortcut to the TransferManager Mock
26- self .cgsnapshots_mock = self .volume_client .cgsnapshots
27- self .cgsnapshots_mock .reset_mock ()
28- self .consistencygroups_mock = self .volume_client .consistencygroups
29- self .consistencygroups_mock .reset_mock ()
30-
31-
32- class TestConsistencyGroupSnapshotCreate (TestConsistencyGroupSnapshot ):
33- _consistency_group_snapshot = (
34- volume_fakes .create_one_consistency_group_snapshot ()
28+ class TestConsistencyGroupSnapshotCreate (volume_fakes .TestVolume ):
29+ _consistency_group_snapshot = sdk_fakes .generate_fake_resource (
30+ _cg_snapshot .ConsistencyGroupSnapshot
31+ )
32+ consistency_group = sdk_fakes .generate_fake_resource (
33+ _consistency_group .ConsistencyGroup
3534 )
36- consistency_group = volume_fakes .create_one_consistency_group ()
3735
3836 columns = (
3937 'consistencygroup_id' ,
@@ -54,10 +52,10 @@ class TestConsistencyGroupSnapshotCreate(TestConsistencyGroupSnapshot):
5452
5553 def setUp (self ):
5654 super ().setUp ()
57- self .cgsnapshots_mock .create .return_value = (
58- self ._consistency_group_snapshot
55+ self .volume_sdk_client .create_consistency_group_snapshot .return_value = self ._consistency_group_snapshot
56+ self .volume_sdk_client .find_consistency_group .return_value = (
57+ self .consistency_group
5958 )
60- self .consistencygroups_mock .get .return_value = self .consistency_group
6159
6260 # Get the command object to test
6361 self .cmd = consistency_group_snapshot .CreateConsistencyGroupSnapshot (
@@ -81,11 +79,11 @@ def test_consistency_group_snapshot_create(self):
8179
8280 columns , data = self .cmd .take_action (parsed_args )
8381
84- self .consistencygroups_mock . get .assert_called_once_with (
85- self .consistency_group .id
82+ self .volume_sdk_client . find_consistency_group .assert_called_once_with (
83+ self .consistency_group .id , ignore_missing = False
8684 )
87- self .cgsnapshots_mock . create .assert_called_once_with (
88- self .consistency_group .id ,
85+ self .volume_sdk_client . create_consistency_group_snapshot .assert_called_once_with (
86+ consistencygroup_id = self .consistency_group .id ,
8987 name = self ._consistency_group_snapshot .name ,
9088 description = self ._consistency_group_snapshot .description ,
9189 )
@@ -107,11 +105,11 @@ def test_consistency_group_snapshot_create_no_consistency_group(self):
107105
108106 columns , data = self .cmd .take_action (parsed_args )
109107
110- self .consistencygroups_mock . get .assert_called_once_with (
111- self ._consistency_group_snapshot .name
108+ self .volume_sdk_client . find_consistency_group .assert_called_once_with (
109+ self ._consistency_group_snapshot .name , ignore_missing = False
112110 )
113- self .cgsnapshots_mock . create .assert_called_once_with (
114- self .consistency_group .id ,
111+ self .volume_sdk_client . create_consistency_group_snapshot .assert_called_once_with (
112+ consistencygroup_id = self .consistency_group .id ,
115113 name = self ._consistency_group_snapshot .name ,
116114 description = self ._consistency_group_snapshot .description ,
117115 )
@@ -120,20 +118,23 @@ def test_consistency_group_snapshot_create_no_consistency_group(self):
120118 self .assertEqual (self .data , data )
121119
122120
123- class TestConsistencyGroupSnapshotDelete (TestConsistencyGroupSnapshot ):
124- consistency_group_snapshots = (
125- volume_fakes .create_consistency_group_snapshots (count = 2 )
126- )
121+ class TestConsistencyGroupSnapshotDelete (volume_fakes .TestVolume ):
122+ consistency_group_snapshots = [
123+ sdk_fakes .generate_fake_resource (
124+ _cg_snapshot .ConsistencyGroupSnapshot
125+ ),
126+ sdk_fakes .generate_fake_resource (
127+ _cg_snapshot .ConsistencyGroupSnapshot
128+ ),
129+ ]
127130
128131 def setUp (self ):
129132 super ().setUp ()
130133
131- self .cgsnapshots_mock .get = (
132- volume_fakes .get_consistency_group_snapshots (
133- self .consistency_group_snapshots
134- )
134+ self .volume_sdk_client .find_consistency_group_snapshot .side_effect = (
135+ self .consistency_group_snapshots
135136 )
136- self .cgsnapshots_mock . delete .return_value = None
137+ self .volume_sdk_client . delete_consistency_group_snapshot .return_value = None
137138
138139 # Get the command object to mock
139140 self .cmd = consistency_group_snapshot .DeleteConsistencyGroupSnapshot (
@@ -152,8 +153,11 @@ def test_consistency_group_snapshot_delete(self):
152153
153154 result = self .cmd .take_action (parsed_args )
154155
155- self .cgsnapshots_mock .delete .assert_called_once_with (
156- self .consistency_group_snapshots [0 ].id
156+ self .volume_sdk_client .find_consistency_group_snapshot .assert_called_once_with (
157+ self .consistency_group_snapshots [0 ].id , ignore_missing = False
158+ )
159+ self .volume_sdk_client .delete_consistency_group_snapshot .assert_called_once_with (
160+ self .consistency_group_snapshots [0 ]
157161 )
158162 self .assertIsNone (result )
159163
@@ -168,25 +172,52 @@ def test_multiple_consistency_group_snapshots_delete(self):
168172 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
169173 result = self .cmd .take_action (parsed_args )
170174
171- calls = []
175+ find_calls = []
176+ delete_calls = []
172177 for c in self .consistency_group_snapshots :
173- calls .append (call (c .id ))
174- self .cgsnapshots_mock .delete .assert_has_calls (calls )
178+ find_calls .append (call (c .id , ignore_missing = False ))
179+ delete_calls .append (call (c ))
180+ self .volume_sdk_client .find_consistency_group_snapshot .assert_has_calls (
181+ find_calls
182+ )
183+ self .volume_sdk_client .delete_consistency_group_snapshot .assert_has_calls (
184+ delete_calls
185+ )
175186 self .assertIsNone (result )
176187
188+ def test_delete_with_exception (self ):
189+ arglist = ['missing-snapshot' ]
190+ verifylist = [('consistency_group_snapshot' , ['missing-snapshot' ])]
177191
178- class TestConsistencyGroupSnapshotList (TestConsistencyGroupSnapshot ):
179- consistency_group_snapshots = (
180- volume_fakes .create_consistency_group_snapshots (count = 2 )
192+ self .volume_sdk_client .find_consistency_group_snapshot .side_effect = (
193+ exceptions .CommandError
194+ )
195+
196+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
197+ self .assertRaises (
198+ exceptions .CommandError , self .cmd .take_action , parsed_args
199+ )
200+
201+
202+ class TestConsistencyGroupSnapshotList (volume_fakes .TestVolume ):
203+ consistency_group_snapshots = [
204+ sdk_fakes .generate_fake_resource (
205+ _cg_snapshot .ConsistencyGroupSnapshot , status = 'available'
206+ ),
207+ sdk_fakes .generate_fake_resource (
208+ _cg_snapshot .ConsistencyGroupSnapshot , status = 'available'
209+ ),
210+ ]
211+ consistency_group = sdk_fakes .generate_fake_resource (
212+ _consistency_group .ConsistencyGroup
181213 )
182- consistency_group = volume_fakes .create_one_consistency_group ()
183214
184- columns = [
215+ column_headers = [
185216 'ID' ,
186217 'Status' ,
187218 'Name' ,
188219 ]
189- columns_long = [
220+ column_headers_long = [
190221 'ID' ,
191222 'Status' ,
192223 'ConsistencyGroup ID' ,
@@ -219,10 +250,12 @@ class TestConsistencyGroupSnapshotList(TestConsistencyGroupSnapshot):
219250 def setUp (self ):
220251 super ().setUp ()
221252
222- self .cgsnapshots_mock . list .return_value = (
253+ self .volume_sdk_client . consistency_group_snapshots .return_value = (
223254 self .consistency_group_snapshots
224255 )
225- self .consistencygroups_mock .get .return_value = self .consistency_group
256+ self .volume_sdk_client .find_consistency_group .return_value = (
257+ self .consistency_group
258+ )
226259 # Get the command to test
227260 self .cmd = consistency_group_snapshot .ListConsistencyGroupSnapshot (
228261 self .app , None
@@ -240,15 +273,12 @@ def test_consistency_group_snapshot_list_without_options(self):
240273 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
241274 columns , data = self .cmd .take_action (parsed_args )
242275
243- search_opts = {
244- 'all_tenants' : False ,
245- 'status' : None ,
246- 'consistencygroup_id' : None ,
247- }
248- self .cgsnapshots_mock .list .assert_called_once_with (
249- detailed = True , search_opts = search_opts
276+ self .volume_sdk_client .consistency_group_snapshots .assert_called_once_with (
277+ all_tenants = False ,
278+ status = None ,
279+ consistencygroup_id = None ,
250280 )
251- self .assertEqual (self .columns , columns )
281+ self .assertEqual (self .column_headers , columns )
252282 self .assertEqual (self .data , list (data ))
253283
254284 def test_consistency_group_snapshot_list_with_long (self ):
@@ -265,15 +295,12 @@ def test_consistency_group_snapshot_list_with_long(self):
265295 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
266296 columns , data = self .cmd .take_action (parsed_args )
267297
268- search_opts = {
269- 'all_tenants' : False ,
270- 'status' : None ,
271- 'consistencygroup_id' : None ,
272- }
273- self .cgsnapshots_mock .list .assert_called_once_with (
274- detailed = True , search_opts = search_opts
298+ self .volume_sdk_client .consistency_group_snapshots .assert_called_once_with (
299+ all_tenants = False ,
300+ status = None ,
301+ consistencygroup_id = None ,
275302 )
276- self .assertEqual (self .columns_long , columns )
303+ self .assertEqual (self .column_headers_long , columns )
277304 self .assertEqual (self .data_long , list (data ))
278305
279306 def test_consistency_group_snapshot_list_with_options (self ):
@@ -294,24 +321,21 @@ def test_consistency_group_snapshot_list_with_options(self):
294321 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
295322 columns , data = self .cmd .take_action (parsed_args )
296323
297- search_opts = {
298- 'all_tenants' : True ,
299- 'status' : self .consistency_group_snapshots [0 ].status ,
300- 'consistencygroup_id' : self .consistency_group .id ,
301- }
302- self .consistencygroups_mock .get .assert_called_once_with (
303- self .consistency_group .id
324+ self .volume_sdk_client .find_consistency_group .assert_called_once_with (
325+ self .consistency_group .id , ignore_missing = False
304326 )
305- self .cgsnapshots_mock .list .assert_called_once_with (
306- detailed = True , search_opts = search_opts
327+ self .volume_sdk_client .consistency_group_snapshots .assert_called_once_with (
328+ all_tenants = True ,
329+ status = self .consistency_group_snapshots [0 ].status ,
330+ consistencygroup_id = self .consistency_group .id ,
307331 )
308- self .assertEqual (self .columns , columns )
332+ self .assertEqual (self .column_headers , columns )
309333 self .assertEqual (self .data , list (data ))
310334
311335
312- class TestConsistencyGroupSnapshotShow (TestConsistencyGroupSnapshot ):
313- _consistency_group_snapshot = (
314- volume_fakes . create_one_consistency_group_snapshot ()
336+ class TestConsistencyGroupSnapshotShow (volume_fakes . TestVolume ):
337+ _consistency_group_snapshot = sdk_fakes . generate_fake_resource (
338+ _cg_snapshot . ConsistencyGroupSnapshot
315339 )
316340
317341 columns = (
@@ -334,7 +358,7 @@ class TestConsistencyGroupSnapshotShow(TestConsistencyGroupSnapshot):
334358 def setUp (self ):
335359 super ().setUp ()
336360
337- self .cgsnapshots_mock . get .return_value = (
361+ self .volume_sdk_client . find_consistency_group_snapshot .return_value = (
338362 self ._consistency_group_snapshot
339363 )
340364 self .cmd = consistency_group_snapshot .ShowConsistencyGroupSnapshot (
@@ -348,8 +372,8 @@ def test_consistency_group_snapshot_show(self):
348372 ]
349373 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
350374 columns , data = self .cmd .take_action (parsed_args )
351- self .cgsnapshots_mock . get .assert_called_once_with (
352- self ._consistency_group_snapshot .id
375+ self .volume_sdk_client . find_consistency_group_snapshot .assert_called_once_with (
376+ self ._consistency_group_snapshot .id , ignore_missing = False
353377 )
354378 self .assertEqual (self .columns , columns )
355379 self .assertEqual (self .data , data )
0 commit comments