Skip to content

Commit 9cf3721

Browse files
committed
Add example for port mapping
Closes #19
1 parent ba4661f commit 9cf3721

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,76 @@ cartridge build at the image build stage.
232232

233233
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
234234

235+
##### Mapping ports
236+
237+
Often there is a need to connect to a container via a certain port. To achieve this goal, you need to know the mapped
238+
port of the port that was specified from the Java code. To get the mapped port use the `getMappedPort(...)` method
239+
of testcontainers API.
240+
241+
As an example, consider the following Java code:
242+
243+
```java
244+
import org.junit.ClassRule;
245+
import org.junit.Test;
246+
import org.junit.rules.ExternalResource;
247+
import org.slf4j.Logger;
248+
import org.slf4j.LoggerFactory;
249+
import org.testcontainers.containers.GenericContainer;
250+
import org.testcontainers.containers.TarantoolCartridgeContainer;
251+
252+
import java.io.IOException;
253+
import java.net.HttpURLConnection;
254+
import java.net.URL;
255+
256+
public class AppTest {
257+
258+
private final Logger logger = LoggerFactory.getLogger(AppTest.class);
259+
260+
private static final GenericContainer<?> container =
261+
new TarantoolCartridgeContainer("cartridge/instances.yml", "cartridge/topology.lua")
262+
.withDirectoryBinding("cartridge")
263+
.withRouterHost("localhost")
264+
.withRouterPort(3301)
265+
// Open http port in container
266+
.withAPIPort(8081)
267+
.withRouterUsername("admin")
268+
.withRouterPassword("tarantool-cartridge-starter-cluster-cookie")
269+
.withReuse(true);
270+
271+
@ClassRule
272+
public static ExternalResource resource = new ExternalResource() {
273+
@Override
274+
public void before() {
275+
container.start();
276+
}
277+
@Override
278+
public void after() {
279+
container.stop();
280+
}
281+
};
282+
283+
284+
@Test
285+
public void shouldAnswerWithTrue() throws IOException {
286+
// Get mapped port
287+
final int mappedHttpPort = container.getMappedPort(8081);
288+
// Get metrics response
289+
final String metricsResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/metrics");
290+
logger.info("Metric response: {}", metricsResponse);
291+
final String helloResponse = sendRequestAndGetResponse("http://localhost:" + mappedHttpPort + "/hello");
292+
logger.info("Hello response: {}", helloResponse);
293+
}
294+
295+
private String sendRequestAndGetResponse(final String urlSource) throws IOException {
296+
final URL url = new URL(urlSource);
297+
// Connect to the URL with mapped port
298+
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
299+
return connection.getResponseMessage();
300+
}
301+
}
302+
```
303+
304+
235305
## License
236306

237307
See [LICENSE](LICENSE).

0 commit comments

Comments
 (0)