可以测试Job DSL脚本

时间:2015-12-02 15:13:19

标签: jenkins jenkins-plugins jenkins-job-dsl

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

除了让jenkins运行它之外,有没有办法测试Job DSL脚本?

3 个答案:

答案 0 :(得分:6)

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节点。

答案 1 :(得分:2)

查看job-dsl-gradle-example。 repo包含用于DSL脚本的test

答案 2 :(得分:0)

以与crasp相同的方式进行操作,但是使用Jenkins测试工具,如Jenkins Unit Test page中所述,该方法速度较慢,但​​可以与自动生成的DSL一起使用,如here中所述,语法错误

按照here设置代码后,您可以进行如下测试:

@Unroll
void 'check descriptions #file.name'(File file) {
    given:
    JobManagement jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))
    Jenkins jenkins = jenkinsRule.jenkins

    when:
    GeneratedItems items = new DslScriptLoader(jobManagement).runScript(file.text)

    then:
    if (!items.jobs.isEmpty()) {
        items.jobs.each { GeneratedJob generatedJob ->
            String text = getItemXml(generatedJob, jenkins)
            with(new XmlParser().parse(new StringReader(text))) {
                // Has some description
                !description.text().isEmpty()
            }
        }
    }

    where:
    file << TestUtil.getJobFiles()
}