使用插件启动ElasticSearch的测试容器

我正在使用 testcontainers.org docker.elastic.co/elasticsearch/elasticsearch-oss:7.3.2,我想用它来测试我正在更新的插件,但我找不到在测试环境中安装它的方法。


我可以尝试复制里面的文件并安装它


ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:$ELASTIC_VERSION")

String pluginPath = "/usr/share/elasticsearch/$PLUGIN_FILE_NAME"

container.start()

container.copyFileToContainer(MountableFile.forHostPath(new File(PLUGIN_PLUGIN_PATH).toPath()), pluginPath)

ExecResult result = container.execInContainer("bin/elasticsearch-plugin", "install", "file://$pluginPath")

但是容器已经启动并且弹性搜索已经在运行,所以插件不会被加载,所以我需要杀死它并复制它的创建方式,听起来像是很多黑客攻击。有更好的方法来做到这一点吗?


扬帆大鱼
浏览 126回答 3
3回答

皈依舞

例如,以下代码片段对我有用:@ClassRulepublic static GenericContainer elastic = new GenericContainer(new ImageFromDockerfile()    .withDockerfileFromBuilder(        builder -> builder.from("elasticsearch:6.8.4")                          .run("bin/elasticsearch-plugin", "install", "analysis-icu")                          .run("bin/elasticsearch-plugin", "install", "analysis-smartcn")                          .build())).withExposedPorts(9200);

慕码人2483693

对我来说这有效:private static final String DOCKER_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch:6.8.5"private static final ElasticsearchContainer container = new ElasticsearchContainer(DOCKER_IMAGE);static {    container.withCreateContainerCmdModifier((cmd) -> {        cmd.withCmd(            "bash", "-c", "./bin/elasticsearch-plugin install analysis-icu && docker-entrypoint.sh eswrapper");    });    container.withStartupTimeout(Duration.ofSeconds(60));}@BeforeClasspublic static void start() {    container.start();}@AfterClasspublic static void stop() {    container.stop();}请注意,本示例中的 Elasticsearch 版本 6.8.5 较旧,您可能应该使用较新的版本。

红颜莎娜

我能够通过这种方式启动带有插件的 Elasticsearch 测试容器(这是 Kotlin 代码):ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.10.0").apply {&nbsp; &nbsp; withCreateContainerCmdModifier { cmd ->&nbsp; &nbsp; &nbsp; &nbsp; cmd.withCmd(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *arrayOf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "bash",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "-c",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; """/usr/share/elasticsearch/bin/elasticsearch-plugin install <URL> &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;su elasticsearch -s /usr/share/elasticsearch/bin/elasticsearch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; """.trimIndent()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }&nbsp; &nbsp; start()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java