ibm-cloudfaasibm-cloud-code-engine

How to locally test IBM Cloud Code Engine function?


I am developing Python functions for IBM Cloud Code Engine and deploy them from local code. How can I test them locally before deploying them? My goal is to speed up the development cycle and only deploy once I have verified the code's basic functionality.


Solution

  • You could use the following pattern. With your actual function code in a subdirectory func, use this wrapper code (wrapper.py) in the parent directory:

    # import the Code Engine function: func/__main__.py
    from func.__main__ import main
    import sys, json
    
    if __name__ == "__main__":
        # open file, read JSON config
        with open(str(sys.argv[1])) as confFile:
            params=json.load(confFile)
        # invoke the CE function and print the result
        print(main(params))
    

    Then invoke it python wrapper.py params.json. The file params.json would hold a JSON object with the same parameters and system variables as it would be passed to the function by Code Engine. You could then check the output (here just the printed) against whatever you need.