diff --git a/build.sbt b/build.sbt index 2dc2cff..3e720c7 100644 --- a/build.sbt +++ b/build.sbt @@ -102,6 +102,7 @@ lazy val root = (project in file(".")) moduleMinIO, moduleWireMock, moduleYugabytedb, + moduleOpensearch, allOld ) .settings(noPublishSettings) @@ -543,3 +544,13 @@ lazy val moduleYugabytedb = (project in file("modules/yugabytedb")) name := "testcontainers-scala-yugabytedb", libraryDependencies ++= Dependencies.moduleYugabytedb.value ) +lazy val moduleOpensearch = (project in file("modules/opensearch")) + .dependsOn( + core % "compile->compile;test->test;provided->provided", + scalatest % "test->test" + ) + .settings(commonSettings) + .settings( + name := "testcontainers-scala-opensearch", + libraryDependencies ++= Dependencies.moduleOpensearch.value + ) diff --git a/docs/src/main/tut/setup.md b/docs/src/main/tut/setup.md index 43f8247..47dc851 100644 --- a/docs/src/main/tut/setup.md +++ b/docs/src/main/tut/setup.md @@ -94,6 +94,7 @@ Here is the full list of the [currently available modules](https://github.com/te * `testcontainers-scala-minio` — module with MinIO container. * `testcontainers-scala-redis` — module with Redis container. * `testcontainers-scala-wiremock` - module with WireMock container. +* `testcontainers-scala-opensearch` - module with Opensearch container. Most of the modules are just proxies to the testcontainers-java modules and behave exactly like java containers. You can find documentation about them in the [testcontainers-java docs pages](https://www.testcontainers.org/). diff --git a/modules/opensearch/src/main/scala/com/dimafeng/testcontainers/OpenSearchContainer.scala b/modules/opensearch/src/main/scala/com/dimafeng/testcontainers/OpenSearchContainer.scala new file mode 100644 index 0000000..5e67036 --- /dev/null +++ b/modules/opensearch/src/main/scala/com/dimafeng/testcontainers/OpenSearchContainer.scala @@ -0,0 +1,53 @@ +package com.dimafeng.testcontainers + +import org.opensearch.testcontainers.{ + OpensearchContainer => JavaOpensearchContainer +} +import org.testcontainers.utility.DockerImageName +class OpensearchContainer( + dockerImageName: DockerImageName, + securityEnabled: Boolean +) extends SingleContainer[JavaOpensearchContainer[_]] { + override val container: JavaOpensearchContainer[_] = { + val c = new JavaOpensearchContainer[JavaOpensearchContainer[_]]( + dockerImageName + ) + if (securityEnabled) + c.withSecurityEnabled() + c + } + + def username: String = container.getUsername() + def password: String = container.getPassword() + def isSecurityEnabled: Boolean = container.isSecurityEnabled() + def httpHost: String = + container.getHttpHostAddress() +} + +object OpensearchContainer { + val defaultImage: String = "opensearchproject/opensearch" + val defaultTag: String = "2.11.0" + val defaultDockerImageName: String = s"$defaultImage:$defaultTag" + + case class Def( + dockerImageName: DockerImageName = + DockerImageName.parse(defaultDockerImageName), + securityEnabled: Boolean = false + ) extends ContainerDef { + override type Container = OpensearchContainer + + override protected def createContainer(): OpensearchContainer = + new OpensearchContainer(dockerImageName, securityEnabled) + } + + def apply( + dockerImageNameOverride: DockerImageName = null, + securityEnabled: Boolean = false + ): OpensearchContainer = + new OpensearchContainer( + Option(dockerImageNameOverride).getOrElse( + DockerImageName.parse(defaultDockerImageName) + ), + securityEnabled + ) +} diff --git a/modules/opensearch/src/test/scala/com/dimafeng/testcontainers/OpenSearchSpec.scala b/modules/opensearch/src/test/scala/com/dimafeng/testcontainers/OpenSearchSpec.scala new file mode 100644 index 0000000..eaabe91 --- /dev/null +++ b/modules/opensearch/src/test/scala/com/dimafeng/testcontainers/OpenSearchSpec.scala @@ -0,0 +1,25 @@ +package com.dimafeng.testcontainers + +import com.dimafeng.testcontainers.scalatest.TestContainersForAll +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client3.{HttpURLConnectionBackend, UriContext, basicRequest} +import sttp.model.StatusCode + +class OpensearchSpec + extends AnyFlatSpec + with TestContainersForAll + with Matchers { + override type Containers = OpensearchContainer + override def startContainers(): Containers = + OpensearchContainer.Def().start() + + "Opensearch container" should "be started" in withContainers { container => + val backend = HttpURLConnectionBackend() + + basicRequest + .get(uri"${container.httpHost}") + .send(backend) + .code should be(StatusCode.Ok) + } +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 3590d19..671cda1 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -43,6 +43,7 @@ object Dependencies { private val quadrantClientVersion = "1.12.0" private val yugabyteJdbcVersion = "42.3.5-yb-6" private val yugabyteJavaDriverVersion = "4.15.0-yb-2-TESTFIX.0" + private val opensearchTestcontainersVersion = "2.1.2" val allOld = Def.setting( PROVIDED( @@ -360,6 +361,16 @@ object Dependencies { ) ++ TEST( "com.yugabyte" % "jdbc-yugabytedb" % yugabyteJdbcVersion, "com.yugabyte" % "java-driver-core" % yugabyteJavaDriverVersion, + "com.yugabyte" % "java-driver-core" % yugabyteJavaDriverVersion + ) + ) + + val moduleOpensearch = Def.setting( + COMPILE( + "org.opensearch" % "opensearch-testcontainers" % opensearchTestcontainersVersion + ) ++ TEST( + "org.scalatest" %% "scalatest" % scalaTestVersion, + "com.softwaremill.sttp.client3" %% "core" % sttpVersion ) ) }