1313
1414from unittest .mock import call
1515
16+ from openstack .block_storage .v3 import backup as _backup
17+ from openstack import exceptions as sdk_exceptions
18+ from openstack .test import fakes as sdk_fakes
1619from osc_lib import exceptions
1720
1821from openstackclient .tests .unit .volume .v3 import fakes as volume_fakes
1922from openstackclient .volume .v3 import volume_backup
2023
2124
22- class TestBackupLegacy (volume_fakes .TestVolume ):
23- def setUp (self ):
24- super ().setUp ()
25-
26- self .backups_mock = self .volume_client .backups
27- self .backups_mock .reset_mock ()
28- self .volumes_mock = self .volume_client .volumes
29- self .volumes_mock .reset_mock ()
30- self .snapshots_mock = self .volume_client .volume_snapshots
31- self .snapshots_mock .reset_mock ()
32- self .restores_mock = self .volume_client .restores
33- self .restores_mock .reset_mock ()
34-
35-
3625class TestBackupCreate (volume_fakes .TestVolume ):
3726 volume = volume_fakes .create_one_volume ()
3827 snapshot = volume_fakes .create_one_snapshot ()
@@ -574,17 +563,15 @@ def test_backup_restore_with_volume_existing(self):
574563 )
575564
576565
577- class TestBackupSet (TestBackupLegacy ):
578- backup = volume_fakes .create_one_backup (
579- attrs = {'metadata' : {'wow' : 'cool' }},
580- )
581-
566+ class TestBackupSet (volume_fakes .TestVolume ):
582567 def setUp (self ):
583568 super ().setUp ()
584569
585- self .backups_mock .get .return_value = self .backup
570+ self .backup = sdk_fakes .generate_fake_resource (
571+ _backup .Backup , metadata = {'wow' : 'cool' }
572+ )
573+ self .volume_sdk_client .find_backup .return_value = self .backup
586574
587- # Get the command object to test
588575 self .cmd = volume_backup .SetVolumeBackup (self .app , None )
589576
590577 def test_backup_set_name (self ):
@@ -601,14 +588,16 @@ def test_backup_set_name(self):
601588 ]
602589 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
603590
604- # In base command class ShowOne in cliff, abstract method take_action()
605- # returns nothing
606591 result = self .cmd .take_action (parsed_args )
607- self .backups_mock .update .assert_called_once_with (
608- self .backup .id , ** {'name' : 'new_name' }
609- )
610592 self .assertIsNone (result )
611593
594+ self .volume_sdk_client .find_backup .assert_called_with (
595+ self .backup .id , ignore_missing = False
596+ )
597+ self .volume_sdk_client .update_backup .assert_called_once_with (
598+ self .backup , name = 'new_name'
599+ )
600+
612601 def test_backup_set_name_pre_v39 (self ):
613602 self .set_volume_api_version ('3.8' )
614603
@@ -644,13 +633,14 @@ def test_backup_set_description(self):
644633 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
645634
646635 result = self .cmd .take_action (parsed_args )
636+ self .assertIsNone (result )
647637
648- # Set expected values
649- kwargs = {'description' : 'new_description' }
650- self .backups_mock .update .assert_called_once_with (
651- self .backup .id , ** kwargs
638+ self .volume_sdk_client .find_backup .assert_called_with (
639+ self .backup .id , ignore_missing = False
640+ )
641+ self .volume_sdk_client .update_backup .assert_called_once_with (
642+ self .backup , description = 'new_description'
652643 )
653- self .assertIsNone (result )
654644
655645 def test_backup_set_description_pre_v39 (self ):
656646 self .set_volume_api_version ('3.8' )
@@ -679,26 +669,34 @@ def test_backup_set_state(self):
679669 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
680670
681671 result = self .cmd .take_action (parsed_args )
682- self .backups_mock .reset_state .assert_called_once_with (
683- self .backup .id , 'error'
684- )
685672 self .assertIsNone (result )
686673
674+ self .volume_sdk_client .find_backup .assert_called_with (
675+ self .backup .id , ignore_missing = False
676+ )
677+ self .volume_sdk_client .reset_backup_status .assert_called_with (
678+ self .backup , status = 'error'
679+ )
680+
687681 def test_backup_set_state_failed (self ):
688- self .backups_mock .reset_state .side_effect = exceptions .CommandError ()
682+ self .volume_sdk_client .reset_backup_status .side_effect = (
683+ sdk_exceptions .NotFoundException ('foo' )
684+ )
685+
689686 arglist = ['--state' , 'error' , self .backup .id ]
690687 verifylist = [('state' , 'error' ), ('backup' , self .backup .id )]
691688
692689 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
693- try :
694- self .cmd .take_action (parsed_args )
695- self .fail ('CommandError should be raised.' )
696- except exceptions .CommandError as e :
697- self .assertEqual (
698- 'One or more of the set operations failed' , str (e )
699- )
700- self .backups_mock .reset_state .assert_called_with (
701- self .backup .id , 'error'
690+ exc = self .assertRaises (
691+ exceptions .CommandError , self .cmd .take_action , parsed_args
692+ )
693+ self .assertEqual ('One or more of the set operations failed' , str (exc ))
694+
695+ self .volume_sdk_client .find_backup .assert_called_with (
696+ self .backup .id , ignore_missing = False
697+ )
698+ self .volume_sdk_client .reset_backup_status .assert_called_with (
699+ self .backup , status = 'error'
702700 )
703701
704702 def test_backup_set_no_property (self ):
@@ -715,15 +713,14 @@ def test_backup_set_no_property(self):
715713 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
716714
717715 result = self .cmd .take_action (parsed_args )
716+ self .assertIsNone (result )
718717
719- # Set expected values
720- kwargs = {
721- 'metadata' : {},
722- }
723- self .backups_mock .update .assert_called_once_with (
724- self .backup .id , ** kwargs
718+ self .volume_sdk_client .find_backup .assert_called_with (
719+ self .backup .id , ignore_missing = False
720+ )
721+ self .volume_sdk_client .update_backup .assert_called_once_with (
722+ self .backup , metadata = {}
725723 )
726- self .assertIsNone (result )
727724
728725 def test_backup_set_no_property_pre_v343 (self ):
729726 self .set_volume_api_version ('3.42' )
@@ -758,15 +755,14 @@ def test_backup_set_property(self):
758755 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
759756
760757 result = self .cmd .take_action (parsed_args )
758+ self .assertIsNone (result )
761759
762- # Set expected values
763- kwargs = {
764- 'metadata' : {'wow' : 'cool' , 'foo' : 'bar' },
765- }
766- self .backups_mock .update .assert_called_once_with (
767- self .backup .id , ** kwargs
760+ self .volume_sdk_client .find_backup .assert_called_with (
761+ self .backup .id , ignore_missing = False
762+ )
763+ self .volume_sdk_client .update_backup .assert_called_once_with (
764+ self .backup , metadata = {'wow' : 'cool' , 'foo' : 'bar' }
768765 )
769- self .assertIsNone (result )
770766
771767 def test_backup_set_property_pre_v343 (self ):
772768 self .set_volume_api_version ('3.42' )
@@ -788,17 +784,16 @@ def test_backup_set_property_pre_v343(self):
788784 self .assertIn ("--os-volume-api-version 3.43 or greater" , str (exc ))
789785
790786
791- class TestBackupUnset (TestBackupLegacy ):
792- backup = volume_fakes .create_one_backup (
793- attrs = {'metadata' : {'foo' : 'bar' }},
794- )
795-
787+ class TestBackupUnset (volume_fakes .TestVolume ):
796788 def setUp (self ):
797789 super ().setUp ()
798790
799- self .backups_mock .get .return_value = self .backup
791+ self .backup = sdk_fakes .generate_fake_resource (
792+ _backup .Backup , metadata = {'foo' : 'bar' , 'wow' : 'cool' }
793+ )
794+ self .volume_sdk_client .find_backup .return_value = self .backup
795+ self .volume_sdk_client .delete_backup_metadata .return_value = None
800796
801- # Get the command object to test
802797 self .cmd = volume_backup .UnsetVolumeBackup (self .app , None )
803798
804799 def test_backup_unset_property (self ):
@@ -816,15 +811,14 @@ def test_backup_unset_property(self):
816811 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
817812
818813 result = self .cmd .take_action (parsed_args )
814+ self .assertIsNone (result )
819815
820- # Set expected values
821- kwargs = {
822- 'metadata' : {},
823- }
824- self .backups_mock .update .assert_called_once_with (
825- self .backup .id , ** kwargs
816+ self .volume_sdk_client .find_backup .assert_called_with (
817+ self .backup .id , ignore_missing = False
818+ )
819+ self .volume_sdk_client .delete_backup_metadata .assert_called_once_with (
820+ self .backup , keys = ['wow' ]
826821 )
827- self .assertIsNone (result )
828822
829823 def test_backup_unset_property_pre_v343 (self ):
830824 self .set_volume_api_version ('3.42' )
@@ -907,7 +901,9 @@ def test_backup_show(self):
907901 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
908902
909903 columns , data = self .cmd .take_action (parsed_args )
910- self .volume_sdk_client .find_backup .assert_called_with (self .backup .id )
904+ self .volume_sdk_client .find_backup .assert_called_with (
905+ self .backup .id , ignore_missing = False
906+ )
911907
912908 self .assertEqual (self .columns , columns )
913909 self .assertEqual (self .data , data )
0 commit comments