【问题标题】:How to use shared libraries with JenkinsPipelineUnit from current branch如何使用当前分支的 JenkinsPipelineUnit 共享库
【发布时间】:2018-02-01 14:01:40
【问题描述】:

我正在尝试使用 jenkinsPipelineUnit 来测试与我的共享库位于同一 git 存储库中的 JenkinsFile。此 Jenkinsfile 引用位于 src 中的共享库。看来我必须先提交我的共享库更改,然后才能测试它们,即使我在检索器中使用 localSource。

如何在不先提交代码的情况下加载我的共享库并对它们进行单元测试?

这是我当前不起作用的代码:

    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")
    }

我收到此错误:

    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") _

【问题讨论】:

  • 这听起来有点像JenkinsPipelineUnit/75JenkinsPipelineUnit/64
  • 谢谢!这两个拉取请求正是我想要实现的目标:直接测试共享库。因此,我的问题的当前答案可能是“当前不支持”。但是我想知道是否有人可以推荐一种直接利用 PipelineTestHelper 类的方法......这似乎是注入允许方法的神奇之处。也许答案就在其中一个拉取请求的细节中。

标签: unit-testing jenkins-pipeline jenkins-pipeline-unit


【解决方案1】:

这并不像听起来那么容易。 JenkinsPipelineUnit 一年以来一直没有移动,而一些有趣的工作正在等待拉取请求。以下是我必须执行的步骤才能在本地工作,但在 jenkins 上我的存储库目录的名称每次都可能不同。

1。创建 JenkinsPipelineUnit 的自定义版本

我从https://github.com/jenkinsci/JenkinsPipelineUnit/pull/75 开始,但必须添加一些其他更改。这些都是变化:

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。将您当前的存储库目录注册为您的共享库

灵感来自:https://github.com/jimcroft/jenkinslib-example/blob/master/test/com/example/TestCase1.groovy

在您的 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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2013-11-17
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多