jenkinsci / JenkinsPipelineUnit

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

post-unsuccessful action is not executed for parallel stage when a substage fails #599

Open darugnaa opened 1 year ago

darugnaa commented 1 year ago

Jenkins and plugins versions report

pasted

Environment ```text Jenkins: 2.387.1 OS: Linux - 6.1.18-200.fc37.x86_64 Java: 11.0.18 - Eclipse Adoptium (OpenJDK 64-Bit Server VM) --- ant:481.v7b_09e538fcca antisamy-markup-formatter:159.v25b_c67cd35fb_ apache-httpcomponents-client-4-api:4.5.14-150.v7a_b_9d17134a_5 authentication-tokens:1.53.v1c90fd9191a_b_ blueocean:1.27.3 blueocean-bitbucket-pipeline:1.27.3 blueocean-commons:1.27.3 blueocean-config:1.27.3 blueocean-core-js:1.27.3 blueocean-dashboard:1.27.3 blueocean-display-url:2.4.1 blueocean-events:1.27.3 blueocean-git-pipeline:1.27.3 blueocean-github-pipeline:1.27.3 blueocean-i18n:1.27.3 blueocean-jwt:1.27.3 blueocean-personalization:1.27.3 blueocean-pipeline-api-impl:1.27.3 blueocean-pipeline-editor:1.27.3 blueocean-pipeline-scm-api:1.27.3 blueocean-rest:1.27.3 blueocean-rest-impl:1.27.3 blueocean-web:1.27.3 bootstrap5-api:5.2.2-2 bouncycastle-api:2.27 branch-api:2.1071.v1a_188a_562481 build-timeout:1.28 caffeine-api:2.9.3-65.v6a_47d0f4d1fe checks-api:2.0.0 cloudbees-bitbucket-branch-source:800.va_b_b_9a_a_5035c1 cloudbees-folder:6.815.v0dd5a_cb_40e0e commons-lang3-api:3.12.0-36.vd97de6465d5b_ commons-text-api:1.10.0-36.vc008c8fcda_7b_ credentials:1224.vc23ca_a_9a_2cb_0 credentials-binding:523.vd859a_4b_122e6 display-url-api:2.3.7 durable-task:504.vb10d1ae5ba2f echarts-api:5.4.0-3 email-ext:2.95 favorite:2.4.1 font-awesome-api:6.3.0-2 git:5.0.0 git-client:4.2.0 github:1.37.0 github-api:1.303-417.ve35d9dd78549 github-branch-source:1703.vd5a_2b_29c6cdc gradle:2.3.2 handy-uri-templates-2-api:2.1.8-22.v77d5b_75e6953 htmlpublisher:1.31 instance-identity:142.v04572ca_5b_265 ionicons-api:45.vf54fca_5d2154 jackson2-api:2.14.2-319.v37853346a_229 jakarta-activation-api:2.0.1-3 jakarta-mail-api:2.0.1-3 javax-activation-api:1.2.0-6 javax-mail-api:1.6.2-9 jaxb:2.3.8-1 jenkins-design-language:1.27.3 jjwt-api:0.11.5-77.v646c772fddb_0 jquery3-api:3.6.4-1 junit:1189.v1b_e593637fa_e ldap:671.v2a_9192a_7419d mailer:448.v5b_97805e3767 matrix-auth:3.1.6 matrix-project:785.v06b_7f47b_c631 mina-sshd-api-common:2.9.2-62.v199162f0a_2f8 mina-sshd-api-core:2.9.2-62.v199162f0a_2f8 okhttp-api:4.10.0-132.v7a_7b_91cef39c pam-auth:1.10 pipeline-build-step:487.va_823138eee8b_ pipeline-github-lib:42.v0739460cda_c4 pipeline-graph-analysis:202.va_d268e64deb_3 pipeline-groovy-lib:629.vb_5627b_ee2104 pipeline-input-step:466.v6d0a_5df34f81 pipeline-milestone-step:111.v449306f708b_7 pipeline-model-api:2.2125.vddb_a_44a_d605e pipeline-model-definition:2.2125.vddb_a_44a_d605e pipeline-model-extensions:2.2125.vddb_a_44a_d605e pipeline-rest-api:2.32 pipeline-stage-step:305.ve96d0205c1c6 pipeline-stage-tags-metadata:2.2125.vddb_a_44a_d605e pipeline-stage-view:2.32 plain-credentials:143.v1b_df8b_d3b_e48 plugin-util-api:3.2.0 pubsub-light:1.17 resource-disposer:0.21 scm-api:631.v9143df5b_e4a_a script-security:1229.v4880b_b_e905a_6 snakeyaml-api:1.33-95.va_b_a_e3e47b_fa_4 sse-gateway:1.26 ssh-credentials:305.v8f4381501156 ssh-slaves:2.877.v365f5eb_a_b_eec sshd:3.275.v9e17c10f2571 structs:324.va_f5d6774f3a_d timestamper:1.23 token-macro:321.vd7cc1f2a_52c8 trilead-api:2.84.v72119de229b_7 variant:59.vf075fe829ccb workflow-aggregator:596.v8c21c963d92d workflow-api:1208.v0cc7c6e0da_9e workflow-basic-steps:1010.vf7a_b_98e847c1 workflow-cps:3653.v07ea_433c90b_4 workflow-durable-task-step:1241.v1a_63e465f943 workflow-job:1289.vd1c337fd5354 workflow-multibranch:733.v109046189126 workflow-scm-step:408.v7d5b_135a_b_d49 workflow-step-api:639.v6eca_cd8c04a_a_ workflow-support:839.v35e2736cfd5c ws-cleanup:0.44 Result: [Plugin:ant, Plugin:antisamy-markup-formatter, Plugin:apache-httpcomponents-client-4-api, Plugin:authentication-tokens, Plugin:blueocean, Plugin:blueocean-bitbucket-pipeline, Plugin:blueocean-commons, Plugin:blueocean-config, Plugin:blueocean-core-js, Plugin:blueocean-dashboard, Plugin:blueocean-display-url, Plugin:blueocean-events, Plugin:blueocean-git-pipeline, Plugin:blueocean-github-pipeline, Plugin:blueocean-i18n, Plugin:blueocean-jwt, Plugin:blueocean-personalization, Plugin:blueocean-pipeline-api-impl, Plugin:blueocean-pipeline-editor, Plugin:blueocean-pipeline-scm-api, Plugin:blueocean-rest, Plugin:blueocean-rest-impl, Plugin:blueocean-web, Plugin:bootstrap5-api, Plugin:bouncycastle-api, Plugin:branch-api, Plugin:build-timeout, Plugin:caffeine-api, Plugin:checks-api, Plugin:cloudbees-bitbucket-branch-source, Plugin:cloudbees-folder, Plugin:commons-lang3-api, Plugin:commons-text-api, Plugin:credentials, Plugin:credentials-binding, Plugin:display-url-api, Plugin:durable-task, Plugin:echarts-api, Plugin:email-ext, Plugin:favorite, Plugin:font-awesome-api, Plugin:git, Plugin:git-client, Plugin:github, Plugin:github-api, Plugin:github-branch-source, Plugin:gradle, Plugin:handy-uri-templates-2-api, Plugin:htmlpublisher, Plugin:instance-identity, Plugin:ionicons-api, Plugin:jackson2-api, Plugin:jakarta-activation-api, Plugin:jakarta-mail-api, Plugin:javax-activation-api, Plugin:javax-mail-api, Plugin:jaxb, Plugin:jenkins-design-language, Plugin:jjwt-api, Plugin:jquery3-api, Plugin:junit, Plugin:ldap, Plugin:mailer, Plugin:matrix-auth, Plugin:matrix-project, Plugin:mina-sshd-api-common, Plugin:mina-sshd-api-core, Plugin:okhttp-api, Plugin:pam-auth, Plugin:pipeline-build-step, Plugin:pipeline-github-lib, Plugin:pipeline-graph-analysis, Plugin:pipeline-groovy-lib, Plugin:pipeline-input-step, Plugin:pipeline-milestone-step, Plugin:pipeline-model-api, Plugin:pipeline-model-definition, Plugin:pipeline-model-extensions, Plugin:pipeline-rest-api, Plugin:pipeline-stage-step, Plugin:pipeline-stage-tags-metadata, Plugin:pipeline-stage-view, Plugin:plain-credentials, Plugin:plugin-util-api, Plugin:pubsub-light, Plugin:resource-disposer, Plugin:scm-api, Plugin:script-security, Plugin:snakeyaml-api, Plugin:sse-gateway, Plugin:ssh-credentials, Plugin:ssh-slaves, Plugin:sshd, Plugin:structs, Plugin:timestamper, Plugin:token-macro, Plugin:trilead-api, Plugin:variant, Plugin:workflow-aggregator, Plugin:workflow-api, Plugin:workflow-basic-steps, Plugin:workflow-cps, Plugin:workflow-durable-task-step, Plugin:workflow-job, Plugin:workflow-multibranch, Plugin:workflow-scm-step, Plugin:workflow-step-api, Plugin:workflow-support, Plugin:ws-cleanup] ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Linux Fedora 37

Reproduction steps

Create a declarative pipeline as follows

pipeline {
    agent any

    stages {
        stage('parallel-stuff') {
            parallel {
                stage('succeeds') {
                    steps {
                        echo 'Hello World'
                    }
                }
                stage('fails') {
                    steps {
                        sh "exit 1"
                    }
                }
            }

            // POST of external stage
            post {
                unsuccessful {
                    echo 'this doesnt happen in unit test'
                }
            }
        }
    }
}

Expected Results

The echo 'this doesnt happen in unit test' post/unsuccessful action is executed by JenkinsPipelineUnit

Actual Results

The action is not executed

OUtput:

   p1.run()
      p1.pipeline(groovy.lang.Closure)
         p1.echo(Executing on agent [label:any])
         p1.stage(succeeds, groovy.lang.Closure)
            p1.echo(Hello World)
         p1.stage(fails, groovy.lang.Closure)
            p1.sh(exit 1)
         p1.stage(parallel-stuff, groovy.lang.Closure)

Anything else?

When one of parallel stages fails, post is not executed. Probable location of bug: https://github.com/jenkinsci/JenkinsPipelineUnit/blob/9ab57682f9f54e83c80555fef45d7ac00046eb66/src/main/groovy/com/lesfurets/jenkins/unit/declarative/StageDeclaration.groovy#L63-L69

Ran on Jenkins:

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/bug1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (parallel-stuff)
[Pipeline] parallel
[Pipeline] { (Branch: succeeds)
[Pipeline] { (Branch: fails)
[Pipeline] stage
[Pipeline] { (succeeds)
[Pipeline] stage
[Pipeline] { (fails)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] sh
[Pipeline] // stage
[Pipeline] }
+ exit 1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch fails
[Pipeline] // parallel
Post stage
[Pipeline] echo
this doesnt happen in unit test
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
cronon commented 5 months ago

any updates on this? I have a similar problem described here https://stackoverflow.com/questions/76980696/jenkins-declarative-pipeline-stage-post-action-goes-to-failure-block-if-previou