Skip to content

Commit 29eabba

Browse files
authored
Merge pull request #187 from nebtrx/fix-issue-186
RabbitMQ Container: Ensure proper configuration preload and add specs
2 parents c4d5fa0 + 62bb026 commit 29eabba

File tree

4 files changed

+125
-10
lines changed

4 files changed

+125
-10
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ lazy val modulePulsar = (project in file("modules/pulsar"))
406406
)
407407

408408
lazy val moduleRabbitmq = (project in file("modules/rabbitmq"))
409-
.dependsOn(core % "compile->compile;test->test;provided->provided")
409+
.dependsOn(core % "compile->compile;test->test;provided->provided", scalatest % "test->test")
410410
.settings(commonSettings: _*)
411411
.settings(
412412
name := "testcontainers-scala-rabbitmq",

modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ case class RabbitMQContainer(
3030

3131
c.withAdminPassword(adminPassword)
3232

33+
vhosts.foreach {
34+
case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing)
35+
case RabbitMQContainer.VHost(name, None) => c.withVhost(name)
36+
}
37+
38+
vhostsLimits.foreach { x =>
39+
c.withVhostLimit(x.vhost, x.name, x.value)
40+
}
41+
3342
queues.foreach { x =>
3443
c.withQueue(x.name, x.autoDelete, x.durable, toJavaArguments(x.arguments))
3544
}
@@ -51,15 +60,6 @@ case class RabbitMQContainer(
5160
if (x.tags.isEmpty) c.withUser(x.name, x.password) else c.withUser(x.name, x.password, x.tags.asJava)
5261
}
5362

54-
vhosts.foreach {
55-
case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing)
56-
case RabbitMQContainer.VHost(name, None) => c.withVhost(name)
57-
}
58-
59-
vhostsLimits.foreach { x =>
60-
c.withVhostLimit(x.vhost, x.name, x.value)
61-
}
62-
6363
operatorPolicies.foreach { x =>
6464
c.withOperatorPolicy(x.name, x.pattern, toJavaArguments(x.definition), x.priority, x.applyTo)
6565
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.dimafeng.testcontainers
2+
3+
import com.dimafeng.testcontainers.RabbitMQContainer.{Exchange, Permission, User, VHost}
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
import org.testcontainers.utility.DockerImageName
7+
import sttp.client3.{HttpURLConnectionBackend, UriContext, basicRequest}
8+
9+
import scala.util.Either
10+
11+
class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers {
12+
import RabbitMQSpec._
13+
14+
override val container: Container = MultipleContainers(
15+
defaultRabbitContainer, customRabbitContainer
16+
)
17+
18+
"Default Rabbit container" should "start" in {
19+
val baseUri = defaultRabbitContainer.httpUrl
20+
val request =
21+
basicRequest
22+
.auth.basic(testUsername, testPassword)
23+
.get(uri"$baseUri/")
24+
25+
val eitherContainerIsOnline =
26+
request.send(httpClientBackend).body match {
27+
case Right(_) => Right(true)
28+
case e@Left(_) => e
29+
}
30+
31+
assertResult(Right(true))(eitherContainerIsOnline)
32+
}
33+
34+
35+
"Custom Rabbit container" should "start and load exchanges config" in {
36+
val baseUri = customRabbitContainer.httpUrl
37+
val request =
38+
basicRequest
39+
.auth.basic(testUsername, testPassword)
40+
.get(uri"$baseUri/api/exchanges")
41+
42+
val eitherExchangeWasLoaded =
43+
request.send(httpClientBackend).body match {
44+
case Right(v) => Right(v.contains(testExchange))
45+
case e@Left(_) => e
46+
}
47+
48+
assertResult(Right(true))(eitherExchangeWasLoaded)
49+
}
50+
51+
"Custom Rabbit container" should "start and load users config" in {
52+
val baseUri = customRabbitContainer.httpUrl
53+
val request =
54+
basicRequest
55+
.auth.basic(testUsername, testPassword)
56+
.get(uri"$baseUri/api/users")
57+
58+
val eitherUserWasLoaded =
59+
request.send(httpClientBackend).body match {
60+
case Right(v) => Right(v.contains(testUsername))
61+
case e@Left(_) => e
62+
}
63+
64+
assertResult(Right(true))(eitherUserWasLoaded)
65+
}
66+
}
67+
68+
object RabbitMQSpec {
69+
val testExchange = "test-exchange"
70+
val testUsername = "test-user"
71+
val testPassword = "test-password"
72+
val httpClientBackend = HttpURLConnectionBackend()
73+
74+
val defaultRabbitContainer: RabbitMQContainer = RabbitMQContainer()
75+
val customRabbitContainer: RabbitMQContainer = RabbitMQContainer(
76+
dockerImageName = DockerImageName.parse(RabbitMQContainer.defaultDockerImageName),
77+
adminPassword = RabbitMQContainer.defaultAdminPassword,
78+
queues = Seq.empty,
79+
exchanges = Seq(
80+
Exchange(
81+
name = testExchange,
82+
exchangeType = "direct",
83+
arguments = Map.empty,
84+
vhost = Some("test-vhost")
85+
)
86+
),
87+
bindings = Seq.empty,
88+
users = Seq(
89+
User(
90+
name = testUsername,
91+
password = testPassword,
92+
tags = Set("administrator")
93+
)
94+
),
95+
vhosts = Seq(VHost(name = "test-vhost")),
96+
vhostsLimits = Seq.empty,
97+
operatorPolicies = Seq.empty,
98+
policies = Seq.empty,
99+
parameters = Seq.empty,
100+
permissions = Seq(
101+
Permission(
102+
vhost = "test-vhost",
103+
user = testUsername,
104+
configure = ".*",
105+
write = ".*",
106+
read = ".*"
107+
)
108+
)
109+
)
110+
}

project/Dependencies.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ object Dependencies {
3131
private val restAssuredVersion = "4.0.0"
3232
private val awsV1Version = "1.11.479"
3333
private val awsV2Version = "2.15.7"
34+
private val sttpVersion = "3.3.13"
3435

3536
val allOld = Def.setting(
3637
PROVIDED(
@@ -234,6 +235,10 @@ object Dependencies {
234235
val moduleRabbitmq = Def.setting(
235236
COMPILE(
236237
"org.testcontainers" % "rabbitmq" % testcontainersVersion
238+
) ++ TEST(
239+
"org.scalatest" %% "scalatest" % scalaTestVersion
240+
) ++ PROVIDED(
241+
"com.softwaremill.sttp.client3" %% "core" % sttpVersion
237242
)
238243
)
239244

0 commit comments

Comments
 (0)