Skip to content

Commit

Permalink
feat: add support for elasticmq
Browse files Browse the repository at this point in the history
  • Loading branch information
j5ik2o committed Jun 25, 2021
1 parent 0ab7b22 commit 8524525
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 1 deletion.
11 changes: 11 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ val `docker-controller-scala-redis` = (project in file("docker-controller-scala-
)
).dependsOn(`docker-controller-scala-core`, `docker-controller-scala-scalatest` % Test)

val `docker-controller-scala-elasticmq` = (project in file("docker-controller-scala-elasticmq"))
.settings(baseSettings)
.settings(
name := "docker-controller-scala-elasticmq",
libraryDependencies ++= Seq(
scalatest.scalatest % Test,
logback.classic % Test,
amazonAws.sqs % Test
)
).dependsOn(`docker-controller-scala-core`, `docker-controller-scala-scalatest` % Test)

val `docker-controller-scala-elasticsearch` = (project in file("docker-controller-scala-elasticsearch"))
.settings(baseSettings)
.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.j5ik2o.dockerController.elasticmq

import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.api.command.CreateContainerCmd
import com.github.dockerjava.api.model.HostConfig.newHostConfig
import com.github.dockerjava.api.model.{ ExposedPort, Ports }
import com.github.j5ik2o.dockerController.DockerControllerImpl
import com.github.j5ik2o.dockerController.elasticmq.ElasticMQController.{
DefaultContainerPorts,
DefaultImageName,
DefaultImageTag
}

import scala.concurrent.duration.{ DurationInt, FiniteDuration }
import scala.jdk.CollectionConverters._

object ElasticMQController {
final val DefaultImageName: String = "softwaremill/elasticmq"
final val DefaultImageTag: Option[String] = Some("1.1.1")
final val DefaultContainerPorts: Seq[Int] = Seq(9324, 9325)

def apply(
dockerClient: DockerClient,
outputFrameInterval: FiniteDuration = 500.millis,
imageName: String = DefaultImageName,
imageTag: Option[String] = DefaultImageTag,
envVars: Map[String, String] = Map.empty
)(dockerHost: String, hostPorts: Seq[Int]): ElasticMQController =
new ElasticMQController(dockerClient, outputFrameInterval, imageName, imageTag, envVars)(
dockerHost,
hostPorts
)
}

