def test_attach_persistent_disk(self): # mock out interactions with GCE # first, mock out the oauth library calls fake_credentials = flexmock(name='fake_credentials') fake_credentials.should_receive('invalid').and_return(False) fake_storage = flexmock(name='fake_storage') fake_storage.should_receive('get').and_return(fake_credentials) flexmock(oauth2client.file) oauth2client.file.should_receive('Storage').with_args( GCEAgent.OAUTH2_STORAGE_LOCATION).and_return(fake_storage) # next, mock out http calls to GCE fake_http = flexmock(name='fake_http') fake_authorized_http = flexmock(name='fake_authorized_http') flexmock(httplib2) httplib2.should_receive('Http').and_return(fake_http) fake_credentials.should_receive('authorize').with_args(fake_http) \ .and_return(fake_authorized_http) fake_instances = flexmock(name='fake_instances') fake_gce = flexmock(name='fake_gce') fake_gce.should_receive('instances').and_return(fake_instances) fake_attach_disk_request = flexmock(name='fake_attach_disk_request') fake_instances.should_receive('get').and_return(fake_attach_disk_request) attach_disk_info = { 'status': 'DONE', 'disks': [] } fake_attach_disk_request.should_receive('execute').with_args( fake_authorized_http).and_return(attach_disk_info) fake_instances.should_receive('attachDisk').with_args(project=self.project, body=dict, instance='my-instance', zone=str).and_return( fake_attach_disk_request) # finally, inject our fake GCE connection flexmock(discovery) discovery.should_receive('build').with_args('compute', GCEAgent.API_VERSION).and_return(fake_gce) iaas = InfrastructureManager(blocking=True) disk_name = 'my-disk-name' instance_id = 'my-instance' expected = '/dev/sdb' actual = iaas.attach_disk(self.params, disk_name, instance_id, 'secret') self.assertTrue(actual['success']) self.assertEquals(expected, actual['location'])
def test_attach_persistent_disk(self): # mock out interactions with GCE # first, mock out the oauth library calls fake_credentials = flexmock(name='fake_credentials', invalid=False) fake_storage = flexmock(name='fake_storage') fake_storage.should_receive('get').and_return(fake_credentials) flexmock(oauth2client.file) oauth2client.file.should_receive('Storage').with_args( GCEAgent.OAUTH2_STORAGE_LOCATION).and_return(fake_storage) # next, mock out http calls to GCE fake_http = flexmock(name='fake_http') fake_authorized_http = flexmock(name='fake_authorized_http') flexmock(httplib2) httplib2.should_receive('Http').and_return(fake_http) fake_credentials.should_receive('authorize').with_args(fake_http) \ .and_return(fake_authorized_http) fake_instances = flexmock(name='fake_instances') fake_gce = flexmock(name='fake_gce') fake_gce.should_receive('instances').and_return(fake_instances) fake_attach_disk_request = flexmock(name='fake_attach_disk_request') fake_instances.should_receive('get').and_return(fake_attach_disk_request) attach_disk_info = { 'status': 'DONE', 'disks': [] } fake_attach_disk_request.should_receive('execute').with_args( fake_authorized_http).and_return(attach_disk_info) fake_instances.should_receive('attachDisk').with_args(project=self.project, body=dict, instance='my-instance', zone=str).and_return( fake_attach_disk_request) # finally, inject our fake GCE connection flexmock(discovery) discovery.should_receive('build').with_args('compute', GCEAgent.API_VERSION).and_return(fake_gce) iaas = InfrastructureManager(blocking=True) disk_name = 'my-disk-name' instance_id = 'my-instance' expected = '/dev/sdb' actual = iaas.attach_disk(self.params, disk_name, instance_id, 'secret') self.assertTrue(actual['success']) self.assertEquals(expected, actual['location'])
def test_gce_run_instances(self): # mock out interactions with GCE # first, mock out the oauth library calls fake_credentials = flexmock(name='fake_credentials') fake_credentials.should_receive('invalid').and_return(False) fake_storage = flexmock(name='fake_storage') fake_storage.should_receive('get').and_return(fake_credentials) flexmock(oauth2client.file) oauth2client.file.should_receive('Storage').with_args( GCEAgent.OAUTH2_STORAGE_LOCATION).and_return(fake_storage) # next, mock out http calls to GCE fake_http = flexmock(name='fake_http') fake_authorized_http = flexmock(name='fake_authorized_http') flexmock(httplib2) httplib2.should_receive('Http').and_return(fake_http) fake_credentials.should_receive('authorize').with_args(fake_http) \ .and_return(fake_authorized_http) # add some fake data in where no instances are initially running, then one # is (in response to our insert request) no_instance_info = { } instance_id = u'appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d' list_instance_info = { u'items': [{ u'status': u'RUNNING', u'kind': u'compute#instance', u'machineType': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/machineTypes/n1-standard-1', u'name': instance_id, u'zone': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/zones/my-zone-1b', u'tags': {u'fingerprint': u'42WmSpB8rSM='}, u'image': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/images/lucid64', u'disks': [{ u'index': 0, u'kind': u'compute#attachedDisk', u'type': u'EPHEMERAL', u'mode': u'READ_WRITE' }], u'canIpForward': False, u'serviceAccounts': [{ u'scopes': [GCEAgent.GCE_SCOPE], u'email': u'*****@*****.**' }], u'metadata': { u'kind': u'compute#metadata', u'fingerprint': u'42WmSpB8rSM=' }, u'creationTimestamp': u'2013-05-22T11:52:33.254-07:00', u'id': u'8684033495853907982', u'selfLink': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/zones/my-zone-1b/instances/appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d', u'networkInterfaces': [{ u'accessConfigs': [{ u'kind': u'compute#accessConfig', u'type': u'ONE_TO_ONE_NAT', u'name': u'External NAT', u'natIP': u'public-ip' }], u'networkIP': u'private-ip', u'network': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/networks/bazgroup', u'name': u'nic0' }] }], u'kind': u'compute#instanceList', u'id': u'projects/appscale.com:appscale/zones/my-zone-1b/instances', u'selfLink': u'https://www.googleapis.com/compute/v1beta14/projects/961228229472/zones/my-zone-1b/instances' } fake_list_instance_request = flexmock(name='fake_list_instance_request') fake_list_instance_request.should_receive('execute').with_args( http=fake_authorized_http).and_return(no_instance_info).and_return(list_instance_info) fake_instances = flexmock(name='fake_instances') fake_gce = flexmock(name='fake_gce') fake_gce.should_receive('instances').and_return(fake_instances) fake_instances.should_receive('list').with_args(project=self.project, filter="name eq boogroup-.*", zone='my-zone-1b') \ .and_return(fake_list_instance_request) # we only need to create one node, so set up mocks for that add_instance = u'operation-1369248752891-4dd5311848461-afc55a20' add_instance_info = { u'status': u'PENDING', u'kind': u'compute#operation', u'name': add_instance, u'azone': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b', u'startTime': u'2013-05-22T11:52:32.939-07:00', u'insertTime': u'2013-05-22T11:52:32.891-07:00', u'targetLink': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b/instances/appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d', u'operationType': u'insert', u'progress': 0, u'id': u'6663616273628949255', u'selfLink': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b/operations/operation-1369248752891-4dd5311848461-afc55a20', u'user': u'*****@*****.**' } fake_add_instance_request = flexmock(name='fake_add_instance_request') fake_add_instance_request.should_receive('execute').with_args( http=fake_authorized_http).and_return(add_instance_info) fake_instances.should_receive('insert').with_args(project=self.project, body=dict, zone=str).and_return(fake_add_instance_request) created_instance_info = { u'status': u'DONE' } fake_instance_checker = flexmock(name='fake_network_checker') fake_instance_checker.should_receive('execute').and_return( created_instance_info) fake_blocker = flexmock(name='fake_blocker') fake_gce.should_receive('globalOperations').and_return(fake_blocker) fake_blocker.should_receive('get').with_args(project=self.project, operation=add_instance).and_return(fake_instance_checker) # finally, inject our fake GCE connection flexmock(discovery) discovery.should_receive('build').with_args('compute', GCEAgent.API_VERSION).and_return(fake_gce) # next, presume that the persistent disk we want to use exists disk_name = 'my-persistent-disk-1' disk_info = {'status':'DONE'} fake_disk_request = flexmock(name='fake_disk_request') fake_disk_request.should_receive('execute').with_args( http=fake_authorized_http).and_return(disk_info) fake_disks = flexmock(name='fake_disks') fake_disks.should_receive('get').with_args(project=self.project, disk=disk_name, zone=str).and_return(fake_disk_request) fake_disks.should_receive('insert').with_args(project=self.project, sourceImage=str, body=dict, zone=str).and_return(fake_disk_request) fake_gce.should_receive('disks').and_return(fake_disks) public_key = 'ssh-rsa long_key_string' flexmock(utils).should_receive('get_public_key').and_return(public_key) i = InfrastructureManager(blocking=True) # first, validate that the run_instances call goes through successfully # and gives the user a reservation id full_result = { 'success': True, 'reservation_id': self.reservation_id, 'reason': 'none' } self.assertEquals(full_result, i.run_instances(self.params, 'secret')) # next, look at run_instances internally to make sure it actually is # updating its reservation info self.assertEquals(InfrastructureManager.STATE_RUNNING, i.reservations.get( self.reservation_id)['state']) vm_info = i.reservations.get(self.reservation_id)['vm_info'] self.assertEquals(['public-ip'], vm_info['public_ips']) self.assertEquals(['private-ip'], vm_info['private_ips']) self.assertEquals([instance_id], vm_info['instance_ids'])
def test_gce_run_instances(self): # mock out interactions with GCE # first, mock out the oauth library calls fake_credentials = flexmock(name='fake_credentials') fake_storage = flexmock(name='fake_storage') fake_storage.should_receive('get').and_return(fake_credentials) flexmock(oauth2client.file) oauth2client.file.should_receive('Storage').with_args( GCEAgent.OAUTH2_STORAGE_LOCATION).and_return(fake_storage) # next, mock out http calls to GCE fake_http = flexmock(name='fake_http') fake_authorized_http = flexmock(name='fake_authorized_http') flexmock(httplib2) httplib2.should_receive('Http').and_return(fake_http) fake_credentials.should_receive('authorize').with_args(fake_http) \ .and_return(fake_authorized_http) # add some fake data in where no instances are initially running, then one # is (in response to our insert request) no_instance_info = { } instance_id = u'appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d' list_instance_info = { u'items': [{ u'status': u'RUNNING', u'kind': u'compute#instance', u'machineType': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/machineTypes/n1-standard-1', u'name': instance_id, u'zone': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/zones/my-zone-1b', u'tags': {u'fingerprint': u'42WmSpB8rSM='}, u'image': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/images/lucid64', u'disks': [{ u'index': 0, u'kind': u'compute#attachedDisk', u'type': u'EPHEMERAL', u'mode': u'READ_WRITE' }], u'canIpForward': False, u'serviceAccounts': [{ u'scopes': [GCEAgent.GCE_SCOPE], u'email': u'*****@*****.**' }], u'metadata': { u'kind': u'compute#metadata', u'fingerprint': u'42WmSpB8rSM=' }, u'creationTimestamp': u'2013-05-22T11:52:33.254-07:00', u'id': u'8684033495853907982', u'selfLink': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/zones/my-zone-1b/instances/appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d', u'networkInterfaces': [{ u'accessConfigs': [{ u'kind': u'compute#accessConfig', u'type': u'ONE_TO_ONE_NAT', u'name': u'External NAT', u'natIP': u'public-ip' }], u'networkIP': u'private-ip', u'network': u'https://www.googleapis.com/compute/v1beta14/projects/appscale.com:appscale/global/networks/bazgroup', u'name': u'nic0' }] }], u'kind': u'compute#instanceList', u'id': u'projects/appscale.com:appscale/zones/my-zone-1b/instances', u'selfLink': u'https://www.googleapis.com/compute/v1beta14/projects/961228229472/zones/my-zone-1b/instances' } fake_list_instance_request = flexmock(name='fake_list_instance_request') fake_list_instance_request.should_receive('execute').with_args( fake_authorized_http).and_return(no_instance_info).and_return( list_instance_info) fake_instances = flexmock(name='fake_instances') fake_gce = flexmock(name='fake_gce') fake_gce.should_receive('instances').and_return(fake_instances) fake_instances.should_receive('list').with_args(project=self.project, filter="name eq appscale-boogroup-.*", zone='my-zone-1b') \ .and_return(fake_list_instance_request) # we only need to create one node, so set up mocks for that add_instance = u'operation-1369248752891-4dd5311848461-afc55a20' add_instance_info = { u'status': u'PENDING', u'kind': u'compute#operation', u'name': add_instance, u'azone': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b', u'startTime': u'2013-05-22T11:52:32.939-07:00', u'insertTime': u'2013-05-22T11:52:32.891-07:00', u'targetLink': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b/instances/appscale-bazgroup-feb10b11-62bc-4536-ac25-9734f2267d6d', u'operationType': u'insert', u'progress': 0, u'id': u'6663616273628949255', u'selfLink': unicode(GCEAgent.GCE_URL) + u'appscale.com:appscale/zones/my-zone-1b/operations/operation-1369248752891-4dd5311848461-afc55a20', u'user': u'*****@*****.**' } fake_add_instance_request = flexmock(name='fake_add_instance_request') fake_add_instance_request.should_receive('execute').with_args( fake_authorized_http).and_return(add_instance_info) fake_instances.should_receive('insert').with_args(project=self.project, body=dict, zone=str).and_return(fake_add_instance_request) created_instance_info = { u'status': u'DONE' } fake_instance_checker = flexmock(name='fake_network_checker') fake_instance_checker.should_receive('execute').and_return( created_instance_info) fake_blocker = flexmock(name='fake_blocker') fake_gce.should_receive('globalOperations').and_return(fake_blocker) fake_blocker.should_receive('get').with_args(project=self.project, operation=add_instance).and_return(fake_instance_checker) # finally, inject our fake GCE connection flexmock(discovery) discovery.should_receive('build').with_args('compute', GCEAgent.API_VERSION).and_return(fake_gce) # next, presume that the persistent disk we want to use exists disk_name = 'my-persistent-disk-1' disk_info = {'status':'DONE'} fake_disk_request = flexmock(name='fake_disk_request') fake_disk_request.should_receive('execute').with_args( http=fake_authorized_http).and_return(disk_info) fake_disks = flexmock(name='fake_disks') fake_disks.should_receive('get').with_args(project=self.project, disk=disk_name, zone=str).and_return(fake_disk_request) fake_disks.should_receive('insert').with_args(project=self.project, sourceImage=str, body=dict, zone=str).and_return(fake_disk_request) fake_gce.should_receive('disks').and_return(fake_disks) i = InfrastructureManager(blocking=True) # first, validate that the run_instances call goes through successfully # and gives the user a reservation id full_result = { 'success': True, 'reservation_id': self.reservation_id, 'reason': 'none' } self.assertEquals(full_result, i.run_instances(self.params, 'secret')) # next, look at run_instances internally to make sure it actually is # updating its reservation info self.assertEquals(InfrastructureManager.STATE_RUNNING, i.reservations.get( self.reservation_id)['state']) vm_info = i.reservations.get(self.reservation_id)['vm_info'] self.assertEquals(['public-ip'], vm_info['public_ips']) self.assertEquals(['private-ip'], vm_info['private_ips']) self.assertEquals([instance_id], vm_info['instance_ids'])