Rest Assured and Concourse – How to integrate?

Reading Time: 3 minutes

In this short blog, we will see how to integrate rest assured with the concourse CI pipeline. As we all know, CI/CD is now a major part of the software industry and we as QA engineers must tussle with CI/CD to have our automated test integrated with the pipeline.

This blog is all about that tussle only. So, let’s begin.

What is Concourse?

Before moving ahead, let us quickly get ourselves introduced to Concourse CI. Just like any other CI/CD platform, say, Jenkins, the concourse is is an open-source platform that runs on two main Docker containers. Once Concourse is installed, all you have to do is to use the docker-compose up command to bring up the Concourse server. Concourse uses Postgres as its database. 

To learn more, you may go through the official documentation https://concourse-ci.org/docs.html.

Moving on, let’s see how can we set up a concourse pipeline locally.

Setting up Concourse.

Setting up a concourse is not a difficult task, we just need to run a couple of commands and that would suffice. Let me break the process into steps.

  • Create a docker-compose.yml file for setting up the concourse. We can do so by executing the following cURL in our terminal
curl -O https://concourse-ci.org/docker-compose.yml
  • Do a docker compose up for the YAML file created in the above step
docker-compose up -d
  • That’s it, you have now concourse up and running on port 8080. You can access the pipeline by opening http://localhost:8080/ on your browser.
  • After this, you need to install fly. The Fly tool provides a command-line interface to Concourse. To install, execute the following commands.
$ curl 'http://localhost:8080/api/v1/cli?arch=amd64&platform=linux' -o fly \
$ chmod +x ./fly && mv ./fly /usr/local/bin/
$ fly -t tutorial login -c http://localhost:8080 -u test -p test

This will configure fly and that will be it. Now, we can proceed ahead and create a pipeline. But to create the pipeline and have rest-assured integrated with it, we need to create a new YAML file for the pipeline. Let’s see how we can do that.

Creating the pipeline for rest-assured tests.

There is not much on the web for having rest-assured configured with concourse. So, I’ve done things my way. What I did was, create two separate YAML files. The primary YAML file serves as a medium to send configurations to the pipeline via the fly tool. The YAML file pulls a docker image of maven and runs a small shell script. The shell contains only the test command through which we run our tests.

The approach is simple, isn’t it? Once this is all done, we can send the configurations to the pipeline and that will be all we need to do.

For your reference, these are the YAML and shell files that I’ve used.

Primary YAML file, named pipeline.yaml

resources:
  - name: rest-assured
    type: git
    source:
      uri: ((your_repo_URL_goes_here))
      branch: RestAssured

jobs:
  - name: mavenjob
    serial: true
    plan:
      - get: rest-assured
        trigger: true
      - task: mvn-test
        privileged: true
        file: rest-assured/test.yml

test.yml file looks something like this.

platform: linux

image_resource:
  type: docker-image
  source:
    repository: maven
    tag: latest

inputs:
  - name: rest-assured
outputs:
  - name: mvn-output

run:
  path: /bin/bash
  args:
    - rest-assured/runTest.sh

The shell script looks something like this,

cd rest-assured

mvn test -Dtest=SampleTest

For your reference, the sample rest-assured test script that I’ve used.

package com.knoldus;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.json.simple.*;

public class SampleTest {
    @Test
        //TestNG annotation
    void postNewEmployees() {
        RestAssured.baseURI = "https://reqres.in/";
        RequestSpecification httpRequest = RestAssured.given();

        //Here we created data through json object that we can send along with POST request
        JSONObject requestParams = new JSONObject();
        requestParams.put("name", "abc");
        requestParams.put("job", "manager");

        // specifying headers here, stating that the request body is json
        httpRequest.header("Content-Type", "application/json");

        // add the json body to the request payload
        httpRequest.body(requestParams.toJSONString());

        // Sending POST request
        Response response = httpRequest.request(Method.POST, "api/users");

        //capturing response body to perform validations
        String responseBody = response.getBody().asString();
        Reporter.log(responseBody); // to log the response
        response.prettyPrint();

        // to capture response code through getStatusCode method.
        int status = response.getStatusCode();

        Assert.assertEquals(status, 201);
    }
}

Sending configurations to the pipeline using fly.

Furthermore, we need to send these pipeline jobs and configurations using fly. We can do so, by using the following command.

fly -t tutorial set-pipeline -p demo -c pipeline.yaml

Moving on, once you have executed this command you will see a message on your terminal screen stating something like this.

the pipeline is currently paused. to unpause, either:
  - run the unpause-pipeline command:
    fly -t tutorial unpause-pipeline -p demo
  - click play next to the pipeline in the web ui

On the concourse dashboard, you can see the pipeline that we have just created but the pipeline is paused currently, it won’t execute. To unpause it, use the following command

fly -t tutorial unpause-pipeline -p demo

Now, we have the pipeline ready to be executed.

Just click on the “+” sign. This will trigger the pipeline.

That’s it, folks, I hope you may have liked it. Do check out our other blogs, https://blog.knoldus.com/category/tech-blogs/test-automation/. Thanks!

Refrences

Written by 

Sparsh is a QA Consultant having experience of more than 2 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.