<s> import os <EOL> import mock <EOL> from oslo_config import cfg <EOL> from oslo_utils import units <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova . tests . unit import fake_instance <EOL> from nova . tests . unit . objects import test_flavor <EOL> from nova . tests . unit . virt . hyperv import test_base <EOL> from nova . virt . hyperv import constants <EOL> from nova . virt . hyperv import imagecache <EOL> CONF = cfg . CONF <EOL> class ImageCacheTestCase ( test_base . HyperVBaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> FAKE_BASE_DIR = '<STR_LIT>' <EOL> FAKE_FORMAT = '<STR_LIT>' <EOL> FAKE_IMAGE_REF = '<STR_LIT>' <EOL> FAKE_VHD_SIZE_GB = <NUM_LIT:1> <EOL> def setUp ( self ) : <EOL> super ( ImageCacheTestCase , self ) . setUp ( ) <EOL> self . context = '<STR_LIT>' <EOL> self . instance = fake_instance . fake_instance_obj ( self . context ) <EOL> patched_get_hostutils = mock . patch . object ( imagecache . utilsfactory , <EOL> "<STR_LIT>" ) <EOL> patched_get_vhdutils = mock . patch . object ( imagecache . utilsfactory , <EOL> "<STR_LIT>" ) <EOL> patched_get_hostutils . start ( ) <EOL> patched_get_vhdutils . start ( ) <EOL> self . addCleanup ( patched_get_hostutils . stop ) <EOL> self . addCleanup ( patched_get_vhdutils . stop ) <EOL> self . imagecache = imagecache . ImageCache ( ) <EOL> self . imagecache . _pathutils = mock . MagicMock ( ) <EOL> self . imagecache . _vhdutils = mock . MagicMock ( ) <EOL> def _test_get_root_vhd_size_gb ( self , old_flavor = True ) : <EOL> if old_flavor : <EOL> mock_flavor = objects . Flavor ( ** test_flavor . fake_flavor ) <EOL> self . instance . old_flavor = mock_flavor <EOL> else : <EOL> self . instance . old_flavor = None <EOL> return self . imagecache . _get_root_vhd_size_gb ( self . instance ) <EOL> def test_get_root_vhd_size_gb_old_flavor ( self ) : <EOL> ret_val = self . _test_get_root_vhd_size_gb ( ) <EOL> self . assertEqual ( test_flavor . fake_flavor [ '<STR_LIT>' ] , ret_val ) <EOL> def test_get_root_vhd_size_gb ( self ) : <EOL> ret_val = self . _test_get_root_vhd_size_gb ( old_flavor = False ) <EOL> self . assertEqual ( self . instance . root_gb , ret_val ) <EOL> @ mock . patch . object ( imagecache . ImageCache , '<STR_LIT>' ) <EOL> def test_resize_and_cache_vhd_smaller ( self , mock_get_vhd_size_gb ) : <EOL> self . imagecache . _vhdutils . get_vhd_size . return_value = { <EOL> '<STR_LIT>' : ( self . FAKE_VHD_SIZE_GB + <NUM_LIT:1> ) * units . Gi <EOL> } <EOL> mock_get_vhd_size_gb . return_value = self . FAKE_VHD_SIZE_GB <EOL> mock_internal_vhd_size = ( <EOL> self . imagecache . _vhdutils . get_internal_vhd_size_by_file_size ) <EOL> mock_internal_vhd_size . return_value = self . FAKE_VHD_SIZE_GB * units . Gi <EOL> self . assertRaises ( exception . FlavorDiskSmallerThanImage , <EOL> self . imagecache . _resize_and_cache_vhd , <EOL> mock . sentinel . instance , <EOL> mock . sentinel . vhd_path ) <EOL> self . imagecache . _vhdutils . get_vhd_size . assert_called_once_with ( <EOL> mock . sentinel . vhd_path ) <EOL> mock_get_vhd_size_gb . assert_called_once_with ( mock . sentinel . instance ) <EOL> mock_internal_vhd_size . assert_called_once_with ( <EOL> mock . sentinel . vhd_path , self . FAKE_VHD_SIZE_GB * units . Gi ) <EOL> def _prepare_get_cached_image ( self , path_exists , use_cow ) : <EOL> self . instance . image_ref = self . FAKE_IMAGE_REF <EOL> self . imagecache . _pathutils . get_base_vhd_dir . return_value = ( <EOL> self . FAKE_BASE_DIR ) <EOL> self . imagecache . _pathutils . exists . return_value = path_exists <EOL> self . imagecache . _vhdutils . get_vhd_format . return_value = ( <EOL> constants . DISK_FORMAT_VHD ) <EOL> CONF . set_override ( '<STR_LIT>' , use_cow ) <EOL> expected_path = os . path . join ( self . FAKE_BASE_DIR , <EOL> self . FAKE_IMAGE_REF ) <EOL> expected_vhd_path = "<STR_LIT>" % ( expected_path , <EOL> constants . DISK_FORMAT_VHD . lower ( ) ) <EOL> return ( expected_path , expected_vhd_path ) <EOL> @ mock . patch . object ( imagecache . images , '<STR_LIT>' ) <EOL> def test_get_cached_image_with_fetch ( self , mock_fetch ) : <EOL> ( expected_path , <EOL> expected_vhd_path ) = self . _prepare_get_cached_image ( False , False ) <EOL> result = self . imagecache . get_cached_image ( self . context , self . instance ) <EOL> self . assertEqual ( expected_vhd_path , result ) <EOL> mock_fetch . assert_called_once_with ( self . context , self . FAKE_IMAGE_REF , <EOL> expected_path , <EOL> self . instance [ '<STR_LIT>' ] , <EOL> self . instance [ '<STR_LIT>' ] ) <EOL> self . imagecache . _vhdutils . get_vhd_format . assert_called_once_with ( <EOL> expected_path ) <EOL> self . imagecache . _pathutils . rename . assert_called_once_with ( <EOL> expected_path , expected_vhd_path ) <EOL> @ mock . patch . object ( imagecache . images , '<STR_LIT>' ) <EOL> def test_get_cached_image_with_fetch_exception ( self , mock_fetch ) : <EOL> ( expected_path , <EOL> expected_vhd_path ) = self . _prepare_get_cached_image ( False , False ) <EOL> self . imagecache . _pathutils . exists . side_effect = [ False , False , True ] <EOL> mock_fetch . side_effect = exception . InvalidImageRef ( <EOL> image_href = self . FAKE_IMAGE_REF ) <EOL> self . assertRaises ( exception . InvalidImageRef , <EOL> self . imagecache . get_cached_image , <EOL> self . context , self . instance ) <EOL> self . imagecache . _pathutils . remove . assert_called_once_with ( <EOL> expected_path ) <EOL> @ mock . patch . object ( imagecache . ImageCache , '<STR_LIT>' ) <EOL> def test_get_cached_image_use_cow ( self , mock_resize ) : <EOL> ( expected_path , <EOL> expected_vhd_path ) = self . _prepare_get_cached_image ( True , True ) <EOL> expected_resized_vhd_path = expected_vhd_path + '<STR_LIT:x>' <EOL> mock_resize . return_value = expected_resized_vhd_path <EOL> result = self . imagecache . get_cached_image ( self . context , self . instance ) <EOL> self . assertEqual ( expected_resized_vhd_path , result ) <EOL> mock_resize . assert_called_once_with ( self . instance , expected_vhd_path ) </s>
