<s> """<STR_LIT>""" <EOL> import apiclient <EOL> import gce_api <EOL> import mock <EOL> from src import basetest <EOL> class GceApiTest ( basetest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . authorized_http = mock . MagicMock ( ) <EOL> self . gce_api = gce_api . GceApi ( <EOL> '<STR_LIT>' , '<STR_LIT>' , self . authorized_http ) <EOL> self . mock_api_build = mock . patch ( '<STR_LIT>' ) . start ( ) <EOL> def tearDown ( self ) : <EOL> mock . patch . stopall ( ) <EOL> def testGetApi ( self ) : <EOL> """<STR_LIT>""" <EOL> self . gce_api . GetApi ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . mock_api_build . call_count ) <EOL> self . assertEqual ( self . authorized_http , <EOL> self . mock_api_build . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:http>' ] ) <EOL> def testGetApi_Twice ( self ) : <EOL> """<STR_LIT>""" <EOL> api1 = self . gce_api . GetApi ( ) <EOL> api2 = self . gce_api . GetApi ( ) <EOL> self . assertEqual ( api1 , api2 ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . mock_api_build . call_count ) <EOL> self . assertEqual ( self . authorized_http , <EOL> self . mock_api_build . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:http>' ] ) <EOL> def testGetInstance ( self ) : <EOL> """<STR_LIT>""" <EOL> instance_info = self . gce_api . GetInstance ( '<STR_LIT>' ) <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_instance_get = mock_api . instances . return_value . get <EOL> mock_instance_get . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , instance = '<STR_LIT>' ) <EOL> mock_instance_get . return_value . execute . assert_called_once_with ( ) <EOL> self . assertEqual ( mock_instance_get . return_value . execute . return_value , <EOL> instance_info ) <EOL> def testGetInstance_NotFound ( self ) : <EOL> """<STR_LIT>""" <EOL> http_error = apiclient . errors . HttpError ( None , None , None ) <EOL> http_error . resp = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_instance_get = mock_api . instances . return_value . get <EOL> mock_instance_get . return_value . execute . side_effect = http_error <EOL> instance_info = self . gce_api . GetInstance ( '<STR_LIT>' ) <EOL> mock_instance_get . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , instance = '<STR_LIT>' ) <EOL> mock_instance_get . return_value . execute . assert_called_once_with ( ) <EOL> self . assertIsNone ( instance_info ) <EOL> def testListInstance_NoFilter ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_instances_list = mock_api . instances . return_value . list <EOL> mock_instances_list . return_value . execute . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:list>' ] <EOL> } <EOL> instance_list = self . gce_api . ListInstances ( ) <EOL> mock_instances_list . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , filter = None ) <EOL> mock_instances_list . return_value . execute . assert_called_once_with ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT:list>' ] , instance_list ) <EOL> def testListInstance_Filter ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_instances_list = mock_api . instances . return_value . list <EOL> mock_instances_list . return_value . execute . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:list>' ] <EOL> } <EOL> instance_list = self . gce_api . ListInstances ( '<STR_LIT>' ) <EOL> mock_instances_list . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , filter = '<STR_LIT>' ) <EOL> mock_instances_list . return_value . execute . assert_called_once_with ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT:list>' ] , instance_list ) <EOL> def testListDisks_NoFilter ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_disk_list = mock_api . disks . return_value . list <EOL> mock_disk_list . return_value . execute . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:list>' ] <EOL> } <EOL> disk_list = self . gce_api . ListDisks ( ) <EOL> mock_disk_list . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , filter = None ) <EOL> mock_disk_list . return_value . execute . assert_called_once_with ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT:list>' ] , disk_list ) <EOL> def testListDisks_Filter ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_disk_list = mock_api . disks . return_value . list <EOL> mock_disk_list . return_value . execute . return_value = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:list>' ] <EOL> } <EOL> disk_list = self . gce_api . ListDisks ( '<STR_LIT>' ) <EOL> mock_disk_list . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , filter = '<STR_LIT>' ) <EOL> mock_disk_list . return_value . execute . assert_called_once_with ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT:list>' ] , disk_list ) <EOL> def testDeleteDisk ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_disk_delete = mock_api . disks . return_value . delete <EOL> self . assertTrue ( self . gce_api . DeleteDisk ( '<STR_LIT>' ) ) <EOL> mock_disk_delete . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , disk = '<STR_LIT>' ) <EOL> mock_disk_delete . return_value . execute . assert_called_once_with ( ) <EOL> def testCreateInstanceWithNewPersistentBootDisk_Success ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_operation_get = mock_api . zoneOperations . return_value . get <EOL> mock_operation_get . return_value . excute . return_value = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_disk_get = mock_api . disks . return_value . get <EOL> mock_disk_get . return_value . execute . side_effect = [ <EOL> None , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> mock_disk_insert = mock_api . disks . return_value . insert <EOL> mock_disk_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> mock_instance_insert = mock_api . instances . return_value . insert <EOL> mock_instance_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> parent_mock = mock . MagicMock ( ) <EOL> parent_mock . attach_mock ( mock_disk_insert , '<STR_LIT>' ) <EOL> parent_mock . attach_mock ( mock_instance_insert , '<STR_LIT>' ) <EOL> self . assertTrue ( self . gce_api . CreateInstanceWithNewPersistentBootDisk ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> method_calls = parent_mock . method_calls . __iter__ ( ) <EOL> call = method_calls . next ( ) <EOL> self . assertEqual ( '<STR_LIT>' , call [ <NUM_LIT:0> ] ) <EOL> call = method_calls . next ( ) <EOL> self . assertEqual ( '<STR_LIT>' , call [ <NUM_LIT:0> ] ) <EOL> def testCreateInstanceWithNewPersistentBootDisk_SuccessWithWarning ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_operation_get = mock_api . zoneOperations . return_value . get <EOL> mock_operation_get . return_value . excute . return_value = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_disk_get = mock_api . disks . return_value . get <EOL> mock_disk_get . return_value . execute . side_effect = [ <EOL> None , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> mock_disk_insert = mock_api . disks . return_value . insert <EOL> mock_disk_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> mock_instance_insert = mock_api . instances . return_value . insert <EOL> mock_instance_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT:code>' : '<STR_LIT>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> } <EOL> self . assertTrue ( self . gce_api . CreateInstanceWithNewPersistentBootDisk ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> mock_disk_insert . return_value . execute . assert_called_once_with ( ) <EOL> mock_instance_insert . return_value . execute . assert_called_once_with ( ) <EOL> def testCreateInstanceWithNewPersistentBootDisk_Error ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_operation_get = mock_api . zoneOperations . return_value . get <EOL> mock_operation_get . return_value . excute . return_value = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_disk_get = mock_api . disks . return_value . get <EOL> mock_disk_get . return_value . execute . side_effect = [ <EOL> None , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> mock_disk_insert = mock_api . disks . return_value . insert <EOL> mock_disk_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> mock_instance_insert = mock_api . instances . return_value . insert <EOL> mock_instance_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:error>' : { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT:code>' : '<STR_LIT>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> } <EOL> } <EOL> self . assertFalse ( self . gce_api . CreateInstanceWithNewPersistentBootDisk ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> mock_disk_insert . return_value . execute . assert_called_once_with ( ) <EOL> mock_instance_insert . return_value . execute . assert_called_once_with ( ) <EOL> def testCreateInstanceWithNewPersistentBootDisk_FailForExistingDisk ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_operation_get = mock_api . zoneOperations . return_value . get <EOL> mock_operation_get . return_value . excute . return_value = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_disk_get = mock_api . disks . return_value . get <EOL> mock_disk_get . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> } <EOL> mock_disk_insert = mock_api . disks . return_value . insert <EOL> mock_disk_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> mock_instance_insert = mock_api . instances . return_value . insert <EOL> mock_instance_insert . return_value . execute . return_value = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> self . assertFalse ( self . gce_api . CreateInstanceWithNewPersistentBootDisk ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertFalse ( mock_disk_insert . called ) <EOL> self . assertFalse ( mock_instance_insert . called ) <EOL> def testDeleteInstance ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_api = self . mock_api_build . return_value <EOL> mock_instances_delete = mock_api . instances . return_value . delete <EOL> self . assertTrue ( self . gce_api . DeleteInstance ( '<STR_LIT>' ) ) <EOL> mock_instances_delete . assert_called_once_with ( <EOL> project = '<STR_LIT>' , zone = '<STR_LIT>' , instance = '<STR_LIT>' ) <EOL> mock_instances_delete . return_value . execute . assert_called_once_with ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> basetest . main ( ) </s>
