Handling Unexpected Closure of Testing Executions

In case, your testing execution is aborted because of any failure, it is important to close all the executions left open because the Testing framework was not able to perform a graceful shutdown for example.

Below is a sample code that checks the list of remaining open executions and closes them one by one as part of a post-build section in a Jenkins pipeline script used to execute the tests.

Details on the Get Executions Status List API can be found in our dedicated documentation page Get Executions Status List. For example, you have the ability to query the list of executions with a filter based on the Test Stage name or the LabId in addition to the execution status “created”.

    post {
        aborted{
            echo '[Sealights] Cleanup executions left open.'
            withCredentials([string(credentialsId: 'sl.agent.token', variable: 'SL_TOKEN')]) {
                sh '''
                    set +x
                    SL_SESSION_IDs=(`curl -sX GET "https://$DOMAIN/sl-api/v1/executions?bsid=$BSID&status=created" \
                                              -H "Authorization: Bearer $SL_TOKEN" -H "Content-Type: application/json" \
                    						  | jq -r '.data.list | map(.executionId) | join(" ")'`)
                    #Optional: filter based on the Test Stage name and/or labId (especially relevant in case of parallel testing)
                    
                    echo "Found ${#SL_SESSION_IDs[@]} executions"
                    
                    for id in ${SL_SESSION_IDs[@]}
                    do 
                    	echo -n "Closing Test Session ID $id: "
                    	curl -isX DELETE "https://$DOMAIN/sl-api/v1/test-sessions/$id" \
                    	  -H "Authorization: Bearer $SL_TOKEN" -H "Content-Type: application/json" | grep HTTP
                    done
                '''
            }
        }
    }

Last updated

Was this helpful?