Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking jenkins pipeline steps

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

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

like image 667
midori Avatar asked May 03 '18 23:05

midori


1 Answers

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
}

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

it outputs:

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

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

This outputs:

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

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
}
like image 87
Jon S Avatar answered Nov 09 '22 04:11

Jon S