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

     1  import com.sap.piper.DefaultValueCache
     2  import org.junit.Before
     3  import org.junit.Rule
     4  import org.junit.Test
     5  import org.junit.rules.ExpectedException
     6  import org.junit.rules.RuleChain
     7  import util.BasePiperTest
     8  import util.JenkinsStepRule
     9  import util.Rules
    10  
    11  import static org.junit.Assert.assertEquals
    12  
    13  
    14  class CheckForLegacyConfigurationTest extends BasePiperTest {
    15      private ExpectedException thrown = ExpectedException.none()
    16      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    17  
    18      String echoOutput = ""
    19  
    20      @Rule
    21      public RuleChain ruleChain = Rules
    22          .getCommonRules(this)
    23          .around(thrown)
    24          .around(stepRule)
    25  
    26  
    27      @Before
    28      void init() {
    29          DefaultValueCache.createInstance([
    30              steps: [
    31                  customStep: [
    32                      param: 'test'
    33                  ]
    34              ]
    35          ])
    36          helper.registerAllowedMethod('addBadge', [Map], {return})
    37          helper.registerAllowedMethod('createSummary', [Map], {return})
    38          helper.registerAllowedMethod("echo", [String.class], { s ->
    39              if (echoOutput) {
    40                  echoOutput += "\n"
    41              }
    42              echoOutput += s
    43          })
    44          helper.registerAllowedMethod('findFiles', [Map], {m ->
    45              if(m.glob == '**/package.json') {
    46                  return [new File("package.json")].toArray()
    47              } else {
    48                  return []
    49              }
    50          })
    51          helper.registerAllowedMethod('readJSON', [Map], { m ->
    52              if (m.file.contains('package.json')) {
    53                  return [scripts: [oldNpmScriptName: "echo test",
    54                                    npmScript2: "echo test"]]
    55              } else {
    56                  return [:]
    57              }
    58          })
    59      }
    60  
    61      @Test
    62      void testCheckForRemovedConfigKeys() {
    63          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [oldConfigKey: false]]]
    64          Map configChanges = [oldConfigKey: [steps: ['someStep'], customMessage: "test"]]
    65  
    66          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
    67  
    68          assertEquals(errors, ["Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
    69                                    "This configuration option was removed. test"])
    70      }
    71  
    72      @Test
    73      void testCheckForReplacedConfigKeys() {
    74          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [oldConfigKey: false]]]
    75          Map configChanges = [oldConfigKey: [steps: ['someStep'], newConfigKey: "newConfigKey", customMessage: "test"]]
    76  
    77          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
    78  
    79          assertEquals(errors, ["Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
    80                                    "This configuration option was removed. Please use the parameter newConfigKey instead. test"])
    81      }
    82  
    83      @Test
    84      void testCheckForRemovedConfigKeysWithWarning() {
    85          String expectedWarning = "[WARNING] Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
    86              "This configuration option was removed. test"
    87  
    88          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [oldConfigKey: false]]]
    89          Map configChanges = [oldConfigKey: [steps: ['someStep'], warnInsteadOfError: true, customMessage: "test"]]
    90  
    91          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
    92          assertEquals(expectedWarning, echoOutput)
    93          assertEquals(errors, [])
    94      }
    95  
    96      @Test
    97      void testCheckForRemovedStageConfigKeys() {
    98          nullScript.commonPipelineEnvironment.configuration = [stages: [someStage: [oldConfigKey: false]]]
    99          Map configChanges = [oldConfigKey: [stages: ['someStage']]]
   100  
   101          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
   102  
   103          assertEquals(errors, ["Your pipeline configuration contains the configuration key oldConfigKey for the stage someStage. " +
   104                                    "This configuration option was removed. "])
   105      }
   106  
   107      @Test
   108      void testCheckForRemovedGeneralConfigKeys() {
   109          nullScript.commonPipelineEnvironment.configuration = [general: [oldConfigKey: false]]
   110          Map configChanges = [oldConfigKey: [general: true]]
   111  
   112          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
   113  
   114          assertEquals(errors, ["Your pipeline configuration contains the configuration key oldConfigKey in the general section. " +
   115                                    "This configuration option was removed. "])
   116      }
   117  
   118      @Test
   119      void testCheckForRemovedPostActionConfigKeys() {
   120          nullScript.commonPipelineEnvironment.configuration = [postActions: [oldConfigKey: false]]
   121          Map configChanges = [oldConfigKey: [postAction: true]]
   122  
   123          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedConfigKeys(nullScript, configChanges)
   124  
   125          assertEquals(errors, ["Your pipeline configuration contains the configuration key oldConfigKey in the postActions section. " +
   126                                    "This configuration option was removed. "])
   127      }
   128  
   129      @Test
   130      void testCheckForMissingConfigKeys() {
   131          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [:]]]
   132          Map configChanges = [importantConfigKey: [steps: ['someStep'], customMessage: "test"]]
   133  
   134          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   135  
   136          assertEquals(errors, ["Your pipeline configuration does not contain the configuration key importantConfigKey for the step someStep. test"])
   137      }
   138  
   139      @Test
   140      void testCheckForMissingConfigKeysWithWarning() {
   141          String expectedWarning = "[WARNING] Your pipeline configuration does not contain the configuration key importantConfigKey for the step someStep. test"
   142  
   143          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [:]]]
   144          Map configChanges = [importantConfigKey: [steps: ['someStep'], warnInsteadOfError: true, customMessage: "test"]]
   145  
   146          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   147          assertEquals(expectedWarning, echoOutput)
   148          assertEquals(errors, [])
   149      }
   150  
   151      @Test
   152      void testCheckForMissingStageConfigKeys() {
   153          nullScript.commonPipelineEnvironment.configuration = [stages: [someStage: [:]]]
   154          Map configChanges = [importantConfigKey: [stages: ['someStage']]]
   155  
   156          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   157  
   158          assertEquals(errors, ["Your pipeline configuration does not contain the configuration key importantConfigKey for the stage someStage. "])
   159      }
   160  
   161      @Test
   162      void testCheckForMissingGeneralConfigKeys() {
   163          nullScript.commonPipelineEnvironment.configuration = [general: [:]]
   164          Map configChanges = [importantConfigKey: [general: true]]
   165  
   166          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   167  
   168          assertEquals(errors, ["Your pipeline configuration does not contain the configuration key importantConfigKey in the general section. "])
   169      }
   170  
   171      @Test
   172      void testCheckForPresentGeneralConfigKeys() {
   173          nullScript.commonPipelineEnvironment.configuration = [general: [importantConfigKey: 'isPresent']]
   174          Map configChanges = [importantConfigKey: [general: true]]
   175  
   176          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   177  
   178          assertEquals(errors, [])
   179      }
   180  
   181      @Test
   182      void testCheckForMissingPostActionConfigKeys() {
   183          nullScript.commonPipelineEnvironment.configuration = [postActions: [:]]
   184          Map configChanges = [importantConfigKey: [postAction: true]]
   185  
   186          List errors = stepRule.step.checkForLegacyConfiguration.checkForMissingConfigKeys(nullScript, configChanges)
   187  
   188          assertEquals(errors, ["Your pipeline configuration does not contain the configuration key importantConfigKey in the postActions section. "])
   189      }
   190  
   191      @Test
   192      void testCheckForReplacedStep() {
   193          String oldStep = "oldStep"
   194          nullScript.commonPipelineEnvironment.configuration = [steps: [oldStep: [configKey: false]]]
   195          Map configChanges = [oldStep: [newStepName: 'newStep', customMessage: "test"]]
   196  
   197          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedSteps(nullScript, configChanges)
   198  
   199          assertEquals(errors, ["Your pipeline configuration contains configuration for the step $oldStep. " +
   200                                    "This step has been removed. Please configure the step newStep instead. test"])
   201      }
   202  
   203      @Test
   204      void testCheckForRemovedStep() {
   205          String oldStep = "oldStep"
   206          nullScript.commonPipelineEnvironment.configuration = [steps: [oldStep: [configKey: false]]]
   207          Map configChanges = [oldStep: [customMessage: "test"]]
   208  
   209          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedSteps(nullScript, configChanges)
   210  
   211          assertEquals(errors, ["Your pipeline configuration contains configuration for the step $oldStep. " +
   212                                    "This step has been removed. test"])
   213      }
   214  
   215      @Test
   216      void testCheckForRemovedStepOnlyProjectConfig() {
   217          DefaultValueCache.createInstance([
   218              steps: [
   219                  oldStep: [
   220                      configKey: false
   221                  ]
   222              ]
   223          ])
   224          nullScript.commonPipelineEnvironment.configuration = [steps: [newStep: [configKey: false]]]
   225          Map configChanges = [oldStep: [onlyCheckProjectConfig: true]]
   226  
   227          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedSteps(nullScript, configChanges)
   228  
   229          assertEquals(errors, [])
   230      }
   231  
   232      @Test
   233      void testCheckForReplacedStage() {
   234          String oldStage = "oldStage"
   235          nullScript.commonPipelineEnvironment.configuration = [stages: [oldStage: [configKey: false]]]
   236          Map configChanges = [oldStage: [newStageName: 'newStage', customMessage: "test"]]
   237  
   238          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedStages(nullScript, configChanges)
   239  
   240          assertEquals(errors, ["Your pipeline configuration contains configuration for the stage $oldStage. " +
   241                                    "This stage has been removed. Please configure the stage newStage instead. test"])
   242      }
   243  
   244      @Test
   245      void testCheckForRemovedStage() {
   246          String oldStage = "oldStage"
   247          nullScript.commonPipelineEnvironment.configuration = [stages: [oldStage: [configKey: false]]]
   248          Map configChanges = [oldStage: []]
   249  
   250          List errors = stepRule.step.checkForLegacyConfiguration.checkForRemovedOrReplacedStages(nullScript, configChanges)
   251  
   252          assertEquals(errors, ["Your pipeline configuration contains configuration for the stage $oldStage. " +
   253                                    "This stage has been removed. "])
   254      }
   255  
   256      @Test
   257      void testCheckForStageParameterTypeChanged() {
   258          String stageName = "productionDeployment"
   259          nullScript.commonPipelineEnvironment.configuration = [stages: [productionDeployment: [configKeyOldType: "string"]]]
   260          Map configChanges = [configKeyOldType: [oldType: "String", newType: "List", stages: ["productionDeployment", "endToEndTests"], customMessage: "test"]]
   261  
   262          List errors = stepRule.step.checkForLegacyConfiguration.checkForParameterTypeChanged(nullScript, configChanges)
   263  
   264          assertEquals(errors, ["Your pipeline configuration contains the configuration key configKeyOldType for the stage $stageName. " +
   265                                    "The type of this configuration parameter was changed from String to List. test"])
   266      }
   267  
   268      @Test
   269      void testCheckForStepParameterTypeChanged() {
   270          String stepName = "testStep"
   271          nullScript.commonPipelineEnvironment.configuration = [steps: [testStep: [configKeyOldType: "string"]]]
   272          Map configChanges = [configKeyOldType: [oldType: "String", newType: "List", steps: ["testStep"], customMessage: "test"]]
   273  
   274          List errors = stepRule.step.checkForLegacyConfiguration.checkForParameterTypeChanged(nullScript, configChanges)
   275  
   276          assertEquals(errors, ["Your pipeline configuration contains the configuration key configKeyOldType for the step $stepName. " +
   277                                    "The type of this configuration parameter was changed from String to List. test"])
   278      }
   279  
   280      @Test
   281      void testCheckForGeneralParameterTypeChanged() {
   282          String key = "configKeyOldType"
   283          nullScript.commonPipelineEnvironment.configuration = [general: [configKeyOldType: "string"]]
   284          Map configChanges = [configKeyOldType: [oldType: "String", newType: "List", general: true, customMessage: "test"]]
   285  
   286          List errors = stepRule.step.checkForLegacyConfiguration.checkForParameterTypeChanged(nullScript, configChanges)
   287  
   288          assertEquals(errors, ["Your pipeline configuration contains the configuration key $key in the general section. " +
   289                                    "The type of this configuration parameter was changed from String to List. test"])
   290      }
   291  
   292      @Test
   293      void testCheckForUnsupportedParameterTypeChanged() {
   294          String expectedWarning = "Your legacy config settings contain an entry for parameterTypeChanged with the key configKeyOldType with the unsupported type Map. " +
   295              "Currently only the type 'String' is supported."
   296          nullScript.commonPipelineEnvironment.configuration = [steps: [testStep: [configKeyOldType: [test: true]]]]
   297          Map configChanges = [configKeyOldType: [oldType: "Map", newType: "List", steps: ["testStep"], customMessage: "test"]]
   298  
   299          List errors = stepRule.step.checkForLegacyConfiguration.checkForParameterTypeChanged(nullScript, configChanges)
   300          assertEquals(expectedWarning, errors[0].toString())
   301      }
   302  
   303      @Test
   304      void testCheckForRenamedNpmScripts() {
   305          Map configChanges = [oldNpmScriptName: [newScriptName: "newNpmScriptName", customMessage: "test"]]
   306  
   307          List errors = stepRule.step.checkForLegacyConfiguration.checkForRenamedNpmScripts(nullScript, configChanges)
   308  
   309          assertEquals(errors, ["Your package.json file package.json contains an npm script using the deprecated name oldNpmScriptName. " +
   310                                    "Please rename the script to newNpmScriptName, since the script oldNpmScriptName will not be executed by the pipeline anymore. test"])
   311      }
   312  
   313      @Test
   314      void testCheckForRenamedNpmScriptsWithWarning() {
   315          Map configChanges = [oldNpmScriptName: [newScriptName: "newNpmScriptName", warnInsteadOfError: true, customMessage: "test"]]
   316          String expectedWarning = "[WARNING] Your package.json file package.json contains an npm script using the deprecated name oldNpmScriptName. " +
   317              "Please rename the script to newNpmScriptName, since the script oldNpmScriptName will not be executed by the pipeline anymore. test"
   318  
   319          stepRule.step.checkForLegacyConfiguration.checkForRenamedNpmScripts(nullScript, configChanges)
   320  
   321          assertEquals(expectedWarning, echoOutput)
   322      }
   323  
   324      @Test
   325      void testCheckConfigurationRemovedOrReplacedConfigKeys() {
   326          nullScript.commonPipelineEnvironment.configuration = [steps: [someStep: [oldConfigKey: false]]]
   327          Map configChanges = [
   328              removedOrReplacedConfigKeys: [
   329                  oldConfigKey: [
   330                      steps: ['someStep'],
   331                      customMessage: "test"
   332                  ]
   333              ]
   334          ]
   335  
   336          String exception = "Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. This configuration option was removed. test\n" +
   337              "Failing pipeline due to configuration errors. Please see log output above."
   338          String output = ""
   339  
   340          assertExceptionAndOutput(exception, output) {
   341              stepRule.step.checkForLegacyConfiguration(script: nullScript, legacyConfigSettings: configChanges)
   342          }
   343      }
   344  
   345      @Test
   346      void testCheckConfigurationRemovedOrReplacedSteps() {
   347          nullScript.commonPipelineEnvironment.configuration = [steps: [oldStep: [configKey: false]]]
   348          Map configChanges = [
   349              removedOrReplacedSteps: [
   350                  oldStep: [
   351                      newStepName: 'newStep',
   352                      customMessage: "test"
   353                  ]
   354              ]
   355          ]
   356  
   357          String exception = "Your pipeline configuration contains configuration for the step oldStep. This step has been removed. Please configure the step newStep instead. test\n" +
   358              "Failing pipeline due to configuration errors. Please see log output above."
   359          String output = ""
   360  
   361          assertExceptionAndOutput(exception, output) {
   362              stepRule.step.checkForLegacyConfiguration(script: nullScript, legacyConfigSettings: configChanges)
   363          }
   364      }
   365  
   366      @Test
   367      void testCheckConfigurationRemovedOrReplacedStages() {
   368          nullScript.commonPipelineEnvironment.configuration = [stages: [oldStage: [configKey: false]]]
   369          Map configChanges = [
   370              removedOrReplacedStages: [
   371                  oldStage: []
   372              ]
   373          ]
   374  
   375          String exception = "Your pipeline configuration contains configuration for the stage oldStage. This stage has been removed. \n" +
   376              "Failing pipeline due to configuration errors. Please see log output above."
   377          String output = ""
   378  
   379          assertExceptionAndOutput(exception, output) {
   380              stepRule.step.checkForLegacyConfiguration(script: nullScript, legacyConfigSettings: configChanges)
   381          }
   382      }
   383  
   384      @Test
   385      void testCheckConfigurationParameterTypeChanged() {
   386          nullScript.commonPipelineEnvironment.configuration = [steps: [testStep: [configKeyOldType: "string"]]]
   387          Map configChanges = [
   388              parameterTypeChanged: [
   389                  configKeyOldType: [
   390                      oldType: "String",
   391                      newType: "List",
   392                      steps: ["testStep"],
   393                      customMessage: "test"]
   394              ]
   395          ]
   396  
   397          String exception = "Your pipeline configuration contains the configuration key configKeyOldType for the step testStep. The type of this configuration parameter was changed from String to List. test\n" +
   398              "Failing pipeline due to configuration errors. Please see log output above."
   399          String output = ""
   400  
   401          assertExceptionAndOutput(exception, output) {
   402              stepRule.step.checkForLegacyConfiguration(script: nullScript, legacyConfigSettings: configChanges)
   403          }
   404      }
   405  
   406      @Test
   407      void testCheckConfigurationRenamedNpmScript() {
   408          Map configChanges = [
   409              renamedNpmScript: [
   410                  oldNpmScriptName: [
   411                      newScriptName: "newNpmScriptName",
   412                      customMessage: "test"]
   413              ]
   414          ]
   415  
   416          String exception = "Your package.json file package.json contains an npm script using the deprecated name oldNpmScriptName. Please rename the script to newNpmScriptName, since the script oldNpmScriptName will not be executed by the pipeline anymore. test\n" +
   417              "Failing pipeline due to configuration errors. Please see log output above."
   418          String output = ""
   419  
   420          assertExceptionAndOutput(exception, output) {
   421              stepRule.step.checkForLegacyConfiguration(script: nullScript, legacyConfigSettings: configChanges)
   422          }
   423      }
   424  
   425      private void assertExceptionAndOutput(String exception, String output, Closure body) {
   426          String actualException = ""
   427          try {
   428              body()
   429          } catch (Exception e) {
   430              actualException = e.getMessage()
   431          }
   432          assertEquals(exception, actualException)
   433          assertEquals(output, echoOutput)
   434      }
   435  
   436  }