Integrate IoTIFY with Jenkins

Integrate continuous performance testing with IOTIFY. Trigger nightly performance tests using IOTIFY APIs.

Introduction

Continuous performance testing is a key requirement to build a scalable cloud platform. Every code commit - even if small - could have a detrimental impact on the performance of your IOT cloud software.
Therefore it is essential to do a nightly performance test on your build and find out which commit had the positive / negative impact on the performance.

Jenkins is one of the most widely used continuous build server in production. In this tutorial we will explain how to invoke IoTIFY Network simulator run through API within a Jenkins build step.

Prerequisite: Create a template

The first step is to create a template in IoTIFY using our web based UI. The template should have the endpoints configured to your staging deployment, or any other deployment which you want to test.
Make sure that the template works by manually running the simulation and validating the results.

Step 1: Configure Jenkins Build server

Install Jenkins using this guide and create a simple build project with Jenkins. If you already have a Jenkins build server, you could skip this step.

Step 2: Acquire an API Token

In order to use IOTIFY API, you will first need to acquire an API token. Go to your settings page in IoTIFY and click on the API section on the left hand menu.
There you could allocate a new token as follows:

Step 3: Install Jenkins HTTP Plugin

We will install HTTP Request Plugin with Jenkins which will make it easy to trigger a performance test once the build is finished. Once the plugin is successfully installed,
we will be able to now add a new build step named HTTP request into our project.

Step 4: Add a new step to your build

Ideally, the testing step should be added once the build is success, unit tests have been passed and software is deployed to pre-staging area.
Add a new build step to your pipeline which invokes HTTP request. Enter following values in the default fields

URL: https://api.iotify.io/api/v1/template
HTTP Mode: POST

In header section, add two custom headers as follows:

Content-Type: application/json
X-Auth: Your API Token as obtained in Step 2.

In the Body tab, enter following values

The parameter template specifies the name of the template which needs to be started.

That’s it. Now save the build step and trigger the build.

Next time your software build is started, the simulation will be automatically triggered.

Note that this step will only trigger the simulation but will not wait for it to finish and log the results. If you would like to also have results included into the build logs, lets follow the next step.

(Optional) Script Based Approach

There are certain cases where you want to wait for the test results to be finished and add them into your build logs. To do that, we will use a bash script instead of HTTP Request plugin.

Note that this step is an alternative step and you would not need any of the above steps if you are following this approach.

Make sure you install jq in your jenkins environment.

Create a new build step with Bash script and enter following in the script

#!/bin/bash
# !Important: Note there is a global rate limit of 10 sec per request. Appropriate delays have been added to honor the rate limits.
# Optional install jq if not available in your system
sudo apt install -y jq
# Provide your API token below
TOKEN=
#Name of the template
TEMPLATE=hive
STATUS="pending"
#Invoke the run. Configure number of clients and iteration below
RESULT=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" \
-d '{"template": "'$TEMPLATE'", "repeat": 5, "interval":10, "clients":1 }' \
https://api.iotify.io/api/v1/template)
JOB=$(echo $RESULT | jq '.job')
SUCCESS=$(echo $RESULT | jq '.success')
if [ "$SUCCESS" == "true" ];then
echo "Successfully launched template ${TEMPLATE} with job ID=${JOB}"
sleep 10
attempt_counter=0
max_attempts=100
while [[ "$STATUS" != *finished* ]]; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
exit 1
fi
attempt_counter=$(($attempt_counter+1))
sleep 10
STATUS=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" -d '{"job": '"$JOB"'}' https://api.iotify.io/api/v1/job | jq '.status.status')
printf .
done
sleep 10
STATUS=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" -d '{"job": '"$JOB"'}' https://api.iotify.io/api/v1/job)
echo "Finished with status ${STATUS}"
else
echo "Failed to launch template run. ${RESULT}"
fi
exit

The script will launch the template and wait for it to finish before exiting with the results.