How to use startAmbassadorContainers method of org.testcontainers.containers.DockerComposeContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers

Source:DockerComposeContainer.java Github

copy

Full Screen

...108 pullImages();109 }110 applyScaling(); // scale before up, so that all scaled instances are available first for linking111 createServices();112 startAmbassadorContainers(profiler);113 waitUntilServiceStarted();114 }115 }116 private void pullImages() {117 runWithCompose("pull");118 }119 private void createServices() {120 // Run the docker-compose container, which starts up the services121 runWithCompose("up -d");122 }123 private void waitUntilServiceStarted() {124 listChildContainers().forEach(this::createServiceInstance);125 serviceInstanceMap.forEach(this::waitUntilServiceStarted);126 }127 private void createServiceInstance(Container container) {128 String serviceName = getServiceNameFromContainer(container);129 final ComposeServiceWaitStrategyTarget containerInstance = new ComposeServiceWaitStrategyTarget(container,130 ambassadorContainer, ambassadorPortMappings.getOrDefault(serviceName, new HashMap<>()));131 String containerId = containerInstance.getContainerId();132 if (tailChildContainers) {133 followLogs(containerId, new Slf4jLogConsumer(logger()).withPrefix(container.getNames()[0]));134 }135 //follow logs using registered consumers for this service136 logConsumers.getOrDefault(serviceName, Collections.emptyList()).forEach(consumer -> followLogs(containerId, consumer));137 serviceInstanceMap.putIfAbsent(serviceName, containerInstance);138 }139 private void waitUntilServiceStarted(String serviceName, ComposeServiceWaitStrategyTarget serviceInstance) {140 final WaitAllStrategy waitAllStrategy = waitStrategyMap.get(serviceName);141 if(waitAllStrategy != null) {142 waitAllStrategy.waitUntilReady(serviceInstance);143 }144 }145 private String getServiceNameFromContainer(Container container) {146 final String containerName = container.getLabels().get("com.docker.compose.service");147 final String containerNumber = container.getLabels().get("com.docker.compose.container-number");148 return String.format("%s_%s", containerName, containerNumber);149 }150 private void runWithCompose(String cmd) {151 final DockerCompose dockerCompose;152 if (localCompose) {153 dockerCompose = new LocalDockerCompose(composeFiles, project);154 } else {155 dockerCompose = new ContainerisedDockerCompose(composeFiles, project);156 }157 dockerCompose158 .withCommand(cmd)159 .withEnv(env)160 .invoke();161 }162 private void applyScaling() {163 // Apply scaling164 if (!scalingPreferences.isEmpty()) {165 StringBuilder sb = new StringBuilder("scale");166 for (Map.Entry<String, Integer> scale : scalingPreferences.entrySet()) {167 sb.append(" ").append(scale.getKey()).append("=").append(scale.getValue());168 }169 runWithCompose(sb.toString());170 }171 }172 private void registerContainersForShutdown() {173 ResourceReaper.instance().registerFilterForCleanup(Arrays.asList(174 new SimpleEntry<>("label", "com.docker.compose.project=" + project)175 ));176 }177 private List<Container> listChildContainers() {178 return dockerClient.listContainersCmd()179 .withShowAll(true)180 .exec().stream()181 .filter(container -> Arrays.stream(container.getNames()).anyMatch(name ->182 name.startsWith("/" + project)))183 .collect(toList());184 }185 private void startAmbassadorContainers(Profiler profiler) {186 profiler.start("Ambassador container startup");187 ambassadorContainer.start();188 profiler.stop().log();189 }190 private Logger logger() {191 return LoggerFactory.getLogger(DockerComposeContainer.class);192 }193 @Override194 @VisibleForTesting195 public void finished(Description description) {196 synchronized (MUTEX) {197 try {198 // shut down the ambassador container199 ambassadorContainer.stop();...

Full Screen

Full Screen

startAmbassadorContainers

Using AI Code Generation

copy

Full Screen

1public class DockerComposeTest {2 public static DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))3 .withLocalCompose(true)4 .withExposedService("ambassador_1", 8080);5 public void test() {6 environment.startAmbassadorContainers();7 }8}

Full Screen

Full Screen

startAmbassadorContainers

Using AI Code Generation

copy

Full Screen

1dockerComposeContainer.startAmbassadorContainers();2dockerComposeContainer.getAmbassadorContainer("ambassador_1");3dockerComposeContainer.getAmbassadorContainer("ambassador_2");4dockerComposeContainer.getAmbassadorContainer("ambassador_3");5dockerComposeContainer.getAmbassadorContainer("ambassador_4");6dockerComposeContainer.getAmbassadorContainer("ambassador_5");7dockerComposeContainer.getAmbassadorContainer("ambassador_6");8dockerComposeContainer.getAmbassadorContainer("ambassador_7");9dockerComposeContainer.getAmbassadorContainer("ambassador_8");10dockerComposeContainer.getAmbassadorContainer("ambassador_9");11dockerComposeContainer.getAmbassadorContainer("ambassador_10");12dockerComposeContainer.getAmbassadorContainer("ambassador_11");13dockerComposeContainer.getAmbassadorContainer("ambassador_12");14dockerComposeContainer.getAmbassadorContainer("ambassador_13");15dockerComposeContainer.getAmbassadorContainer("ambassador_14");

Full Screen

Full Screen

startAmbassadorContainers

Using AI Code Generation

copy

Full Screen

1public void startAmbassadorContainers() throws IOException, InterruptedException {2 if (ambassadorContainers == null || ambassadorContainers.isEmpty()) {3 return;4 }5 for (final Map.Entry<String, Integer> entry : ambassadorContainers.entrySet()) {6 final String containerName = entry.getKey();7 final Integer hostPort = entry.getValue();8 final String containerId = execInContainer("docker-compose", "ps", "-q", containerName).getStdout().trim();9 final String containerIpAddress = getContainerIpAddress();10 final InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();11 final Integer containerPort = containerInfo.getNetworkSettings().getPorts().getBindings()[0].getHostPortSpecs()[0];12 final GenericContainer<?> ambassadorContainer = new GenericContainer<>(DockerImageName.parse("richnorth/ambassador"))13 .withNetworkMode("container:" + containerId)14 .withExposedPorts(containerPort)15 .withCommand("-host", containerIpAddress, "-port", containerPort.toString());16 ambassadorContainer.start();17 final Integer mappedPort = ambassadorContainer.getMappedPort(containerPort);18 log.info("Started ambassador container '{}' mapping container port '{}' to host port '{}'",19 ambassadorContainer.getContainerId(), containerPort, mappedPort);20 final String internalServiceHost = containerIpAddress + ":" + mappedPort;21 final String externalServiceHost = containerIpAddress + ":" + hostPort;22 log.info("Replacing '{}' with '{}' in service '{}' environment variables", containerName, internalServiceHost, containerName);23 replaceServiceEnvVars(containerName, containerName, internalServiceHost);24 log.info("Replacing '{}' with '{}' in service '{}' links", containerName, internalServiceHost, containerName);25 replaceServiceLinks(containerName, containerName, internalServiceHost);26 log.info("Replacing '{}' with '{}' in service '{}' links", containerName, externalServiceHost, containerName);27 replaceServiceLinks(containerName, containerName, externalServiceHost);28 }29}30public void startAmbassadorContainers() throws IOException, InterruptedException {31 if (ambassadorContainers == null || ambassadorContainers.isEmpty()) {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful