Testing DSL Scripts / (Groovy) Builders for Pipeline?

3,542 views
Skip to first unread message

Steffen Elste

unread,
Feb 2, 2017, 6:30:55 AM2/2/17
to job-dsl-plugin
Hi,
after reading Testing DSL Scripts and working through the linked Jenkins Job DSL Gradle example i tried to create a Builder and associated Test Specification for Workflow/Pipeline jobs - so far without success.
JenkinsJobManagement throws a NullPointerException because a required Plugin - workflow-aggregator and Dependencies - ist not available.
This makes sense because Pipeline ist not part of the basic Jenkins package - but i'm at a loss at the moment on how to work around that problem, if it's possible at all.
Any hints are appreciated.
Thanks,

S. Elste

Victor Martinez

unread,
Feb 2, 2017, 5:12:10 PM2/2/17
to job-dsl-plugin

Steffen Elste

unread,
Feb 3, 2017, 1:31:59 AM2/3/17
to job-dsl-plugin
Hi Victor,
yes, that was the first thing i did - should have included that in my first post. Here's the part of my build.gradle:

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.7'
    compile "org.jenkins-ci.plugins:job-dsl-core:${jobDslVersion}"

    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'cglib:cglib-nodep:2.2.2' // used by Spock

    // Jenkins test harness dependencies
    testCompile 'org.jenkins-ci.main:jenkins-test-harness:2.8'
    testCompile "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}"
    testCompile "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}:war-for-test@jar"

    // Job DSL plugin including plugin dependencies
    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}"
    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}@jar"
    testCompile 'org.jenkins-ci.plugins:structs:1.2@jar'
    testCompile 'org.jenkins-ci.plugins:cloudbees-folder:5.0@jar'

    // plugins to install in test instance
    testPlugins 'org.jenkins-ci.plugins:ghprb:1.31.4'
    testPlugins 'com.coravy.hudson.plugins.github:github:1.19.0'
    testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:5.0'
    testPlugins 'org.jenkins-ci.plugins:credentials:2.1.11'
    testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'

    // for the RestApiScriptRunner
    compile('org.codehaus.groovy.modules.http-builder:http-builder:0.7.2') {
        exclude(module: 'groovy')
    }

    // for the RestApiScriptRunner
    compile('org.apache.ant:ant:1.9.7')
}


Adding 'workflow-aggregator' as a testCompile Dependency also didn't work.
I thought about trying to attach to a running Jenkins Test Harness, but up to now i haven't figured out how to do this - don't know if it's a good idea anyway.
Cheers,

Steffen

Steffen Elste

unread,
Feb 7, 2017, 8:09:12 AM2/7/17
to job-dsl-plugin
Hi,
stupid mistake on my part - JobManagement configuration error in the Specification ... Test is working as expected now.
Cheers,

Steffen

Susana Rodriguez

unread,
Feb 16, 2017, 2:38:30 PM2/16/17
to job-dsl-plugin
Hi Steffen, 
I am having a similar situation.  
My tests are failing because gradle is installing Jenkins core default plugins
instead of the dependency versions in the gradle file. 
Can you elaborate on what was the actual and how did you solved it? 
The error I am getting is below. Let me know if you have any suggestions. 
I want to compare your scenario to? 


10:39:44.213 [DEBUG] [TestEventLogger] JobScriptsSpec > test script aegisthusJobsScratch.groovy STANDARD_OUT

10:39:44.213 [DEBUG] [TestEventLogger]     Processing provided DSL script

10:39:44.367 [DEBUG] [TestEventLogger]     Building aegisthus_emr_bootstrap_SCRATCH...

10:39:44.367 [DEBUG] [TestEventLogger]     ...defining permission records to hudson.model.Item.Workspace:authenticated...

10:39:44.369 [DEBUG] [TestEventLogger]     Warning: (script, line 46) plugin 'matrix-auth' needs to be updated to version 1.2 or later

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting to block on upstream projects...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting SCM retry count...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting description for aegisthus_emr_bootstrap_SCRATCH...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting display name for aegisthus_emr_bootstrap_SCRATCH...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting JDK type to Slave_Oracle_JDK8...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting label specifying packer nodes this job can run on...

10:39:44.370 [DEBUG] [TestEventLogger]     ...setting log rotation for aegisthus_emr_bootstrap_SCRATCH...

10:39:44.372 [DEBUG] [TestEventLogger]     ...setting parameters for aegisthus_emr_bootstrap_SCRATCH...

10:39:44.377 [DEBUG] [TestEventLogger]     ...setting mailer to dkirkpatrick@kenzan.com...

10:39:44.378 [DEBUG] [TestEventLogger]     Warning: (script, line 93) version 2.26 or later of plugin 'parameterized-trigger' needs to be installed

10:39:44.382 [DEBUG] [TestEventLogger]     ...setting scm configuration for aegisthus_emr_bootstrap_SCRATCH...

10:39:44.383 [DEBUG] [TestEventLogger]     Warning: (script, line 108) support for GIT plugin versions older than 2.5.3 is deprecated

10:39:44.467 [DEBUG] [TestEventLogger]

10:39:44.467 [DEBUG] [TestEventLogger] JobScriptsSpec > test script aegisthusJobsScratch.groovy FAILED

10:39:44.468 [DEBUG] [TestEventLogger]     Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'

10:39:44.468 [DEBUG] [TestEventLogger]         at spock.lang.Specification.noExceptionThrown(Specification.java:118)

10:39:44.468 [DEBUG] [TestEventLogger]         at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:23)

10:39:44.468 [DEBUG] [TestEventLogger]

10:39:44.468 [DEBUG] [TestEventLogger]         Caused by:

10:39:44.468 [DEBUG] [TestEventLogger]         javaposse.jobdsl.dsl.DslScriptException: (script, line 110) No signature of method: javaposse.jobdsl.dsl.helpers.scm.GitContext.cloneTimeout() is applicable for argument types: (java.lang.Integer) values: [3]

10:39:44.468 [DEBUG] [TestEventLogger]             at javaposse.jobdsl.dsl.DslScriptLoader.runScriptEngine(DslScriptLoader.groovy:126)

10:39:44.468 [DEBUG] [TestEventLogger]             at javaposse.jobdsl.dsl.DslScriptLoader.runScripts_closure1(DslScriptLoader.groovy:55)

10:39:44.468 [DEBUG] [TestEventLogger]             at groovy.lang.Closure.call(Closure.java:414)

10:39:44.468 [DEBUG] [TestEventLogger]             at groovy.lang.Closure.call(Closure.java:430)

10:39:44.468 [DEBUG] [TestEventLogger]             at javaposse.jobdsl.dsl.DslScriptLoader.runScripts(DslScriptLoader.groovy:42)

10:39:44.468 [DEBUG] [TestEventLogger]             at javaposse.jobdsl.dsl.DslScriptLoader.runScript(DslScriptLoader.groovy:91)

10:39:44.468 [DEBUG] [TestEventLogger]             at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:20)

10:39:44.468 [DEBUG] [TestEventLogger]

10:39:44.468 [DEBUG] [TestEventLogger]             Caused by:

10:39:44.468 [DEBUG] [TestEventLogger]             groovy.lang.MissingMethodException: No signature of method: javaposse.jobdsl.dsl.helpers.scm.GitContext.cloneTimeout() is applicable for argument types: (java.lang.Integer) values: [3]

10:39:44.468 [DEBUG] [TestEventLogger]                 at script$_run_closure1.closure6$_closure12(script:110)

10:39:44.468 [DEBUG] [TestEventLogger]                 at script$_run_closure1.closure6$_closure12(script)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.ContextHelper.executeInContext(ContextHelper.groovy:16)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.helpers.ScmContext.git(ScmContext.groovy:96)

10:39:44.468 [DEBUG] [TestEventLogger]                 at script.run_closure1$_closure6(script:108)

10:39:44.468 [DEBUG] [TestEventLogger]                 at script.run_closure1$_closure6(script)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.ContextHelper.executeInContext(ContextHelper.groovy:16)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.Job.scm(Job.groovy:525)

10:39:44.468 [DEBUG] [TestEventLogger]                 at script.run_closure1(script:106)

10:39:44.468 [DEBUG] [TestEventLogger]                 at groovy.lang.Closure.call(Closure.java:414)

10:39:44.468 [DEBUG] [TestEventLogger]                 at groovy.lang.Closure.call(Closure.java:430)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.JobParent.processItem(JobParent.groovy:114)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.JobParent.freeStyleJob(JobParent.groovy:46)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.JobParent.job(JobParent.groovy:38)

10:39:44.468 [DEBUG] [TestEventLogger]                 at script.run(script:42)

10:39:44.468 [DEBUG] [TestEventLogger]                 at javaposse.jobdsl.dsl.DslScriptLoader.runScriptEngine(DslScriptLoader.groovy:120)

10:39:44.468 [DEBUG] [TestEventLogger]                 ... 6 more

10:39:44.469 [DEBUG] [TestEventLogger]





Susana 

Daniel Spilker

unread,
Feb 20, 2017, 11:28:21 AM2/20/17
to job-dsl...@googlegroups.com
@Susana: cloneTimout in the git context has been deprecated in v1.44 and removed in v1.51. See https://github.com/jenkinsci/job-dsl-plugin/wiki/Migration#migrating-to-144

Daniel

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugin+unsubscribe@googlegroups.com.
To post to this group, send email to job-dsl-plugin@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/73de9a18-3777-4a00-9197-0e5ac0b36a2d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Susana Rodriguez

unread,
Feb 21, 2017, 5:56:37 PM2/21/17
to job-dsl-plugin
Hi Daniel. 
I did notice that earlier. I was looking at which was the proper fix for the error. I just removed the call to that method, since it was deprecated and it worked. 

Susana 
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.

Michael

unread,
Mar 21, 2018, 4:58:23 AM3/21/18
to job-dsl-plugin
Hi Steffen,

It seems you resolved this issue - but I was not clear to me how.
When running the test by ./gradlew clean test I get:

Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at org.kie.jenkins.jobdsl.JobScriptsSpec.test DSL script #file.name(JobScriptsSpec.groovy:45)
Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 105) plugin 'workflow-aggregator' needs to be installed
	at javaposse.jobdsl.plugin.JenkinsJobManagement.failOrMarkBuildAsUnstable(JenkinsJobManagement.java:462)
	at javaposse.jobdsl.plugin.JenkinsJobManagement.requirePlugin(JenkinsJobManagement.java:330)
	at script.run(script:105)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:132)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:105)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts_closure1(AbstractDslScriptLoader.groovy:60)
	at groovy.lang.Closure.call(Closure.java:414)
	at groovy.lang.Closure.call(Closure.java:430)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:85)
	at org.kie.jenkins.jobdsl.JobScriptsSpec.test DSL script #file.name(JobScriptsSpec.groovy:41)

but I have added this line in my build.gradle:  testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.5'
I don't understand well why he is complaining about installong this plugin since it is.

Regards,

Michael

Steffen Elste

unread,
Mar 22, 2018, 4:49:05 AM3/22/18
to job-dsl-plugin
Hi Michael,
if i remember correctly i botched the JobManagement setup (in JobScriptsSpec) ... should habe copied & pasted instead of trying to type it in.

I can't tell from the stacktrace You provided what's wrong - but i recently set up another test based upon the current job-dsl-gradle-example and the (declarative) pipeline script tests just run fine.
Maybe one of the Job-DSL experts will be able to help if You post a complete stacktrace, possibly a script as well.
Cheers,

Steffen

mbk

unread,
Mar 23, 2018, 8:15:36 AM3/23/18
to job-dsl-plugin
tried to copy&paste but this didn't help at all
thanX

