Ejemplo n.º 1
0
    def test_call_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]')
        data = 'data3076010'

        # Mock response
        execution_id = 'executionId-1217171550'
        result = 'result-934426595'
        error = 'error96784904'
        expected_response = functions_pb2.CallFunctionResponse(
            execution_id=execution_id, result=result, error=error)
        grpc_stub.CallFunction.return_value = expected_response

        response = client.call_function(name, data)
        self.assertEqual(expected_response, response)

        grpc_stub.CallFunction.assert_called_once()
        args, kwargs = grpc_stub.CallFunction.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = functions_pb2.CallFunctionRequest(name=name,
                                                             data=data)
        self.assertEqual(expected_request, actual_request)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test_delete_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]')

        # Mock response
        expected_response = empty_pb2.Empty()
        operation = operations_pb2.Operation(
            name='operations/test_delete_function', done=True)
        operation.response.Pack(expected_response)
        grpc_stub.DeleteFunction.return_value = operation

        response = client.delete_function(name)
        self.assertEqual(expected_response, response.result())

        grpc_stub.DeleteFunction.assert_called_once()
        args, kwargs = grpc_stub.DeleteFunction.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = functions_pb2.DeleteFunctionRequest(name=name)
        self.assertEqual(expected_request, actual_request)
Ejemplo n.º 4
0
    def test_get_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]')

        # Mock exception response
        grpc_stub.GetFunction.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.get_function, name)
Ejemplo n.º 5
0
    def test_list_functions_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
        location = client.location_path('[PROJECT]', '[LOCATION]')

        # Mock exception response
        grpc_stub.ListFunctions.side_effect = CustomException()

        paged_list_response = client.list_functions(location)
        self.assertRaises(errors.GaxError, list, paged_list_response)
Ejemplo n.º 6
0
    def test_delete_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]')

        # Mock exception response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_delete_function_exception', done=True)
        operation.error.CopyFrom(error)
        grpc_stub.DeleteFunction.return_value = operation

        response = client.delete_function(name)
        self.assertEqual(error, response.exception())
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
from google.cloud.functions.v1beta2 import cloud_functions_service_client
from google.cloud.proto.functions.v1beta2 import functions_pb2
from google.gax.errors import GaxError
from grpc import StatusCode

# point this at your project ID
PROJECT_ID = 'gapic-test'

LOCATION_ID = 'us-central1'
FUNCTION_ID = 'helloWorld'

# upload helloWorld.zip from this directory to Google Storage for your project
SOURCE_URI = 'gs://gapic-functions-v1beta2/helloWorld.zip'

api = cloud_functions_service_client.CloudFunctionsServiceClient()
location = api.location_path(PROJECT_ID, LOCATION_ID)
function_name = api.function_path(PROJECT_ID, LOCATION_ID, FUNCTION_ID)


def on_delete(operation_future):
    try:
        api.get_function(function_name)
    except GaxError as e:
        code = getattr(e.cause, "code", None)
        if callable(code) and code() == StatusCode.NOT_FOUND:
            print(
                'Expect error here since the function should have been deleted'
            )
        else:
            raise