File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -197,9 +197,6 @@ multiple ``delete_network()`` calls.
197197
198198 if ret > 0 :
199199 total = len (parsed_args.network)
200- msg = _(" Failed to delete %(ret)s of %(total)s networks." ) %
201- {
202- " ret" : ret,
203- " total" : total,
204- }
200+ msg = (_(" Failed to delete %(ret)s of %(total)s networks." )
201+ % {" ret" : ret, " total" : total})
205202 raise exceptions.CommandError(msg)
Original file line number Diff line number Diff line change @@ -372,13 +372,27 @@ def get_parser(self, prog_name):
372372 return parser
373373
374374 def take_action (self , parsed_args ):
375+
376+ del_result = 0
375377 image_client = self .app .client_manager .image
376378 for image in parsed_args .images :
377- image_obj = utils .find_resource (
378- image_client .images ,
379- image ,
380- )
381- image_client .images .delete (image_obj .id )
379+ try :
380+ image_obj = utils .find_resource (
381+ image_client .images ,
382+ image ,
383+ )
384+ image_client .images .delete (image_obj .id )
385+ except Exception as e :
386+ del_result += 1
387+ self .app .log .error (_ ("Failed to delete image with "
388+ "name or ID '%(image)s': %(e)s" )
389+ % {'image' : image , 'e' : e })
390+
391+ total = len (parsed_args .images )
392+ if (del_result > 0 ):
393+ msg = (_ ("Failed to delete %(dresult)s of %(total)s images." )
394+ % {'dresult' : del_result , 'total' : total })
395+ raise exceptions .CommandError (msg )
382396
383397
384398class ListImage (command .Lister ):
Original file line number Diff line number Diff line change @@ -473,6 +473,37 @@ def test_image_delete_multi_images(self):
473473 self .images_mock .delete .assert_has_calls (calls )
474474 self .assertIsNone (result )
475475
476+ def test_image_delete_multi_images_exception (self ):
477+
478+ images = image_fakes .FakeImage .create_images (count = 2 )
479+ arglist = [
480+ images [0 ].id ,
481+ images [1 ].id ,
482+ 'x-y-x' ,
483+ ]
484+ verifylist = [
485+ ('images' , arglist )
486+ ]
487+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
488+
489+ # Fake exception in utils.find_resource()
490+ # In image v2, we use utils.find_resource() to find a network.
491+ # It calls get() several times, but find() only one time. So we
492+ # choose to fake get() always raise exception, then pass through.
493+ # And fake find() to find the real network or not.
494+ ret_find = [
495+ images [0 ],
496+ images [1 ],
497+ exceptions .NotFound ('404' ),
498+ ]
499+
500+ self .images_mock .get = Exception ()
501+ self .images_mock .find .side_effect = ret_find
502+ self .assertRaises (exceptions .CommandError , self .cmd .take_action ,
503+ parsed_args )
504+ calls = [mock .call (i .id ) for i in images ]
505+ self .images_mock .delete .assert_has_calls (calls )
506+
476507
477508class TestImageList (TestImage ):
478509
You can’t perform that action at this time.
0 commit comments