Skip to content

Commit

Permalink
Merge pull request #187 from nebtrx/fix-issue-186
Browse files Browse the repository at this point in the history
RabbitMQ Container: Ensure proper configuration preload and add specs
  • Loading branch information
dimafeng authored Aug 30, 2021
2 parents c4d5fa0 + 62bb026 commit 29eabba
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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}

import scala.util.Either

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 match {
case Right(_) => Right(true)
case e@Left(_) => e
}

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 match {
case Right(v) => Right(v.contains(testExchange))
case e@Left(_) => e
}

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 match {
case Right(v) => Right(v.contains(testUsername))
case e@Left(_) => e
}

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 = ".*"
)
)
)
}
5 changes: 5 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
)

Expand Down

0 comments on commit 29eabba

Please sign in to comment.