Reputation: 534
I'm trying to use a JUnit test container for OpenSearch, in Kotlin, with the Spring Boot framework. (Enough qualifiers?) I've been able to get it mostly working, roughly like this:
class TestConfiguration : ApplicationContextInitializer<ConfigurableApplicationContext> {
val container = OpensearchContainer(
DockerImageName.parse("opensearchproject/opensearch:2.11.1")
)
override fun initialize(applicationContext: ConfigurableApplicationContext) {
container.start()
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext,
"opensearch.username=${container.getUsername()}",
"opensearch.password=${container.getPassword()}",
"opensearch.host=${container.getHost()}",
"opensearch.scheme=http",
"opensearch.port=${container.getFirstMappedPort()}",
)
}
}
(That is, we start the container, then pull the user/pass/host/port out of it and stick that into the properties, so that the OpenSearch client will then pick up those properties and connect to the test container instance.)
The above does seem to work as intended. However, I do not want to use the vanilla upstream OpenSearch image. I need to install an OpenSearch plugin, which means I have a custom Dockerfile
like so:
FROM opensearchproject/opensearch:2.11.1
RUN bin/opensearch-plugin install analysis-phonetic
Which when I reference it from docker-compose.yml
works perfectly fine. However, I cannot for the life of me figure out how to get testcontainers to use a custom Dockerfile
. It seems to be in part an issue of the OpenSearchContainer
class not actually allowing anything but a DockerImageName
instance as an argument, even though its parent GenericContainer
supports more. But not using the OpenSearchContainer
means I don't have access to the user or password; although looking at the class, they're just hard-coded constants.
There is ImageFromDockerfile
(cf: https://java.testcontainers.org/features/creating_images/), though again that seems to only work with the GenericContainer
, not with the OpenSearchContainer
.
I've tried to use the DockerComposeContainer
support (cf: https://codeal.medium.com/how-to-run-docker-compose-with-testcontainers-7d1ba73afeeb), but I keep getting errors I cannot decypher trying to reference the docker-compose.yml
file. If I try to wrap the file name in a File
instance, it simply crashes. If I don't, then it runs but seems to not find the file, so does not load anything.
Has anyone successfully done this? Any idea how I can?
Merci bien.
Upvotes: 1
Views: 379
Reputation: 1
Have you tried this with ImageFromDockerFile?
private fun createOpenSearchContainer(): GenericContainer<*> {
val image: ImageFromDockerfile = ImageFromDockerfile()
.withDockerfile(Paths.get("develop-scripts/Dockerfile"))
return GenericContainer(image)
.withEnv(mapOf("discovery.type" to "single-node", "DISABLE_SECURITY_PLUGIN" to "true", "OPENSEARCH_JAVA_OPTS" to "-Xms512m -Xmx512m"))
.withExposedPorts(9200, 9600)
.waitingFor(
HttpWaitStrategy()
.forPort(9200)
.forStatusCodeMatching { response ->
response === HttpURLConnection.HTTP_OK ||
response === HttpURLConnection.HTTP_UNAUTHORIZED
}
.withStartupTimeout(Duration.ofMinutes(2)),
)
}
Heres an example of OpenSearchContainer being initialized instead of a GenericContainer https://github.com/barkstone2/DailyQuest/blob/8a69f571c4bc0e1b18c11f0ec4a3472422b6e6bf/opensearch-test/src/main/kotlin/dailyquest/common/CustomOpenSearchContainer.kt#L4
Upvotes: 0