<s> import os <EOL> import dogpile . cache <EOL> import mock <EOL> from ironicclient . common import filecache <EOL> from ironicclient . tests . unit import utils <EOL> class FileCacheTest ( utils . BaseTestCase ) : <EOL> def test__build_key_ok ( self ) : <EOL> result = filecache . _build_key ( '<STR_LIT:localhost>' , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , result ) <EOL> def test__build_key_none ( self ) : <EOL> result = filecache . _build_key ( None , None ) <EOL> self . assertEqual ( '<STR_LIT>' , result ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( dogpile . cache , '<STR_LIT>' , autospec = True ) <EOL> def test__get_cache_mkdir ( self , mock_makeregion , mock_makedirs , <EOL> mock_exists ) : <EOL> cache_val = <NUM_LIT:6> <EOL> filecache . CACHE = None <EOL> mock_exists . return_value = False <EOL> cache_region = mock . Mock ( spec = dogpile . cache . region . CacheRegion ) <EOL> cache_region . configure . return_value = cache_val <EOL> mock_makeregion . return_value = cache_region <EOL> self . assertEqual ( cache_val , filecache . _get_cache ( ) ) <EOL> mock_exists . assert_called_once_with ( filecache . CACHE_DIR ) <EOL> mock_makedirs . assert_called_once_with ( filecache . CACHE_DIR ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> def test__get_cache_dir_already_exists ( self , mock_makedirs , mock_exists ) : <EOL> cache_val = <NUM_LIT> <EOL> mock_exists . return_value = True <EOL> filecache . CACHE = cache_val <EOL> self . assertEqual ( cache_val , filecache . _get_cache ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , mock_exists . call_count ) <EOL> self . assertEqual ( <NUM_LIT:0> , mock_makedirs . call_count ) <EOL> @ mock . patch . object ( dogpile . cache . region , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( filecache , '<STR_LIT>' , autospec = True ) <EOL> def test_save_data_ok ( self , mock_get_cache , mock_cache ) : <EOL> mock_get_cache . return_value = mock_cache <EOL> host = '<STR_LIT>' <EOL> port = '<STR_LIT>' <EOL> hostport = '<STR_LIT>' % ( host , port ) <EOL> data = '<STR_LIT>' <EOL> filecache . save_data ( host , port , data ) <EOL> mock_cache . set . assert_called_once_with ( hostport , data ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( dogpile . cache . region , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( filecache , '<STR_LIT>' , autospec = True ) <EOL> def test_retrieve_data_ok ( self , mock_get_cache , mock_cache , mock_isfile ) : <EOL> s = '<STR_LIT>' <EOL> mock_isfile . return_value = True <EOL> mock_cache . get . return_value = s <EOL> mock_get_cache . return_value = mock_cache <EOL> host = '<STR_LIT>' <EOL> port = '<STR_LIT>' <EOL> hostport = '<STR_LIT>' % ( host , port ) <EOL> result = filecache . retrieve_data ( host , port ) <EOL> mock_cache . get . assert_called_once_with ( hostport , expiration_time = None ) <EOL> self . assertEqual ( s , result ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( dogpile . cache . region , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( filecache , '<STR_LIT>' , autospec = True ) <EOL> def test_retrieve_data_ok_with_expiry ( self , mock_get_cache , mock_cache , <EOL> mock_isfile ) : <EOL> s = '<STR_LIT>' <EOL> mock_isfile . return_value = True <EOL> mock_cache . get . return_value = s <EOL> mock_get_cache . return_value = mock_cache <EOL> host = '<STR_LIT>' <EOL> port = '<STR_LIT>' <EOL> expiry = '<STR_LIT>' <EOL> hostport = '<STR_LIT>' % ( host , port ) <EOL> result = filecache . retrieve_data ( host , port , expiry ) <EOL> mock_cache . get . assert_called_once_with ( hostport , <EOL> expiration_time = expiry ) <EOL> self . assertEqual ( s , result ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( dogpile . cache . region , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( filecache , '<STR_LIT>' , autospec = True ) <EOL> def test_retrieve_data_not_found ( self , mock_get_cache , mock_cache , <EOL> mock_isfile ) : <EOL> mock_isfile . return_value = True <EOL> mock_cache . get . return_value = dogpile . cache . api . NO_VALUE <EOL> mock_get_cache . return_value = mock_cache <EOL> host = '<STR_LIT>' <EOL> port = '<STR_LIT>' <EOL> hostport = '<STR_LIT>' % ( host , port ) <EOL> result = filecache . retrieve_data ( host , port ) <EOL> mock_cache . get . assert_called_once_with ( hostport , expiration_time = None ) <EOL> self . assertIsNone ( result ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , autospec = True ) <EOL> def test_retrieve_data_no_cache_file ( self , mock_isfile ) : <EOL> mock_isfile . return_value = False <EOL> self . assertIsNone ( filecache . retrieve_data ( host = '<STR_LIT>' , port = '<STR_LIT>' ) ) </s>
