JenkinsPipelineUnit 如何与共享声明性管道一起使用?

时间:2021-06-28 01:21:14

标签: jenkins jenkins-pipeline jenkins-groovy

这是我的测试(从文档中的片段中复制)

package vars

import org.junit.Before
import org.junit.Test
import com.lesfurets.jenkins.unit.declarative.*

class ExampleDeclarativePipelineTest extends DeclarativePipelineTest {
  // Setup environment for running unit tests
  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();
  }

  @Test
  public void should_execute_without_errors() throws Exception {
      //def script = runScript("vars/exampleDeclarativePipeline.groovy")
      def script = runScript("vars/Jenkinsfile")
      assertJobStatusSuccess()
      printCallStack()
  }
}

如果我有一个实际的 Jenkinsfile,这会起作用,但我在我的共享库中定义了我的管道(此时的常见模式),它看起来像这样:

def call(Map config) {
    pipeline {
        agent none
        stages {
            stage('Example Build') {
                agent { docker 'maven:3-alpine' }
                steps {
                    echo 'Hello, Maven'
                    sh 'mvn --version'
                }
            }
            stage('Example Test') {
                agent { docker 'openjdk:8-jre' }
                steps {
                    echo 'Hello, JDK'
                    echo 'Hello, JDK'
                    echo 'Hello, JDK'
                    echo 'Hello, JDK'
                    echo 'Hello, JDK'
                    sh 'java -version'
                }
            }
        }
    }
}

如果我将测试指向这个 Groovy 脚本 (def script = runScript("vars/exampleDeclarativePipeline.groovy")),它似乎什么也没做。它执行但我没有得到堆栈跟踪或任何东西(如果它是标准的 Jenkinsfile,它就可以工作)。

如何测试我的共享管道代码?

不确定这是否是一个错误,所以我打开了一个:https://github.com/jenkinsci/JenkinsPipelineUnit/issues/380

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题/障碍;我使用的“解决方案”是创建一个 Jenkinsfile(每个 var 变量),并通过 helper.registerSharedLibrary()ProjectSource)将我的库作为“共享库”包含在内.基于我的解决方法的示例代码:

文件结构:

project_root
├── test
|    ├── vars
|    |    └── test-jenkinsfile.groovy
|    └── TestSpec.groovy
├── vars
|    └── test.groovy
...

test.groovy:

def call(stuff) {
    pipelines {
        agent none
        stages('hello') {
            stage {
                echo 'world'
            }
        }
    }
}

TestSpec.groovy:

...
class TestSpec extends DeclarativePipelineTest {
    @Override
    @Before
    void setUp() throws Exception {
        // Set dir info
        baseScriptRoot = ''
        scriptRoots = ['test/vars/']
        scriptExtension = 'groovy'
        
        
        super.setUp()

        // load own shared library
        def library = LibraryConfiguration.library().name('hello-world')
                        .defaultVersion('<notNeeded>')
                        .allowOverride(true)
                        .implicit(true)
                        .targetPath('<notNeeded>')
                        .retriever(ProjectSource.projectSource())
                        .build()
        helper.registerSharedLibrary(library)

        ...
    }

    @Test
    void runHelloWorldPipeline() {
        runScript('test-jenkinsfile.groovy')
        printCallStack()
        assertJobStatusSuccess()
    }
}

test-jenkinsfile.groovy:

...
@Library('hello-world')  _
test {}

警告:我刚开始(8 小时前)沿着这条路线走,它似乎对我和现在都有效。

相关问题