Jenkins pipeline support for Spock

Test Jenkins pipeline code with Spock

License

License

Categories

Categories

Jenkins Build Tools Continuous Integration and Continuous Delivery Spock Application Testing & Monitoring
GroupId

GroupId

com.homeaway.devtools.jenkins
ArtifactId

ArtifactId

jenkins-spock
Last Version

Last Version

2.1.5
Release Date

Release Date

Type

Type

jar
Description

Description

Jenkins pipeline support for Spock
Test Jenkins pipeline code with Spock
Project URL

Project URL

https://github.com/homeaway/jenkins-spock
Source Code Management

Source Code Management

https://github.com/homeaway/jenkins-spock

Download jenkins-spock

How to add to project

<!-- https://jarcasting.com/artifacts/com.homeaway.devtools.jenkins/jenkins-spock/ -->
<dependency>
    <groupId>com.homeaway.devtools.jenkins</groupId>
    <artifactId>jenkins-spock</artifactId>
    <version>2.1.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.homeaway.devtools.jenkins/jenkins-spock/
implementation 'com.homeaway.devtools.jenkins:jenkins-spock:2.1.5'
// https://jarcasting.com/artifacts/com.homeaway.devtools.jenkins/jenkins-spock/
implementation ("com.homeaway.devtools.jenkins:jenkins-spock:2.1.5")
'com.homeaway.devtools.jenkins:jenkins-spock:jar:2.1.5'
<dependency org="com.homeaway.devtools.jenkins" name="jenkins-spock" rev="2.1.5">
  <artifact name="jenkins-spock" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.homeaway.devtools.jenkins', module='jenkins-spock', version='2.1.5')
)
libraryDependencies += "com.homeaway.devtools.jenkins" % "jenkins-spock" % "2.1.5"
[com.homeaway.devtools.jenkins/jenkins-spock "2.1.5"]

Dependencies

compile (7)

Group / Artifact Type Version
org.spockframework : spock-core jar 1.1-groovy-2.4
cglib : cglib-nodep jar 3.2.7
org.objenesis : objenesis jar 2.6
org.reflections : reflections jar 0.9.12
io.github.classgraph : classgraph jar 4.8.104
org.codehaus.groovy : groovy-all jar 2.4.11
org.slf4j : slf4j-api jar 1.7.25

provided (6)

Group / Artifact Type Version
org.jenkins-ci.main » jenkins-core jar 2.102
org.jenkins-ci.plugins.workflow » workflow-step-api jar 2.10
org.jenkins-ci.plugins.workflow » workflow-cps jar 2.36
org.jenkins-ci » symbol-annotation jar 1.10
javax.servlet : javax.servlet-api jar 3.1.0
junit : junit jar 4.12

runtime (4)

Group / Artifact Type Version
org.slf4j : log4j-over-slf4j jar 1.7.25
org.slf4j : jcl-over-slf4j jar 1.7.25
ch.qos.logback : logback-core jar 1.2.3
ch.qos.logback : logback-classic jar 1.2.3

test (5)

Group / Artifact Type Version
org.jenkins-ci.plugins.workflow » workflow-durable-task-step jar 2.17
org.jenkins-ci.plugins.workflow » workflow-basic-steps jar 2.6
org.jenkins-ci.plugins » docker-workflow jar 1.14
org.jenkins-ci.plugins » pipeline-stage-step jar 2.3
org.mockito : mockito-all jar 1.10.19

Project Modules

There are no modules declared in this project.

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
com.homeaway.devtools.jenkins

HomeAway

Versions

Version
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-RC6