2020-09-24 13:47:20 +02:00
|
|
|
import org.junit.After
|
2018-10-12 16:06:41 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsLoggingRule
|
|
|
|
import util.JenkinsReadYamlRule
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
2020-09-24 13:47:20 +02:00
|
|
|
import com.sap.piper.Utils
|
|
|
|
|
2018-10-12 16:06:41 +02:00
|
|
|
import static org.hamcrest.Matchers.*
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
|
|
|
|
class HealthExecuteCheckTest extends BasePiperTest {
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2018-10-12 16:06:41 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.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-10-12 16:06:41 +02:00
|
|
|
.around(thrown)
|
|
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Exception {
|
|
|
|
// register Jenkins commands with mock values
|
|
|
|
def command1 = "curl -so /dev/null -w '%{response_code}' http://testserver"
|
|
|
|
def command2 = "curl -so /dev/null -w '%{response_code}' http://testserver/endpoint"
|
|
|
|
helper.registerAllowedMethod('sh', [Map.class], {map ->
|
|
|
|
return map.script == command1 || map.script == command2 ? "200" : "404"
|
|
|
|
})
|
2020-09-24 13:47:20 +02:00
|
|
|
Utils.metaClass.echo = { def m -> }
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
Utils.metaClass = null
|
2018-10-12 16:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHealthCheckOk() throws Exception {
|
|
|
|
def testUrl = 'http://testserver/endpoint'
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.healthExecuteCheck(
|
2018-10-12 16:06:41 +02:00
|
|
|
script: nullScript,
|
|
|
|
testServerUrl: testUrl
|
|
|
|
)
|
|
|
|
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString("Health check for ${testUrl} successful"))
|
2018-10-12 16:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHealthCheck404() throws Exception {
|
|
|
|
def testUrl = 'http://testserver/404'
|
|
|
|
|
|
|
|
thrown.expect(Exception)
|
|
|
|
thrown.expectMessage('Health check failed: 404')
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.healthExecuteCheck(
|
2018-10-12 16:06:41 +02:00
|
|
|
script: nullScript,
|
|
|
|
testServerUrl: testUrl
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHealthCheckWithEndPoint() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.healthExecuteCheck(
|
2018-10-12 16:06:41 +02:00
|
|
|
script: nullScript,
|
|
|
|
testServerUrl: 'http://testserver',
|
|
|
|
healthEndpoint: 'endpoint'
|
|
|
|
)
|
|
|
|
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString("Health check for http://testserver/endpoint successful"))
|
2018-10-12 16:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHealthCheckWithEndPointTrailingSlash() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.healthExecuteCheck(
|
2018-10-12 16:06:41 +02:00
|
|
|
script: nullScript,
|
|
|
|
testServerUrl: 'http://testserver/',
|
|
|
|
healthEndpoint: 'endpoint'
|
|
|
|
)
|
|
|
|
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString("Health check for http://testserver/endpoint successful"))
|
2018-10-12 16:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|