From 9cf3721681dd2f94c89f2f1d4ae86d058bd9f846 Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Wed, 10 Jan 2024 19:13:26 +0300 Subject: [PATCH 1/6] Add example for port mapping Closes #19 --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/README.md b/README.md index 10e6594..b67d0b9 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,76 @@ cartridge build at the image build stage. An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter: https://github.com/tarantool/testcontainers-java-tarantool/blob/355d1e985bd10beca83bc7ca77f919a288709419/src/test/java/org/testcontainers/containers/TarantoolCartridgeBootstrapFromLuaWithFixedPortsTest.java#L57-L82 +##### Mapping ports + +Often there is a need to connect to a container via a certain port. To achieve this goal, you need to know the mapped +port of the port that was specified from the Java code. To get the mapped port use the `getMappedPort(...)` method +of testcontainers API. + +As an example, consider the following Java code: + +```java +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.ExternalResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.TarantoolCartridgeContainer; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class AppTest { + + private final Logger logger = LoggerFactory.getLogger(AppTest.class); + + private static final GenericContainer container = + new TarantoolCartridgeContainer("cartridge/instances.yml", "cartridge/topology.lua") + .withDirectoryBinding("cartridge") + .withRouterHost("localhost") + .withRouterPort(3301) + // Open http port in container + .withAPIPort(8081) + .withRouterUsername("admin") + .withRouterPassword("tarantool-cartridge-starter-cluster-cookie") + .withReuse(true); + + @ClassRule + public static ExternalResource resource = new ExternalResource() { + @Override + public void before() { + container.start(); + } + @Override + public void after() { + container.stop(); + } + }; + + + @Test + public void shouldAnswerWithTrue() throws IOException { + // Get mapped port + final int mappedHttpPort = container.getMappedPort(8081); + // Get metrics response + final String metricsResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/metrics"); + logger.info("Metric response: {}", metricsResponse); + final String helloResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/hello"); + logger.info("Hello response: {}", helloResponse); + } + + private String sendRequestAndGetResponse(final String urlSource) throws IOException { + final URL url = new URL(urlSource); + // Connect to the URL with mapped port + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + return connection.getResponseMessage(); + } +} +``` + + ## License See [LICENSE](LICENSE). From ae580cb59d4621d7c11e5f80c394ec141d4c92b3 Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Mon, 15 Jan 2024 15:04:08 +0300 Subject: [PATCH 2/6] Fix after review Closes #19 --- README.md | 69 +------------ pom.xml | 7 +- .../TarantoolCartridgePortMappingTest.java | 98 +++++++++++++++++++ 3 files changed, 107 insertions(+), 67 deletions(-) create mode 100644 src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java diff --git a/README.md b/README.md index b67d0b9..c218e8e 100644 --- a/README.md +++ b/README.md @@ -234,72 +234,9 @@ An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter: https://githu ##### Mapping ports -Often there is a need to connect to a container via a certain port. To achieve this goal, you need to know the mapped -port of the port that was specified from the Java code. To get the mapped port use the `getMappedPort(...)` method -of testcontainers API. - -As an example, consider the following Java code: - -```java -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.ExternalResource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.TarantoolCartridgeContainer; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; - -public class AppTest { - - private final Logger logger = LoggerFactory.getLogger(AppTest.class); - - private static final GenericContainer container = - new TarantoolCartridgeContainer("cartridge/instances.yml", "cartridge/topology.lua") - .withDirectoryBinding("cartridge") - .withRouterHost("localhost") - .withRouterPort(3301) - // Open http port in container - .withAPIPort(8081) - .withRouterUsername("admin") - .withRouterPassword("tarantool-cartridge-starter-cluster-cookie") - .withReuse(true); - - @ClassRule - public static ExternalResource resource = new ExternalResource() { - @Override - public void before() { - container.start(); - } - @Override - public void after() { - container.stop(); - } - }; - - - @Test - public void shouldAnswerWithTrue() throws IOException { - // Get mapped port - final int mappedHttpPort = container.getMappedPort(8081); - // Get metrics response - final String metricsResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/metrics"); - logger.info("Metric response: {}", metricsResponse); - final String helloResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/hello"); - logger.info("Hello response: {}", helloResponse); - } - - private String sendRequestAndGetResponse(final String urlSource) throws IOException { - final URL url = new URL(urlSource); - // Connect to the URL with mapped port - final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - return connection.getResponseMessage(); - } -} -``` +Often there is a need to connect to a container through a specific port. To achieve this goal it is necessary +know the mapped port specified in the Java code. To get the mapped port, use the getMappedPort(...)` method of +testcontainers API. See examples: ## License diff --git a/pom.xml b/pom.xml index b1c100d..05d5bd8 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,12 @@ - + + org.apache.httpcomponents + httpclient + 4.5.1 + + ch.qos.logback logback-classic 1.3.4 diff --git a/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java new file mode 100644 index 0000000..d145f59 --- /dev/null +++ b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java @@ -0,0 +1,98 @@ +package org.testcontainers.containers; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Socket; +import java.time.Duration; +import java.util.concurrent.ThreadLocalRandom; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.junit.jupiter.Container; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TarantoolCartridgePortMappingTest { + + @Container + private final static TarantoolCartridgeContainer container = new TarantoolCartridgeContainer( + "Dockerfile", + "mapping-ports-container", + "cartridge/instances.yml", + "cartridge/replicasets.yml") + .withEnv(TarantoolCartridgeContainer.ENV_TARANTOOL_CLUSTER_COOKIE, "secret") + .withRouterUsername("admin") + .withRouterPassword("secret") + .withStartupTimeout(Duration.ofMinutes(5)) + .withLogConsumer(new Slf4jLogConsumer( + LoggerFactory.getLogger(TarantoolCartridgeBootstrapFromYamlTest.class))); + + @Test + void portMappingTest() throws IOException { + + final int httpPortToFirstRouter = 8081; + final int httpPortToSecondRouter = 8082; + final int portToFirstRouter = 3301; + final int portToSecondRouter = 3302; + final String url = "localhost"; + + container.addExposedPorts(httpPortToFirstRouter, httpPortToSecondRouter); + container.start(); + + final StringBuilder curlCommandToConnectToRouters = new StringBuilder("http://localhost:") + .append(container.getMappedPort(httpPortToFirstRouter)); + + // send get request to first router via http + HttpResponse response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); + assertEquals(200, response.getStatusLine().getStatusCode()); + + curlCommandToConnectToRouters.delete(0, curlCommandToConnectToRouters.length()) + .append("http://localhost:") + .append(container.getMappedPort(httpPortToSecondRouter)); + + // send get request to second router via http + response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); + assertEquals(200, response.getStatusLine().getStatusCode()); + + // connect to first router via socket + String result = connectToRouterViaSocket(url, container.getMappedPort(portToFirstRouter)); + assertFalse(result.isEmpty()); + assertTrue(result.contains("Tarantool")); + + // connect to second router via socket + result = connectToRouterViaSocket(url, container.getMappedPort(portToSecondRouter)); + assertFalse(result.isEmpty()); + assertTrue(result.contains("Tarantool")); + + // Connect to random port + result = connectToRouterViaSocket(url, ThreadLocalRandom.current().nextInt(49152, 65535)); + assertTrue(result.isEmpty()); + } + + private HttpResponse sendCurlToRouterHttpAPI(String url) throws IOException { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet httpGetRequest = new HttpGet(url); + return httpClient.execute(httpGetRequest); + } + } + + private String connectToRouterViaSocket(String url, int port) { + final String returnedString; + + try (Socket socket = new Socket(url, port); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { + + returnedString = in.readLine(); + } catch (IOException e) { + return ""; + } + return returnedString; + } +} From 64a32c051b07413d3a8964e74c28f8b3c63b16c3 Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Mon, 15 Jan 2024 15:19:11 +0300 Subject: [PATCH 3/6] Add link to test Closes #19 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c218e8e..3bf5d74 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter: https://githu Often there is a need to connect to a container through a specific port. To achieve this goal it is necessary know the mapped port specified in the Java code. To get the mapped port, use the getMappedPort(...)` method of -testcontainers API. See examples: +testcontainers API. See examples: https://github.com/tarantool/testcontainers-java-tarantool/blob/ae580cb59d4621d7c11e5f80c394ec141d4c92b3/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java ## License From 674173b9bd1838990d6b84697ec70eb041faedbd Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Mon, 15 Jan 2024 16:43:09 +0300 Subject: [PATCH 4/6] Fix after review Closes #19 --- pom.xml | 63 ++++++++++--------- .../TarantoolCartridgePortMappingTest.java | 42 +++++++------ 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index 05d5bd8..6306d0a 100644 --- a/pom.xml +++ b/pom.xml @@ -76,45 +76,46 @@ - - org.testcontainers - testcontainers - ${testcontainers.version} - - - org.yaml - snakeyaml - 2.0 - - - org.slf4j - slf4j-api - + + org.testcontainers + testcontainers + ${testcontainers.version} + + + org.yaml + snakeyaml + 2.0 + + + org.slf4j + slf4j-api + org.apache.httpcomponents httpclient 4.5.1 + test - ch.qos.logback - logback-classic - 1.3.4 - test - - - org.junit.jupiter - junit-jupiter - 5.8.1 - test - - - org.testcontainers - junit-jupiter - ${testcontainers.version} - test - + ch.qos.logback + logback-classic + 1.3.4 + test + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + + org.testcontainers + junit-jupiter + ${testcontainers.version} + test + diff --git a/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java index d145f59..f6a2e41 100644 --- a/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java +++ b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; +import java.net.URI; +import java.net.URISyntaxException; import java.time.Duration; import java.util.concurrent.ThreadLocalRandom; @@ -35,58 +37,62 @@ public class TarantoolCartridgePortMappingTest { LoggerFactory.getLogger(TarantoolCartridgeBootstrapFromYamlTest.class))); @Test - void portMappingTest() throws IOException { + void portMappingTest() throws IOException, URISyntaxException { final int httpPortToFirstRouter = 8081; final int httpPortToSecondRouter = 8082; final int portToFirstRouter = 3301; final int portToSecondRouter = 3302; - final String url = "localhost"; + final String host = "localhost"; - container.addExposedPorts(httpPortToFirstRouter, httpPortToSecondRouter); + container.addExposedPorts(httpPortToFirstRouter, httpPortToSecondRouter, portToFirstRouter, portToSecondRouter); container.start(); - final StringBuilder curlCommandToConnectToRouters = new StringBuilder("http://localhost:") - .append(container.getMappedPort(httpPortToFirstRouter)); + URI uri = new URI("http", null, host, container.getMappedPort(httpPortToFirstRouter), null, + null, null); // send get request to first router via http - HttpResponse response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); + HttpResponse response = sendCurlToRouterHttpAPI(uri); assertEquals(200, response.getStatusLine().getStatusCode()); - curlCommandToConnectToRouters.delete(0, curlCommandToConnectToRouters.length()) - .append("http://localhost:") - .append(container.getMappedPort(httpPortToSecondRouter)); - + uri = new URI("http", null, host, container.getMappedPort(httpPortToSecondRouter), null, + null, null); // send get request to second router via http - response = sendCurlToRouterHttpAPI(curlCommandToConnectToRouters.toString()); + response = sendCurlToRouterHttpAPI(uri); assertEquals(200, response.getStatusLine().getStatusCode()); // connect to first router via socket - String result = connectToRouterViaSocket(url, container.getMappedPort(portToFirstRouter)); + uri = new URI(null, null, host, container.getMappedPort(portToFirstRouter), null, + null, null); + String result = connectToRouterViaSocket(uri); assertFalse(result.isEmpty()); assertTrue(result.contains("Tarantool")); // connect to second router via socket - result = connectToRouterViaSocket(url, container.getMappedPort(portToSecondRouter)); + uri = new URI(null, null, host, container.getMappedPort(portToSecondRouter), null, + null, null); + result = connectToRouterViaSocket(uri); assertFalse(result.isEmpty()); assertTrue(result.contains("Tarantool")); // Connect to random port - result = connectToRouterViaSocket(url, ThreadLocalRandom.current().nextInt(49152, 65535)); + uri = new URI(null, null, host, ThreadLocalRandom.current().nextInt(49152, 65535), + null, null, null); + result = connectToRouterViaSocket(uri); assertTrue(result.isEmpty()); } - private HttpResponse sendCurlToRouterHttpAPI(String url) throws IOException { + private HttpResponse sendCurlToRouterHttpAPI(URI uri) throws IOException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpGet httpGetRequest = new HttpGet(url); + HttpGet httpGetRequest = new HttpGet(uri); return httpClient.execute(httpGetRequest); } } - private String connectToRouterViaSocket(String url, int port) { + private String connectToRouterViaSocket(URI uri) { final String returnedString; - try (Socket socket = new Socket(url, port); + try (Socket socket = new Socket(uri.getHost(), uri.getPort()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { returnedString = in.readLine(); From f5dd2d30fda523c28344509e40261716d91e034d Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Mon, 15 Jan 2024 18:48:08 +0300 Subject: [PATCH 5/6] Fix after review Closes #19 --- .../TarantoolCartridgePortMappingTest.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java index f6a2e41..b2eb8ac 100644 --- a/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java +++ b/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java @@ -44,41 +44,42 @@ void portMappingTest() throws IOException, URISyntaxException { final int portToFirstRouter = 3301; final int portToSecondRouter = 3302; final String host = "localhost"; + final String schema = "http"; container.addExposedPorts(httpPortToFirstRouter, httpPortToSecondRouter, portToFirstRouter, portToSecondRouter); container.start(); - URI uri = new URI("http", null, host, container.getMappedPort(httpPortToFirstRouter), null, - null, null); + URI firstRouterConnectionURI = new URI(schema, null, host, + container.getMappedPort(httpPortToFirstRouter), null, null, null); // send get request to first router via http - HttpResponse response = sendCurlToRouterHttpAPI(uri); + HttpResponse response = sendCurlToRouterHttpAPI(firstRouterConnectionURI); assertEquals(200, response.getStatusLine().getStatusCode()); - uri = new URI("http", null, host, container.getMappedPort(httpPortToSecondRouter), null, - null, null); + URI secondRouterConnectionURI = new URI(schema, null, host, + container.getMappedPort(httpPortToSecondRouter), null, null, null); // send get request to second router via http - response = sendCurlToRouterHttpAPI(uri); + response = sendCurlToRouterHttpAPI(secondRouterConnectionURI); assertEquals(200, response.getStatusLine().getStatusCode()); // connect to first router via socket - uri = new URI(null, null, host, container.getMappedPort(portToFirstRouter), null, - null, null); - String result = connectToRouterViaSocket(uri); + URI firstRouterConnectionURIViaSocket = new URI(null, null, host, + container.getMappedPort(portToFirstRouter), null, null, null); + String result = connectToRouterViaSocket(firstRouterConnectionURIViaSocket); assertFalse(result.isEmpty()); assertTrue(result.contains("Tarantool")); // connect to second router via socket - uri = new URI(null, null, host, container.getMappedPort(portToSecondRouter), null, - null, null); - result = connectToRouterViaSocket(uri); + URI secondRouterConnectionURIViaSocket = new URI(null, null, host, + container.getMappedPort(portToSecondRouter), null, null, null); + result = connectToRouterViaSocket(secondRouterConnectionURIViaSocket); assertFalse(result.isEmpty()); assertTrue(result.contains("Tarantool")); // Connect to random port - uri = new URI(null, null, host, ThreadLocalRandom.current().nextInt(49152, 65535), - null, null, null); - result = connectToRouterViaSocket(uri); + URI randomPortConnectionURIViaSocket = new URI(null, null, host, + ThreadLocalRandom.current().nextInt(49152, 65535), null, null, null); + result = connectToRouterViaSocket(randomPortConnectionURIViaSocket); assertTrue(result.isEmpty()); } From 37bbb720fccad40dccbe2a2cbab768af77a53f30 Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Mon, 15 Jan 2024 19:18:23 +0300 Subject: [PATCH 6/6] Fix after review Closes #19 --- README.md | 4 +-- pom.xml | 78 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 3bf5d74..270ab6b 100644 --- a/README.md +++ b/README.md @@ -235,8 +235,8 @@ An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter: https://githu ##### Mapping ports Often there is a need to connect to a container through a specific port. To achieve this goal it is necessary -know the mapped port specified in the Java code. To get the mapped port, use the getMappedPort(...)` method of -testcontainers API. See examples: https://github.com/tarantool/testcontainers-java-tarantool/blob/ae580cb59d4621d7c11e5f80c394ec141d4c92b3/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java +to know the mapped port specified in the Java code. To get the mapped port, use the getMappedPort(...)` method of +testcontainers API. See examples: https://github.com/tarantool/testcontainers-java-tarantool/blob/ae580cb59d4621d7c11e5f80c394ec141d4c92b3/src/test/java/org/testcontainers/containers/TarantoolCartridgePortMappingTest.java#L22-L98 ## License diff --git a/pom.xml b/pom.xml index 6306d0a..026f6d9 100644 --- a/pom.xml +++ b/pom.xml @@ -76,46 +76,48 @@ - - org.testcontainers - testcontainers - ${testcontainers.version} - - - org.yaml - snakeyaml - 2.0 - - - org.slf4j - slf4j-api - + + org.testcontainers + testcontainers + ${testcontainers.version} + + + org.yaml + snakeyaml + 2.0 + + + org.slf4j + slf4j-api + - - org.apache.httpcomponents - httpclient - 4.5.1 - test - - - ch.qos.logback - logback-classic - 1.3.4 - test - - - org.junit.jupiter - junit-jupiter - 5.8.1 - test - - - org.testcontainers - junit-jupiter - ${testcontainers.version} - test - + + org.apache.httpcomponents + httpclient + 4.5.1 + test + + + + ch.qos.logback + logback-classic + 1.3.4 + test + + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + + org.testcontainers + junit-jupiter + ${testcontainers.version} + test +