2
0
mirror of https://github.com/VinylDNS/vinyldns synced 2025-08-22 10:10:12 +00:00
vinyldns/modules/portal/test/controllers/HealthControllerSpec.scala
Peter Cline aa49c1813c
.asHealthCheck takes a class param for reporting (#954)
* .asHealthCheck takes a class param for reporting

Previously if a health check failed, there was no way of knowing _which_
one failed, unless you can isolate the particular type of failure --
though they're often very general (connection timeout, etc). If a health
check fails, it's important to know which one, or at least to log which
one it was.

I'm not sure if there's a way to infer the calling class without doing
some gnarly and expensive call-stack stuff, so here's a simple fix. If
there's a better way, I'm all ears.

I did experiment a little bit parameterizing the HealthCheck, but that
honestly seemed like more trouble than it was worth.
2020-06-29 09:43:32 -05:00

64 lines
2.1 KiB
Scala

/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controllers
import cats.effect.IO
import org.junit.runner.RunWith
import org.specs2.mutable.Specification
import org.specs2.runner.JUnitRunner
import play.api.mvc.ControllerComponents
import play.api.test.Helpers.{GET, status}
import play.api.test.{FakeRequest, Helpers}
import vinyldns.core.health.HealthService
import vinyldns.core.health.HealthCheck._
import play.api.test.Helpers._
@RunWith(classOf[JUnitRunner])
class HealthControllerSpec extends Specification {
val components: ControllerComponents = Helpers.stubControllerComponents()
"HealthController" should {
"send 200 if the healthcheck succeeds" in {
val healthService =
new HealthService(List(IO.unit.attempt.asHealthCheck(classOf[HealthControllerSpec])))
val controller = new HealthController(components, healthService)
val result = controller
.health()
.apply(FakeRequest(GET, "/health"))
status(result) must beEqualTo(200)
}
"send 500 if a healthcheck fails" in {
val err = IO
.raiseError(new RuntimeException("bad!!"))
.attempt
.asHealthCheck(classOf[HealthControllerSpec])
val healthService =
new HealthService(List(IO.unit.attempt.asHealthCheck(classOf[HealthControllerSpec]), err))
val controller = new HealthController(components, healthService)
val result = controller
.health()
.apply(FakeRequest(GET, "/health"))
status(result) must beEqualTo(500)
}
}
}