模拟詹金斯管道步骤 [英] Mocking jenkins pipeline steps

查看:88
本文介绍了模拟詹金斯管道步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jenkinsfile中使用一个类,这里​​是其简化版本:

I have a class that i use in my jenkinsfile, simplified version of it here:

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

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

And i supply this as a jenkins parameter when using it in the jenkinsfile. What would be the best way to mock jenkins object here that has script and sh ? Thanks for your help

推荐答案

前一周,我遇到了类似的问题,我想到了:

I had similar problems the other week, I came up with this:

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
}

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

and used together with your script (extended with non-named sh call):

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())

它输出:

[Pipeline] echo
Calling sh with script: echo build
[Pipeline] echo
Calling sh with script: echo no named arguments

现在,它本身不是很有用,但是很容易添加定义模拟方法行为的逻辑,例如,此版本根据正在读取的目录和文件来控制readFile返回的内容:

Now this it self isn't very useful, but it easy to add logic which defines behaviour of the mock methods, for example, this version controls the contents returned by readFile depending of what directory and file is being read:

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']))

这将输出:

[Pipeline] echo
Contents of first file
[Pipeline] echo
Some other contents

如果您需要使用currentBuild或类似的属性,则需要在闭包强制之后分配这些属性:

If you need to use currentBuild or similar properties, then you can need to assign those after the closure coercion:

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
}

这篇关于模拟詹金斯管道步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