Can a Job DSL script be tested

流过昼夜 提交于 2019-12-05 01:47:36

问题


Ideally I'd like to be able to invoke the script with some kind of unit test before I have it execute on a Jenkins.

Is there any way to test a Job DSL script other than having jenkins run it?


回答1:


Besides the examples in job-dsl-gradle-example, you can also go a step further and write tests for individual files or jobs. For example let's assume you have a job configuration file located in jobs/deployJob.groovy

import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.dsl.MemoryJobManagement
import javaposse.jobdsl.dsl.ScriptRequest
import spock.lang.Specification

class TestDeployJobs extends Specification {

    def 'test basic job configuration'() {
        given:
        URL scriptURL = new File('jobs').toURI().toURL()
        ScriptRequest scriptRequest = new ScriptRequest('deployJob.groovy', null, scriptURL)
        MemoryJobManagement jobManagement = new MemoryJobManagement()

        when:
        DslScriptLoader.runDslEngine(scriptRequest, jobManagement)

        then:
        jobManagement.savedConfigs.each { String name, String xml ->
            with(new XmlParser().parse(new StringReader(xml))) {
                // Make sure jobs only run manually
                triggers.'hudson.triggers.TimerTrigger'.spec.text().isEmpty()
                // only deploy every environment once at a time
                concurrentBuild.text().equals('false')
                // do a workspace cleanup
                buildWrappers.'hudson.plugins.ws__cleanup.PreBuildCleanup'
                // make sure masked passwords are active
                !buildWrappers.'com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper'.isEmpty()
            }
        }
    }
}

This way you are able to go through every XML node you want to make sure to have all the right values set.




回答2:


Have a look at the job-dsl-gradle-example. The repo contains a test for DSL scripts.



来源:https://stackoverflow.com/questions/34046200/can-a-job-dsl-script-be-tested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!