Search code examples
jenkinsjenkins-pluginsspockjenkins-job-dsl

How to update Jenkins instance before testing?


The Problem

I'm using Jenkins Job DSL to create my jobs and testing my scripts with Spock and jenkins-test-harness.

I'm able to install new plugins with success, based on the Gradle example, but how can I update the plugins that already are installed?

I tried to use the UpdateCenter but even using LinkedBlockingQueue I'm unable to force the test to wait the update process.

The Code

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

Output

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

Solution

  • You can add any plugins to be installed or updated to the testPlugins configuration in your build.gradle file:

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

    See the Job DSL wiki for details: https://github.com/jenkinsci/job-dsl-plugin/wiki/Testing-DSL-Scripts