嘲弄詹金斯管道步骤

mid*_*ori 3 groovy mocking jenkins jenkins-pipeline

我有一个我在jenkinsfile中使用的类,它的简化版本在这里:

class TestBuild {
    def build(jenkins) {
        jenkins.script {
            jenkins.sh(returnStdout: true, script: "echo build")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

this当我在jenkinsfile中使用它时,我提供了jenkins参数.什么是模拟jenkins对象的最佳方法,这里有脚本和sh?谢谢你的帮助

Jon*_*n S 6

我在另一周有类似的问题,我想出了这个:

import org.jenkinsci.plugins.workflow.cps.CpsScript

def mockCpsScript() {
    return [
        'sh': { arg ->
            def script
            def returnStdout
            // depending on sh is called arg is either a map or a string vector with arguments
            if (arg.length == 1 && arg[0] instanceof Map) {
                script = arg[0]['script']
                returnStdout = arg[0]['returnStdout']
            } else {
                script = arg[0]
            }
            println "Calling sh with script: ${script}"
        },
        'script' : { arg ->
              arg[0]()
        },
    ] as CpsScript
}
Run Code Online (Sandbox Code Playgroud)

并与您的脚本一起使用(使用非命名的sh调用扩展):

class TestBuild {
    def build(jenkins) {
        jenkins.script {
            jenkins.sh(returnStdout: true, script: "echo build")
            jenkins.sh("echo no named arguments")
        }
    }
}

def obj = new TestBuild()
obj.build(mockCpsScript())
Run Code Online (Sandbox Code Playgroud)

它输出:

[Pipeline] echo
Calling sh with script: echo build
[Pipeline] echo
Calling sh with script: echo no named arguments
Run Code Online (Sandbox Code Playgroud)

现在这本身并不是很有用,但很容易添加定义mock方法行为的逻辑,例如,这个版本控制readFile返回的内容,具体取决于正在读取的目录和文件:

import org.jenkinsci.plugins.workflow.cps.CpsScript

def mockCpsScript(Map<String, String> readFileMap) {
    def currentDir = null
    return [
        'dir' : { arg ->
            def dir = arg[0]
            def subClosure = arg[1]
            if (currentDir != null) {
                throw new IllegalStateException("Dir '${currentDir}' is already open, trying to open '${dir}'")
            }
            currentDir = dir
            try {
                subClosure()
            } finally {
                currentDir = null
            }
        },
        'echo': { arg ->
            println(arg[0])
        },
        'readFile' : { arg ->
            def file = arg[0]
            if (currentDir != null) {
                file = currentDir + '/' + file
            }
            def contents = readFileMap[file]
            if (contents == null) {
                throw new IllegalStateException("There is no mapped file '${file}'!")
            }
            return contents
        },
        'script' : { arg ->
              arg[0]()
        },
    ] as CpsScript
}

class TestBuild {
    def build(jenkins) {
        jenkins.script {
            jenkins.dir ('a') {
                jenkins.echo(jenkins.readFile('some.file'))
            }
            jenkins.echo(jenkins.readFile('another.file'))
        }
    }
}

def obj = new TestBuild()
obj.build(mockCpsScript(['a/some.file' : 'Contents of first file', 'another.file' : 'Some other contents']))
Run Code Online (Sandbox Code Playgroud)

这输出:

[Pipeline] echo
Contents of first file
[Pipeline] echo
Some other contents
Run Code Online (Sandbox Code Playgroud)

如果您需要使用currentBuild或类似属性,那么您可能需要在关闭强制后分配它们:

import org.jenkinsci.plugins.workflow.cps.CpsScript

def mockCpsScript() {
    def jenkins = [
        // same as above
    ] as CpsScript
    jenkins.currentBuild = [
        // Add attributes you need here. E.g. result:
        result:null,
    ]
    return jenkins
}
Run Code Online (Sandbox Code Playgroud)