How to use shared libraries with JenkinsPipelineUnit from current branch

2.3k Views Asked by At

I'm trying to use jenkinsPipelineUnit to test a JenkinsFile that exists in the same git repository as my shared libraries. This Jenkinsfile references shared libraries located in src. It appears that I must commit my shared library changes before I can test them even if I use localSource from within the retriever.

How can I load my shared libraries and unit test them without committing the code first?

Here is my current code that doesn't work:

    def library = library().name('pipeline-utils')
            .defaultVersion("master")
            .allowOverride(true)
            .implicit(false)
            .targetPath(sharedLibs)
            .retriever(localSource(sharedLibs))
            .build()
    helper.registerSharedLibrary(library)

    try {
         def script = runScript("pipelines/test.groovy")
    }

I get this error:

    file:/Users/<myuserid>/git/pipelines/test.groovy: 2: 
    Error on loading library pipeline-utils@myteam/pipelineUnitTest : 
    Directory /Users/<myuserid>/git/out/test/classes/com/company/test/pipeline-utils@myteam/pipelineUnitTest does not exists @ line 2, column 1. 
    @Library("pipeline-utils@myteam/pipelineUnitTest") _
1

There are 1 best solutions below

0
On

This isn't as easy as it sounds. JenkinsPipelineUnit isn't moving any more since one year while some interesting work is waiting on pull-requests. Here are the steps I had to go through to get this working locally, but also on jenkins where the name of my repository directory can be different each time.

1. Create a custom version of JenkinsPipelineUnit

I started from https://github.com/jenkinsci/JenkinsPipelineUnit/pull/75 but had to add some other changes. These are all the changes:

diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
index f4eeb17..dc13b9c 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
@@ -18,7 +18,7 @@ class LibraryConfiguration {
     String targetPath

     LibraryConfiguration validate() {
-        if (name && defaultVersion && retriever && targetPath)
+        if (name && retriever && targetPath && ((retriever instanceof LocalSource || defaultVersion)))
             return this
         throw new IllegalStateException("LibraryConfiguration is not properly initialized ${this.toString()}")
     }
diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
index 120a316..a253f2d 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
@@ -117,11 +117,14 @@ class LibraryLoader {
     }

     private static boolean matches(String libName, String version, LibraryConfiguration libraryDescription) {
+        if (libraryDescription.allowOverride) {
+            return true
+        }
         if (libraryDescription.name == libName) {
             if (version == null) {
                 return true
             }
-            if (libraryDescription.allowOverride || libraryDescription.defaultVersion == version) {
+            if (libraryDescription.defaultVersion == version) {
                 return true
             }
         }
diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
index 61babde..4edca23 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
@@ -11,7 +11,13 @@ class LocalSource implements SourceRetriever {

     @Override
     List<URL> retrieve(String repository, String branch, String targetPath) {
-        def sourceDir = new File(sourceURL).toPath().resolve("$repository@$branch").toFile()
+        def sourceURLPath = new File(sourceURL).toPath()
+        def sourceDir
+        if (branch) {
+            sourceDir = sourceURLPath.resolve("$repository@$branch").toFile()
+        } else {
+            sourceDir = sourceURLPath.resolve(repository).toFile()
+        }
         if (sourceDir.exists()) {
             return [sourceDir.toURI().toURL()]
         }

2. Register your current repository directory as your shared library

Inspired from: https://github.com/jimcroft/jenkinslib-example/blob/master/test/com/example/TestCase1.groovy

in your TestClass.groovy:

void setup() {
    String repositoryDirectoryName = FilenameUtils.getName(System.getProperty("user.dir"))
    String dirPath = new File( System.getProperty("user.dir") )
            .getAbsoluteFile()
            .getParentFile()
            .getAbsolutePath()

    // This next call bypasses registerSharedLibrary; to allow registering a library with a different directory name
    helper.libraries.put('my-jenkins-library', library(repositoryDirectoryName)
                    .allowOverride(true)
                    .implicit(false)
                    .targetPath(dirPath)
                    .retriever(localSource(dirPath))
                    .build())