<s> import uuid <EOL> import mock <EOL> from ooi . api import helpers <EOL> from ooi . api import storage_link as storage_link_api <EOL> from ooi import exception <EOL> from ooi . occi . core import collection <EOL> from ooi . occi . infrastructure import compute <EOL> from ooi . occi . infrastructure import storage <EOL> from ooi . occi . infrastructure import storage_link <EOL> from ooi . tests import base <EOL> from ooi . tests import fakes <EOL> class TestStorageLinkController ( base . TestController ) : <EOL> def setUp ( self ) : <EOL> super ( TestStorageLinkController , self ) . setUp ( ) <EOL> self . controller = storage_link_api . Controller ( mock . MagicMock ( ) , None ) <EOL> @ mock . patch . object ( helpers . OpenStackHelper , "<STR_LIT>" ) <EOL> def test_index ( self , m_volumes ) : <EOL> for tenant in fakes . tenants . values ( ) : <EOL> vols = fakes . volumes [ tenant [ "<STR_LIT:id>" ] ] <EOL> m_volumes . return_value = vols <EOL> ret = self . controller . index ( None ) <EOL> self . assertIsInstance ( ret , collection . Collection ) <EOL> if tenant [ "<STR_LIT:name>" ] == "<STR_LIT>" : <EOL> for idx , vol in enumerate ( vols ) : <EOL> self . assertIsInstance ( ret . resources [ idx ] , <EOL> storage_link . StorageLink ) <EOL> server_id = vol [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> link_id = "<STR_LIT>" % ( server_id , vol [ "<STR_LIT:id>" ] ) <EOL> self . assertEqual ( link_id , ret . resources [ idx ] . id ) <EOL> else : <EOL> self . assertEqual ( [ ] , ret . resources ) <EOL> m_volumes . assert_called_with ( None ) <EOL> @ mock . patch . object ( helpers . OpenStackHelper , "<STR_LIT>" ) <EOL> @ mock . patch . object ( storage_link_api . Controller , "<STR_LIT>" ) <EOL> def test_delete ( self , mock_get , mock_delete ) : <EOL> server_id = uuid . uuid4 ( ) . hex <EOL> vol_id = uuid . uuid4 ( ) . hex <EOL> link_id = "<STR_LIT>" % ( server_id , vol_id ) <EOL> mock_get . return_value = { "<STR_LIT>" : server_id , "<STR_LIT>" : vol_id } <EOL> mock_delete . return_value = [ ] <EOL> ret = self . controller . delete ( None , link_id ) <EOL> self . assertEqual ( [ ] , ret ) <EOL> mock_delete . assert_called_with ( None , server_id , vol_id ) <EOL> mock_get . assert_called_with ( None , link_id ) <EOL> @ mock . patch . object ( storage_link_api . Controller , "<STR_LIT>" ) <EOL> def test_show ( self , m_get ) : <EOL> server_id = uuid . uuid4 ( ) . hex <EOL> vol_id = uuid . uuid4 ( ) . hex <EOL> link_id = "<STR_LIT>" % ( server_id , vol_id ) <EOL> m_get . return_value = { <EOL> "<STR_LIT>" : server_id , <EOL> "<STR_LIT>" : vol_id , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> ret = self . controller . show ( None , link_id ) <EOL> link = ret . pop ( ) <EOL> self . assertIsInstance ( link , storage_link . StorageLink ) <EOL> self . assertIsInstance ( link . source , compute . ComputeResource ) <EOL> self . assertIsInstance ( link . target , storage . StorageResource ) <EOL> self . assertEqual ( vol_id , link . target . id ) <EOL> self . assertEqual ( server_id , link . source . id ) <EOL> m_get . assert_called_with ( None , link_id ) <EOL> @ mock . patch . object ( helpers . OpenStackHelper , "<STR_LIT>" ) <EOL> def test_get_attachment_from_id ( self , m_get ) : <EOL> server_uuid = uuid . uuid4 ( ) . hex <EOL> vol_uuid = uuid . uuid4 ( ) . hex <EOL> link_id = "<STR_LIT>" % ( server_uuid , vol_uuid ) <EOL> m_get . return_value = [ <EOL> { "<STR_LIT>" : uuid . uuid4 ( ) . hex } , <EOL> { "<STR_LIT>" : vol_uuid } , <EOL> ] <EOL> self . assertEqual ( { "<STR_LIT>" : vol_uuid } , <EOL> self . controller . _get_attachment_from_id ( None , <EOL> link_id ) ) <EOL> link_id = "<STR_LIT>" % ( uuid . uuid4 ( ) . hex , uuid . uuid4 ( ) . hex ) <EOL> self . assertRaises ( exception . LinkNotFound , <EOL> self . controller . _get_attachment_from_id , <EOL> None , <EOL> link_id ) <EOL> def test_get_attachment_from_id_invalid ( self ) : <EOL> self . assertRaises ( exception . LinkNotFound , <EOL> self . controller . _get_attachment_from_id , <EOL> None , <EOL> "<STR_LIT>" ) <EOL> @ mock . patch . object ( helpers . OpenStackHelper , "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_create_link ( self , m_get_id , m_validator , m_create ) : <EOL> tenant = fakes . tenants [ "<STR_LIT:foo>" ] <EOL> req = self . _build_req ( tenant [ "<STR_LIT:id>" ] ) <EOL> vol_id = uuid . uuid4 ( ) . hex <EOL> server_id = uuid . uuid4 ( ) . hex <EOL> obj = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : vol_id , <EOL> "<STR_LIT>" : server_id , <EOL> } <EOL> } <EOL> req . get_parser = mock . MagicMock ( ) <EOL> req . get_parser . return_value . return_value . parse . return_value = obj <EOL> m_validator . validate . return_value = True <EOL> m_get_id . side_effect = [ ( '<STR_LIT>' , vol_id ) , ( '<STR_LIT>' , server_id ) ] <EOL> attachment = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : server_id , <EOL> "<STR_LIT>" : vol_id , <EOL> } <EOL> m_create . return_value = attachment <EOL> ret = self . controller . create ( req , None ) <EOL> link = ret . resources . pop ( ) <EOL> self . assertIsInstance ( link , storage_link . StorageLink ) <EOL> self . assertIsInstance ( link . source , compute . ComputeResource ) <EOL> self . assertIsInstance ( link . target , storage . StorageResource ) <EOL> self . assertEqual ( vol_id , link . target . id ) <EOL> self . assertEqual ( server_id , link . source . id ) <EOL> m_create . assert_called_with ( mock . ANY , server_id , vol_id , dev = None ) </s>