Mchael

mbk

unread,
Mar 23, 2018, 11:23:37 AM3/23/18
to job-dsl-plugin

james....@silvaco.com

unread,
May 2, 2018, 9:32:38 PM5/2/18
to job-dsl-plugin
Hi Steffen,

I have been using the job-dsl-gradle-example to build,test, and push jobs to Jenkins instance using dslUpdateJenkins. When I found your post about declarative pipelines I was looking for a way to use the same job-dsl-gradle-example gradle build flow to build,test and push pipelines to Jenkins.
After adding compile 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4' to my build.gradle I was not able to do that.

Here is a simple scripted pipeline with the build errors below:

node {
    stage 'test'
    echo "Hello World"
}

Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at com.dslexample.JobScriptsSpecAlternative.test script #file.name(JobScriptsSpecAlternative.groovy:49)
Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 1) No signature of method: script.node() is applicable for argument types: (script$_run_closure1) values: [script$_run_closure1@3d1e291f]
Possible solutions: folder(java.lang.String), job(java.lang.String), use([Ljava.lang.Object;), notify(), wait(), run()
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:112)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts_closure1(AbstractDslScriptLoader.groovy:59)
	at groovy.lang.Closure.call(Closure.java:414)
	at groovy.lang.Closure.call(Closure.java:430)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:85)
	at com.dslexample.JobScriptsSpecAlternative.test script #file.name(JobScriptsSpecAlternative.groovy:45)
Caused by: groovy.lang.MissingMethodException: No signature of method: script.node() is applicable for argument types: (script$_run_closure1) values: [script$_run_closure1@3d1e291f]
Possible solutions: folder(java.lang.String), job(java.lang.String), use([Ljava.lang.Object;), notify(), wait(), run()
	at script.run(script:1)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:132)
	at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:106)
	... 6 more

I was expecting this to fail because job-dsl-gradle-example is setup to use jobDSL thru the gradle-jenkins-jobdsl-plugin not pipeline syntax.
There is no gradle-jenkins-workflow-plugin wouldn't you test pipelines with something like  https://github.com/jenkinsci/JenkinsPipelineUnit?
I could create a pipeline with same code using jobDSL:
 
pipelineJob('example_pipeline') {
    displayName('Job DSL Example Pipeline')
    definition {
        cps {
            script('''
        node {
            stage 'test'
            echo "Hello World"
        }
    ''')
        }
    }
}

And as fun as that example is there is no practical use for creating the pipeline like that. You can't test the pipeline.
I also tried the same Hello World example with descriptive pipeline syntax and it doesn't work.

I must be misunderstanding what you are doing with declarative pipelines and job-dsl-gradle-example.

What are you doing specifically? Can I see an example of one of the declarative pipelines you are testing with job-dsl-gradle-example?

I have included my build.gradle below. 

Thanks,

James

buildscript {
    ext.jenkinsJobdslPluginVersion = '3.1.0'

    repositories {
        maven {
            url 'https://plugins.gradle.org/m2'
        }

        maven {
        }
    }

    dependencies {
        classpath("com.here.gradle.plugins:gradle-jenkins-jobdsl-plugin:${jenkinsJobdslPluginVersion}") {
            // Exclude unneeded transitive dependencies that can cause dependency resolution issues.
            exclude(module: 'bootstrap')
            exclude(module: 'groovy-all')
            exclude(module: 'org-netbeans-insane')

            // Exclude unneeded transitive dependencies on XML libraries that can break the generation of config files.
            exclude(module: 'xalan')
            exclude(module: 'xercesImpl')
        }
    }
}

apply plugin: 'com.here.jobdsl'

repositories {
    jcenter()
    maven {
    }
    maven {
    }
}

configurations {
    testPlugins {}

    // see JENKINS-45512





    testCompile {
        exclude group: 'xalan'
        exclude group: 'xerces'
    }
}

dependencies {
    compile("com.here.gradle.plugins:gradle-jenkins-jobdsl-plugin:${jenkinsJobdslPluginVersion}") {
        exclude(module: 'org-netbeans-insane')
        exclude(module: 'xalan')
        exclude(module: 'xercesImpl')
    }
    compile 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'
    //Added because I couldn't do anything useful once I created pipeline with jobDsl
    //compile 'org.jenkins-ci.plugins.workflow:workflow-cps:2.17'

    //Added for TAP testing support
    compile ('org.tap4j:tap:2.2.1')
    //Added to be able to slect node to run on
    compile ('org.jenkins-ci.plugins:nodelabelparameter:1.7.2')
    //compile('org.eclipse.hudson.main:hudson-core:3.0.0-M1')
    compile('org.jenkins-ci.plugins:junit:1.6@jar')
    compile('com.cloudbees:groovy-cps:1.22')


    testCompile ('org.spockframework:spock-core:1.0-groovy-2.4')

    // Jenkins test harness dependencies
    testCompile('org.jenkins-ci.main:jenkins-test-harness:2.33'){
        exclude(module: 'org-netbeans-insane')
    }
    testCompile "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}"

    // Job DSL plugin including plugin dependencies
    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}"
    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}@jar"
    testCompile 'org.jenkins-ci.plugins:structs:1.6@jar'
    testCompile 'org.jenkins-ci.plugins:jenkins-multijob-plugin:1.29'
    testCompile 'org.jenkins-ci.plugins:matrix-project:1.12'
    testCompile 'com.cloudbees:groovy-cps:1.22'
    testCompile 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'


    //JOB had to add this
    testCompile 'cglib:cglib:2.2'
    //testcompile 'org.tap4j:tap:2.2.1'

    // plugins to install in test instance
    testPlugins 'org.jenkins-ci.plugins:ghprb:1.31.4'
    testPlugins 'com.coravy.hudson.plugins.github:github:1.19.0'
    testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:5.0'
    testPlugins 'org.jenkins-ci.plugins:nodelabelparameter:1.7.2'
    testPlugins 'org.jenkins-ci.plugins:jenkins-multijob-plugin:1.29'
    testPlugins 'org.jenkins-ci.plugins:matrix-project:1.12'
    testPlugins 'com.cloudbees:groovy-cps:1.22'
    testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'
}

task resolveTestPlugins(type: Copy) {
    from configurations.testPlugins
    into new File(sourceSets.test.output.resourcesDir, 'test-dependencies')
    include '*.hpi'
    include '*.jpi'

    doLast {
        def baseNames = source.collect { it.name[0..it.name.lastIndexOf('.')-1] }
        new File(destinationDir, 'index').setText(baseNames.join('\n'), 'UTF-8')
    }
}

sourceSets {
    jobdsl {
        groovy {
            srcDirs 'src/jobs'
        }
    }
}

test {
    dependsOn tasks.resolveTestPlugins
    inputs.files sourceSets.jobdsl.groovy.srcDirs

    // set build directory for Jenkins test harness, JENKINS-26331
    systemProperty 'buildDirectory', project.buildDir.absolutePath
}



jobdsl {
    servers {
        localhost {
            jenkinsUrl = 'http://REDACTED/'
            jenkinsUser = 'REDACTED'
            jenkinsApiToken = 'REDACTED'
        }

        staging {
            jenkinsUrl = 'REDACTED/'
            jenkinsUser = 'REDACTED'
            jenkinsApiToken = 'REDACTED'
        }
    }
}
task wrapper(type: Wrapper) {
    gradleVersion = '2.4' //version required
}

Steffen Elste

unread,
May 4, 2018, 2:35:25 AM5/4/18
to job-dsl-plugin
Hi James,
i'll try to post an example early next week.

We're using a shared library - e.g. to wrap the differences between Git and Subversion calls - and what i'm basically trying to accomplish is:
- unit test the build steps while developing/modifying the library
- at least syntactically verify the Jenkinsfiles once the library is included
Cheers,
Reply all
Reply to author
Forward
0 new messages