package com.gentics.mesh.test.docker;

import java.nio.charset.Charset;
import java.time.Duration;

import org.apache.commons.io.IOUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.Wait;
import org.testcontainers.images.builder.ImageFromDockerfile;

/**
 * Testcontainer for a non-clustered Elasticsearch instance.
 */
public class ElasticsearchContainer extends GenericContainer<ElasticsearchContainer> {

	public static final String VERSION = "6.3.1";

	public ElasticsearchContainer(boolean withIngestPlugin) {
		super(prepareDockerImage(withIngestPlugin));
	}

	private static ImageFromDockerfile prepareDockerImage(boolean withIngestPlugin) {
		try {
			ImageFromDockerfile dockerImage = new ImageFromDockerfile("elasticsearch", false);
			String dockerFile = IOUtils.toString(ElasticsearchContainer.class.getResourceAsStream("/elasticsearch/Dockerfile.ingest"),
				Charset.defaultCharset());
			dockerFile = dockerFile.replace("%VERSION%", VERSION);
			if (!withIngestPlugin) {
				dockerFile = dockerFile.replace("RUN", "#RUN");
			}
			dockerImage.withFileFromString("Dockerfile", dockerFile);
			return dockerImage;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected void configure() {
		addEnv("discovery.type", "single-node");
		// addEnv("xpack.security.enabled", "false");
		withExposedPorts(9200);
		withStartupTimeout(Duration.ofSeconds(250L));
		waitingFor(Wait.forHttp("/"));
	}

}

@@ -91,16 +91,46 @@
 uration;
+%0Aimport java.util.Collections;
 %0A%0Aimport
@@ -1355,16 +1355,103 @@
 node%22);%0A
+%09%09withTmpFs(Collections.singletonMap(%22/usr/share/elasticsearch/data%22, %22rw,size=64m%22));%0A
 %09%09// add

Use tmpfs for elasticsearch in tests