Skip to content

Instantly share code, notes, and snippets.

@daxroc
Last active January 20, 2020 10:03
Show Gist options
  • Save daxroc/de8bce8b69fdaf767f33933c3466f850 to your computer and use it in GitHub Desktop.
Save daxroc/de8bce8b69fdaf767f33933c3466f850 to your computer and use it in GitHub Desktop.
Jenkins Pipeline Unit testing shared library
#!/usr/bin/env groovy
import com.lesfurets.jenkins.unit.global.lib.Library
@Library("example.common@master")
import com.example.jenkins.agents.DeploymentAgent
def execute(){
node() {
DeploymentAgent agent = new DeploymentAgent()
//println new DeploymentAgent().metaClass.methods*.name.sort().unique()
stage("Setup"){
agent.example()
}
}
}
return this
package com.example.jenkins.agents
void example(){
echo "example running"
}
return this
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.LocalSource.localSource
import com.lesfurets.jenkins.unit.BaseRegressionTest
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Before
import org.junit.Test
class TestDeploymentAgent extends BaseRegressionTest {
@Override
@Before
void setUp() throws Exception{
helper.scriptRoots += 'src'
helper.scriptRoots += 'vars'
helper.scriptRoots += 'test'
helper.scriptRoots += 'jobs'
callStackPath = 'test/resources/callstacks/'
String sharedLibs = this.class.getResource('./').getFile()
def library = library()
.name('example.common')
.allowOverride(false)
.retriever(localSource(sharedLibs))
.targetPath(sharedLibs)
.defaultVersion("master")
.implicit(false)
.build()
helper.registerSharedLibrary(library)
super.setUp()
}
@Test
void should_execute_without_errors() throws Exception {
Script script = loadScript("jobs/agent.jenkins")
script.execute()
printCallStack()
assertJobStatusSuccess()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment