Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-33925

Test framework for Jenkinsfile

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      It would be desirable to have a standard mechanism for testing Pipeline scripts without running them on a production server. There are two competing suggestions:

      Mock framework

      Inspired by Job DSL (example).

      We could set up a GroovyShell in which step functions and global variables were predefined as mocks (in a fashion similar to Powermock, but easier in Groovy given its dynamic nature), and stub out the expected return value / exception for each, with some standard predefinitions such as for currentBuild.

      Ideally the shell would be CPS-transformed, with the program state serialized and then reloaded between every continuation (though this might involve a lot of code duplication with workflow-cps).

      Should be easy to pass it through the Groovy sandbox (if requested), though the live Whitelist.all from Jenkins would be unavailable, so we would be limited to known static whitelists, the @Whitelisted annotation, and perhaps some custom additions.

      Quick and flexible, but low fidelity to real behavior.

      JenkinsRule-style

      Use an embedded Jenkins server, as per JenkinsRule in jenkins-test-harness, and actually create a WorkflowJob with the specified definition. Can use for example mock-slave to create nodes.

      Need to have a "dry-run" flag so that attempts to do things like deploy artifacts or send email do not really take action. This could perhaps be a general API in Jenkins core, as it would be useful also for test instances (shadows of production servers), acceptance-test-harness, etc.

      Slower to run (seconds per test case rather than milliseconds), and trickier to set up, but much more realistic coverage. The tests for Pipeline (and Pipeline steps) themselves use this technique.

        Attachments

          Issue Links

            Activity

            Hide
            jglick Jesse Glick added a comment -

            The linter supports only Declarative Pipeline, not general scripts, much less libraries.

            validate pipelines to a remote Jenkins server (or spin one up in a Docker)

            Well this is most easily done with the JenkinsRule-style approach mentioned in the original issue description, which is really complementary to unit testing.

            Show
            jglick Jesse Glick added a comment - The linter supports only Declarative Pipeline, not general scripts, much less libraries. validate pipelines to a remote Jenkins server (or spin one up in a Docker) Well this is most easily done with the JenkinsRule -style approach mentioned in the original issue description, which is really complementary to unit testing.
            Hide
            oleg_nenashev Oleg Nenashev added a comment -

            I'd guess it is going to be resolved by hosting PipelineUnit as a part of the Jenkins project: https://groups.google.com/forum/#!topic/jenkinsci-dev/VTHyvXqQ0s8

             

             

            Show
            oleg_nenashev Oleg Nenashev added a comment - I'd guess it is going to be resolved by hosting PipelineUnit as a part of the Jenkins project: https://groups.google.com/forum/#!topic/jenkinsci-dev/VTHyvXqQ0s8    
            Hide
            jglick Jesse Glick added a comment -

            Well that is the first option, with all its advantages and drawbacks. I suppose there is not much practical purpose in keeping this open, anyway.

            Show
            jglick Jesse Glick added a comment - Well that is the first option, with all its advantages and drawbacks. I suppose there is not much practical purpose in keeping this open, anyway.
            Hide
            jglick Jesse Glick added a comment -

            Jenkinsfile Runner was recently published, which attacks the problem from the opposite end of the spectrum, literally using JenkinsRule internally.

            Show
            jglick Jesse Glick added a comment - Jenkinsfile Runner was recently published, which attacks the problem from the opposite end of the spectrum, literally using JenkinsRule internally.
            Hide
            oleg_nenashev Oleg Nenashev added a comment -

            The latest version of Jenkinsfile Runner does not use JenkinsRule anymore, but yes it is one of the possible approaches to testing nowadays

            Show
            oleg_nenashev Oleg Nenashev added a comment - The latest version of Jenkinsfile Runner does not use JenkinsRule anymore, but yes it is one of the possible approaches to testing nowadays

              People

              • Assignee:
                Unassigned
                Reporter:
                jglick Jesse Glick
              • Votes:
                105 Vote for this issue
                Watchers:
                121 Start watching this issue

                Dates

                • Created:
                  Updated: