Can a Job DSL script be tested

2020-06-23 07:02发布

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?

3条回答
不美不萌又怎样
2楼-- · 2020-06-23 07:21

Doing it in the same way as crasp but using Jenkins test harness as explained in Jenkins Unit Test page, which is slower but would work with auto-generated DSL giving syntax errors as explained here.

After setting the code as explained here, you can just do a test like this one:

@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()
}
查看更多
趁早两清
3楼-- · 2020-06-23 07:29

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.

查看更多
等我变得足够好
4楼-- · 2020-06-23 07:40

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

查看更多
登录 后发表回答