Skip to content

A helper library that enables shared libraries to be validated before running in Jenkins

License

Notifications You must be signed in to change notification settings

HealthPartners/jenkins-shared-library-test-helper

Repository files navigation

Jenkins Shared Library Test Helper

Build StatusBintray Versioncodecov

This code was developed to allow Jenkins shared libraries to be tested before being pushed to Jenkins. This library allows Spock tests to be written against these Jenkins scripts.

Getting Started

Example

There is an example in the example-shared-lib-structure directory, providing you with a basic outline for beginning to write tests for Jenkins scripts. As you can see in the build.gradle file, this library is published to JCenter for consumption by any project.

Here is a simple Gradle block that calls out the dependency and loads it for use for tests.

Gradle
repositories {
  jcenter()
}

dependencies {
  testCompile 'com.healthpartners.jenkins:jenkins-shared-library-test-helper:VERSION'
}

Testing Shared Library Scripts

This library is then meant to be used with Groovy scripts in the vars/ directory that are loaded into Jenkins as functions. The special rule is that the script must end with return this.

def call(args) {
    //execution
}

return this

To test these scripts, do the following:

  1. Construct a binding object
  2. Initialize the script
def MyScriptSpec extends BasePipelineTest {

    def 'this is a spock test'() {
        given:
        //binding object
        def binding = initializeMocks('mockFunction', 'anotherFunction')
        //initialize the script
        def script = getShell(binding, 'test/testScript.groovy')
        
        when:
        //execute the call function on the script
        def scriptOutput = script.call()
        
        then:
        //do validations
        scriptOutput == "great stuff here"
        
        //interact with binding mocks
        1*binding.mockFunction.call() >> 2
        
        //binding mocks can throw exceptions too
        1*binding.anotherFunction.call() >> { throw new RuntimeException("badness")}
    }
}

Other Features

This library comes with three utility interfaces that can be used as mocks within testing your groovy scripts. Each of them is meant to encompass some of the features that Jenkins would provide in the pipeline interface when executing the script.

Built With

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

A helper library that enables shared libraries to be validated before running in Jenkins

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages