Advanced example

The installation of ElasTest, Jenkins and the collaboration between them, allows several configurations and offers the following options:

  • tss: ['EUS']: is used to indicate to ElasTest that the job needs to use the EUS service to use a web browser.
  • sut: allows to select an external Sut against which to execute the job.
  • project: allows you to select the ElasTest Project where to create the new TJob from Jenkins.
  • surefireReportsPattern: used to tell ElasTest the path where the test results will be located.
  • monitoring: used to send or not the Sut monitoring traces to Elastest.

Bellow, you will find an example of pipeline where using the ElasTest plugin and implements severals configurations, a SUT is started and a test battery is executed on it. To configure this option, ElasTest provides the connection info using environment variables. This example is included in the Jenkins instance provided by ElasTest. If you use your own Jenkins, you will have to configure it manually in the following way:

WebApp

This pipeline configures the ElasTes plugin, starts the SUT and clones the repository with the test to execute it.

node{
    elastest(tss: ['EUS'], surefireReportsPattern: '**/target/surefire-reports/TEST-*.xml', monitoring: true) {
        stage ('Executing Test') {
            echo 'Print env variables'
            sh 'env'
            def sutImage = docker.image('elastest/demo-web-java-test-sut')
            echo 'Start SUT'
            sutImage.withRun("--name ${env.ET_SUT_CONTAINER_NAME}") { c ->
                echo "${c.id}"
                def sutContainerName = env.ET_SUT_CONTAINER_NAME;
                def sutNetwork = getFirstNetwork(sutContainerName)
                def sutIp = containerIp(sutContainerName, sutNetwork)
                sh 'docker run -e IP=' + sutIp + ' -e PORT=8080 --network=' + sutNetwork + ' elastest/etm-check-service-up'
                withEnv(['ET_SUT_HOST=' + sutIp]) {
                    echo 'Set up test environment'
                    mvnHome = tool 'M3.3.9'
                    echo 'Cloning repository'
                    git 'https://github.com/elastest/demo-projects'
                    echo 'Run test'
                    sh "cd ./web-java-test/;'${mvnHome}/bin/mvn' -Dtest=WebAppTest test"
                }
            }

        }        
    }
}

def getFirstNetwork(containerName) {
    echo "Inside getFirstNetwork function"
    network = sh (
        script: "docker inspect " + containerName + " -f \"\" | awk \"{sub(/:.*/,\\\"\\\")}1\" | awk \"{sub(/\\\"/,\\\"\\\")}1\" | awk \"{sub(/\\\"/,\\\"\\\")}1\" | awk \"{sub(/{/,\\\"\\\")}1\"",
        returnStdout: true
    ).trim()

    echo containerName+" Network = " + network;
    return network;
}

def containerIp(containerName, network) {
    echo "Inside containerIp function"
    containerIp = sh (
        script: "docker inspect --format=\"\" "+ containerName,
        returnStdout: true
    ).trim()

    echo containerName+" IP = " + containerIp;
    return containerIp;
}

The example above can be split into the following sections:

  • ElasTest plugin configuration : You can choose to send the logs of your build

node{
    elastest() {
        .......
    {

  • Sut configuration : The SUT must be started, passing the ${env.ET_SUT_CONTAINER_NAME} env variable (provided by ElasTest) as name of the container. This will allow ElasTest to receive logs and metrics from the Sut.

def sutImage = docker.image('elastest/demo-web-java-test-sut')
            echo 'Start SUT'
            sutImage.withRun("--name ${env.ET_SUT_CONTAINER_NAME}") { c ->

  • Wait for Sut : You have to obtain the Sut network and ip and run check image (elastest/etm-check-service-up) provided by ElasTest to wait for the Sut to be ready to be used.

def sutContainerName = env.ET_SUT_CONTAINER_NAME;
            def sutNetwork = getFirstNetwork(sutContainerName)
            def sutIp = containerIp(sutContainerName,network)
            sh 'docker run -e IP=' + sutIp + ' -e PORT=8080 --network=' + sutNetwork + ' elastest/etm-check-service-up'

  • Test Execution : Finally, the tests should be executed to verify that the SUT is working correctly. Remember that you have to configure the Sut ip as a environment variable or pass it as a maven property due to the browsers we have used need to know where is the SUT located.

....
withEnv(['ET_SUT_HOST=' + sutIp]) {
            echo 'Set up test environment'
            mvnHome = tool 'M3.3.9'
            echo 'Cloning repository'
            git 'https://github.com/elastest/demo-projects'
            echo 'Run test'
            sh "cd ./web-java-test/;'${mvnHome}/bin/mvn' -Dtest=WebAppTest test"
        }
....

You can refresh your mind visiting our tutorial How implement a test.