Kubernetes

What do I need to run jenkins jobs at ElasTest with Kubernetes?

If you have already used ElasTest with docker and have run Jenkins jobs there are only a few small changes, if not, check out this documentation to see how to configure the ElasTest plugin for Jenkins.

Once you have configured the ElasTest plugin and modified your Job's pipeline, all you have to do is follow these steps:

  • Install Kubernetes CLI Plugin
  • Modify your pipeline script to add this block just below the elastest block:

    withKubeConfig([credentialsId: 'k8s-api-token', serverUrl: env.K8S_URL]) {

    Your pipeline will look like this:

        elastest(...) {
            withKubeConfig([credentialsId: 'k8s-api-token', serverUrl: env.K8S_URL]) {
                ...
            }
        }
    

Running a Demo Job

The Jenkins instance provided by ElasTest offers by default several example Jobs to run with Kubernetes. Let's see the pipeline script of one of them, jenkins-junit5-multi-browser-test-on-k8s:

node {
    elastest(tss: ['EUS'], surefireReportsPattern: '**/target/surefire-reports/TEST-*.xml', project: 'Jenkins Examples') {
        withKubeConfig([credentialsId: 'k8s-api-token', serverUrl: env.K8S_URL]) {
            try {
                stage("Start SUT") {
                    git(
                        url: 'https://github.com/elastest/demo-projects'
                        )
                    sh "cd ./webapp/junit5-web-multiple-browsers-test/k8s;./addSutPrefix.sh;kubectl create -f ."
                    checkPodStatus(1, "${ET_SUT_CONTAINER_NAME}-demo-web-java-sut")
                }

                stage("Run tests") {
                    mvnHome = tool 'M3.3.9'
                    def sutIp = getPodIp('demo-web-java-sut')
                    waitForService(5, "http://" + sutIp + ":8080")
                    withEnv(['ET_SUT_HOST=' + sutIp]) {
                        echo "Running test"
                        sh "cd ./webapp/junit5-web-multiple-browsers-test/;'${mvnHome}/bin/mvn' -Dbrowser=chrome -DforkCount=0 test"
                    }
                }

            } finally {
                sh "cd ./webapp/junit5-web-multiple-browsers-test/;kubectl delete -f k8s/"
                junit "target/*-reports/TEST-*.xml"
            }
        }
    }
}

def getPodIp(podName) {
    echo "Retrive pod ip"
    def podIp = sh (
        script: "kubectl get pod -o wide | grep " + podName + " | awk '{print \$6}'",
        returnStdout: true
    ).split( '\n' ).first()

    echo podName+" IP = " + podIp;
    return podIp;
}

def waitForService(time, serviceUrl) {
    timeout(time) {
        waitUntil {
            echo "Waiting for the service " + serviceUrl + " is ready"
            script {
                def r = sh script: 'wget -q ' + serviceUrl + ' -O /dev/null', returnStatus: true
                return (r == 0);
           }
        }
    }
}

def checkPodStatus(time, podName) {
    timeout(time) {
        waitUntil {
            echo "Waiting for the pod " + podName + " is created"
            script {
                def r = sh script: 'kubectl get pods -l io.elastest.service=' + podName.replace('_','-') + " | awk '{print \$3}'", returnStdout: true
                def status = r.split("\n").last()
                echo "Pod ${podName} current status: " + status
                return ( status == 'Running');
           }
        }
    }
}

Let's look at a few lines in detail:

  • ElasTest configuration: This Job executes a E2E test. For this make use of EUS to start browsers

          elastest(tss: ['EUS'], surefireReportsPattern: '**/target/surefire-reports/TEST-*.xml', project: 'Jenkins Examples') {
              ...
          }
    
  • The Kubernetes config block:

          withKubeConfig([credentialsId: 'k8s-api-token', serverUrl: env.K8S_URL]) {
              ...
          }
    
  • Start and wait for Sut: This Job starts a Sut via Kubernetes using a yaml file.

          git(
              url: 'https://github.com/elastest/demo-projects'
              )
          sh "cd ./webapp/junit5-web-multiple-browsers-test/k8s;./addSutPrefix.sh;kubectl create -f ."
    

    Then waits until the Sut pod is created:

          checkPodStatus(1, "${ET_SUT_CONTAINER_NAME}-demo-web-java-sut")
    

    After, gets the ip of the Sut and waits until the Sut is ready:

          def sutIp = getPodIp('demo-web-java-sut')
          waitForService(5, "http://" + sutIp + ":8080")
    
  • Run tests:

          withEnv(['ET_SUT_HOST=' + sutIp]) {
              echo "Running test"
              sh "cd ./webapp/junit5-web-multiple-browsers-test/;'${mvnHome}/bin/mvn' -Dbrowser=chrome -DforkCount=0 test"
          }
    
Running Job

You can run the Job and navigate to ElasTest to view it. You can easily access it from the Jenkins run console:

This test suite has 3 test cases, of which 2 finish correctly and 1 of them fails. Once the execution is finished you can view all the information in ElasTest: