From 184a6063b04dc4618e2f9598b566d65edf817738 Mon Sep 17 00:00:00 2001 From: Omar Ahmed Date: Mon, 23 Aug 2021 17:42:12 +0200 Subject: [PATCH 1/3] RabbitMQ Container: Ensure proper configuration preload and add specs --- build.sbt | 2 +- .../testcontainers/RabbitMQContainer.scala | 18 ++-- .../testcontainers/RabbitMQSpec.scala | 99 +++++++++++++++++++ project/Dependencies.scala | 5 + 4 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala diff --git a/build.sbt b/build.sbt index 2a1dc18b..47482d9e 100644 --- a/build.sbt +++ b/build.sbt @@ -406,7 +406,7 @@ lazy val modulePulsar = (project in file("modules/pulsar")) ) lazy val moduleRabbitmq = (project in file("modules/rabbitmq")) - .dependsOn(core % "compile->compile;test->test;provided->provided") + .dependsOn(core % "compile->compile;test->test;provided->provided", scalatest % "test->test") .settings(commonSettings: _*) .settings( name := "testcontainers-scala-rabbitmq", diff --git a/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala b/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala index 95311fb7..2e55d2c6 100644 --- a/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala +++ b/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala @@ -30,6 +30,15 @@ case class RabbitMQContainer( c.withAdminPassword(adminPassword) + vhosts.foreach { + case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing) + case RabbitMQContainer.VHost(name, None) => c.withVhost(name) + } + + vhostsLimits.foreach { x => + c.withVhostLimit(x.vhost, x.name, x.value) + } + queues.foreach { x => c.withQueue(x.name, x.autoDelete, x.durable, toJavaArguments(x.arguments)) } @@ -51,15 +60,6 @@ case class RabbitMQContainer( if (x.tags.isEmpty) c.withUser(x.name, x.password) else c.withUser(x.name, x.password, x.tags.asJava) } - vhosts.foreach { - case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing) - case RabbitMQContainer.VHost(name, None) => c.withVhost(name) - } - - vhostsLimits.foreach { x => - c.withVhostLimit(x.vhost, x.name, x.value) - } - operatorPolicies.foreach { x => c.withOperatorPolicy(x.name, x.pattern, toJavaArguments(x.definition), x.priority, x.applyTo) } diff --git a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala new file mode 100644 index 00000000..82e7582b --- /dev/null +++ b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala @@ -0,0 +1,99 @@ +package com.dimafeng.testcontainers + +import com.dimafeng.testcontainers.RabbitMQContainer.{Exchange, Permission, User, VHost} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.testcontainers.utility.DockerImageName +import sttp.client3.{HttpURLConnectionBackend, UriContext, basicRequest} + +class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { + import RabbitMQSpec._ + + override val container: Container = MultipleContainers( + defaultRabbitContainer, customRabbitContainer + ) + + "Default Rabbit container" should "start" in { + val baseUri = defaultRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/") + + val eitherContainerIsOnline = + request.send(httpClientBackend).body.map(_ => true) + + assertResult(Right(true))(eitherContainerIsOnline) + } + + + "Custom Rabbit container" should "start and load exchanges config" in { + val baseUri = customRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/api/exchanges") + + val eitherExchangeWasLoaded = + request.send(httpClientBackend).body.map(_.contains(testExchange)) + + assertResult(Right(true))(eitherExchangeWasLoaded) + } + + "Custom Rabbit container" should "start and load users config" in { + val baseUri = customRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/api/users") + + val eitheruserWasLoaded = + request.send(httpClientBackend).body.map(_.contains(testUsername)) + + assertResult(Right(true))(eitheruserWasLoaded) + } +} + +object RabbitMQSpec { + val testExchange = "test-exchange" + val testUsername = "test-user" + val testPassword = "test-password" + val httpClientBackend = HttpURLConnectionBackend() + + val defaultRabbitContainer: RabbitMQContainer = RabbitMQContainer() + val customRabbitContainer: RabbitMQContainer = RabbitMQContainer( + dockerImageName = DockerImageName.parse(RabbitMQContainer.defaultDockerImageName), + adminPassword = RabbitMQContainer.defaultAdminPassword, + queues = Seq.empty, + exchanges = Seq( + Exchange( + name = testExchange, + exchangeType = "direct", + arguments = Map.empty, + vhost = Some("test-vhost") + ), + ), + bindings = Seq.empty, + users = Seq( + User( + name = testUsername, + password = testPassword, + tags = Set("administrator") + ) + ), + vhosts = Seq(VHost(name = "test-vhost")), + vhostsLimits = Seq.empty, + operatorPolicies = Seq.empty, + policies = Seq.empty, + parameters = Seq.empty, + permissions = Seq( + Permission( + vhost = "test-vhost", + user = testUsername, + configure = ".*", + write = ".*", + read = ".*" + ) + ) + ) +} \ No newline at end of file diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 907f4fdd..8c2df629 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -31,6 +31,7 @@ object Dependencies { private val restAssuredVersion = "4.0.0" private val awsV1Version = "1.11.479" private val awsV2Version = "2.15.7" + private val sttpVersion = "3.3.13" val allOld = Def.setting( PROVIDED( @@ -234,6 +235,10 @@ object Dependencies { val moduleRabbitmq = Def.setting( COMPILE( "org.testcontainers" % "rabbitmq" % testcontainersVersion + ) ++ TEST( + "org.scalatest" %% "scalatest" % scalaTestVersion + ) ++ PROVIDED( + "com.softwaremill.sttp.client3" %% "core" % sttpVersion ) ) From 06cdf7f1d2073c7bf60e60ef59ebe597c4d222af Mon Sep 17 00:00:00 2001 From: Omar Ahmed Date: Tue, 24 Aug 2021 11:39:04 +0200 Subject: [PATCH 2/3] Remove unnecesary coma causing the build to fail --- .../test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala index 82e7582b..b7e51b6a 100644 --- a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala +++ b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala @@ -71,7 +71,7 @@ object RabbitMQSpec { exchangeType = "direct", arguments = Map.empty, vhost = Some("test-vhost") - ), + ) ), bindings = Seq.empty, users = Seq( From 62bb02650f5744332b2ad3cfe21b7775b813b32c Mon Sep 17 00:00:00 2001 From: Omar Ahmed Date: Mon, 30 Aug 2021 17:26:20 +0200 Subject: [PATCH 3/3] Fix cross build compilation issues --- .../testcontainers/RabbitMQSpec.scala | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala index b7e51b6a..5943d2bf 100644 --- a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala +++ b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala @@ -6,6 +6,8 @@ import org.scalatest.matchers.should.Matchers import org.testcontainers.utility.DockerImageName import sttp.client3.{HttpURLConnectionBackend, UriContext, basicRequest} +import scala.util.Either + class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { import RabbitMQSpec._ @@ -21,7 +23,10 @@ class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { .get(uri"$baseUri/") val eitherContainerIsOnline = - request.send(httpClientBackend).body.map(_ => true) + request.send(httpClientBackend).body match { + case Right(_) => Right(true) + case e@Left(_) => e + } assertResult(Right(true))(eitherContainerIsOnline) } @@ -35,7 +40,10 @@ class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { .get(uri"$baseUri/api/exchanges") val eitherExchangeWasLoaded = - request.send(httpClientBackend).body.map(_.contains(testExchange)) + request.send(httpClientBackend).body match { + case Right(v) => Right(v.contains(testExchange)) + case e@Left(_) => e + } assertResult(Right(true))(eitherExchangeWasLoaded) } @@ -47,10 +55,13 @@ class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { .auth.basic(testUsername, testPassword) .get(uri"$baseUri/api/users") - val eitheruserWasLoaded = - request.send(httpClientBackend).body.map(_.contains(testUsername)) + val eitherUserWasLoaded = + request.send(httpClientBackend).body match { + case Right(v) => Right(v.contains(testUsername)) + case e@Left(_) => e + } - assertResult(Right(true))(eitheruserWasLoaded) + assertResult(Right(true))(eitherUserWasLoaded) } }