小编典典

可以测试Job DSL脚本

jenkins

理想情况下,我希望能够在Jenkins上执行脚本之前使用某种单元测试来调用该脚本。

除了让jenkins运行之外,还有什么方法可以测试Job DSL脚本?


阅读 418

收藏
2020-07-25

共1个答案

小编典典

除了job-dsl-gradle-example中的示例之外,您还可以更进一步,为单个文件或作业编写测试。例如,假设您在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()
            }
        }
    }
}

这样,您便可以遍历要确保设置所有正确值的每个XML节点。

2020-07-25