class ElasticMQController(
dockerClient: DockerClient,
outputFrameInterval: FiniteDuration = 500.millis,
imageName: String = DefaultImageName,
imageTag: Option[String] = DefaultImageTag,
envVars: Map[String, String] = Map.empty
)(dockerHost: String, hostPorts: Seq[Int])
extends DockerControllerImpl(dockerClient, outputFrameInterval)(imageName, imageTag) {

private val environmentVariables = Map(
"JAVA_OPTS" -> "-Dconfig.override_with_env_vars=true",
"CONFIG_FORCE_node__address_host" -> "*",
"CONFIG_FORCE_rest__sqs_bind__hostname" -> "0.0.0.0",
"CONFIG_FORCE_generate__node__address" -> "false"
) ++
envVars

override protected def newCreateContainerCmd(): CreateContainerCmd = {
val containerPorts = DefaultContainerPorts.map(ExposedPort.tcp)
val ports = new Ports()
containerPorts.zip(hostPorts).foreach { case (containerPort, hostPort) =>
ports.bind(containerPort, Ports.Binding.bindPort(hostPort))
}
super
.newCreateContainerCmd()
.withEnv(environmentVariables.map { case (k, v) => s"$k=$v" }.toArray: _*)
.withExposedPorts(containerPorts.toList.asJava)
.withHostConfig(newHostConfig().withPortBindings(ports))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>

<logger name="org.apache" level="INFO">
</logger>

<root level="DEBUG">
<appender-ref ref="ASYNCSTDOUT" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.github.j5ik2o.dockerController.elasticmq

import com.amazonaws.auth.{ AWSCredentialsProviderChain, AWSStaticCredentialsProvider, BasicAWSCredentials }
import com.amazonaws.client.builder.AwsClientBuilder
import com.amazonaws.regions.Regions
import com.amazonaws.services.sqs.AmazonSQSClientBuilder
import com.amazonaws.services.sqs.model.{ CreateQueueRequest, SendMessageRequest, SetQueueAttributesRequest }
import com.github.j5ik2o.dockerController.{
DockerController,
DockerControllerSpecSupport,
RandomPortUtil,
WaitPredicates
}
import org.scalatest.freespec.AnyFreeSpec

import java.util.UUID
import scala.concurrent.duration._

class ElasticMQControllerSpec extends AnyFreeSpec with DockerControllerSpecSupport {

val testTimeFactor: Int = sys.env.getOrElse("TEST_TIME_FACTOR", "1").toInt
logger.debug(s"testTimeFactor = $testTimeFactor")

val hostPorts: Seq[Int] = Seq(RandomPortUtil.temporaryServerPort(), RandomPortUtil.temporaryServerPort())
val controller: ElasticMQController = ElasticMQController(dockerClient)(dockerHost, hostPorts)

override protected val dockerControllers: Vector[DockerController] = Vector(controller)

override protected val waitPredicatesSettings: Map[DockerController, WaitPredicateSetting] =
Map(
controller -> WaitPredicateSetting(
Duration.Inf,
WaitPredicates.forListeningHostTcpPort(
dockerHost,
hostPorts.head,
(1 * testTimeFactor).seconds,
Some((5 * testTimeFactor).seconds)
)
)
)

"ElasticMQController" - {
"run" in {
val client = AmazonSQSClientBuilder
.standard()
.withCredentials(
new AWSCredentialsProviderChain(new AWSStaticCredentialsProvider(new BasicAWSCredentials("x", "x")))
)
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
s"http://${dockerHost}:${hostPorts.head}",
Regions.DEFAULT_REGION.getName
)
).build()

val queueName = "test"
val request = new CreateQueueRequest(queueName)
.addAttributesEntry("VisibilityTimeout", "5")
.addAttributesEntry("DelaySeconds", "1")

val createQueueResult = client.createQueue(request)
assert(createQueueResult.getSdkHttpMetadata.getHttpStatusCode == 200)
val queueUrlResult = client.getQueueUrl(queueName)
assert(queueUrlResult.getSdkHttpMetadata.getHttpStatusCode == 200)
val queueUrl = queueUrlResult.getQueueUrl

val setAttrsRequest = new SetQueueAttributesRequest()
.withQueueUrl(queueUrl)
.addAttributesEntry("ReceiveMessageWaitTimeSeconds", "5")
val queueAttributesResult = client.setQueueAttributes(setAttrsRequest)
assert(queueAttributesResult.getSdkHttpMetadata.getHttpStatusCode == 200)

val text = UUID.randomUUID().toString
val sendMessageRequest = new SendMessageRequest(queueUrl, text)
val sendMessageResult = client.sendMessage(sendMessageRequest)
assert(sendMessageResult.getSdkHttpMetadata.getHttpStatusCode == 200)

val receiveMessageResult = client.receiveMessage(queueUrl)
assert(receiveMessageResult.getSdkHttpMetadata.getHttpStatusCode == 200)
assert(receiveMessageResult.getMessages.size() > 0)
val message = receiveMessageResult.getMessages.get(0)
assert(message.getBody == text)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RedisController(

override protected def newCreateContainerCmd(): CreateContainerCmd = {
val containerPort = ExposedPort.tcp(DefaultContainerPort)
val portBinding = new Ports()
val portBinding = new Ports
portBinding.bind(containerPort, Ports.Binding.bindPort(hostPort))
super
.newCreateContainerCmd()
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object Dependencies {
object amazonAws {
val dynamodb = "com.amazonaws" % "aws-java-sdk-dynamodb" % "1.12.12"
val s3 = "com.amazonaws" % "aws-java-sdk-s3" % "1.12.12"
val sqs = "com.amazonaws" % "aws-java-sdk-sqs" % "1.12.12"
}

object apache {
Expand Down

0 comments on commit 8524525

Please sign in to comment.