<s> """<STR_LIT>""" <EOL> import datetime <EOL> import os <EOL> import tempfile <EOL> import time <EOL> import uuid <EOL> import mock <EOL> from oslo_utils import uuidutils <EOL> import six <EOL> from ironic . common import exception <EOL> from ironic . common import image_service <EOL> from ironic . common import images <EOL> from ironic . common import utils <EOL> from ironic . drivers . modules import image_cache <EOL> from ironic . tests import base <EOL> def touch ( filename ) : <EOL> open ( filename , '<STR_LIT:w>' ) . close ( ) <EOL> class TestImageCacheFetch ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestImageCacheFetch , self ) . setUp ( ) <EOL> self . master_dir = tempfile . mkdtemp ( ) <EOL> self . cache = image_cache . ImageCache ( self . master_dir , None , None ) <EOL> self . dest_dir = tempfile . mkdtemp ( ) <EOL> self . dest_path = os . path . join ( self . dest_dir , '<STR_LIT>' ) <EOL> self . uuid = uuidutils . generate_uuid ( ) <EOL> self . master_path = os . path . join ( self . master_dir , self . uuid ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_fetch_image_no_master_dir ( self , mock_download , mock_clean_up , <EOL> mock_fetch ) : <EOL> self . cache . master_dir = None <EOL> self . cache . fetch_image ( self . uuid , self . dest_path ) <EOL> self . assertFalse ( mock_download . called ) <EOL> mock_fetch . assert_called_once_with ( <EOL> None , self . uuid , self . dest_path , True ) <EOL> self . assertFalse ( mock_clean_up . called ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = True , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = True , autospec = True ) <EOL> def test_fetch_image_dest_and_master_uptodate ( <EOL> self , mock_cache_upd , mock_dest_upd , mock_link , mock_download , <EOL> mock_clean_up ) : <EOL> self . cache . fetch_image ( self . uuid , self . dest_path ) <EOL> mock_cache_upd . assert_called_once_with ( self . master_path , self . uuid , <EOL> None ) <EOL> mock_dest_upd . assert_called_once_with ( self . master_path , self . dest_path ) <EOL> self . assertFalse ( mock_link . called ) <EOL> self . assertFalse ( mock_download . called ) <EOL> self . assertFalse ( mock_clean_up . called ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = False , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = True , autospec = True ) <EOL> def test_fetch_image_dest_out_of_date ( <EOL> self , mock_cache_upd , mock_dest_upd , mock_link , mock_download , <EOL> mock_clean_up ) : <EOL> self . cache . fetch_image ( self . uuid , self . dest_path ) <EOL> mock_cache_upd . assert_called_once_with ( self . master_path , self . uuid , <EOL> None ) <EOL> mock_dest_upd . assert_called_once_with ( self . master_path , self . dest_path ) <EOL> mock_link . assert_called_once_with ( self . master_path , self . dest_path ) <EOL> self . assertFalse ( mock_download . called ) <EOL> self . assertFalse ( mock_clean_up . called ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = True , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = False , autospec = True ) <EOL> def test_fetch_image_master_out_of_date ( <EOL> self , mock_cache_upd , mock_dest_upd , mock_link , mock_download , <EOL> mock_clean_up ) : <EOL> self . cache . fetch_image ( self . uuid , self . dest_path ) <EOL> mock_cache_upd . assert_called_once_with ( self . master_path , self . uuid , <EOL> None ) <EOL> mock_dest_upd . assert_called_once_with ( self . master_path , self . dest_path ) <EOL> self . assertFalse ( mock_link . called ) <EOL> mock_download . assert_called_once_with ( <EOL> self . cache , self . uuid , self . master_path , self . dest_path , <EOL> ctx = None , force_raw = True ) <EOL> mock_clean_up . assert_called_once_with ( self . cache ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = True , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , <EOL> return_value = False , autospec = True ) <EOL> def test_fetch_image_both_master_and_dest_out_of_date ( <EOL> self , mock_cache_upd , mock_dest_upd , mock_link , mock_download , <EOL> mock_clean_up ) : <EOL> self . cache . fetch_image ( self . uuid , self . dest_path ) <EOL> mock_cache_upd . assert_called_once_with ( self . master_path , self . uuid , <EOL> None ) <EOL> mock_dest_upd . assert_called_once_with ( self . master_path , self . dest_path ) <EOL> self . assertFalse ( mock_link . called ) <EOL> mock_download . assert_called_once_with ( <EOL> self . cache , self . uuid , self . master_path , self . dest_path , <EOL> ctx = None , force_raw = True ) <EOL> mock_clean_up . assert_called_once_with ( self . cache ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_fetch_image_not_uuid ( self , mock_download , mock_clean_up ) : <EOL> href = u'<STR_LIT>' <EOL> href_encoded = href . encode ( '<STR_LIT:utf-8>' ) if six . PY2 else href <EOL> href_converted = str ( uuid . uuid5 ( uuid . NAMESPACE_URL , href_encoded ) ) <EOL> master_path = os . path . join ( self . master_dir , href_converted ) <EOL> self . cache . fetch_image ( href , self . dest_path ) <EOL> mock_download . assert_called_once_with ( <EOL> self . cache , href , master_path , self . dest_path , <EOL> ctx = None , force_raw = True ) <EOL> self . assertTrue ( mock_clean_up . called ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> def test__download_image ( self , mock_fetch ) : <EOL> def _fake_fetch ( ctx , uuid , tmp_path , * args ) : <EOL> self . assertEqual ( self . uuid , uuid ) <EOL> self . assertNotEqual ( self . dest_path , tmp_path ) <EOL> self . assertNotEqual ( os . path . dirname ( tmp_path ) , self . master_dir ) <EOL> with open ( tmp_path , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( "<STR_LIT>" ) <EOL> mock_fetch . side_effect = _fake_fetch <EOL> self . cache . _download_image ( self . uuid , self . master_path , self . dest_path ) <EOL> self . assertTrue ( os . path . isfile ( self . dest_path ) ) <EOL> self . assertTrue ( os . path . isfile ( self . master_path ) ) <EOL> self . assertEqual ( os . stat ( self . dest_path ) . st_ino , <EOL> os . stat ( self . master_path ) . st_ino ) <EOL> with open ( self . dest_path ) as fp : <EOL> self . assertEqual ( "<STR_LIT>" , fp . read ( ) ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> class TestUpdateImages ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestUpdateImages , self ) . setUp ( ) <EOL> self . master_dir = tempfile . mkdtemp ( ) <EOL> self . dest_dir = tempfile . mkdtemp ( ) <EOL> self . dest_path = os . path . join ( self . dest_dir , '<STR_LIT>' ) <EOL> self . uuid = uuidutils . generate_uuid ( ) <EOL> self . master_path = os . path . join ( self . master_dir , self . uuid ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , return_value = False , autospec = True ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_glance_img_not_cached ( <EOL> self , mock_gis , mock_path_exists , mock_unlink ) : <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , <EOL> self . uuid , None ) <EOL> self . assertFalse ( mock_gis . called ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> mock_path_exists . assert_called_once_with ( self . master_path ) <EOL> self . assertFalse ( res ) <EOL> @ mock . patch . object ( os . path , '<STR_LIT>' , return_value = True , autospec = True ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_glance_img ( <EOL> self , mock_gis , mock_path_exists , mock_unlink ) : <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , <EOL> self . uuid , None ) <EOL> self . assertFalse ( mock_gis . called ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> mock_path_exists . assert_called_once_with ( self . master_path ) <EOL> self . assertTrue ( res ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_no_master ( self , mock_gis , <EOL> mock_unlink ) : <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , <EOL> '<STR_LIT>' , None ) <EOL> self . assertFalse ( mock_gis . called ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertFalse ( res ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_no_updated_at ( self , mock_gis , <EOL> mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> href = '<STR_LIT>' <EOL> mock_gis . return_value . show . return_value = { } <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , href , <EOL> None ) <EOL> mock_gis . assert_called_once_with ( href , context = None ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertTrue ( res ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_master_up_to_date ( self , mock_gis , <EOL> mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> href = '<STR_LIT>' <EOL> mock_gis . return_value . show . return_value = { <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:15> , <NUM_LIT:8> , <NUM_LIT:12> , <NUM_LIT> ) <EOL> } <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , href , <EOL> None ) <EOL> mock_gis . assert_called_once_with ( href , context = None ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertTrue ( res ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_master_same_time ( self , mock_gis , <EOL> mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> mtime = utils . unix_file_modification_datetime ( self . master_path ) <EOL> href = '<STR_LIT>' <EOL> mock_gis . return_value . show . return_value = { <EOL> '<STR_LIT>' : mtime <EOL> } <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , href , <EOL> None ) <EOL> mock_gis . assert_called_once_with ( href , context = None ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertTrue ( res ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> def test__delete_master_path_if_stale_out_of_date ( self , mock_gis , <EOL> mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> href = '<STR_LIT>' <EOL> mock_gis . return_value . show . return_value = { <EOL> '<STR_LIT>' : datetime . datetime ( ( datetime . datetime . utcnow ( ) . year <EOL> + <NUM_LIT:1> ) , <NUM_LIT:11> , <NUM_LIT:15> , <NUM_LIT:8> , <NUM_LIT:12> , <NUM_LIT> ) <EOL> } <EOL> res = image_cache . _delete_master_path_if_stale ( self . master_path , href , <EOL> None ) <EOL> mock_gis . assert_called_once_with ( href , context = None ) <EOL> mock_unlink . assert_called_once_with ( self . master_path ) <EOL> self . assertFalse ( res ) <EOL> def test__delete_dest_path_if_stale_no_dest ( self , mock_unlink ) : <EOL> res = image_cache . _delete_dest_path_if_stale ( self . master_path , <EOL> self . dest_path ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertFalse ( res ) <EOL> def test__delete_dest_path_if_stale_no_master ( self , mock_unlink ) : <EOL> touch ( self . dest_path ) <EOL> res = image_cache . _delete_dest_path_if_stale ( self . master_path , <EOL> self . dest_path ) <EOL> mock_unlink . assert_called_once_with ( self . dest_path ) <EOL> self . assertFalse ( res ) <EOL> def test__delete_dest_path_if_stale_out_of_date ( self , mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> touch ( self . dest_path ) <EOL> res = image_cache . _delete_dest_path_if_stale ( self . master_path , <EOL> self . dest_path ) <EOL> mock_unlink . assert_called_once_with ( self . dest_path ) <EOL> self . assertFalse ( res ) <EOL> def test__delete_dest_path_if_stale_up_to_date ( self , mock_unlink ) : <EOL> touch ( self . master_path ) <EOL> os . link ( self . master_path , self . dest_path ) <EOL> res = image_cache . _delete_dest_path_if_stale ( self . master_path , <EOL> self . dest_path ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertTrue ( res ) <EOL> class TestImageCacheCleanUp ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestImageCacheCleanUp , self ) . setUp ( ) <EOL> self . master_dir = tempfile . mkdtemp ( ) <EOL> self . cache = image_cache . ImageCache ( self . master_dir , <EOL> cache_size = <NUM_LIT:10> , <EOL> cache_ttl = <NUM_LIT> ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_old_deleted ( self , mock_clean_size ) : <EOL> mock_clean_size . return_value = None <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:2> ) ] <EOL> for filename in files : <EOL> touch ( filename ) <EOL> new_current_time = time . time ( ) + <NUM_LIT> <EOL> os . utime ( files [ <NUM_LIT:0> ] , ( new_current_time - <NUM_LIT:100> , new_current_time - <NUM_LIT:100> ) ) <EOL> with mock . patch . object ( time , '<STR_LIT:time>' , lambda : new_current_time ) : <EOL> self . cache . clean_up ( ) <EOL> mock_clean_size . assert_called_once_with ( self . cache , mock . ANY , None ) <EOL> survived = mock_clean_size . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( survived ) ) <EOL> self . assertEqual ( files [ <NUM_LIT:0> ] , survived [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( int ( new_current_time - <NUM_LIT:100> ) , int ( survived [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( int ( new_current_time - <NUM_LIT:100> ) , <EOL> int ( survived [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] . st_mtime ) ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_old_with_amount ( self , mock_clean_size ) : <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:2> ) ] <EOL> for filename in files : <EOL> open ( filename , '<STR_LIT:wb>' ) . write ( b'<STR_LIT:X>' ) <EOL> new_current_time = time . time ( ) + <NUM_LIT> <EOL> with mock . patch . object ( time , '<STR_LIT:time>' , lambda : new_current_time ) : <EOL> self . cache . clean_up ( amount = <NUM_LIT:1> ) <EOL> self . assertFalse ( mock_clean_size . called ) <EOL> self . assertTrue ( any ( os . path . exists ( f ) for f in files ) ) <EOL> self . assertFalse ( all ( os . path . exists ( f ) for f in files ) ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_files_with_links_untouched ( self , mock_clean_size ) : <EOL> mock_clean_size . return_value = None <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:2> ) ] <EOL> for filename in files : <EOL> touch ( filename ) <EOL> os . link ( filename , filename + '<STR_LIT>' ) <EOL> new_current_time = time . time ( ) + <NUM_LIT> <EOL> with mock . patch . object ( time , '<STR_LIT:time>' , lambda : new_current_time ) : <EOL> self . cache . clean_up ( ) <EOL> for filename in files : <EOL> self . assertTrue ( os . path . exists ( filename ) ) <EOL> mock_clean_size . assert_called_once_with ( mock . ANY , [ ] , None ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_ensure_cache_size ( self , mock_clean_ttl ) : <EOL> mock_clean_ttl . side_effect = lambda * xx : xx [ <NUM_LIT:1> : ] <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:6> ) ] <EOL> for filename in files : <EOL> with open ( filename , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> new_current_time = time . time ( ) + <NUM_LIT:100> <EOL> for filename in files [ : <NUM_LIT:3> ] : <EOL> os . utime ( filename , ( new_current_time , new_current_time ) ) <EOL> with mock . patch . object ( time , '<STR_LIT:time>' , lambda : new_current_time ) : <EOL> self . cache . clean_up ( ) <EOL> for filename in files [ : <NUM_LIT:3> ] : <EOL> self . assertTrue ( os . path . exists ( filename ) ) <EOL> for filename in files [ <NUM_LIT:3> : ] : <EOL> self . assertFalse ( os . path . exists ( filename ) ) <EOL> mock_clean_ttl . assert_called_once_with ( mock . ANY , mock . ANY , None ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_ensure_cache_size_with_amount ( self , mock_clean_ttl ) : <EOL> mock_clean_ttl . side_effect = lambda * xx : xx [ <NUM_LIT:1> : ] <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:6> ) ] <EOL> for filename in files : <EOL> with open ( filename , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> new_current_time = time . time ( ) + <NUM_LIT:100> <EOL> os . utime ( files [ <NUM_LIT:0> ] , ( new_current_time , new_current_time ) ) <EOL> with mock . patch . object ( time , '<STR_LIT:time>' , lambda : new_current_time ) : <EOL> self . cache . clean_up ( amount = <NUM_LIT:15> ) <EOL> self . assertTrue ( os . path . exists ( files [ <NUM_LIT:0> ] ) ) <EOL> for filename in files [ <NUM_LIT:5> : ] : <EOL> self . assertFalse ( os . path . exists ( filename ) ) <EOL> mock_clean_ttl . assert_called_once_with ( mock . ANY , mock . ANY , <NUM_LIT:15> ) <EOL> @ mock . patch . object ( image_cache . LOG , '<STR_LIT:info>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_cache_still_large ( self , mock_clean_ttl , mock_log ) : <EOL> mock_clean_ttl . side_effect = lambda * xx : xx [ <NUM_LIT:1> : ] <EOL> files = [ os . path . join ( self . master_dir , str ( i ) ) <EOL> for i in range ( <NUM_LIT:2> ) ] <EOL> for filename in files : <EOL> with open ( filename , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> os . link ( filename , filename + '<STR_LIT>' ) <EOL> self . cache . clean_up ( ) <EOL> for filename in files : <EOL> self . assertTrue ( os . path . exists ( filename ) ) <EOL> self . assertTrue ( mock_log . called ) <EOL> mock_clean_ttl . assert_called_once_with ( mock . ANY , mock . ANY , None ) <EOL> @ mock . patch . object ( utils , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> def test_temp_images_not_cleaned ( self , mock_fetch , mock_rmtree ) : <EOL> def _fake_fetch ( ctx , uuid , tmp_path , * args ) : <EOL> with open ( tmp_path , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( "<STR_LIT>" * <NUM_LIT:10> ) <EOL> self . cache . clean_up ( ) <EOL> self . assertTrue ( os . path . exists ( tmp_path ) ) <EOL> mock_fetch . side_effect = _fake_fetch <EOL> master_path = os . path . join ( self . master_dir , '<STR_LIT>' ) <EOL> dest_path = os . path . join ( tempfile . mkdtemp ( ) , '<STR_LIT>' ) <EOL> self . cache . _download_image ( '<STR_LIT>' , master_path , dest_path ) <EOL> self . assertTrue ( mock_rmtree . called ) <EOL> @ mock . patch . object ( utils , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> def test_temp_dir_exception ( self , mock_fetch , mock_rmtree ) : <EOL> mock_fetch . side_effect = exception . IronicException <EOL> self . assertRaises ( exception . IronicException , <EOL> self . cache . _download_image , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( mock_rmtree . called ) <EOL> @ mock . patch . object ( image_cache . LOG , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> @ mock . patch . object ( image_cache . ImageCache , '<STR_LIT>' , <EOL> autospec = True ) <EOL> def test_clean_up_amount_not_satisfied ( self , mock_clean_size , <EOL> mock_clean_ttl , mock_log ) : <EOL> mock_clean_ttl . side_effect = lambda * xx : xx [ <NUM_LIT:1> : ] <EOL> mock_clean_size . side_effect = lambda self , listing , amount : amount <EOL> self . cache . clean_up ( amount = <NUM_LIT:15> ) <EOL> self . assertTrue ( mock_log . called ) <EOL> def test_cleanup_ordering ( self ) : <EOL> class ParentCache ( image_cache . ImageCache ) : <EOL> def __init__ ( self ) : <EOL> super ( ParentCache , self ) . __init__ ( '<STR_LIT:a>' , <NUM_LIT:1> , <NUM_LIT:1> , None ) <EOL> @ image_cache . cleanup ( priority = <NUM_LIT> ) <EOL> class Cache1 ( ParentCache ) : <EOL> pass <EOL> @ image_cache . cleanup ( priority = <NUM_LIT> ) <EOL> class Cache2 ( ParentCache ) : <EOL> pass <EOL> @ image_cache . cleanup ( priority = <NUM_LIT> ) <EOL> class Cache3 ( ParentCache ) : <EOL> pass <EOL> self . assertEqual ( image_cache . _cache_cleanup_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , Cache2 ) <EOL> item_possibilities = [ Cache1 , Cache3 ] <EOL> second_item_actual = image_cache . _cache_cleanup_list [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> self . assertIn ( second_item_actual , item_possibilities ) <EOL> item_possibilities . remove ( second_item_actual ) <EOL> third_item_actual = image_cache . _cache_cleanup_list [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( item_possibilities [ <NUM_LIT:0> ] , third_item_actual ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_service , '<STR_LIT>' , autospec = True ) <EOL> class CleanupImageCacheTestCase ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( CleanupImageCacheTestCase , self ) . setUp ( ) <EOL> self . mock_first_cache = mock . MagicMock ( spec_set = [ ] ) <EOL> self . mock_second_cache = mock . MagicMock ( spec_set = [ ] ) <EOL> self . cache_cleanup_list = [ ( <NUM_LIT:50> , self . mock_first_cache ) , <EOL> ( <NUM_LIT:20> , self . mock_second_cache ) ] <EOL> self . mock_first_cache . return_value . master_dir = '<STR_LIT>' <EOL> self . mock_second_cache . return_value . master_dir = '<STR_LIT>' <EOL> def test_no_clean_up ( self , mock_image_service , mock_statvfs , <EOL> cache_cleanup_list_mock ) : <EOL> mock_show = mock_image_service . return_value . show <EOL> mock_show . return_value = dict ( size = <NUM_LIT> ) <EOL> mock_statvfs . return_value = mock . MagicMock ( <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] , f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT> ) <EOL> cache_cleanup_list_mock . __iter__ . return_value = self . cache_cleanup_list <EOL> image_cache . clean_up_caches ( None , '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT:path>' ) ] ) <EOL> mock_show . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_statvfs . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . mock_first_cache . return_value . clean_up . called ) <EOL> self . assertFalse ( self . mock_second_cache . return_value . clean_up . called ) <EOL> mock_statvfs . assert_called_once_with ( '<STR_LIT>' ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> def test_one_clean_up ( self , mock_stat , mock_image_service , mock_statvfs , <EOL> cache_cleanup_list_mock ) : <EOL> mock_stat . return_value . st_dev = <NUM_LIT:1> <EOL> mock_show = mock_image_service . return_value . show <EOL> mock_show . return_value = dict ( size = <NUM_LIT> ) <EOL> mock_statvfs . side_effect = [ <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT:1> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ] <EOL> cache_cleanup_list_mock . __iter__ . return_value = self . cache_cleanup_list <EOL> image_cache . clean_up_caches ( None , '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT:path>' ) ] ) <EOL> mock_show . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_statvfs . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , mock_statvfs . call_count ) <EOL> self . mock_first_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . assertFalse ( self . mock_second_cache . return_value . clean_up . called ) <EOL> mock_stat_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> mock_statvfs_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( mock_stat_calls_expected , mock_stat . mock_calls ) <EOL> self . assertEqual ( mock_statvfs_calls_expected , mock_statvfs . mock_calls ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> def test_clean_up_another_fs ( self , mock_stat , mock_image_service , <EOL> mock_statvfs , cache_cleanup_list_mock ) : <EOL> mock_stat . side_effect = [ mock . MagicMock ( st_dev = <NUM_LIT:1> , spec_set = [ '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( st_dev = <NUM_LIT:2> , spec_set = [ '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( st_dev = <NUM_LIT:1> , spec_set = [ '<STR_LIT>' ] ) ] <EOL> mock_show = mock_image_service . return_value . show <EOL> mock_show . return_value = dict ( size = <NUM_LIT> ) <EOL> mock_statvfs . side_effect = [ <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT:1> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ] <EOL> cache_cleanup_list_mock . __iter__ . return_value = self . cache_cleanup_list <EOL> image_cache . clean_up_caches ( None , '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT:path>' ) ] ) <EOL> mock_show . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_statvfs . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , mock_statvfs . call_count ) <EOL> self . mock_second_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . assertFalse ( self . mock_first_cache . return_value . clean_up . called ) <EOL> mock_stat_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> mock_statvfs_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( mock_stat_calls_expected , mock_stat . mock_calls ) <EOL> self . assertEqual ( mock_statvfs_calls_expected , mock_statvfs . mock_calls ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> def test_both_clean_up ( self , mock_stat , mock_image_service , mock_statvfs , <EOL> cache_cleanup_list_mock ) : <EOL> mock_stat . return_value . st_dev = <NUM_LIT:1> <EOL> mock_show = mock_image_service . return_value . show <EOL> mock_show . return_value = dict ( size = <NUM_LIT> ) <EOL> mock_statvfs . side_effect = [ <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT:1> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT:2> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . MagicMock ( f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT> , <EOL> spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ] <EOL> cache_cleanup_list_mock . __iter__ . return_value = self . cache_cleanup_list <EOL> image_cache . clean_up_caches ( None , '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT:path>' ) ] ) <EOL> mock_show . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_statvfs . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:3> , mock_statvfs . call_count ) <EOL> self . mock_first_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . mock_second_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:2> ) ) <EOL> mock_stat_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> mock_statvfs_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( mock_stat_calls_expected , mock_stat . mock_calls ) <EOL> self . assertEqual ( mock_statvfs_calls_expected , mock_statvfs . mock_calls ) <EOL> @ mock . patch . object ( os , '<STR_LIT>' , autospec = True ) <EOL> def test_clean_up_fail ( self , mock_stat , mock_image_service , mock_statvfs , <EOL> cache_cleanup_list_mock ) : <EOL> mock_stat . return_value . st_dev = <NUM_LIT:1> <EOL> mock_show = mock_image_service . return_value . show <EOL> mock_show . return_value = dict ( size = <NUM_LIT> ) <EOL> mock_statvfs . return_value = mock . MagicMock ( <EOL> f_frsize = <NUM_LIT:1> , f_bavail = <NUM_LIT:1> , spec_set = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> cache_cleanup_list_mock . __iter__ . return_value = self . cache_cleanup_list <EOL> self . assertRaises ( exception . InsufficientDiskSpace , <EOL> image_cache . clean_up_caches , <EOL> None , '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT:path>' ) ] ) <EOL> mock_show . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_statvfs . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:3> , mock_statvfs . call_count ) <EOL> self . mock_first_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . mock_second_cache . return_value . clean_up . assert_called_once_with ( <EOL> amount = ( <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> mock_stat_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> mock_statvfs_calls_expected = [ mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( mock_stat_calls_expected , mock_stat . mock_calls ) <EOL> self . assertEqual ( mock_statvfs_calls_expected , mock_statvfs . mock_calls ) <EOL> class TestFetchCleanup ( base . TestCase ) : <EOL> @ mock . patch . object ( images , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( images , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( images , '<STR_LIT>' , autospec = True ) <EOL> @ mock . patch . object ( image_cache , '<STR_LIT>' , autospec = True ) <EOL> def test__fetch ( self , mock_clean , mock_raw , mock_fetch , mock_size ) : <EOL> mock_size . return_value = <NUM_LIT:100> <EOL> image_cache . _fetch ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , force_raw = True ) <EOL> mock_fetch . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , force_raw = False ) <EOL> mock_clean . assert_called_once_with ( '<STR_LIT>' , <NUM_LIT:100> ) <EOL> mock_raw . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) </s>
