All Projects → ExpediaGroup → Jenkins Spock

ExpediaGroup / Jenkins Spock

Licence: other
Unit-test Jenkins pipeline code with Spock

Programming Languages

groovy
2714 projects

Jenkins Pipeline Support for Spock

Utility classes to help with testing Jenkins pipeline scripts and functions written in Groovy.

User Guide (GroovyDoc)

Add this library to pom.xml in the test scope:

<dependency>
	<groupId>com.homeaway.devtools.jenkins</groupId>
	<artifactId>jenkins-spock</artifactId>
	<scope>test</scope>
</dependency>

Check the CHANGELOG.md to find details about the available versions.

Working Examples

The examples directory contains working sample projects that show off the major kinds of project this library can be used with. Check them out and try building them yourself!

Specifications

This library provides a JenkinsPipelineSpecification class that extends the Spock testing framework's Specification class. To test Jenkins pipeline Groovy code, extend JenkinsPipelineSpecification instead of Specification. Please see the GroovyDoc for JenkinsPipelineSpecification for specific usage information and the Spock Framework Documentation for general usage information.

During the tests of a JenkinsPipelineSpecification suite,

  1. All Jenkins pipeline steps (@StepDescriptors) will be globally callable, e.g. you can just write sh( "echo hello" ) anywhere.
    1. "Body" closure blocks passed to any mock pipeline steps will be executed.
  2. All Jenkins pipeline variables (@Symbols and GlobalVariables) will be globally accessible, e.g. you can just write docker.inside(...) anywhere
  3. All Pipeline Shared Library Global Variables (from the /vars directory) will be globally accessible, so you can just use them anywhere.
  4. All interactions with any of those pipeline extension points will be captured by Spock mock objects.
  5. You can load any Groovy script (Jenkinsfile or Shared Library variable) to unit-test it in isolation.
  6. The Jenkins singleton instance will exist as a Spock mock object.
  7. The CpsScript execution will exist as a Spock spy object (you should never need to interact with this, but it's there).

Dependencies

There are some dependencies of this library that are marked with Maven's provided scope. This means that Maven will pull them in for building and testing this library, but when you use this library you must pull those libraries in as dependencies yourself.

This is done because these dependencies - things like the Jenkins Pipeline API, JUnit, etc - are things that

  1. You absolutely have to have as dependencies in your project in order for this library to be of any use
  2. Should not have their version or final scope controlled by this library

The dependencies that should already be in your project in order for using this library to make any sense are:

<dependency>
	<groupId>org.jenkins-ci.main</groupId>
	<artifactId>jenkins-core</artifactId>
	<version>${jenkins.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.jenkins-ci.plugins.workflow</groupId>
	<artifactId>workflow-step-api</artifactId>
	<version>${jenkins.workflow.step.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.jenkins-ci.plugins.workflow</groupId>
	<artifactId>workflow-cps</artifactId>
	<version>${jenkins.workflow.cps.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.jenkins-ci</groupId>
	<artifactId>symbol-annotation</artifactId>
	<version>${jenkins.symbol.version}</version>
	<scope>test</scope>
</dependency>

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>${junit.version}</version>
	<scope>test</scope>
</dependency>

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>${jenkins.servlet.version}</version>
	<scope>test</scope>
</dependency>

Depending on your parent pom, some of the ${jenkins.version} properties may already be defined. Be sure you define any that are not.

If your code actually writes code against classes in any of these dependencies, remove the <scope>test</scope> entry for the corresponding block(s).

Developer Guide

Building

The build of jenkins-spock is built with Maven. Normal Maven lifecycle phases apply. As long as you have a contemporary (1.8+) JDK and Maven (3.3+), it should build fine.

Testing

Unit tests of jenkins-spock will happen automatically during the test phase of the Maven build.

There is an it Maven Profile that can be activated to run integration tests:

mvn verify -Pit

The integration tests will run mvn verify on some of the Working Example Projects, using the current jenkins-spock code.

Releasing

jenkins-spock should be released by the maven-release-plugin:

mvn clean release:prepare release:perform

In order for this to succeed, the user running this must

  1. Configure GitHub credentials with push access to this repository.
  2. Configure Sonatype Nexus credentials with deploy access to the com.homeaway groupId.
  3. Configure a PGP identity so that the maven-gpg-plugin can sign artifacts.
    1. Locally, run GPG 2.1 or newer
    2. Set the default signing key to the key you want to use
    3. Provide -Dgpg.passphrase on the command-line or run interactively to be able to enter a passphrase
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].