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