github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PipelineRestartStepsTest.groovy (about) 1 import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException 2 import org.junit.Rule 3 import org.junit.Test 4 import org.junit.rules.RuleChain 5 import util.* 6 7 import static org.hamcrest.CoreMatchers.containsString 8 import static org.hamcrest.CoreMatchers.is 9 import static org.junit.Assert.assertThat 10 11 class PipelineRestartStepsTest extends BasePiperTest { 12 13 private JenkinsErrorRule errorRule = new JenkinsErrorRule(this) 14 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 15 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 16 17 @Rule 18 public RuleChain chain = Rules.getCommonRules(this) 19 .around(new JenkinsReadYamlRule(this)) 20 .around(errorRule) 21 .around(loggingRule) 22 .around(stepRule) 23 24 @Test 25 void testError() throws Exception { 26 27 def mailBuildResult = '' 28 helper.registerAllowedMethod('mailSendNotification', [Map.class], { m -> 29 mailBuildResult = m.buildResult 30 return null 31 }) 32 33 helper.registerAllowedMethod('timeout', [Map.class, Closure.class], { m, closure -> 34 assertThat(m.time, is(1)) 35 assertThat(m.unit, is('SECONDS')) 36 return closure() 37 }) 38 39 def iterations = 0 40 helper.registerAllowedMethod('input', [Map.class], { m -> 41 iterations ++ 42 assertThat(m.message, is('Do you want to restart?')) 43 assertThat(m.ok, is('Restart')) 44 if (iterations > 1) { 45 throw new FlowInterruptedException() 46 } else { 47 return null 48 } 49 }) 50 51 try { 52 stepRule.step.pipelineRestartSteps ([ 53 script: nullScript, 54 jenkinsUtilsStub: jenkinsUtils, 55 sendMail: true, 56 timeoutInSeconds: 1 57 58 ]) { 59 throw new hudson.AbortException('I just created an error') 60 } 61 } catch(err) { 62 assertThat(loggingRule.log, containsString('ERROR occurred: hudson.AbortException: I just created an error')) 63 assertThat(mailBuildResult, is('UNSTABLE')) 64 } 65 } 66 67 @Test 68 void testErrorNoMail() throws Exception { 69 70 def mailBuildResult = '' 71 helper.registerAllowedMethod('mailSendNotification', [Map.class], { m -> 72 mailBuildResult = m.buildResult 73 return null 74 }) 75 76 helper.registerAllowedMethod('timeout', [Map.class, Closure.class], { m, closure -> 77 assertThat(m.time, is(1)) 78 assertThat(m.unit, is('SECONDS')) 79 return closure() 80 }) 81 82 def iterations = 0 83 helper.registerAllowedMethod('input', [Map.class], { m -> 84 iterations ++ 85 assertThat(m.message, is('Do you want to restart?')) 86 assertThat(m.ok, is('Restart')) 87 if (iterations > 1) { 88 throw new FlowInterruptedException() 89 } else { 90 return null 91 } 92 }) 93 94 try { 95 stepRule.step.pipelineRestartSteps ([ 96 script: nullScript, 97 jenkinsUtilsStub: jenkinsUtils, 98 sendMail: false, 99 timeoutInSeconds: 1 100 101 ]) { 102 throw new hudson.AbortException('I just created an error') 103 } 104 } catch(err) { 105 assertThat(loggingRule.log, containsString('ERROR occurred: hudson.AbortException: I just created an error')) 106 assertThat(mailBuildResult, is('')) 107 } 108 } 109 110 @Test 111 void testSuccess() throws Exception { 112 113 stepRule.step.pipelineRestartSteps ([ 114 script: nullScript, 115 jenkinsUtilsStub: jenkinsUtils, 116 sendMail: false, 117 timeoutInSeconds: 1 118 119 ]) { 120 nullScript.echo 'This is a test' 121 } 122 123 assertThat(loggingRule.log, containsString('This is a test')) 124 } 125 }