Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. print "=========================================================="
  2. print " Cucumber Testing"
  3. print "=========================================================="
  4.  
  5. node('jenkins-agent'){
  6.  
  7. workspace = pwd() // Set the main workspace in your Jenkins agent
  8.  
  9. authToken = "" // Get your user auth token for OpenShift
  10. apiURL = "" // URL for your OpenShift cluster API
  11.  
  12. gitUser = "" // Set your Git username
  13. gitPass = "" // Set your Git password
  14. gitURL = "" // Set the URL of your test suite repo
  15. gitName = "" // Set the name of your test suite repo
  16. gitBranch = "" // Set the branch of your test suite repo
  17.  
  18. jenkinsUser = "" // Set username for Jenkins
  19. jenkinsPass = "" // Set API token for Jenkins
  20.  
  21. // Set location of OpenShift objects in workspace
  22. buildConfigPath = "${workspace}/${gitName}/ocp/build-config.yaml"
  23. imageStreamPath = "${workspace}/${gitName}/ocp/image-stream.yaml"
  24. jobTemplatePath = "${workspace}/${gitName}/ocp/job-template.yaml"
  25.  
  26. project = "" // Set the OpenShift project you're working in
  27. testSuiteName = "cucumber-test-suite" // Name of the job/build/imagestream
  28.  
  29. // Login to the OpenShift cluster
  30. sh """
  31. set +x
  32. oc login --insecure-skip-tls-verify=true --token=${authToken} ${apiURL}
  33. """
  34.  
  35. // Checkout the test suite repo into your Jenkins agent workspace
  36. int slashIdx = gitURL.indexOf("://")
  37. String urlWithCreds = gitURL.substring(0, slashIdx + 3) +
  38. "\"${gitUser}:${gitPass}\"@" + gitURL.substring(slashIdx + 3);
  39.  
  40. sh """
  41. rm -rf ${workspace}/${gitName}
  42. git clone -b ${gitBranch} ${urlWithCreds} ${gitName}
  43. echo `pwd && ls -l`
  44. """
  45.  
  46. // Create your ImageStream and BuildConfig in OpenShift
  47. // Then start the build for the test suite image
  48. sh """
  49. oc apply -f ${imageStreamPath} -n ${project}
  50. oc apply -f ${buildConfigPath} -n ${project}
  51. oc start-build ${testSuiteName} -n ${project} --follow
  52. """
  53.  
  54. // Get test suite image from correct cluster & project
  55. String imageURL = sh (
  56. script:"""
  57. oc get is/${testSuiteName} -n ${project} --output=jsonpath={.status.dockerImageRepository}
  58. """,
  59. returnStdout: true
  60. )
  61.  
  62. // Set the return URL for the Jenkins input step
  63. inputURL = env.BUILD_URL + "input/Cucumber/proceedEmpty"
  64.  
  65. // Delete existing test suite job for previous test run
  66. // Create new test suite job with latest image
  67. // Pass in input URL, Jenkins username/password, and image
  68. sh """
  69. oc delete job/${testSuiteName} -n ${project} --ignore-not-found=true
  70. oc process -f ${jobTemplatePath} -p \
  71. JENKINS_PIPELINE_RETURN_URL=${inputURL} \
  72. USER_NAME=${jenkinsUser} \
  73. PASSWORD=${jenkinsPass} \
  74. IMAGE=${imageURL}:latest \
  75. -n ${project} | oc create -f - -n ${project}
  76. """
  77.  
  78. // Get list of all running pods in your OpenShift project
  79. String podResults = sh (
  80. script:"""
  81. oc get pods -n ${project} --output=name
  82. """,
  83. returnStdout: true
  84. )
  85.  
  86. // Get job logs and exit upon job completion
  87. // If job pod hasn't spun up yet, waits 5s and tries again
  88. // If job pod hasn't spun up after 10 tries, fails
  89. numTry = 0
  90. podList = podResults.split('\n')
  91. for (int x = 0; x < podList.size(); x++){
  92. String pod = podList[x]
  93. // Look for running test suite job and not the build pod
  94. if(pod.contains("${testSuiteName}") && !pod.contains("build")){
  95. while(numTry < 10){
  96. try {
  97. sh """
  98. sleep 5s
  99. oc logs ${pod} -n ${project}
  100. """
  101. numTry = 11
  102. } catch(e) {
  103. print e
  104. numTry++
  105. print "Checking if job container is up and running..."
  106. }
  107. }
  108. if(numTry == 10) {
  109. error("Job did not spin up in ${project}.")
  110. }
  111. jobPod = pod
  112. }
  113. }
  114.  
  115. // Get succinct pod name
  116. jobPod = jobPod.replaceAll("pod/","")
  117.  
  118. // Print a link to the test suite job in OpenShift
  119. print "Watch the test suite logs as they run here:"
  120. print "${apiURL}/console/project/${project}/browse/pods/${jobPod}?tab=logs"
  121.  
  122. // Run two branches in parallel:
  123. // - one waits for the job to return and gets the test results
  124. // - the other follows the logs of the test suite pod
  125. def testingBranches = [:]
  126.  
  127. testingBranches["input"] = {
  128.  
  129. // Create an input step that will be called by the test suite job
  130. // This will let Jenkins know that it's time to retrieve the Cucumber results
  131. print "Please don't click Proceed. The OpenShift pod will call Jenkins when the tests have completed."
  132. input id: "Cucumber", message: "Waiting for testing to finish..."
  133.  
  134. // Copy the results folder from the test suite pod to the Jenkins agent
  135. print "Retrieving Cucumber results files from pod..."
  136. sh """
  137. oc rsync ${jobPod}:/tmp/src/reports . -n ${project}
  138. """
  139.  
  140. print "=========================================================="
  141. print " Cucumber Reports"
  142. print "=========================================================="
  143.  
  144. // Cucumber Reports Plugin parses the JSON files
  145. // Uses "reports" directory copied from test suite pod
  146. step([$class: 'CucumberReportPublisher',
  147. jenkinsBasePath: '',
  148. fileIncludePattern: '',
  149. fileExcludePattern: '',
  150. jsonReportDirectory: 'reports',
  151. ignoreFailedTests: true,
  152. missingFails: false,
  153. pendingFails: false,
  154. skippedFails: false,
  155. undefinedFails: false,
  156. parallelTesting: false])
  157.  
  158. // Cucumber Reports Plugin charts/graphs at this link
  159. print "Cucumber test results available here:"
  160. def intTestURL = env.BUILD_URL + "cucumber-html-reports/overview-features.html"
  161. print intTestURL
  162.  
  163. } // branch for input
  164.  
  165. testingBranches["logs"] = {
  166.  
  167. print "=========================================================="
  168. print " Test Suite Logs"
  169. print "=========================================================="
  170.  
  171. // Print test suite logs as they happen
  172. // This branch will exit after the "sleep 1m" in runjob.sh
  173. sh """
  174. oc logs -f ${jobPod} -n ${project}
  175. """
  176.  
  177. } // branch for logs
  178.  
  179. // Runs the two branches in parallel
  180. parallel testingBranches
  181.  
  182. } // node
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement