job-dsl-gradle-example icon indicating copy to clipboard operation
job-dsl-gradle-example copied to clipboard

Test failing if script file includes *pipelineJob()* config

Open wunzeco opened this issue 8 years ago • 5 comments

Hey guys,

Since updating to the latest job-dsl-gradle-example (5a63306b740b7cf46079df2b07a2c24dcc7b3968), the tests are failing on pipelineJob() config. For instance,

def product = 'catalog'
def toolsDslRepo = 'infra-tools'
pipelineJob("${product}-infra-tools-pipeline") {
    triggers {
        scm 'H/2 * * * *'
    }
    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        github("wunzeco/${toolsDslRepo}", 'ssh')
                        credentials("ci-user-git-creds-id")
                    }
                    branch('master')
                    extensions {
                        relativeTargetDirectory("${toolsDslRepo}")
                    }
                }
            }
            scriptPath("${toolsDslRepo}/Jenkinsfile")
        }
    }
}

When I run ./gradlew test --info I notice the warning below, which I reckon might be the cause. I've attached the output log.

com.dslexample.JobScriptsSpec > test script infraJobs.groovy STANDARD_ERROR
    Nov 29, 2016 4:49:11 PM javaposse.jobdsl.plugin.JenkinsJobManagement createOrUpdateConfig
    INFO: createOrUpdateConfig for catalog-infra-tools-pipeline
    Nov 29, 2016 4:49:11 PM javaposse.jobdsl.plugin.JenkinsJobManagement createNewItem
    WARNING: Error writing config for new item catalog-infra-tools-pipeline.
    java.io.IOException: Unable to read /var/folders/wx/4rccdhxs56jf89mhg7t93x8c0000gn/T/hudson1305438357693048548test/jobs/catalog-infra-tools-pipeline/config.xml
        at hudson.XmlFile.read(XmlFile.java:144)
        at hudson.model.Items.load(Items.java:326)
        at hudson.model.ItemGroupMixIn$4.call(ItemGroupMixIn.java:271)
        at hudson.model.ItemGroupMixIn$4.call(ItemGroupMixIn.java:269)
        at hudson.model.Items.whileUpdatingByXml(Items.java:96)

If anyone has idea how to overcome this issue, I'd be grateful. Jenkins Job DSL plugin is able to process the script file without any issue though. Right now I've had to disable gradle test for the moment until I can find/figure out a fix.

info-log.txt stacktrace-log.txt

wunzeco avatar Nov 29 '16 17:11 wunzeco

I have a similar problem.

My understanding is that the actual error from the above info-log.txt is this:

 com.dslexample.JobScriptsSpec > test script infraJobs.groovy FAILED
 Expected no exception to be thrown, but got 'java.lang.NullPointerException'
    at spock.lang.Specification.noExceptionThrown(Specification.java:118)
    at com.dslexample.JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:45)

    Caused by:
    java.lang.NullPointerException: Cannot get property 'url' on null object
        at com.dslexample.JobScriptsSpec.writeItems_closure1(JobScriptsSpec.groovy:66)
        at groovy.lang.Closure.call(Closure.java:414)
        at groovy.lang.Closure.call(Closure.java:430)
        at com.dslexample.JobScriptsSpec.writeItems(JobScriptsSpec.groovy:57)
        at com.dslexample.JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:42)

It seems that the latest commit on JobScriptsSpec.groovy ( afcf9c4825) added code to generate debug xml. I find those xml very useful and like that getting the xml became a lot easier than in the past.

In my current scenario, the tests for the pipeline view returns NULL for jenkins.getView(viewName) when the code iterates over items.views.each and which in turn throws the above NullPointerException.

My work around is changing JobScriptsSpec slightly:

    items.views.each { GeneratedView generatedView ->
             String viewName = generatedView.name
             View view = jenkins.getView(viewName)
-            String text = new URL(jenkins.rootUrl + view.url + 'config.xml').text
-            writeFile new File(outputDir, 'views'), viewName, text
+            if (view) {
+              String text = new URL(jenkins.rootUrl + view.url + 'config.xml').text
+              writeFile new File(outputDir, 'views'), viewName, text
+            } else {
+                println "view: '" + generatedView.name + "' doest have a view";
+            }
         }
     }

as a side, the actual xml for my pipeline is still written as part of the items.jobs.each loop

markus-mnm avatar Jan 23 '17 02:01 markus-mnm

@wunzeco are you still seeing this issue? I just tried your example with the latest and it worked for me. I did add:

testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'

sheehan avatar Jan 28 '17 14:01 sheehan

@markus-mnm do you have an example that returns null?

sheehan avatar Jan 28 '17 14:01 sheehan

Hey. I am also running into the above NPE while iterating the views.

Here is a short example reproducing the issue: build.gradle:

testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:5.18'
testPlugins 'org.jenkins-ci.plugins:build-pipeline-plugin:1.4.9'

jobs.groovy:


folder('a') {
    displayName 'A'
}

folder('a/b') {
    displayName 'B'
}

buildPipelineView('a/b/My View') {
  title('My View')
  displayedBuilds(5)
  selectedJob('some job')
}

It works when the view is not inside a folder!

carstenlenz avatar May 15 '17 15:05 carstenlenz

Needed these in build.gradle

testPlugins 'org.jenkins-ci.plugins:cloudbees-folder:6.3'
testPlugins 'org.jenkins-ci.plugins:credentials:2.1.16'
testPlugins 'org.jenkins-ci.plugins.workflow:workflow-aggregator:2.4'

And this is my pipeline and it works

pipelineJob("${folderName}/examplePipeline") {
  definition {
    cps {
      sandbox()
      script("""
        node {
          stage('hello') {
            echo 'hello'
          }
          stage('world') {
            echo 'world'
          }
        }
      """.stripIndent())
    }
  }
}

markhollingsworth avatar Feb 28 '18 22:02 markhollingsworth