def setUp(self): self.flag_values = copy.deepcopy(FLAGS) self.flag_values.project = 'myproject' self.command = self._CreateCommand() self.api = old_mock_api.CreateMockApi('v1') self.command.SetFlags(self.flag_values) self.command.SetApi(self.api)
def _DoTestDeleteRegionOperation(self, flag_values): command = operation_cmds.DeleteOperation('deleteoperation', flag_values) expected_project = 'region-test-project' expected_operation = 'region-test-operation' submitted_region = 'my-test-region' flag_values.project = expected_project flag_values.region = submitted_region command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() results, exceptions = command.Handle(expected_operation) self.assertEqual(exceptions, []) self.assertEqual(results, '') api = command.api.region_operations self.assertEquals(1, len(api.requests)) request = api.requests[0] self.assertEqual(submitted_region, request.request_payload['region']) self.assertEqual(expected_project, request.request_payload['project']) self.assertEqual(expected_operation, request.request_payload['operation'])
def _DoTestGetOperationGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = operation_cmds.GetOperation('getoperation', flag_values) expected_project = 'test_project' expected_operation = 'test_operation' flag_values.project = expected_project flag_values.service_version = service_version command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() submitted_zone = 'myzone' flag_values.zone = submitted_zone result = command.Handle(expected_operation) self.assertEqual(result['project'], expected_project) self.assertEqual(result['operation'], expected_operation) api = command.api.global_operations api = command.api.zone_operations self.assertEquals(1, len(api.requests)) request = api.requests[0] self.assertEqual(submitted_zone, request.request_payload['zone']) self.assertEqual(expected_project, request.request_payload['project']) self.assertEqual(expected_operation, request.request_payload['operation'])
def _DoTestDeleteOperationGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = operation_cmds.DeleteOperation('deleteoperation', flag_values) expected_project = 'test_project' expected_operation = 'test_operation' flag_values.project = expected_project flag_values.service_version = service_version command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() submitted_zone = 'myzone' flag_values.zone = submitted_zone results, exceptions = command.Handle(expected_operation) self.assertEqual(exceptions, []) self.assertEqual(results, '') # Verify the request api = command.api.zone_operations self.assertEquals(1, len(api.requests)) request = api.requests[0] self.assertEqual(submitted_zone, request.request_payload['zone']) self.assertEqual(expected_project, request.request_payload['project']) self.assertEqual(expected_operation, request.request_payload['operation'])
def _DoTestDeleteGlobalOperationGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = operation_cmds.DeleteOperation('deleteoperation', flag_values) expected_project = 'test_project' expected_operation = 'test_operation' flag_values.project = expected_project flag_values.service_version = service_version flag_values.zone = command_base.GLOBAL_SCOPE_NAME command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() results, exceptions = command.Handle(expected_operation) self.assertEqual(exceptions, []) self.assertEqual(results, '') # Verify the request self.assertEquals(1, len(command.api.global_operations.requests)) request = command.api.global_operations.requests[0] self.assertEqual('delete', request.method_name) self.assertEqual(expected_project, request.request_payload['project']) self.assertEqual(expected_operation, request.request_payload['operation'])
def testSetUsageExportBucketGeneratesCorrectRequest(self): expected_project = 'test_project' bucket = 'my_bucket' report_name_prefix = 'some/interesting/prefix/here' flag_values = copy.deepcopy(FLAGS) command = project_cmds.SetUsageExportBucket( 'setusagebucket', flag_values) flag_values.project = expected_project # Parse is required to set .present to true flag_values['bucket'].Parse(bucket) flag_values['prefix'].Parse(report_name_prefix) command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.projects.setUsageExportBucket = ( OldProjectCmdsTest.SetUsageExportBucket()) result = command.Handle() self.assertEquals(expected_project, result['project']) self.assertEquals( {'kind': 'compute#usageExportLocation', 'bucketName': bucket, 'reportNamePrefix': report_name_prefix}, result['body'])
def _DoTestAddSnapshotWithoutZoneGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.AddSnapshot('addsnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' expected_description = 'test snapshot' submitted_source_disk = 'disk1' disk_zone = 'us-east-a' api_base = 'https://www.googleapis.com/compute/%s' % service_version disk_self_link = '%s/projects/%s/zones/%s/disks/%s' % ( api_base, expected_project, disk_zone, submitted_source_disk) flag_values.service_version = service_version flag_values.source_disk = submitted_source_disk flag_values.project = expected_project flag_values.description = expected_description disks = { 'items': { ('zones/%s' % disk_zone): { 'disks': [{ 'name': 'disk1', 'selfLink': disk_self_link }] } } } class MockDisksApi(old_mock_api.MockDisksApi): def aggregatedList(self, **unused_kwargs): return old_mock_api.MockRequest(disks) command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.disks = MockDisksApi() expected_source_disk = command.NormalizePerZoneResourceName( expected_project, disk_zone, 'disks', submitted_source_disk) result = command.Handle(expected_snapshot) self.assertEqual(result['project'], expected_project) self.assertEqual(result['body']['name'], expected_snapshot) self.assertEqual(result['body']['description'], expected_description) self.assertEqual(result['disk'], submitted_source_disk) self.assertEqual(result['zone'], disk_zone) expected_source_disk = None if expected_source_disk: self.assertEqual(result['body']['sourceDisk'], expected_source_disk)
def testGetProjectGeneratesCorrectRequest(self): flag_values = copy.deepcopy(FLAGS) command = project_cmds.GetProject('getproject', flag_values) expected_project = 'test_project' flag_values.project = expected_project command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() result = command.Handle() self.assertEqual(result['project'], expected_project)
def testSetCommonInstanceMetadataChecksForOverwrites(self): flag_values = copy.deepcopy(FLAGS) command = project_cmds.SetCommonInstanceMetadata( 'setcommoninstancemetadata', flag_values) expected_project = 'test_project' flag_values.project = expected_project flag_values.service_version = 'v1' command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.projects.get = old_mock_api.CommandExecutor( {'commonInstanceMetadata': [{'key': 'sshKeys', 'value': 'foo:bar'}]}) self.assertRaises(gcutil_errors.CommandError, command.Handle)
def testGetRouteGeneratesCorrectRequest(self): flag_values = copy.deepcopy(FLAGS) command = route_cmds.GetRoute('getroute', flag_values) expected_route = 'test_route' flag_values.project = EXPECTED_PROJECT command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() result = command.Handle(expected_route) self.assertEqual(result['project'], EXPECTED_PROJECT) self.assertEqual(result['route'], expected_route)
def testSetUsageExportBucketErrorWhenNoBucket(self): expected_project = 'test_project' flag_values = copy.deepcopy(FLAGS) command = project_cmds.SetUsageExportBucket( 'setusagebucket', flag_values) flag_values.project = expected_project command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.projects.setUsageExportBucket = ( OldProjectCmdsTest.SetUsageExportBucket()) self.assertRaises(app.UsageError, command.Handle)
def _DoTestGetSnapshotGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.GetSnapshot('getsnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' flag_values.project = expected_project flag_values.service_version = service_version command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() result = command.Handle(expected_snapshot) self.assertEqual(result['project'], expected_project) self.assertEqual(result['snapshot'], expected_snapshot)
def testSetCommonInstanceMetadataGeneratesCorrectRequest(self): class SetCommonInstanceMetadata(object): def __call__(self, project, body): self._project = project self._body = body return self def execute(self): return {'project': self._project, 'body': self._body} flag_values = copy.deepcopy(FLAGS) command = project_cmds.SetCommonInstanceMetadata( 'setcommoninstancemetadata', flag_values) expected_project = 'test_project' flag_values.project = expected_project flag_values.service_version = 'v1' handle, path = tempfile.mkstemp() try: with os.fdopen(handle, 'w') as metadata_file: metadata_file.write('foo:bar') metadata_file.flush() flag_values.metadata_from_file = ['sshKeys:%s' % path] command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.projects.get = old_mock_api.CommandExecutor( {'commonInstanceMetadata': [{'key': 'sshKeys', 'value': ''}]}) command.api.projects.setCommonInstanceMetadata = ( SetCommonInstanceMetadata()) result = command.Handle() self.assertEquals(expected_project, result['project']) self.assertEquals( {'kind': 'compute#metadata', 'items': [{'key': 'sshKeys', 'value': 'foo:bar'}]}, result['body']) finally: os.remove(path)
def testDeleteRouteGeneratesCorrectRequest(self): flag_values = copy.deepcopy(FLAGS) command = route_cmds.DeleteRoute('deleteroute', flag_values) expected_route = 'test_route' flag_values.project = EXPECTED_PROJECT command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() results, exceptions = command.Handle(expected_route) self.assertEqual(exceptions, []) self.assertEqual(len(results['items']), 1) result = results['items'][0] self.assertEqual(result['project'], EXPECTED_PROJECT) self.assertEqual(result['route'], expected_route)
def testDeleteMultipleRoutes(self): flag_values = copy.deepcopy(FLAGS) command = route_cmds.DeleteRoute('deleteroute', flag_values) expected_routes = ['test-routes-%02d' % x for x in xrange(100)] flag_values.project = EXPECTED_PROJECT command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() results, exceptions = command.Handle(*expected_routes) self.assertEqual(exceptions, []) results = results['items'] self.assertEqual(len(results), len(expected_routes)) for expected_route, result in zip(expected_routes, results): self.assertEqual(result['project'], EXPECTED_PROJECT) self.assertEqual(result['route'], expected_route)
def testClearUsageExportGeneratesCorrectRequest(self): expected_project = 'test_project' flag_values = copy.deepcopy(FLAGS) command = project_cmds.ClearUsageExportBucket( 'clearusagebucket', flag_values) flag_values.project = expected_project command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.projects.setUsageExportBucket = ( OldProjectCmdsTest.SetUsageExportBucket()) result = command.Handle() self.assertEquals(expected_project, result['project']) self.assertEquals( {'kind': 'compute#usageExportLocation'}, result['body'])
def testDeleteMultipleSnapshots(self): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.DeleteSnapshot('deletesnapshot', flag_values) expected_project = 'test_project' expected_snapshots = ['test-snapshot-%02d' % x for x in xrange(100)] flag_values.project = expected_project command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() results, exceptions = command.Handle(*expected_snapshots) self.assertEqual(exceptions, []) results = results['items'] self.assertEqual(len(results), len(expected_snapshots)) for expected_snapshot, result in zip(expected_snapshots, results): self.assertEqual(result['project'], expected_project) self.assertEqual(result['snapshot'], expected_snapshot)
def _DoTestAddSnapshotRequiresSourceDisk(self, version): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.AddSnapshot('addsnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' expected_description = 'test snapshot' submitted_source_disk = 'disk1' flag_values.service_version = version flag_values.project = expected_project flag_values.description = expected_description flag_values.require_tty = False command.SetFlags(flag_values) def GetDiskPath(disk_name): return 'projects/test_project/zones/zone-a/disks/%s' % (disk_name) disks = { 'items': [ {'name': GetDiskPath('disk1'), 'selfLink': GetDiskPath('disk1')}, {'name': GetDiskPath('disk2'), 'selfLink': GetDiskPath('disk2')}, {'name': GetDiskPath('disk3'), 'selfLink': GetDiskPath('disk3')}]} class MockDisksApi(old_mock_api.MockDisksApi): def list(self, **unused_kwargs): return old_mock_api.MockRequest(disks) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command.api.disks = MockDisksApi() with gcutil_unittest.CaptureStandardIO('1\n\r'): result = command.Handle(expected_snapshot) self.assertEqual(result['disk'], submitted_source_disk)
def _DoTestDeleteSnapshotGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.DeleteSnapshot('deletesnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' flag_values.project = expected_project command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() command._credential = old_mock_api.MockCredential() flag_values.service_version = service_version results, exceptions = command.Handle(expected_snapshot) self.assertEquals(exceptions, []) self.assertEquals(len(results['items']), 1) result = results['items'][0] self.assertEqual(result['project'], expected_project) self.assertEqual(result['snapshot'], expected_snapshot)
def _DoAddRouteGeneratesCorrectRequest(self, service_version, dest_range, next_hop_flag_name, next_hop_str_val): flag_values = copy.deepcopy(FLAGS) command = route_cmds.AddRoute('addroute', flag_values) expected_route = 'test_route' submitted_network = 'test_network' expected_description = 'test route' expected_tags = ['a', 'b'] expected_priority = 7 flag_values.service_version = service_version flag_values.project = EXPECTED_PROJECT flag_values.description = expected_description flag_values.network = submitted_network flag_values.priority = expected_priority flag_values.tags = expected_tags flag_values[next_hop_flag_name].Parse(next_hop_str_val) command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() expected_network = command.NormalizeGlobalResourceName( EXPECTED_PROJECT, 'networks', submitted_network) result = command.Handle(expected_route, dest_range) self.assertEqual(result['project'], EXPECTED_PROJECT) response_body = result['body'] self.assertEqual(response_body['name'], expected_route) self.assertEqual(response_body['network'], expected_network) self.assertEqual(response_body['description'], expected_description) self.assertEqual(response_body['priority'], expected_priority) self.assertEqual(response_body['tags'], expected_tags) return command, response_body
def _DoTestAddSnapshotGeneratesCorrectRequest(self, service_version): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.AddSnapshot('addsnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' expected_description = 'test snapshot' submitted_source_disk = 'disk1' submitted_zone = 'myzone' flag_values.service_version = service_version flag_values.source_disk = submitted_source_disk flag_values.project = expected_project flag_values.description = expected_description command.SetFlags(flag_values) command.SetApi(old_mock_api.CreateMockApi()) command._InitializeContextParser() flag_values.zone = submitted_zone expected_source_disk = command.NormalizePerZoneResourceName( expected_project, submitted_zone, 'disks', submitted_source_disk) result = command.Handle(expected_snapshot) self.assertEqual(result['project'], expected_project) self.assertEqual(result['body']['name'], expected_snapshot) self.assertEqual(result['body']['description'], expected_description) self.assertEqual(result['disk'], submitted_source_disk) self.assertEqual(result['zone'], submitted_zone) expected_source_disk = None if expected_source_disk: self.assertEqual(result['body']['sourceDisk'], expected_source_disk)