|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.net.Socket; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.concurrent.ThreadLocalRandom; |
| 9 | + |
| 10 | +import org.apache.http.HttpResponse; |
| 11 | +import org.apache.http.client.methods.HttpGet; |
| 12 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 13 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 17 | +import org.testcontainers.junit.jupiter.Container; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +public class TarantoolCartridgePortMappingTest { |
| 23 | + |
| 24 | + @Container |
| 25 | + private final static TarantoolCartridgeContainer container = new TarantoolCartridgeContainer( |
| 26 | + "Dockerfile", |
| 27 | + "mapping-ports-container", |
| 28 | + "cartridge/instances.yml", |
| 29 | + "cartridge/replicasets.yml") |
| 30 | + .withEnv(TarantoolCartridgeContainer.ENV_TARANTOOL_CLUSTER_COOKIE, "secret") |
| 31 | + .withRouterUsername("admin") |
| 32 | + .withRouterPassword("secret") |
| 33 | + .withStartupTimeout(Duration.ofMinutes(5)) |
| 34 | + .withLogConsumer(new Slf4jLogConsumer( |
| 35 | + LoggerFactory.getLogger(TarantoolCartridgeBootstrapFromYamlTest.class))); |
| 36 | + |
| 37 | + @Test |
| 38 | + void portMappingTest() throws IOException { |
| 39 | + |
| 40 | + final int httpPortToFirstRouter = 8081; |
| 41 | + final int httpPortToSecondRouter = 8082; |
| 42 | + final int portToFirstRouter = 3301; |
| 43 | + final int portToSecondRouter = 3302; |
| 44 | + final String url = "localhost"; |
| 45 | + |
| 46 | + container.addExposedPorts(httpPortToFirstRouter, httpPortToSecondRouter); |
| 47 | + container.start(); |
| 48 | + |
| 49 | + final StringBuilder curlCommandToConnectToRouters = new StringBuilder("http://localhost:") |
| 50 | + .append(container.getMappedPort(httpPortToFirstRouter)); |
| 51 | + |
| 52 | + // send get request to first router via http |
| 53 | + HttpResponse response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); |
| 54 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 55 | + |
| 56 | + curlCommandToConnectToRouters.delete(0, curlCommandToConnectToRouters.length()) |
| 57 | + .append("http://localhost:") |
| 58 | + .append(container.getMappedPort(httpPortToSecondRouter)); |
| 59 | + |
| 60 | + // send get request to second router via http |
| 61 | + response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); |
| 62 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 63 | + |
| 64 | + // connect to first router via socket |
| 65 | + String result = connectToRouterViaSocket(url, container.getMappedPort(portToFirstRouter)); |
| 66 | + assertFalse(result.isEmpty()); |
| 67 | + assertTrue(result.contains("Tarantool")); |
| 68 | + |
| 69 | + // connect to second router via socket |
| 70 | + result = connectToRouterViaSocket(url, container.getMappedPort(portToSecondRouter)); |
| 71 | + assertFalse(result.isEmpty()); |
| 72 | + assertTrue(result.contains("Tarantool")); |
| 73 | + |
| 74 | + // Connect to random port |
| 75 | + result = connectToRouterViaSocket(url, ThreadLocalRandom.current().nextInt(49152, 65535)); |
| 76 | + assertTrue(result.isEmpty()); |
| 77 | + } |
| 78 | + |
| 79 | + private HttpResponse sendCurlToRouterHttpAPI(String url) throws IOException { |
| 80 | + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { |
| 81 | + HttpGet httpGetRequest = new HttpGet(url); |
| 82 | + return httpClient.execute(httpGetRequest); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private String connectToRouterViaSocket(String url, int port) { |
| 87 | + final String returnedString; |
| 88 | + |
| 89 | + try (Socket socket = new Socket(url, port); |
| 90 | + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { |
| 91 | + |
| 92 | + returnedString = in.readLine(); |
| 93 | + } catch (IOException e) { |
| 94 | + return ""; |
| 95 | + } |
| 96 | + return returnedString; |
| 97 | + } |
| 98 | +} |
0 commit comments