Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Saturday, November 2, 2013

Using the Copy Artifact Plugin in a system Groovy script

When Groovy Plugin for Jenkins added build context variables (build, launcher, listener) to the script environment in version 1.13, it gave users the power to do great things in a Groovy script. We have a build that has numerous builds that are triggered by a parent job. Some jobs may not be triggered on every build. The nicest solution was to iterate over the projects that were built and copy their artifacts so that the test results could be aggregated.

One of the difficulties in writing this was dealing with Groovy's dispatch. Jenkins has a compatibility layer to keep older plugins working with a newer version of Jenkins. When written as you would write the code in Java, Groovy selected the deprecated overload of perform regardless of the argument types used in the call. All the deprecated overload did was throw an exception. Groovy looks at the run-time type of arguments and does not care about the declared or reference type of the argument you are using. In this case, Groovy's logic made the deprecated method appear to be the best choice when there were multiple overloads with compatible parameters. Casting did not help here.

This was overcome by using reflection to manually pick the newer method. It is ugly but it works.

Below you can see an example of a system Groovy Script that repeatedly invokes the Copy Artifact Plugin to aggregate the artifacts of jobs triggered with the Parameterized Trigger Plugin. The list of jobs that were built is stored as a comma-delimited list in the build variable PROJS prior to executing this.

 // copy the output from all the triggered jobs by invoking the Copy Artifact plugin for the  
 // builds that were triggered  
 // Assumptions: test output is in test-output folder, output and report files are  
 // named output.xml and report.html  
 import hudson.plugins.copyartifact.SpecificBuildSelector  
 import hudson.plugins.copyartifact.CopyArtifact  
 import hudson.model.AbstractBuild  
 import hudson.Launcher  
 import hudson.model.BuildListener  
 import hudson.FilePath  
   
 def envVars= build.getEnvironment()  
 def projs = envVars.get("PROJS")  
   
 projs.split(",").each {  
   println "Fetching output from " + it + "..."  
   copyTriggeredResults(it)  
 }  
   
 def copyTriggeredResults(projName) {  
   def buildNbr = build.getEnvironment().get("TRIGGERED_BUILD_NUMBER_" + projName)  
   def selector = new SpecificBuildSelector(buildNbr)  
   
   // CopyArtifact(String projectName, String parameters, BuildSelector selector,  
   // String filter, String target, boolean flatten, boolean optional)  
   def copyArtifact = new CopyArtifact(projName, selector, "**/*.*", null, false, false)  
   
   // use reflection because direct call invokes deprecated method  
   // perform(Build<?, ?> build, Launcher launcher, BuildListener listener)  
   def perform = copyArtifact.class.getMethod("perform", AbstractBuild, Launcher, BuildListener)  
   perform.invoke(copyArtifact, build, launcher, listener)  
   
   // rename test output to be output_${project name}.xml  
   def target = new FilePath(build.workspace, "test-output/output_" + projName + ".xml")  
   def source = new FilePath(build.workspace, "test-output/output.xml")  
   source.renameTo(target)  
 }  

Saturday, August 11, 2012

Getting Robot Framework Results in the Email from Jenkins

The report and log pages generated by Robot Framework are great, but it is also nice to have an executive summary of the test results. If you use the Robot Framework Plugin and Email-ext plugin for Jenkins, you can get results in the email body that look like this:

Robot Framework Results


Detailed Report
Pass Percentage: 80.0%
Test Name Status Execution Datetime
Customer
Customer.Ordering
Place Credit Card Order PASS Mon Jul 30 16:17:25 CDT 2012
Save to Wishlist PASS Mon Jul 30 16:19:42 CDT 2012
Place Paypal Order PASS Mon Jul 30 16:29:01 CDT 2012
Customer.History
View Last Order PASS Mon Jul 30 16:32:00 CDT 2012
Shipping Status FAIL Mon Jul 30 16:38:02 CDT 2012

Here is a snippet from the Groovy template that generated the above:

 <%  
 import java.text.DateFormat  
 import java.text.SimpleDateFormat  
 %>  
 <!-- Robot Framework Results -->  
 <%  
 def robotResults = false  
 def actions = build.actions // List<hudson.model.Action>  
 actions.each() { action ->  
  if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction  
   robotResults = true %>  
 <p><h4>Robot Framework Results</h4></p>  
 <p><a href="${rooturl}${build.url}robot/report/report.html">Detailed Report</a></p>  
 <p>Pass Percentage: <%= action.overallPassPercentage %>%</p>  
 <table cellspacing="0" cellpadding="4" border="1" align="center">  
 <thead>  
 <tr bgcolor="#F3F3F3">  
  <td><b>Test Name</b></td>  
  <td><b>Status</b></td>  
  <td><b>Execution Datetime</b></td>  
 </tr>  
 </thead>  
 <tbody>  
 <% def suites = action.result.allSuites  
   suites.each() { suite ->   
    def currSuite = suite  
    def suiteName = currSuite.displayName  
    // ignore top 2 elements in the structure as they are placeholders  
    while (currSuite.parent != null && currSuite.parent.parent != null) {  
     currSuite = currSuite.parent  
     suiteName = currSuite.displayName + "." + suiteName  
    } %>  
 <tr><td colspan="3"><b><%= suiteName %></b></td></tr>  
 <%  DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SS")
    def execDateTcPairs = []
    suite.caseResults.each() { tc ->  
      Date execDate = format.parse(tc.starttime)
      execDateTcPairs << [execDate, tc]
    }
    // primary sort execDate, secondary displayName
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[1].displayName <=> b[1].displayName }
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[0] <=> b[0] }
    execDateTcPairs.each() {
      def execDate = it[0]
      def tc = it[1]  %>
 <tr>  
  <td><%= tc.displayName %></td>  
  <td style="color: <%= tc.isPassed() ? "#66CC00" : "#FF3333" %>"><%= tc.isPassed() ? "PASS" : "FAIL" %></td>  
  <td><%= execDate %></td>  
 </tr>  
 <%  } // tests  
   } // suites %>  
 </tbody></table><%  
  } // robot results  
 }  
 if (!robotResults) { %>  
 <p>No Robot Framework test results found.</p>  
 <%  
 } %>  
The date conversion is there to convert the date from the format Robot uses to the Java default format, which is more familiar to us.

This is the first thing I have ever done in Groovy, and I must say it is a pleasant language to write in. I especially like how getters and setters are mapped to the Groovy concept of properties.

Happy roboting.