github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/PiperPipelineStageConfirmTest.groovy (about) 1 package templates 2 3 import org.junit.Before 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.rules.RuleChain 7 import util.* 8 9 import static org.hamcrest.Matchers.containsString 10 import static org.hamcrest.Matchers.is 11 import static org.junit.Assert.assertThat 12 13 class PiperPipelineStageConfirmTest extends BasePiperTest { 14 private JenkinsStepRule jsr = new JenkinsStepRule(this) 15 private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) 16 17 private Map timeoutSettings 18 private Map inputSettings 19 private milestoneCalled = false 20 21 @Rule 22 public RuleChain rules = Rules 23 .getCommonRules(this) 24 .around(new JenkinsReadYamlRule(this)) 25 .around(jlr) 26 .around(jsr) 27 28 @Before 29 void init() { 30 binding.variables.env.STAGE_NAME = 'Confirm' 31 32 helper.registerAllowedMethod('timeout', [Map.class, Closure.class], {m, body -> 33 timeoutSettings = m 34 return body() 35 }) 36 37 helper.registerAllowedMethod('input', [Map.class], {m -> 38 inputSettings = m 39 return [reason: 'this is my test reason for failing step 1 and step 3', acknowledgement: true] 40 }) 41 42 helper.registerAllowedMethod('milestone', [],{ 43 milestoneCalled = true 44 }) 45 } 46 47 @Test 48 void testStageDefault() { 49 50 jsr.step.piperPipelineStageConfirm( 51 script: nullScript 52 ) 53 assertThat(timeoutSettings.unit, is('HOURS')) 54 assertThat(timeoutSettings.time, is(720)) 55 assertThat(inputSettings.message, is('Shall we proceed to Promote & Release?')) 56 } 57 58 @Test 59 void testStageBuildUnstable() { 60 61 binding.setVariable('currentBuild', [result: 'UNSTABLE']) 62 nullScript.commonPipelineEnvironment.setValue('unstableSteps', ['step1', 'step3']) 63 64 helper.registerAllowedMethod('text', [Map.class], {m -> 65 assertThat(m.defaultValue, is('')) 66 assertThat(m.description, containsString('Please provide a reason for overruling the failed steps step1, step3, with 10 characters or more:')) 67 assertThat(m.name, is('reason')) 68 }) 69 70 helper.registerAllowedMethod('booleanParam', [Map.class], {m -> 71 assertThat(m.description, is('I acknowledge that for traceability purposes the approval reason is stored together with my user name / user id:')) 72 assertThat(m.name, is('acknowledgement')) 73 }) 74 75 jsr.step.piperPipelineStageConfirm( 76 script: nullScript 77 ) 78 assertThat(inputSettings.message, is('Approve continuation of pipeline, although some steps failed.')) 79 80 assertThat(jlr.log, containsString('step1')) 81 assertThat(jlr.log, containsString('step3')) 82 assertThat(jlr.log, containsString('this is my test reason')) 83 assertThat(jlr.log, containsString('Acknowledgement\n---------------\n☑ I acknowledge that for traceability purposes the approval reason is stored together with my user name / user id')) 84 } 85 86 @Test 87 void callsMilestone(){ 88 jsr.step.piperPipelineStageConfirm( 89 script: nullScript 90 ) 91 92 assertThat(milestoneCalled, is(true)) 93 } 94 }