如何在测试前更新 Jenkins 实例?

How to update Jenkins instance before testing?

问题

我正在使用 Jenkins Job DSL 创建我的作业并使用 Spockjenkins-test-harness 测试我的脚本。

基于 the Gradle example,我可以成功安装新插件,但是如何更新已安装的插件?

我尝试使用 UpdateCenter 但即使使用 LinkedBlockingQueue 我也无法强制测试等待更新过程。

代码

import groovy.io.FileType
import hudson.PluginWrapper
import hudson.model.UpdateCenter
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.plugin.JenkinsJobManagement
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import java.util.concurrent.Future
import java.util.concurrent.LinkedBlockingQueue

/**
 * Tests that all dsl scripts in the jobs directory will compile.
 */
class JobScriptsSpec extends Specification {
    @Shared
    @ClassRule
    JenkinsRule jenkinsRule = new JenkinsRule()

    def setupSpec() {
        printPlugins()
        jenkinsRule.getInstance().getUpdateCenter().updateAllSites()
        def plugin = jenkinsRule.getInstance().getUpdateCenter().getPlugin('subversion')
        println "Wait for subversion update..."
        def queue = new LinkedBlockingQueue<Future<UpdateCenter>>()
        queue.add(plugin.deploy())
        queue.take()
        println "Wait Done..."
    }

    @Unroll
    void 'test script #file.name'(File file) {
        given:

        JobManagement jm = new JenkinsJobManagement(System.out, [:], new File('.'))

        when:
        new DslScriptLoader(jm).runScript(file.text)

        then:
        noExceptionThrown()

        where:
        file << jobFiles
    }

    private void printPlugins() {
        println "****** Plugins *****"
        List<PluginWrapper> plugins = jenkinsRule.getPluginManager().getPlugins()
        plugins.each { PluginWrapper plugin ->
            println "Plugin: ${plugin.getLongName()}"
        }
    }

    static List<File> getJobFiles() {
        List<File> files = []
        new File('jobs').eachFileRecurse(FileType.FILES) {
            if (it.name.endsWith('.groovy')) {
                files << it
            }
        }
        files
    }
}

输出

INFO: Obtained the latest update center data file for UpdateSource default
Wait for subversion update...
Jul 01, 2016 3:58:42 PM hudson.model.UpdateSite$Plugin deploy
INFO: Adding dependent install of workflow-scm-step for plugin subversion
Jul 01, 2016 3:58:42 PM hudson.model.UpdateSite$Plugin deploy
INFO: Adding dependent install of git-client for plugin workflow-scm-step
Jul 01, 2016 3:58:42 PM hudson.model.UpdateSite$Plugin deploy
INFO: Adding dependent install of workflow-step-api for plugin workflow-scm-step
Jul 01, 2016 3:58:42 PM hudson.model.UpdateSite$Plugin deploy
INFO: Adding dependent install of structs for plugin workflow-step-api
Jul 01, 2016 3:58:42 PM hudson.model.UpdateSite$Plugin deploy
INFO: Adding dependent install of mapdb-api for plugin subversion
Wait Done...
Jul 01, 2016 3:58:42 PM hudson.model.UpdateCenter$DownloadJob run
INFO: Starting the installation of Git client plugin on behalf of SYSTEM
Processing provided DSL script

您可以将要安装或更新的任何插件添加到 build.gradle 文件中的 testPlugins 配置:

dependencies {
  // ...
  testPlugins 'org.jenkins-ci.plugins:subversion:2.6'
  // ...
}

有关详细信息,请参阅作业 DSL wiki:https://github.com/jenkinsci/job-dsl-plugin/wiki/Testing-DSL-Scripts