Skip to content

Testing your deployment locally

When you have made your deployment package you might want to test it out before you deploy it to UbiOps, to make sure your code runs as expected and to quickly filter out small mistakes. You can do so by simulating how UbiOps would initialize your deployment and how it would handle a request. We have prepared a template for you to do just that:

import os
import sys


# Deployment_directory points to the base folder of the deployment, we assume 
# your deployment directory is called 'deployment_package'.
deployment_directory = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 
    'deployment_package'
    )

# Update $PATH with the deployment base directory and libraries before 
# importing the deployment package.
paths = [deployment_directory, os.path.join(deployment_directory, 'libraries')]
sys.path = paths + sys.path

# Import the deployment package
import deployment_package.deployment

print("Starting deployment request example")

# Initialise the deployment. The platform calls this method in exactly the
# same way.
deployment = deployment_package.deployment.Deployment(
    base_directory=deployment_directory, 
    context=dict()
)

# Mock input data. When a request is made, this dictionary is initialized.
# In case of deployments with structured data, it will contain the keys defined 
# by the user upon deployment creation via the platform. In case of a deployment
# that receives a plain input, it will be a string.

# TODO Adjust this example to mimic your input data to test deployment input and
# request processing
input_data = {
    'input_field_1': 10.9
}

# Make the request. UbiOps calls this method in exactly the same way
request_result = deployment.request(input_data)

print("Deployment request result: %s" % request_result)
# Deployment_directory points to the base folder of the deployment, we assume 
# your deployment directory is called 'deployment_package'.
# TODO enter your local absolute path here.
deployment_directory <- file.path("YOUR_LOCAL_ABS_PATH", "deployment_package")

# Import the deployment package
source(file.path(deployment_directory, "deployment.R"))

init( base_directory = deployment_directory, context = list() )

# Mock input data. When a request is made, this named list is initialized.
# In case of deployments with structured data, it will contain the keys defined
# by the user upon deployment creation via the platform. In case of a deployment
# that receives a plain input, it will be a string.

# TODO Adjust this example to mimic your input data to test deployment input and
# request processing
input_data <- list(input_field_1=10.9)

# Make the request. UbiOps calls this method in exactly the same way
request_result <- request( 
    input_data = input_data,
    base_directory = deployment_directory,
    context = list() 
)

print(paste("Deployment request result:", request_result))

The code snippet above should be run from the same directory where your deployment package directory is present. Your folder structure could thus look like this:

-- root folder
    - deployment package directory
    - local_testing_code (Python or R file containing the relevant code snippet)

Make sure to adapt the example input data to a data structure that represents the actual input data your deployment expects. If your deployment expects an input called image for example, which is a jpeg file, your input_data could look like this:

input_data = {
    'image': 'image.jpeg'
}
input_data <- list(
    image="image.jpeg"
)

If you are having any troubles in using this template, do not hesitate to ask for help in our Slack community.