@@ -4031,3 +4031,91 @@ def test_do_cache_clear_endpoint_not_provided(self):
40314031 mock_exit .assert_called_once_with (
40324032 'Direct server endpoint needs to be provided. Do '
40334033 'not use loadbalanced or catalog endpoints.' )
4034+
4035+ def _test_cache_clean (self , supported = True , forbidden = False ):
4036+ args = self ._make_args ({})
4037+ with mock .patch .object (self .gc .cache , 'clean' ) as mocked_cache_clean :
4038+ if supported :
4039+ mocked_cache_clean .return_value = None
4040+ else :
4041+ mocked_cache_clean .side_effect = exc .HTTPNotImplemented
4042+ if forbidden :
4043+ mocked_cache_clean .side_effect = exc .HTTPForbidden
4044+
4045+ test_shell .do_cache_clean (self .gc , args )
4046+ if supported :
4047+ mocked_cache_clean .assert_called_once_with ()
4048+
4049+ def test_do_cache_clean (self ):
4050+ self ._test_cache_clean ()
4051+
4052+ def test_do_cache_clean_unsupported (self ):
4053+ with mock .patch (
4054+ 'glanceclient.common.utils.print_err' ) as mock_print_err :
4055+ self ._test_cache_clean (supported = False )
4056+ mock_print_err .assert_called_once_with (
4057+ "'HTTP HTTPNotImplemented': Unable to clean the image cache." )
4058+
4059+ def test_do_cache_clean_forbidden (self ):
4060+ with mock .patch (
4061+ 'glanceclient.common.utils.print_err' ) as mock_print_err :
4062+ self ._test_cache_clean (forbidden = True )
4063+ mock_print_err .assert_called_once_with (
4064+ "You are not permitted to clean the image cache." )
4065+
4066+ def test_do_cache_clean_endpoint_not_provided (self ):
4067+ args = self ._make_args ({})
4068+ self .gc .endpoint_provided = False
4069+ with mock .patch ('glanceclient.common.utils.exit' ) as mock_exit :
4070+ test_shell .do_cache_clean (self .gc , args )
4071+ mock_exit .assert_called_once_with (
4072+ 'Direct server endpoint needs to be provided. Do '
4073+ 'not use loadbalanced or catalog endpoints.' )
4074+
4075+ def _test_cache_prune (self , supported = True , forbidden = False ):
4076+ args = self ._make_args ({})
4077+ with mock .patch .object (self .gc .cache , 'prune' ) as mocked_cache_prune :
4078+ if supported :
4079+ mocked_cache_prune .return_value = {
4080+ 'total_files_pruned' : 5 ,
4081+ 'total_bytes_pruned' : 104857600 ,
4082+ }
4083+ else :
4084+ mocked_cache_prune .side_effect = exc .HTTPNotImplemented
4085+ if forbidden :
4086+ mocked_cache_prune .side_effect = exc .HTTPForbidden
4087+
4088+ with mock .patch ('builtins.print' ) as mock_print :
4089+ test_shell .do_cache_prune (self .gc , args )
4090+ if supported and not forbidden :
4091+ mocked_cache_prune .assert_called_once_with ()
4092+ mock_print .assert_called_once_with (
4093+ 'Pruned 5 file(s), 104857600 byte(s).' )
4094+
4095+ def test_do_cache_prune (self ):
4096+ self ._test_cache_prune ()
4097+
4098+ def test_do_cache_prune_unsupported (self ):
4099+ with mock .patch (
4100+ 'glanceclient.common.utils.print_err' ) as mock_print_err :
4101+ self ._test_cache_prune (supported = False )
4102+ mock_print_err .assert_called_once_with (
4103+ "'HTTP HTTPNotImplemented': Unable to prune the image cache." )
4104+
4105+ def test_do_cache_prune_forbidden (self ):
4106+ with mock .patch (
4107+ 'glanceclient.common.utils.print_err' ) as mock_print_err :
4108+ self ._test_cache_prune (forbidden = True )
4109+ mock_print_err .assert_called_once_with (
4110+ "You are not permitted to prune the image cache." )
4111+
4112+ def test_do_cache_prune_endpoint_not_provided (self ):
4113+ args = self ._make_args ({})
4114+ self .gc .endpoint_provided = False
4115+ with mock .patch ('glanceclient.common.utils.exit' ) as mock_exit :
4116+ mock_exit .side_effect = SystemExit
4117+ self .assertRaises (SystemExit ,
4118+ test_shell .do_cache_prune , self .gc , args )
4119+ mock_exit .assert_called_once_with (
4120+ 'Direct server endpoint needs to be provided. Do not use '
4121+ 'loadbalanced or catalog endpoints.' )
0 commit comments