-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package no.nav.syfo.plugins | ||
|
||
import io.ktor.server.application.* | ||
import no.nav.syfo.ApplicationState | ||
|
||
fun Application.configureLifecycleHooks(applicationState: ApplicationState) { | ||
|
||
this.monitor.subscribe(ApplicationStarted) { applicationState.ready = true } | ||
this.monitor.subscribe(ApplicationStopped) { | ||
applicationState.ready = false | ||
applicationState.alive = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package no.nav.syfo.plugins | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.routing.* | ||
import no.nav.syfo.ApplicationState | ||
import no.nav.syfo.nais.isalive.naisIsAliveRoute | ||
import no.nav.syfo.nais.isready.naisIsReadyRoute | ||
import no.nav.syfo.nais.prometheus.naisPrometheusRoute | ||
|
||
fun Application.configureRouting(applicationState: ApplicationState) { | ||
routing { | ||
naisIsAliveRoute(applicationState) | ||
naisIsReadyRoute(applicationState) | ||
naisPrometheusRoute() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,86 @@ | ||
package no.nav.syfo | ||
|
||
import io.ktor.http.HttpMethod | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.routing.routing | ||
import io.ktor.server.testing.TestApplicationEngine | ||
import io.ktor.server.testing.handleRequest | ||
import io.ktor.util.InternalAPI | ||
import io.ktor.server.testing.* | ||
import no.nav.syfo.nais.isalive.naisIsAliveRoute | ||
import no.nav.syfo.nais.isready.naisIsReadyRoute | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class SelfTest { | ||
|
||
@InternalAPI | ||
@Test | ||
internal fun `Returns ok on is_alive`() { | ||
with(TestApplicationEngine()) { | ||
start() | ||
val applicationState = ApplicationState() | ||
applicationState.ready = true | ||
applicationState.alive = true | ||
application.routing { naisIsAliveRoute(applicationState) } | ||
|
||
with(handleRequest(HttpMethod.Get, "/internal/is_alive")) { | ||
assertEquals(HttpStatusCode.OK, response.status()) | ||
assertEquals("I'm alive! :)", response.content) | ||
testApplication { | ||
application { | ||
routing { | ||
val applicationState = ApplicationState() | ||
applicationState.ready = true | ||
applicationState.alive = true | ||
naisIsAliveRoute(applicationState) | ||
} | ||
} | ||
val response = client.get("/internal/is_alive") | ||
|
||
assertEquals(HttpStatusCode.OK, response.status) | ||
assertEquals("I'm alive! :)", response.bodyAsText()) | ||
} | ||
} | ||
|
||
@InternalAPI | ||
@Test | ||
internal fun `Returns ok in is_ready`() { | ||
with(TestApplicationEngine()) { | ||
start() | ||
val applicationState = ApplicationState() | ||
applicationState.ready = true | ||
applicationState.alive = true | ||
application.routing { naisIsReadyRoute(applicationState) } | ||
|
||
with(handleRequest(HttpMethod.Get, "/internal/is_ready")) { | ||
assertEquals(HttpStatusCode.OK, response.status()) | ||
assertEquals("I'm ready! :)", response.content) | ||
testApplication { | ||
application { | ||
routing { | ||
val applicationState = ApplicationState() | ||
applicationState.ready = true | ||
applicationState.alive = true | ||
naisIsReadyRoute(applicationState) | ||
} | ||
} | ||
val response = client.get("/internal/is_ready") | ||
|
||
assertEquals(HttpStatusCode.OK, response.status) | ||
assertEquals("I'm ready! :)", response.bodyAsText()) | ||
} | ||
} | ||
|
||
@InternalAPI | ||
@Test | ||
internal fun `Returns internal server error when liveness check fails`() { | ||
with(TestApplicationEngine()) { | ||
start() | ||
val applicationState = ApplicationState() | ||
applicationState.ready = false | ||
applicationState.alive = false | ||
application.routing { naisIsAliveRoute(applicationState) } | ||
|
||
with(handleRequest(HttpMethod.Get, "/internal/is_alive")) { | ||
assertEquals(HttpStatusCode.InternalServerError, response.status()) | ||
assertEquals("I'm dead x_x", response.content) | ||
testApplication { | ||
application { | ||
routing { | ||
val applicationState = ApplicationState() | ||
applicationState.ready = false | ||
applicationState.alive = false | ||
naisIsAliveRoute(applicationState) | ||
} | ||
} | ||
val response = client.get("/internal/is_alive") | ||
|
||
assertEquals(HttpStatusCode.InternalServerError, response.status) | ||
assertEquals("I'm dead x_x", response.bodyAsText()) | ||
} | ||
} | ||
|
||
@InternalAPI | ||
@Test | ||
internal fun `Returns internal server error when readyness check fails`() { | ||
with(TestApplicationEngine()) { | ||
start() | ||
val applicationState = ApplicationState() | ||
applicationState.ready = false | ||
applicationState.alive = false | ||
application.routing { naisIsReadyRoute(applicationState) } | ||
with(handleRequest(HttpMethod.Get, "/internal/is_ready")) { | ||
assertEquals(HttpStatusCode.InternalServerError, response.status()) | ||
assertEquals("Please wait! I'm not ready :(", response.content) | ||
testApplication { | ||
application { | ||
routing { | ||
val applicationState = ApplicationState() | ||
applicationState.ready = false | ||
applicationState.alive = false | ||
naisIsReadyRoute(applicationState) | ||
} | ||
} | ||
val response = client.get("/internal/is_ready") | ||
|
||
assertEquals(HttpStatusCode.InternalServerError, response.status) | ||
assertEquals("Please wait! I'm not ready :(", response.bodyAsText()) | ||
} | ||
} | ||
} |