2020-11-09 11:15:43 +02:00
import com.sap.piper.DefaultValueCache
2020-09-21 14:29:07 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
2020-11-09 11:15:43 +02:00
import util.JenkinsStepRule
2020-09-21 14:29:07 +02:00
import util.Rules
import static org . junit . Assert . assertEquals
2020-11-09 11:15:43 +02:00
class CheckForLegacyConfigurationTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException . none ( )
private JenkinsStepRule stepRule = new JenkinsStepRule ( this )
2020-09-21 14:29:07 +02:00
String echoOutput = ""
@Rule
public RuleChain ruleChain = Rules
. getCommonRules ( this )
2020-11-09 11:15:43 +02:00
. around ( thrown )
. around ( stepRule )
2020-09-21 14:29:07 +02:00
@Before
void init ( ) {
DefaultValueCache . createInstance ( [
steps: [
customStep: [
param: 'test'
]
]
] )
helper . registerAllowedMethod ( 'addBadge' , [ Map ] , { return } )
helper . registerAllowedMethod ( 'createSummary' , [ Map ] , { return } )
2020-11-06 11:17:26 +02:00
helper . registerAllowedMethod ( "echo" , [ String . class ] , { s - >
if ( echoOutput ) {
echoOutput + = "\n"
}
echoOutput + = s
} )
2020-09-21 14:29:07 +02:00
helper . registerAllowedMethod ( 'findFiles' , [ Map ] , { m - >
if ( m . glob = = '**/package.json' ) {
return [ new File ( "package.json" ) ] . toArray ( )
} else {
return [ ]
}
} )
helper . registerAllowedMethod ( 'readJSON' , [ Map ] , { m - >
if ( m . file . contains ( 'package.json' ) ) {
return [ scripts: [ oldNpmScriptName: "echo test" ,
npmScript2: "echo test" ] ]
} else {
return [ : ]
}
} )
}
@Test
void testCheckForRemovedConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ oldConfigKey: false ] ] ]
Map configChanges = [ oldConfigKey: [ steps: [ 'someStep' ] , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
2020-11-09 11:15:43 +02:00
"This configuration option was removed. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForReplacedConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ oldConfigKey: false ] ] ]
Map configChanges = [ oldConfigKey: [ steps: [ 'someStep' ] , newConfigKey: "newConfigKey" , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
2020-11-09 11:15:43 +02:00
"This configuration option was removed. Please use the parameter newConfigKey instead. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedConfigKeysWithWarning ( ) {
String expectedWarning = "[WARNING] Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
"This configuration option was removed. test"
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ oldConfigKey: false ] ] ]
Map configChanges = [ oldConfigKey: [ steps: [ 'someStep' ] , warnInsteadOfError: true , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
assertEquals ( expectedWarning , echoOutput )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedStageConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ stages: [ someStage: [ oldConfigKey: false ] ] ]
Map configChanges = [ oldConfigKey: [ stages: [ 'someStage' ] ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key oldConfigKey for the stage someStage. " +
2020-11-09 11:15:43 +02:00
"This configuration option was removed. " ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedGeneralConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ general: [ oldConfigKey: false ] ]
Map configChanges = [ oldConfigKey: [ general: true ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key oldConfigKey in the general section. " +
2020-11-09 11:15:43 +02:00
"This configuration option was removed. " ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedPostActionConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ postActions: [ oldConfigKey: false ] ]
Map configChanges = [ oldConfigKey: [ postAction: true ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedConfigKeys ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key oldConfigKey in the postActions section. " +
2020-11-09 11:15:43 +02:00
"This configuration option was removed. " ] )
2020-09-21 14:29:07 +02:00
}
2020-11-10 15:18:37 +02:00
@Test
void testCheckForMissingConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ : ] ] ]
Map configChanges = [ importantConfigKey: [ steps: [ 'someStep' ] , customMessage: "test" ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( errors , [ "Your pipeline configuration does not contain the configuration key importantConfigKey for the step someStep. test" ] )
}
@Test
void testCheckForMissingConfigKeysWithWarning ( ) {
String expectedWarning = "[WARNING] Your pipeline configuration does not contain the configuration key importantConfigKey for the step someStep. test"
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ : ] ] ]
Map configChanges = [ importantConfigKey: [ steps: [ 'someStep' ] , warnInsteadOfError: true , customMessage: "test" ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( expectedWarning , echoOutput )
assertEquals ( errors , [ ] )
}
@Test
void testCheckForMissingStageConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ stages: [ someStage: [ : ] ] ]
Map configChanges = [ importantConfigKey: [ stages: [ 'someStage' ] ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( errors , [ "Your pipeline configuration does not contain the configuration key importantConfigKey for the stage someStage. " ] )
}
@Test
void testCheckForMissingGeneralConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ general: [ : ] ]
Map configChanges = [ importantConfigKey: [ general: true ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( errors , [ "Your pipeline configuration does not contain the configuration key importantConfigKey in the general section. " ] )
}
@Test
void testCheckForPresentGeneralConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ general: [ importantConfigKey: 'isPresent' ] ]
Map configChanges = [ importantConfigKey: [ general: true ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( errors , [ ] )
}
@Test
void testCheckForMissingPostActionConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ postActions: [ : ] ]
Map configChanges = [ importantConfigKey: [ postAction: true ] ]
List errors = stepRule . step . checkForLegacyConfiguration . checkForMissingConfigKeys ( nullScript , configChanges )
assertEquals ( errors , [ "Your pipeline configuration does not contain the configuration key importantConfigKey in the postActions section. " ] )
}
2020-09-21 14:29:07 +02:00
@Test
void testCheckForReplacedStep ( ) {
2020-11-06 11:17:26 +02:00
String oldStep = "oldStep"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ steps: [ oldStep: [ configKey: false ] ] ]
2020-11-09 11:15:43 +02:00
Map configChanges = [ oldStep: [ newStepName: 'newStep' , customMessage: "test" ] ]
2020-09-21 14:29:07 +02:00
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedSteps ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains configuration for the step $oldStep. " +
2020-11-09 11:15:43 +02:00
"This step has been removed. Please configure the step newStep instead. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedStep ( ) {
2020-11-06 11:17:26 +02:00
String oldStep = "oldStep"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ steps: [ oldStep: [ configKey: false ] ] ]
Map configChanges = [ oldStep: [ customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedSteps ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains configuration for the step $oldStep. " +
2020-11-09 11:15:43 +02:00
"This step has been removed. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedStepOnlyProjectConfig ( ) {
DefaultValueCache . createInstance ( [
steps: [
oldStep: [
configKey: false
]
]
] )
nullScript . commonPipelineEnvironment . configuration = [ steps: [ newStep: [ configKey: false ] ] ]
Map configChanges = [ oldStep: [ onlyCheckProjectConfig: true ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedSteps ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForReplacedStage ( ) {
2020-11-06 11:17:26 +02:00
String oldStage = "oldStage"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ stages: [ oldStage: [ configKey: false ] ] ]
Map configChanges = [ oldStage: [ newStageName: 'newStage' , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedStages ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains configuration for the stage $oldStage. " +
2020-11-09 11:15:43 +02:00
"This stage has been removed. Please configure the stage newStage instead. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRemovedStage ( ) {
2020-11-06 11:17:26 +02:00
String oldStage = "oldStage"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ stages: [ oldStage: [ configKey: false ] ] ]
Map configChanges = [ oldStage: [ ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRemovedOrReplacedStages ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains configuration for the stage $oldStage. " +
2020-11-09 11:15:43 +02:00
"This stage has been removed. " ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForStageParameterTypeChanged ( ) {
2020-11-06 11:17:26 +02:00
String stageName = "productionDeployment"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ stages: [ productionDeployment: [ configKeyOldType: "string" ] ] ]
Map configChanges = [ configKeyOldType: [ oldType: "String" , newType: "List" , stages: [ "productionDeployment" , "endToEndTests" ] , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForParameterTypeChanged ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key configKeyOldType for the stage $stageName. " +
2020-11-09 11:15:43 +02:00
"The type of this configuration parameter was changed from String to List. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForStepParameterTypeChanged ( ) {
2020-11-06 11:17:26 +02:00
String stepName = "testStep"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ steps: [ testStep: [ configKeyOldType: "string" ] ] ]
Map configChanges = [ configKeyOldType: [ oldType: "String" , newType: "List" , steps: [ "testStep" ] , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForParameterTypeChanged ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key configKeyOldType for the step $stepName. " +
2020-11-09 11:15:43 +02:00
"The type of this configuration parameter was changed from String to List. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForGeneralParameterTypeChanged ( ) {
2020-11-06 11:17:26 +02:00
String key = "configKeyOldType"
2020-09-21 14:29:07 +02:00
nullScript . commonPipelineEnvironment . configuration = [ general: [ configKeyOldType: "string" ] ]
Map configChanges = [ configKeyOldType: [ oldType: "String" , newType: "List" , general: true , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForParameterTypeChanged ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your pipeline configuration contains the configuration key $key in the general section. " +
2020-11-09 11:15:43 +02:00
"The type of this configuration parameter was changed from String to List. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForUnsupportedParameterTypeChanged ( ) {
String expectedWarning = "Your legacy config settings contain an entry for parameterTypeChanged with the key configKeyOldType with the unsupported type Map. " +
"Currently only the type 'String' is supported."
nullScript . commonPipelineEnvironment . configuration = [ steps: [ testStep: [ configKeyOldType: [ test: true ] ] ] ]
Map configChanges = [ configKeyOldType: [ oldType: "Map" , newType: "List" , steps: [ "testStep" ] , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForParameterTypeChanged ( nullScript , configChanges )
assertEquals ( expectedWarning , errors [ 0 ] . toString ( ) )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRenamedNpmScripts ( ) {
Map configChanges = [ oldNpmScriptName: [ newScriptName: "newNpmScriptName" , customMessage: "test" ] ]
2020-11-09 11:15:43 +02:00
List errors = stepRule . step . checkForLegacyConfiguration . checkForRenamedNpmScripts ( nullScript , configChanges )
2020-11-06 11:17:26 +02:00
assertEquals ( errors , [ "Your package.json file package.json contains an npm script using the deprecated name oldNpmScriptName. " +
2020-11-09 11:15:43 +02:00
"Please rename the script to newNpmScriptName, since the script oldNpmScriptName will not be executed by the pipeline anymore. test" ] )
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckForRenamedNpmScriptsWithWarning ( ) {
Map configChanges = [ oldNpmScriptName: [ newScriptName: "newNpmScriptName" , warnInsteadOfError: true , customMessage: "test" ] ]
String expectedWarning = "[WARNING] 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"
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration . checkForRenamedNpmScripts ( nullScript , configChanges )
2020-09-21 14:29:07 +02:00
assertEquals ( expectedWarning , echoOutput )
}
@Test
void testCheckConfigurationRemovedOrReplacedConfigKeys ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ someStep: [ oldConfigKey: false ] ] ]
Map configChanges = [
removedOrReplacedConfigKeys: [
oldConfigKey: [
steps: [ 'someStep' ] ,
customMessage: "test"
]
]
]
2020-11-06 11:17:26 +02:00
String exception = "Failing pipeline due to configuration errors. Please see log output above."
String output = "Your pipeline configuration contains the configuration key oldConfigKey for the step someStep. " +
"This configuration option was removed. test"
assertExceptionAndOutput ( exception , output ) {
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration ( script: nullScript , legacyConfigSettings: configChanges )
2020-11-06 11:17:26 +02:00
}
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckConfigurationRemovedOrReplacedSteps ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ oldStep: [ configKey: false ] ] ]
Map configChanges = [
removedOrReplacedSteps: [
oldStep: [
newStepName: 'newStep' ,
customMessage: "test"
]
]
]
2020-11-06 11:17:26 +02:00
String exception = "Failing pipeline due to configuration errors. Please see log output above."
String output = "Your pipeline configuration contains configuration for the step oldStep. " +
"This step has been removed. Please configure the step newStep instead. test"
assertExceptionAndOutput ( exception , output ) {
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration ( script: nullScript , legacyConfigSettings: configChanges )
2020-11-06 11:17:26 +02:00
}
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckConfigurationRemovedOrReplacedStages ( ) {
nullScript . commonPipelineEnvironment . configuration = [ stages: [ oldStage: [ configKey: false ] ] ]
Map configChanges = [
removedOrReplacedStages: [
oldStage: [ ]
]
]
2020-11-06 11:17:26 +02:00
String exception = "Failing pipeline due to configuration errors. Please see log output above."
String output = "Your pipeline configuration contains configuration for the stage oldStage. " +
"This stage has been removed. "
assertExceptionAndOutput ( exception , output ) {
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration ( script: nullScript , legacyConfigSettings: configChanges )
2020-11-06 11:17:26 +02:00
}
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckConfigurationParameterTypeChanged ( ) {
nullScript . commonPipelineEnvironment . configuration = [ steps: [ testStep: [ configKeyOldType: "string" ] ] ]
Map configChanges = [
parameterTypeChanged: [
configKeyOldType: [
oldType: "String" ,
newType: "List" ,
steps: [ "testStep" ] ,
customMessage: "test" ]
]
]
2020-11-06 11:17:26 +02:00
String exception = "Failing pipeline due to configuration errors. Please see log output above."
String output = "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"
assertExceptionAndOutput ( exception , output ) {
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration ( script: nullScript , legacyConfigSettings: configChanges )
2020-11-06 11:17:26 +02:00
}
2020-09-21 14:29:07 +02:00
}
@Test
void testCheckConfigurationRenamedNpmScript ( ) {
Map configChanges = [
renamedNpmScript: [
oldNpmScriptName: [
newScriptName: "newNpmScriptName" ,
customMessage: "test" ]
]
]
2020-11-06 11:17:26 +02:00
String exception = "Failing pipeline due to configuration errors. Please see log output above."
String output = "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"
assertExceptionAndOutput ( exception , output ) {
2020-11-09 11:15:43 +02:00
stepRule . step . checkForLegacyConfiguration ( script: nullScript , legacyConfigSettings: configChanges )
2020-11-06 11:17:26 +02:00
}
}
private void assertExceptionAndOutput ( String exception , String output , Closure body ) {
String actualException = ""
try {
body ( )
} catch ( Exception e ) {
actualException = e . getMessage ( )
}
assertEquals ( exception , actualException )
assertEquals ( output , echoOutput )
2020-09-21 14:29:07 +02:00
}
2020-11-09 11:15:43 +02:00
2020-09-21 14:29:07 +02:00
}