github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PiperStageWrapperTest.groovy (about)

     1  import com.sap.piper.DebugReport
     2  import hudson.AbortException
     3  import org.junit.Before
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.ExpectedException
     7  import org.junit.rules.RuleChain
     8  import util.BasePiperTest
     9  import util.JenkinsEnvironmentRule
    10  import util.JenkinsLoggingRule
    11  import util.JenkinsReadYamlRule
    12  import util.JenkinsStepRule
    13  import util.Rules
    14  
    15  import static org.hamcrest.CoreMatchers.containsString
    16  import static org.hamcrest.Matchers.contains
    17  import static org.hamcrest.Matchers.is
    18  import static org.hamcrest.Matchers.not
    19  import static org.junit.Assert.assertThat
    20  
    21  class PiperStageWrapperTest extends BasePiperTest {
    22  
    23      private ExpectedException thrown = ExpectedException.none()
    24  
    25      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    26      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    27  
    28      private Map lockMap = [:]
    29      private int countNodeUsage = 0
    30      private String nodeLabel = ''
    31      private boolean executedOnKubernetes = false
    32      private List customEnv = []
    33  
    34      @Rule
    35      public RuleChain rules = Rules
    36          .getCommonRules(this)
    37          .around(thrown)
    38          .around(new JenkinsReadYamlRule(this))
    39          .around(loggingRule)
    40          .around(stepRule)
    41  
    42      @Before
    43      void init() throws Exception {
    44  
    45          helper.registerAllowedMethod('deleteDir', [], {return null})
    46          helper.registerAllowedMethod('lock', [Map.class, Closure.class], {m, body ->
    47              assertThat(m.resource.toString(), containsString('/10'))
    48              lockMap = m
    49              body()
    50          })
    51          helper.registerAllowedMethod('milestone', [Integer.class], {ordinal ->
    52              assertThat(ordinal, is(10))
    53          })
    54          helper.registerAllowedMethod('node', [String.class, Closure.class], {s, body ->
    55              nodeLabel = s
    56              countNodeUsage++
    57              body()
    58  
    59          })
    60  
    61          helper.registerAllowedMethod('dockerExecuteOnKubernetes', [Map.class, Closure.class], {params, body ->
    62              executedOnKubernetes = true
    63              body()
    64          })
    65  
    66          helper.registerAllowedMethod('withEnv', [List.class, Closure.class], {env, body ->
    67              customEnv = env
    68              body()
    69          })
    70  
    71  
    72          helper.registerAllowedMethod('fileExists', [String.class], {s ->
    73              return false
    74          })
    75      }
    76  
    77      @Test
    78      void testDefault() {
    79          def executed = false
    80          stepRule.step.piperStageWrapper(
    81              script: nullScript,
    82              juStabUtils: utils,
    83              ordinal: 10,
    84              stageName: 'test'
    85  
    86          ) {
    87              executed = true
    88          }
    89          assertThat(executed, is(true))
    90          assertThat(executedOnKubernetes, is(false))
    91          assertThat(lockMap.size(), is(2))
    92          assertThat(countNodeUsage, is(1))
    93      }
    94  
    95      @Test
    96      void testNoLocking() {
    97          def executed = false
    98          stepRule.step.piperStageWrapper(
    99              script: nullScript,
   100              juStabUtils: utils,
   101              nodeLabel: 'testLabel',
   102              ordinal: 10,
   103              stageLocking: false,
   104              stageName: 'test'
   105  
   106          ) {
   107              executed = true
   108          }
   109          assertThat(executed, is(true))
   110          assertThat(lockMap.size(), is(0))
   111          assertThat(countNodeUsage, is(1))
   112          assertThat(nodeLabel, is('testLabel'))
   113      }
   114  
   115      @Test
   116      void testExecuteStageOnKubernetes() {
   117          def executed = false
   118  
   119          binding.variables.env.ON_K8S = true
   120          nullScript.commonPipelineEnvironment.configuration = [general: [runStageInPod: true]]
   121  
   122          stepRule.step.piperStageWrapper(
   123              script: nullScript,
   124              juStabUtils: utils,
   125              stageName: 'test',
   126              ordinal: 10
   127          ) {
   128              executed = true
   129          }
   130          assertThat(executed, is(true))
   131          assertThat(executedOnKubernetes, is(true))
   132          assertThat(customEnv[1].toString(), is("POD_NAME=test"))
   133      }
   134  
   135      @Test
   136      void testStageNameInEnv() {
   137          def executed = false
   138  
   139          binding.variables.env.STAGE_NAME = 'label'
   140  
   141          stepRule.step.piperStageWrapper(
   142              script: nullScript,
   143              juStabUtils: utils,
   144              stageName: 'test',
   145              ordinal: 10
   146          ) {
   147              executed = true
   148          }
   149          assertThat(executed, is(true))
   150          assertThat(customEnv[0].toString(), is("STAGE_NAME=test"))
   151      }
   152  
   153      @Test
   154      void testStageNameAlreadyInEnv() {
   155          def executed = false
   156  
   157          binding.variables.env.STAGE_NAME = 'test'
   158  
   159          stepRule.step.piperStageWrapper(
   160              script: nullScript,
   161              juStabUtils: utils,
   162              ordinal: 10
   163          ) {
   164              executed = true
   165          }
   166          assertThat(executed, is(true))
   167          assertThat(customEnv.size(), is(0))
   168      }
   169  
   170      @Test
   171      void testStageExit() {
   172          helper.registerAllowedMethod('fileExists', [String.class], {s ->
   173              return (s == '.pipeline/extensions/test.groovy')
   174          })
   175  
   176          helper.registerAllowedMethod('load', [String.class], {
   177              return helper.loadScript('test/resources/stages/test.groovy')
   178          })
   179          nullScript.commonPipelineEnvironment.gitBranch = 'testBranch'
   180  
   181          def executed = false
   182          stepRule.step.piperStageWrapper(
   183              script: nullScript,
   184              juStabUtils: utils,
   185              ordinal: 10,
   186              stageName: 'test'
   187          ) {
   188              executed = true
   189          }
   190  
   191          assertThat(executed, is(true))
   192          assertThat(loggingRule.log, containsString('[piperStageWrapper] Running project interceptor \'.pipeline/extensions/test.groovy\' for test.'))
   193          assertThat(loggingRule.log, containsString('Stage Name: test'))
   194          assertThat(loggingRule.log, containsString('Config: ['))
   195          assertThat(loggingRule.log, containsString('testBranch'))
   196      }
   197  
   198      @Test
   199      void testGlobalOverwritingExtension() {
   200          helper.registerAllowedMethod('fileExists', [String.class], {s ->
   201              return (s == '.pipeline/tmp/global_extensions/test_global_overwriting.groovy')
   202          })
   203  
   204          helper.registerAllowedMethod('load', [String.class], {
   205              return helper.loadScript('test/resources/stages/test_global_overwriting.groovy')
   206          })
   207          nullScript.commonPipelineEnvironment.gitBranch = 'testBranch'
   208  
   209          def executed = false
   210          stepRule.step.piperStageWrapper(
   211              script: nullScript,
   212              juStabUtils: utils,
   213              ordinal: 10,
   214              stageName: 'test_global_overwriting'
   215          ) {
   216              executed = true
   217          }
   218  
   219          assertThat(executed, is(false))
   220          assertThat(loggingRule.log, containsString('Stage Name: test_global_overwriting'))
   221          assertThat(loggingRule.log, containsString('Config: ['))
   222          assertThat(loggingRule.log, containsString('testBranch'))
   223          assertThat(loggingRule.log, containsString('Not calling test_global_overwriting'))
   224          assertThat(DebugReport.instance.globalExtensions.test_global_overwriting, is('Overwrites'))
   225      }
   226  
   227      @Test
   228      void testStageOldInterceptor() {
   229          helper.registerAllowedMethod('fileExists', [String.class], { path ->
   230              return (path == '.pipeline/extensions/test_old_extension.groovy')
   231          })
   232  
   233          helper.registerAllowedMethod('load', [String.class], {
   234              return helper.loadScript('test/resources/stages/test_old_extension.groovy')
   235          })
   236          nullScript.commonPipelineEnvironment.gitBranch = 'testBranch'
   237  
   238          def executed = false
   239          stepRule.step.piperStageWrapper(
   240              script: nullScript,
   241              juStabUtils: utils,
   242              ordinal: 10,
   243              stageName: 'test_old_extension'
   244          ) {
   245              executed = true
   246          }
   247  
   248          assertThat(executed, is(true))
   249          assertThat(loggingRule.log, containsString('[piperStageWrapper] Running project interceptor \'.pipeline/extensions/test_old_extension.groovy\' for test_old_extension.'))
   250          assertThat(loggingRule.log, containsString('[Warning] The interface to implement extensions has changed.'))
   251          assertThat(loggingRule.log, containsString('Stage Name: test_old_extension'))
   252          assertThat(loggingRule.log, containsString('Config: ['))
   253          assertThat(loggingRule.log, containsString('testBranch'))
   254          assertThat(DebugReport.instance.localExtensions.test_old_extension, is('Extends'))
   255      }
   256  
   257      @Test
   258      void testExtensionDeactivation() {
   259          helper.registerAllowedMethod('fileExists', [String.class], { path ->
   260              return (path == '.pipeline/extensions/test_old_extension.groovy')
   261          })
   262          helper.registerAllowedMethod('load', [String.class], {
   263              return helper.loadScript('test/resources/stages/test_old_extension.groovy')
   264          })
   265  
   266          nullScript.commonPipelineEnvironment.gitBranch = 'testBranch'
   267          nullScript.env = [PIPER_DISABLE_EXTENSIONS: 'true']
   268          stepRule.step.piperStageWrapper(
   269              script: nullScript,
   270              juStabUtils: utils,
   271              ordinal: 10,
   272              stageName: 'test_old_extension'
   273          ) {}
   274          //setting above parameter to 'true' bypasses the below message
   275          assertThat(loggingRule.log, not(containsString("[piperStageWrapper] Running project interceptor '.pipeline/extensions/test_old_extension.groovy' for test_old_extension.")))
   276      }
   277  
   278      @Test
   279      void testPipelineResilienceMandatoryStep() {
   280          thrown.expectMessage('expected error')
   281  
   282          nullScript.commonPipelineEnvironment.configuration = [general: [failOnError: false]]
   283  
   284          stepRule.step.piperStageWrapper (script: nullScript, stageLocking: false, stageName: 'testStage', juStabUtils: utils) {
   285              throw new AbortException('expected error')
   286          }
   287      }
   288  
   289      @Test
   290      void testStageCrashesInExtension() {
   291          helper.registerAllowedMethod('fileExists', [String.class], { path ->
   292              return (path == '.pipeline/tmp/global_extensions/test_crashing_extension.groovy')
   293          })
   294  
   295          helper.registerAllowedMethod('load', [String.class], {
   296              return helper.loadScript('test/resources/stages/test_crashing_extension.groovy')
   297          })
   298  
   299          Throwable caught = null
   300          def executed = false
   301          // Clear DebugReport to avoid left-overs from another UnitTest
   302          DebugReport.instance.failedBuild = [:]
   303  
   304          try {
   305              stepRule.step.piperStageWrapper(
   306                  script: nullScript,
   307                  juStabUtils: utils,
   308                  ordinal: 10,
   309                  stageName: 'test_crashing_extension'
   310              ) {
   311                  executed = true
   312              }
   313          } catch (Throwable t) {
   314              caught = t
   315          }
   316  
   317          assertThat(executed, is(true))
   318          assertThat(loggingRule.log, containsString('[piperStageWrapper] Found global interceptor \'.pipeline/tmp/global_extensions/test_crashing_extension.groovy\' for test_crashing_extension.'))
   319          assertThat(DebugReport.instance.failedBuild.step, is('test_crashing_extension(extended)'))
   320          assertThat(DebugReport.instance.failedBuild.fatal, is('true'))
   321          assertThat(DebugReport.instance.failedBuild.reason, is(caught))
   322      }
   323  }