Interview Prep/Jenkins/Shared libraries

How do you unit test a shared library?

AdvancedConceptShared libraries
Answer

Use JenkinsPipelineUnit (Groovy, run with Gradle or Maven). It loads library scripts, mocks steps (helper.registerAllowedMethod('sh', [Map]) { ... }) and records the call stack so you can assert what commands would run.

class TrivyScanTest extends BasePipelineTest {
  @Test void failsOnHighByDefault() {
    def script = loadScript('vars/trivyScan.groovy')
    script.call(input: 'image.tar')
    assert helper.callStack.find { it.methodName == 'sh' }.argsToString().contains('--severity HIGH,CRITICAL')
  }
}

Integration test new versions with a test Jenkinsfile on a staging controller before moving the v1 tag.

All Jenkins questions Roadmap: Jenkins

More on Shared libraries