From 12d7ceb912dbfe36d4d25942b31563e3d3c21bb2 Mon Sep 17 00:00:00 2001 From: Roman Greshny Date: Fri, 6 Dec 2024 11:57:32 +0100 Subject: [PATCH] OpenSearch integration --- build.sbt | 12 +++++ docs/src/main/tut/setup.md | 1 + .../testcontainers/OpenSearchContainer.scala | 54 +++++++++++++++++++ .../testcontainers/OpenSearchSpec.scala | 25 +++++++++ project/Dependencies.scala | 12 ++++- 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 modules/opensearch/src/main/scala/com/dimafeng/testcontainers/OpenSearchContainer.scala create mode 100644 modules/opensearch/src/test/scala/com/dimafeng/testcontainers/OpenSearchSpec.scala diff --git a/build.sbt b/build.sbt index 2dc2cff1..92d0c5e5 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,14 @@ 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 43f82479..47dc851c 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 00000000..fa5658b5 --- /dev/null +++ b/modules/opensearch/src/main/scala/com/dimafeng/testcontainers/OpenSearchContainer.scala @@ -0,0 +1,54 @@ +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( + 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 00000000..eaabe91d --- /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 3590d198..0fb3407d 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 = "1.0.0" val allOld = Def.setting( PROVIDED( @@ -359,7 +360,16 @@ object Dependencies { "org.testcontainers" % "yugabytedb" % testcontainersVersion ) ++ 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 ) ) }