可以测试Job DSL脚本 [英] Can a Job DSL script be tested

查看:376
本文介绍了可以测试Job DSL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

除了让詹金斯(Jenkins)运行它之外,还有其他方法可以测试Job DSL脚本吗?

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

推荐答案

除了 job-dsl-gradle-example ,您还可以更进一步,为单个文件或作业编写测试.例如,假设您在Jobs/deployJob.groovy

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

通过这种方式,您可以遍历要确保设置所有正确值的每个XML节点.

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

这篇关于可以测试Job DSL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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