JenkinsPipelineUnit icon indicating copy to clipboard operation
JenkinsPipelineUnit copied to clipboard

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

Open pcoulso1 opened this issue 4 years ago • 14 comments

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

pcoulso1 avatar May 27 '20 08:05 pcoulso1

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.

lucasvc avatar Jul 23 '20 07:07 lucasvc

Hey @pcoulso1.

I recently added support for anyOf here.

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

IuryAlves avatar Aug 22 '20 16:08 IuryAlves

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

epsilo avatar Sep 09 '20 23:09 epsilo

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.

IuryAlves avatar Sep 10 '20 12:09 IuryAlves

Thank you very much!

epsilo avatar Sep 10 '20 14:09 epsilo

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

IuryAlves avatar Sep 16 '20 12:09 IuryAlves

@IuryAlves it is working great now! Thanks

epsilo avatar Sep 16 '20 17:09 epsilo

Nice. I think this issue can be closed now.

IuryAlves avatar Sep 18 '20 07:09 IuryAlves

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

jcmendez-guerrero avatar Jan 26 '21 12:01 jcmendez-guerrero

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

jordarlu avatar Oct 07 '22 21:10 jordarlu