2020-02-03 10:37:04 +02:00
import com.sap.piper.DebugReport
2020-03-18 09:32:28 +02:00
import hudson.AbortException
2018-12-12 12:45:11 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
2020-03-18 09:32:28 +02:00
import org.junit.rules.ExpectedException
2018-12-12 12:45:11 +02:00
import org.junit.rules.RuleChain
import util.BasePiperTest
2020-04-17 10:31:04 +02:00
import util.JenkinsEnvironmentRule
2018-12-12 12:45:11 +02:00
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
import static org . hamcrest . CoreMatchers . containsString
2020-04-17 10:31:04 +02:00
import static org . hamcrest . Matchers . contains
2020-02-04 11:35:34 +02:00
import static org . hamcrest . Matchers . is
2020-03-31 12:51:34 +02:00
import static org . hamcrest . Matchers . not
2018-12-12 12:45:11 +02:00
import static org . junit . Assert . assertThat
class PiperStageWrapperTest extends BasePiperTest {
2020-03-18 09:32:28 +02:00
private ExpectedException thrown = ExpectedException . none ( )
2019-01-22 10:22:15 +02:00
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule ( this )
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule ( this )
2018-12-12 12:45:11 +02:00
private Map lockMap = [ : ]
private int countNodeUsage = 0
private String nodeLabel = ''
2020-04-17 10:31:04 +02:00
private boolean executedOnKubernetes = false
private List customEnv = [ ]
2018-12-12 12:45:11 +02:00
@Rule
public RuleChain rules = Rules
. getCommonRules ( this )
2020-03-18 09:32:28 +02:00
. around ( thrown )
2018-12-12 12:45:11 +02:00
. around ( new JenkinsReadYamlRule ( this ) )
2019-01-22 10:22:15 +02:00
. around ( loggingRule )
2019-01-22 10:25:42 +02:00
. around ( stepRule )
2018-12-12 12:45:11 +02:00
@Before
void init ( ) throws Exception {
helper . registerAllowedMethod ( 'deleteDir' , [ ] , { return null } )
helper . registerAllowedMethod ( 'lock' , [ Map . class , Closure . class ] , { m , body - >
assertThat ( m . resource . toString ( ) , containsString ( '/10' ) )
lockMap = m
body ( )
} )
helper . registerAllowedMethod ( 'milestone' , [ Integer . class ] , { ordinal - >
assertThat ( ordinal , is ( 10 ) )
} )
helper . registerAllowedMethod ( 'node' , [ String . class , Closure . class ] , { s , body - >
nodeLabel = s
countNodeUsage + +
body ( )
} )
2020-04-17 10:31:04 +02:00
helper . registerAllowedMethod ( 'dockerExecuteOnKubernetes' , [ Map . class , Closure . class ] , { params , body - >
executedOnKubernetes = true
body ( )
} )
helper . registerAllowedMethod ( 'withEnv' , [ List . class , Closure . class ] , { env , body - >
customEnv = env
body ( )
} )
2018-12-12 12:45:11 +02:00
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { s - >
return false
} )
}
@Test
void testDefault ( ) {
2019-01-14 11:25:47 +02:00
def executed = false
2019-01-22 10:25:42 +02:00
stepRule . step . piperStageWrapper (
2018-12-12 12:45:11 +02:00
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test'
) {
2019-01-14 11:25:47 +02:00
executed = true
2018-12-12 12:45:11 +02:00
}
2019-01-14 11:25:47 +02:00
assertThat ( executed , is ( true ) )
2020-04-17 10:31:04 +02:00
assertThat ( executedOnKubernetes , is ( false ) )
2018-12-12 12:45:11 +02:00
assertThat ( lockMap . size ( ) , is ( 2 ) )
assertThat ( countNodeUsage , is ( 1 ) )
}
@Test
void testNoLocking ( ) {
2019-01-14 11:25:47 +02:00
def executed = false
2019-01-22 10:25:42 +02:00
stepRule . step . piperStageWrapper (
2018-12-12 12:45:11 +02:00
script: nullScript ,
juStabUtils: utils ,
nodeLabel: 'testLabel' ,
ordinal: 10 ,
stageLocking: false ,
stageName: 'test'
) {
2019-01-14 11:25:47 +02:00
executed = true
2018-12-12 12:45:11 +02:00
}
2019-01-14 11:25:47 +02:00
assertThat ( executed , is ( true ) )
2018-12-12 12:45:11 +02:00
assertThat ( lockMap . size ( ) , is ( 0 ) )
assertThat ( countNodeUsage , is ( 1 ) )
assertThat ( nodeLabel , is ( 'testLabel' ) )
}
2020-04-17 10:31:04 +02:00
@Test
void testExecuteStageOnKubernetes ( ) {
def executed = false
binding . variables . env . ON_K8S = true
nullScript . commonPipelineEnvironment . configuration = [ general: [ runStageInPod: true ] ]
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
stageName: 'test' ,
ordinal: 10
) {
executed = true
}
assertThat ( executed , is ( true ) )
assertThat ( executedOnKubernetes , is ( true ) )
2020-08-13 17:01:22 +02:00
assertThat ( customEnv [ 1 ] . toString ( ) , is ( "POD_NAME=test" ) )
}
@Test
void testStageNameInEnv ( ) {
def executed = false
binding . variables . env . STAGE_NAME = 'label'
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
stageName: 'test' ,
ordinal: 10
) {
executed = true
}
assertThat ( executed , is ( true ) )
assertThat ( customEnv [ 0 ] . toString ( ) , is ( "STAGE_NAME=test" ) )
}
@Test
void testStageNameAlreadyInEnv ( ) {
def executed = false
binding . variables . env . STAGE_NAME = 'test'
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
ordinal: 10
) {
executed = true
}
assertThat ( executed , is ( true ) )
assertThat ( customEnv . size ( ) , is ( 0 ) )
2020-04-17 10:31:04 +02:00
}
2018-12-12 12:45:11 +02:00
@Test
void testStageExit ( ) {
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { s - >
return ( s = = '.pipeline/extensions/test.groovy' )
} )
helper . registerAllowedMethod ( 'load' , [ String . class ] , {
return helper . loadScript ( 'test/resources/stages/test.groovy' )
} )
2019-01-14 11:25:47 +02:00
nullScript . commonPipelineEnvironment . gitBranch = 'testBranch'
2018-12-12 12:45:11 +02:00
2019-01-14 11:25:47 +02:00
def executed = false
2019-01-22 10:25:42 +02:00
stepRule . step . piperStageWrapper (
2018-12-12 12:45:11 +02:00
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test'
) {
2019-01-14 11:25:47 +02:00
executed = true
2018-12-12 12:45:11 +02:00
}
2019-01-14 11:25:47 +02:00
assertThat ( executed , is ( true ) )
2019-01-22 10:22:15 +02:00
assertThat ( loggingRule . log , containsString ( '[piperStageWrapper] Running project interceptor \'.pipeline/extensions/test.groovy\' for test.' ) )
assertThat ( loggingRule . log , containsString ( 'Stage Name: test' ) )
2019-02-05 16:37:59 +02:00
assertThat ( loggingRule . log , containsString ( 'Config: [' ) )
2019-01-22 10:22:15 +02:00
assertThat ( loggingRule . log , containsString ( 'testBranch' ) )
2018-12-12 12:45:11 +02:00
}
2020-02-03 12:34:31 +02:00
@Test
void testGlobalOverwritingExtension ( ) {
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { s - >
2020-06-22 17:52:11 +02:00
return ( s = = '.pipeline/tmp/global_extensions/test_global_overwriting.groovy' )
2020-02-03 12:34:31 +02:00
} )
helper . registerAllowedMethod ( 'load' , [ String . class ] , {
return helper . loadScript ( 'test/resources/stages/test_global_overwriting.groovy' )
} )
nullScript . commonPipelineEnvironment . gitBranch = 'testBranch'
def executed = false
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test_global_overwriting'
) {
executed = true
}
assertThat ( executed , is ( false ) )
assertThat ( loggingRule . log , containsString ( 'Stage Name: test_global_overwriting' ) )
assertThat ( loggingRule . log , containsString ( 'Config: [' ) )
assertThat ( loggingRule . log , containsString ( 'testBranch' ) )
assertThat ( loggingRule . log , containsString ( 'Not calling test_global_overwriting' ) )
assertThat ( DebugReport . instance . globalExtensions . test_global_overwriting , is ( 'Overwrites' ) )
}
2020-01-20 16:50:22 +02:00
@Test
void testStageOldInterceptor ( ) {
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { path - >
return ( path = = '.pipeline/extensions/test_old_extension.groovy' )
} )
helper . registerAllowedMethod ( 'load' , [ String . class ] , {
return helper . loadScript ( 'test/resources/stages/test_old_extension.groovy' )
} )
nullScript . commonPipelineEnvironment . gitBranch = 'testBranch'
def executed = false
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test_old_extension'
) {
executed = true
}
assertThat ( executed , is ( true ) )
assertThat ( loggingRule . log , containsString ( '[piperStageWrapper] Running project interceptor \'.pipeline/extensions/test_old_extension.groovy\' for test_old_extension.' ) )
assertThat ( loggingRule . log , containsString ( '[Warning] The interface to implement extensions has changed.' ) )
assertThat ( loggingRule . log , containsString ( 'Stage Name: test_old_extension' ) )
assertThat ( loggingRule . log , containsString ( 'Config: [' ) )
assertThat ( loggingRule . log , containsString ( 'testBranch' ) )
2020-02-03 10:37:04 +02:00
assertThat ( DebugReport . instance . localExtensions . test_old_extension , is ( 'Extends' ) )
2020-01-20 16:50:22 +02:00
}
2020-02-04 11:35:34 +02:00
2020-03-31 12:51:34 +02:00
@Test
void testExtensionDeactivation ( ) {
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { path - >
return ( path = = '.pipeline/extensions/test_old_extension.groovy' )
} )
helper . registerAllowedMethod ( 'load' , [ String . class ] , {
return helper . loadScript ( 'test/resources/stages/test_old_extension.groovy' )
} )
nullScript . commonPipelineEnvironment . gitBranch = 'testBranch'
2020-08-27 15:49:09 +02:00
nullScript . env = [ PIPER_DISABLE_EXTENSIONS: 'true' ]
2020-03-31 12:51:34 +02:00
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test_old_extension'
) { }
//setting above parameter to 'true' bypasses the below message
assertThat ( loggingRule . log , not ( containsString ( "[piperStageWrapper] Running project interceptor '.pipeline/extensions/test_old_extension.groovy' for test_old_extension." ) ) )
}
2020-03-18 09:32:28 +02:00
@Test
void testPipelineResilienceMandatoryStep ( ) {
thrown . expectMessage ( 'expected error' )
nullScript . commonPipelineEnvironment . configuration = [ general: [ failOnError: false ] ]
stepRule . step . piperStageWrapper ( script: nullScript , stageLocking: false , stageName: 'testStage' , juStabUtils: utils ) {
throw new AbortException ( 'expected error' )
}
}
2020-02-04 11:35:34 +02:00
@Test
void testStageCrashesInExtension ( ) {
helper . registerAllowedMethod ( 'fileExists' , [ String . class ] , { path - >
2020-06-22 17:52:11 +02:00
return ( path = = '.pipeline/tmp/global_extensions/test_crashing_extension.groovy' )
2020-02-04 11:35:34 +02:00
} )
helper . registerAllowedMethod ( 'load' , [ String . class ] , {
return helper . loadScript ( 'test/resources/stages/test_crashing_extension.groovy' )
} )
Throwable caught = null
def executed = false
2020-04-15 14:02:41 +02:00
// Clear DebugReport to avoid left-overs from another UnitTest
DebugReport . instance . failedBuild = [ : ]
2020-02-04 11:35:34 +02:00
try {
stepRule . step . piperStageWrapper (
script: nullScript ,
juStabUtils: utils ,
ordinal: 10 ,
stageName: 'test_crashing_extension'
) {
executed = true
}
} catch ( Throwable t ) {
caught = t
}
assertThat ( executed , is ( true ) )
2020-06-22 17:52:11 +02:00
assertThat ( loggingRule . log , containsString ( '[piperStageWrapper] Found global interceptor \'.pipeline/tmp/global_extensions/test_crashing_extension.groovy\' for test_crashing_extension.' ) )
2020-04-15 14:02:41 +02:00
assertThat ( DebugReport . instance . failedBuild . step , is ( 'test_crashing_extension(extended)' ) )
2020-02-04 11:35:34 +02:00
assertThat ( DebugReport . instance . failedBuild . fatal , is ( 'true' ) )
assertThat ( DebugReport . instance . failedBuild . reason , is ( caught ) )
}
2020-01-20 16:50:22 +02:00
}