/cluster-http/src/test/scala/akka/management/cluster/MultiDcSpec.scala

https://github.com/akka/akka-management · Scala · 86 lines · 75 code · 8 blank · 3 comment · 0 complexity · be40e467b461bc0dc1a0b7d36f801ffc MD5 · raw file

  1. /*
  2. * Copyright (C) 2017-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package akka.management.cluster
  5. import akka.actor.ActorSystem
  6. import akka.http.scaladsl.Http
  7. import akka.http.scaladsl.model.{ HttpRequest, StatusCodes }
  8. import akka.http.scaladsl.unmarshalling.Unmarshal
  9. import akka.management.scaladsl.ManagementRouteProviderSettings
  10. import akka.stream.ActorMaterializer
  11. import akka.testkit.SocketUtil
  12. import com.typesafe.config.ConfigFactory
  13. import org.scalatest.concurrent.{ Eventually, ScalaFutures }
  14. import org.scalatest.time.{ Millis, Seconds, Span }
  15. import org.scalatest.matchers.should.Matchers
  16. import org.scalatest.wordspec.AnyWordSpec
  17. class MultiDcSpec
  18. extends AnyWordSpec
  19. with Matchers
  20. with ScalaFutures
  21. with ClusterHttpManagementJsonProtocol
  22. with Eventually {
  23. implicit val patience: PatienceConfig = PatienceConfig(timeout = Span(10, Seconds), interval = Span(50, Millis))
  24. val config = ConfigFactory.parseString(
  25. """
  26. |akka.actor.provider = "cluster"
  27. |akka.remote.log-remote-lifecycle-events = off
  28. |akka.remote.artery.canonical.hostname = "127.0.0.1"
  29. |akka.remote.artery.enabled = true
  30. |#akka.loglevel = DEBUG
  31. """.stripMargin
  32. )
  33. "Http cluster management" must {
  34. "allow multiple DCs" in {
  35. val Vector(httpPortA, portA, portB) = SocketUtil.temporaryServerAddresses(3, "127.0.0.1").map(_.getPort)
  36. val dcA = ConfigFactory.parseString(
  37. s"""
  38. |akka.management.http.hostname = "127.0.0.1"
  39. |akka.management.http.port = $httpPortA
  40. |akka.cluster.seed-nodes = ["akka://MultiDcSystem@127.0.0.1:$portA"]
  41. |akka.cluster.multi-data-center.self-data-center = "DC-A"
  42. |akka.remote.artery.canonical.port = $portA
  43. |akka.remote.artery.canonical.port = $portA
  44. | """.stripMargin
  45. )
  46. val dcB = ConfigFactory.parseString(
  47. s"""
  48. |akka.cluster.seed-nodes = ["akka://MultiDcSystem@127.0.0.1:$portA"]
  49. |akka.cluster.multi-data-center.self-data-center = "DC-B"
  50. |akka.remote.artery.canonical.port = $portB
  51. | """.stripMargin
  52. )
  53. implicit val dcASystem = ActorSystem("MultiDcSystem", config.withFallback(dcA))
  54. val dcBSystem = ActorSystem("MultiDcSystem", config.withFallback(dcB))
  55. implicit val materializer = ActorMaterializer()
  56. val routeSettings =
  57. ManagementRouteProviderSettings(selfBaseUri = s"http://127.0.0.1:$httpPortA", readOnly = false)
  58. try {
  59. Http()
  60. .bindAndHandle(ClusterHttpManagementRouteProvider(dcASystem).routes(routeSettings), "127.0.0.1", httpPortA)
  61. .futureValue
  62. eventually {
  63. val response =
  64. Http().singleRequest(HttpRequest(uri = s"http://127.0.0.1:$httpPortA/cluster/members")).futureValue
  65. response.status should equal(StatusCodes.OK)
  66. val members = Unmarshal(response.entity).to[ClusterMembers].futureValue
  67. members.members.size should equal(2)
  68. members.members.map(_.status) should equal(Set("Up"))
  69. }
  70. } finally {
  71. dcASystem.terminate()
  72. dcBSystem.terminate()
  73. }
  74. }
  75. }
  76. }