jenkinsci / JenkinsPipelineUnit

Framework for unit testing Jenkins pipelines
MIT License
1.54k stars 396 forks source link

DeclarativePipelineTest - 'when' is missing the option for 'beforeAgent', 'anyOf' and 'allOf' #227

Open pcoulso1 opened 4 years ago

pcoulso1 commented 4 years ago

The class WhenDeclaration in com.lesfurets.jenkins.unit.declarative seems to be missing the option for 'beforeAgent', 'anyOf' and 'allOf'

e.g. when used in the stage section of the jenkinsfile

stage('build') {
  when {
    allOf {
      environment name: 'cleanBuild', value: 'true'
      expression { BRANCH_NAME ==~ /(develop|hotfix\/.*)/ }
    }
    beforeAgent true
  }
  steps {
    …
  }
}

And results in the following exception when the tests are run

groovy.lang.MissingMethodException: No signature of method: com.lesfurets.jenkins.unit.declarative.WhenDeclaration.allOf() is applicable for argument types: (Jenkinsfile$_run_closure1$_closure6$_closure11$_closure21$_closure24) values: [Jenkinsfile$_run_closure1$_closure6$_closure11$_closure21$_closure24@4149c063]
Possible solutions: any(), sleep(long), any(groovy.lang.Closure), split(groovy.lang.Closure)
            at Jenkinsfile$_run_closure1$_closure6.closure11$_closure21(Jenkinsfile:153)
            at Jenkinsfile$_run_closure1$_closure6.closure11$_closure21(Jenkinsfile)
            at com.lesfurets.jenkins.unit.declarative.DeclarativePipeline.createComponent(DeclarativePipeline.groovy:18)
            at com.lesfurets.jenkins.unit.declarative.StageDeclaration.when(StageDeclaration.groovy:32)
            at Jenkinsfile$_run_closure1.closure6$_closure11(Jenkinsfile:152)

See https://www.jenkins.io/doc/book/pipeline/syntax/ for details

lucasvc commented 4 years ago

Other conditions (like not) are not working either:

...
stage('Example Test') {
  when {
    not {
      expression { return false }
    }
  }
agent { docker 'openjdk:8-jre' }
...

Is there any way to workaround this in my own test code? I have tried,

        testee = loadScript("vars/mypipeline.groovy")
        helper.registerAllowedMethod("anyOf", [Closure.class], { it -> it() })
        testee.call()

But still same exception.

Sorry if this is somewhere explained, but I could not find it and I'm totally groovy-newbie.

IuryAlves commented 4 years ago

Hey @pcoulso1.

I recently added support for anyOf here.

It shouldn't be hard to replicate the behavior for allOf

epsilo commented 4 years ago

Hi @IuryAlves

The implementation of anyOf you made only work for a single validation inside anyOf. anyOf { branch 'master' } In this case works to check if the branch is master or not.

If we have more than one validation e will only validate the last one anyOf { branch 'master'; branch 'release'; branch 'develop' } In this case if the branch is master it will be false, but if it is develop it will be true.

Do you know how to make this to work as a "anyOf" behaviour ? Thanks

IuryAlves commented 4 years ago

Hey @epsilo

You're right!

I have done a new PR for adding support to multiple validations for branch and expression.

Bear in mind that there are other validations still missing.

epsilo commented 4 years ago

Thank you very much!

IuryAlves commented 4 years ago

@epsilo My PR was merged. Can you try again?

epsilo commented 4 years ago

@IuryAlves it is working great now! Thanks

IuryAlves commented 4 years ago

Nice. I think this issue can be closed now.

jcmendez-guerrero commented 3 years ago

Technically The issue covered BeforeAgent as well which is not implemented yet, therefore the issue might be still open

e.g:

 groovy.lang.MissingMethodException: No signature of method: Jenkinsfile.beforeAgent() is applicable for argument types: (Boolean) values: [true]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

And the Jenkins File

      stage('Before Agent Test') {
            when {
              beforeAgent true
              not { branch MY_BRANCH }
            }
        steps {
          script {
            ....
          }
        }
      }
jordarlu commented 2 years ago

Hi, Just wondering if any update on the issue to support BeforeAgent? the left issue quoted by @jcmendez-guerrero .. Thanks!

a-margaritis commented 1 year ago

I don't see an assignee on the issue, could someone provide an update for it?

MKITConsulting commented 1 month ago

I faced the same problems and created a pull request to solve the issue.

MKITConsulting commented 1 month ago

Thx for merging @nre-ableton. I guess we can close this issue here. Any plans on when a new tag will be created?

nre-ableton commented 1 month ago

Thx for merging @nre-ableton. I guess we can close this issue here. Any plans on when a new tag will be created?

I'll try to make a release sometime this week when I have some time.