Best Testcontainers-java code snippet using org.testcontainers.containers.SocatContainer
Source:SocatContainer.java
...7/**8 * A socat container is used as a TCP proxy, enabling any TCP port of another container to be exposed9 * publicly, even if that container does not make the port public itself.10 */11public class SocatContainer extends GenericContainer<SocatContainer> {12 private final Map<Integer, String> targets = new HashMap<>();13 public SocatContainer() {14 this(DockerImageName.parse("alpine/socat:1.7.3.4-r0"));15 }16 public SocatContainer(final DockerImageName dockerImageName) {17 super(dockerImageName);18 withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh"));19 withCreateContainerCmdModifier(it -> it.withName("testcontainers-socat-" + Base58.randomString(8)));20 }21 public SocatContainer withTarget(int exposedPort, String host) {22 return withTarget(exposedPort, host, exposedPort);23 }24 public SocatContainer withTarget(int exposedPort, String host, int internalPort) {25 addExposedPort(exposedPort);26 targets.put(exposedPort, String.format("%s:%s", host, internalPort));27 return self();28 }29 @Override30 protected void configure() {31 withCommand("-c",32 targets.entrySet().stream()33 .map(entry -> "socat TCP-LISTEN:" + entry.getKey() + ",fork,reuseaddr TCP:" + entry.getValue())34 .collect(Collectors.joining(" & "))35 );36 }37}...
SocatContainer
Using AI Code Generation
1package org.testcontainers.containers;2import java.io.IOException;3import java.util.ArrayList;4import java.util.List;5import org.testcontainers.containers.output.OutputFrame;6import org.testcontainers.containers.output.WaitingConsumer;7public class SocatContainer extends GenericContainer<SocatContainer> {8 public SocatContainer() {9 super("alpine/socat");10 }11 public void start() {12 super.start();13 String containerIpAddress = getContainerIpAddress();14 Integer mappedPort = getMappedPort(2375);15 String socatCommand = "TCP4-LISTEN:2375,fork,reuseaddr TCP4:" + containerIpAddress + ":" + mappedPort;16 WaitingConsumer waitingConsumer = new WaitingConsumer();17 ExecResult execResult = execInContainer("sh", "-c", "socat " + socatCommand);18 }19 public void stop() {20 super.stop();21 }22 public String getDockerHostIpAddress() {23 return getContainerIpAddress();24 }25 public Integer getDockerHostPort() {26 return getMappedPort(2375);27 }28}29package org.testcontainers;30import org.testcontainers.containers.SocatContainer;31public class SocatContainerTest {32 public static void main(String[] args) {33 SocatContainer socatContainer = new SocatContainer();34 socatContainer.start();35 System.out.println(socatContainer.getDockerHostIpAddress());36 System.out.println(socatContainer.getDockerHostPort());37 socatContainer.stop();38 }39}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!