def test_list_functions(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = cloud_functions_service_client.CloudFunctionsServiceClient() # Mock request location = client.location_path('[PROJECT]', '[LOCATION]') # Mock response next_page_token = '' functions_element = functions_pb2.CloudFunction() functions = [functions_element] expected_response = functions_pb2.ListFunctionsResponse( next_page_token=next_page_token, functions=functions) grpc_stub.ListFunctions.return_value = expected_response paged_list_response = client.list_functions(location) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.functions[0], resources[0]) grpc_stub.ListFunctions.assert_called_once() args, kwargs = grpc_stub.ListFunctions.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = functions_pb2.ListFunctionsRequest( location=location) self.assertEqual(expected_request, actual_request)
def on_init(_): function = functions_pb2.CloudFunction( name=function_name, source_archive_url=SOURCE_URI, pubsub_trigger=('projects/%s/topics/hello_world' % PROJECT_ID)) response = api.create_function(location, function) response.add_done_callback(on_create) print('Metadata: \n%s\n' % response.metadata())
def test_update_function(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = cloud_functions_service_client.CloudFunctionsServiceClient() # Mock request name = client.function_path('[PROJECT]', '[LOCATION]', '[FUNCTION]') function = functions_pb2.CloudFunction() # Mock response name_2 = 'name2-1052831874' source_archive_url = 'sourceArchiveUrl-289007026' latest_operation = 'latestOperation-1633164625' entry_point = 'entryPoint-799136893' available_memory_mb = 1964533661 service_account = 'serviceAccount-1948028253' expected_response = functions_pb2.CloudFunction( name=name_2, source_archive_url=source_archive_url, latest_operation=latest_operation, entry_point=entry_point, available_memory_mb=available_memory_mb, service_account=service_account) operation = operations_pb2.Operation( name='operations/test_update_function', done=True) operation.response.Pack(expected_response) grpc_stub.UpdateFunction.return_value = operation response = client.update_function(name, function) self.assertEqual(expected_response, response.result()) grpc_stub.UpdateFunction.assert_called_once() args, kwargs = grpc_stub.UpdateFunction.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = functions_pb2.UpdateFunctionRequest( name=name, function=function) self.assertEqual(expected_request, actual_request)
def on_create(operation_future): result = operation_future.result() print('Function created: \n%s\n' % result) updated_function = functions_pb2.CloudFunction( name=result.name, source_archive_url=result.source_archive_url, pubsub_trigger=('projects/%s/topics/hello_world2' % PROJECT_ID)) response = api.update_function(result.name, updated_function) response.add_done_callback(on_update) print('Metadata: \n%s\n' % response.metadata())
def test_update_function_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = cloud_functions_service_client.CloudFunctionsServiceClient() # Mock request name = client.function_path('[PROJECT]', '[LOCATION]', '[FUNCTION]') function = functions_pb2.CloudFunction() # Mock exception response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_update_function_exception', done=True) operation.error.CopyFrom(error) grpc_stub.UpdateFunction.return_value = operation response = client.update_function(name, function) self.assertEqual(error, response.exception())