-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...cmq/src/main/scala/com/github/j5ik2o/dockerController/elasticmq/ElasticMQController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
docker-controller-scala-elasticmq/src/test/resources/logback-test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
85 changes: 85 additions & 0 deletions
85
...src/test/scala/com/github/j5ik2o/dockerController/elasticmq/ElasticMQControllerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters