github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/HealthExecuteCheckTest.groovy (about) 1 import org.junit.After 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.JenkinsLoggingRule 9 import util.JenkinsReadYamlRule 10 import util.JenkinsStepRule 11 import util.Rules 12 13 import com.sap.piper.Utils 14 15 import static org.hamcrest.Matchers.* 16 import static org.junit.Assert.assertThat 17 18 class HealthExecuteCheckTest extends BasePiperTest { 19 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 20 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 21 private ExpectedException thrown = ExpectedException.none() 22 23 @Rule 24 public RuleChain rules = Rules 25 .getCommonRules(this) 26 .around(new JenkinsReadYamlRule(this)) 27 .around(loggingRule) 28 .around(stepRule) 29 .around(thrown) 30 31 32 @Before 33 void init() throws Exception { 34 // register Jenkins commands with mock values 35 def command1 = "curl -so /dev/null -w '%{response_code}' http://testserver" 36 def command2 = "curl -so /dev/null -w '%{response_code}' http://testserver/endpoint" 37 helper.registerAllowedMethod('sh', [Map.class], {map -> 38 return map.script == command1 || map.script == command2 ? "200" : "404" 39 }) 40 Utils.metaClass.echo = { def m -> } 41 } 42 43 @After 44 public void tearDown() { 45 Utils.metaClass = null 46 } 47 48 @Test 49 void testHealthCheckOk() throws Exception { 50 def testUrl = 'http://testserver/endpoint' 51 52 stepRule.step.healthExecuteCheck( 53 script: nullScript, 54 testServerUrl: testUrl 55 ) 56 57 assertThat(loggingRule.log, containsString("Health check for ${testUrl} successful")) 58 } 59 60 @Test 61 void testHealthCheck404() throws Exception { 62 def testUrl = 'http://testserver/404' 63 64 thrown.expect(Exception) 65 thrown.expectMessage('Health check failed: 404') 66 67 stepRule.step.healthExecuteCheck( 68 script: nullScript, 69 testServerUrl: testUrl 70 ) 71 } 72 73 74 @Test 75 void testHealthCheckWithEndPoint() throws Exception { 76 stepRule.step.healthExecuteCheck( 77 script: nullScript, 78 testServerUrl: 'http://testserver', 79 healthEndpoint: 'endpoint' 80 ) 81 82 assertThat(loggingRule.log, containsString("Health check for http://testserver/endpoint successful")) 83 } 84 85 @Test 86 void testHealthCheckWithEndPointTrailingSlash() throws Exception { 87 stepRule.step.healthExecuteCheck( 88 script: nullScript, 89 testServerUrl: 'http://testserver/', 90 healthEndpoint: 'endpoint' 91 ) 92 93 assertThat(loggingRule.log, containsString("Health check for http://testserver/endpoint successful")) 94 } 95 96 }