简体   繁体   中英

How do I test event trigger cloud function?

I want to do unit testing on cloud functions, however, the function itself is an event trigger function that contains some helper functions. I want to test those helper functions as well. Here is the code sample

import os
from google.cloud import vision
from google.cloud import storage


def face_detection(uri):
    """
    This function detects the faces in the file
    located in Google Cloud Storage or the web
    Args:
        uri: the file located in Google Cloud Storage or the web
    returns:
        None: Prints the likelihood of the face expressions
        or returns an errors resonse in string format
    """

def trigger_event(event, context):
    """
    Background Cloud Function to be triggered by Cloud Storage.
    This generic function logs relevant data when a file is changed.
    Args:
        event (dict):  The dictionary with data specific to this type of event.
                        The `data` field contains a description of the event in
                        the Cloud Storage `object` format described here:
                        https://cloud.google.com/storage/docs/json_api/v1/objects#resource
        context (google.cloud.functions.Context): Metadata of triggering event.
    Returns:
        None; the output is written to Stackdriver Logging
    """

    bucket_name = event["bucket"]
    source_img = event["name"]
    tmp_download_path = f"/tmp/{source_img}"

    try:
        download_image(bucket_name, source_img, tmp_download_path)
        print(f"Image {source_img} downloaded to {tmp_download_path}")
        # currenlty this makes sure there's one person
        # in the frame and prints a few other details
        face_detection(tmp_download_path)
    except Exception:
        pass
    finally:
        if os.path.isfile(tmp_download_path):
            os.remove(tmp_download_path)
        else:
            print("Error: %s file not found" % tmp_download_path)


def download_image(bucket_name, source_blob_name, destination_file_name):
    """Downloads the specified image from cloud storage
    Args:
        bucket_name = "your-bucket-name"
        source_blob_name = "image-name"
        destination_file_name = "local/path/to/file"
    Returns:
        Local path to downloaded image
    """

def upload_processed_images(bucket_name, source_file_folder):
    """Uploads images to the bucket.
    Args:
        bucket_name = "your-bucket-name"
        source_file_folder = Path to the folder that contains
                                all the processed images
    Returns:
        None;
    """

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    for file_name in os.listdir(source_file_folder):
        blob = bucket.blob(file_name)
        blob.upload_from_filename(os.path.join(source_file_folder, file_name))

        print("File {} uploaded to {}.".format(file_name, bucket_name))

For example, if I want to test if the function upload_processed_images is actually working. I want it to be an unit test. Should I test locally? If the function is tested locally, will it connect to GCP with correct storage.Client()? Or I need to test it on the GCP platform?

Thanks for any help.

I actually found a way of testing this. I used functions-framework for the testing. You can find the answer https://github.com/neu-self-image-experiments/sie-backend#test-guidelines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM