-
Notifications
You must be signed in to change notification settings - Fork 4
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
5 changed files
with
64 additions
and
29 deletions.
There are no files selected for viewing
24 changes: 0 additions & 24 deletions
24
.../main/kotlin/io/kotest/extensions/testcontainers/elastic/ElasticTestContainerExtension.kt
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...ain/kotlin/io/kotest/extensions/testcontainers/elastic/ElasticsearchContainerExtension.kt
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,51 @@ | ||
package io.kotest.extensions.testcontainers.elastic | ||
|
||
import co.elastic.clients.elasticsearch.ElasticsearchClient | ||
import co.elastic.clients.json.jackson.JacksonJsonpMapper | ||
import co.elastic.clients.transport.rest_client.RestClientTransport | ||
import io.kotest.core.extensions.MountableExtension | ||
import io.kotest.core.listeners.AfterProjectListener | ||
import io.kotest.core.listeners.AfterSpecListener | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.extensions.testcontainers.TestContainerLifecycleMode | ||
import org.apache.http.HttpHost | ||
import org.elasticsearch.client.RestClient | ||
import org.testcontainers.elasticsearch.ElasticsearchContainer | ||
import org.testcontainers.utility.DockerImageName | ||
|
||
class ElasticsearchContainerExtension( | ||
private val container: ElasticsearchContainer, | ||
private val mode: TestContainerLifecycleMode = TestContainerLifecycleMode.Project, | ||
) : AfterProjectListener, | ||
AfterSpecListener, | ||
MountableExtension<ElasticsearchContainer, ElasticsearchContainer>, | ||
AutoCloseable { | ||
|
||
constructor( | ||
image: DockerImageName, | ||
mode: TestContainerLifecycleMode = TestContainerLifecycleMode.Project | ||
) : this(ElasticsearchContainer(image), mode) | ||
|
||
override suspend fun afterProject() { | ||
if (container.isRunning) close() | ||
} | ||
|
||
override suspend fun afterSpec(spec: Spec) { | ||
if (mode == TestContainerLifecycleMode.Spec && container.isRunning) close() | ||
} | ||
|
||
override fun mount(configure: ElasticsearchContainer.() -> Unit): ElasticsearchContainer { | ||
container.start() | ||
return container | ||
} | ||
|
||
override fun close() { | ||
container.stop() | ||
} | ||
} | ||
|
||
fun ElasticsearchContainer.client(): ElasticsearchClient { | ||
val restClient = RestClient.builder(HttpHost.create(httpHostAddress)).build() | ||
val transport = RestClientTransport(restClient, JacksonJsonpMapper()) | ||
return ElasticsearchClient(transport) | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/main/kotlin/io/kotest/extensions/testcontainers/LifecycleMode.kt
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