How to use configure method of org.testcontainers.containers.GenericContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.GenericContainer.configure

Source:FlinkTestcontainersConfigurator.java Github

copy

Full Screen

...39 TestcontainersSettings testcontainersSettings) {40 this.testcontainersSettings = testcontainersSettings;41 this.flinkContainersSettings = flinkContainersSettings;42 }43 private GenericContainer<?> configureJobManagerContainer(Path tempDirectory) {44 // Configure JobManager45 final Configuration jobManagerConf = new Configuration();46 jobManagerConf.addAll(flinkContainersSettings.getFlinkConfig());47 // Build JobManager container48 final ImageFromDockerfile jobManagerImage;49 try {50 jobManagerImage =51 new FlinkImageBuilder()52 .setTempDirectory(tempDirectory)53 .setConfiguration(jobManagerConf)54 .setLogProperties(flinkContainersSettings.getLogProperties())55 .setBaseImage(flinkContainersSettings.getBaseImage())56 .asJobManager()57 .build();58 } catch (ImageBuildException e) {59 throw new RuntimeException("Failed to build JobManager image", e);60 }61 return configureContainer(62 new GenericContainer<>(jobManagerImage),63 flinkContainersSettings.getJobManagerHostname(),64 "JobManager")65 .withExposedPorts(jobManagerConf.get(RestOptions.PORT));66 }67 private List<GenericContainer<?>> configureTaskManagerContainers(Path tempDirectory) {68 List<GenericContainer<?>> taskManagers = new ArrayList<>();69 for (int i = 0; i < flinkContainersSettings.getNumTaskManagers(); i++) {70 // Configure TaskManager71 final Configuration taskManagerConf = new Configuration();72 taskManagerConf.addAll(flinkContainersSettings.getFlinkConfig());73 final String taskManagerHostName =74 flinkContainersSettings.getTaskManagerHostnamePrefix() + i;75 taskManagerConf.set(TaskManagerOptions.HOST, taskManagerHostName);76 // Build TaskManager container77 final ImageFromDockerfile taskManagerImage;78 try {79 taskManagerImage =80 new FlinkImageBuilder()81 .setTempDirectory(tempDirectory)82 .setConfiguration(taskManagerConf)83 .setLogProperties(flinkContainersSettings.getLogProperties())84 .setBaseImage(flinkContainersSettings.getBaseImage())85 .asTaskManager()86 .build();87 } catch (ImageBuildException e) {88 throw new RuntimeException("Failed to build TaskManager image", e);89 }90 taskManagers.add(91 configureContainer(92 new GenericContainer<>(taskManagerImage),93 taskManagerHostName,94 "TaskManager-" + i));95 }96 return taskManagers;97 }98 private GenericContainer<?> configureZookeeperContainer() {99 return configureContainer(100 new GenericContainer<>(DockerImageName.parse("zookeeper").withTag("3.5.9")),101 flinkContainersSettings.getZookeeperHostname(),102 "Zookeeper");103 }104 private GenericContainer<?> configureContainer(105 GenericContainer<?> container, String networkAlias, String logPrefix) {106 // Set dependent containers107 for (GenericContainer<?> dependentContainer : testcontainersSettings.getDependencies()) {108 dependentContainer.withNetwork(testcontainersSettings.getNetwork());109 container.dependsOn(dependentContainer);110 }111 // Setup network112 container.withNetwork(testcontainersSettings.getNetwork());113 container.withNetworkAliases(networkAlias);114 // Setup logger115 Logger logger = testcontainersSettings.getLogger();116 if (logger != null) {117 container.withLogConsumer(new Slf4jLogConsumer(logger).withPrefix(logPrefix));118 }119 // Add environment variables120 container.withEnv(testcontainersSettings.getEnvVars());121 container.withWorkingDirectory(flinkContainersSettings.getFlinkHome());122 return container;123 }124 /** Configures and creates {@link FlinkContainers}. */125 public FlinkContainers configure() {126 // Create temporary directory for building Flink image127 final Path imageBuildingTempDir;128 try {129 imageBuildingTempDir = Files.createTempDirectory("flink-image-build");130 } catch (IOException e) {131 throw new RuntimeException("Failed to create temporary directory", e);132 }133 // Build JobManager134 final GenericContainer<?> jobManager = configureJobManagerContainer(imageBuildingTempDir);135 // Build TaskManagers136 final List<GenericContainer<?>> taskManagers =137 configureTaskManagerContainers(imageBuildingTempDir);138 // Setup Zookeeper HA139 GenericContainer<?> zookeeper = null;140 // Mount HA storage to JobManager141 if (flinkContainersSettings.isZookeeperHA()) {142 zookeeper = configureZookeeperContainer();143 createTempDirAndMountToContainer(144 "flink-recovery", flinkContainersSettings.getHaStoragePath(), jobManager);145 }146 // Mount checkpoint storage to JobManager147 createTempDirAndMountToContainer(148 "flink-checkpoint", flinkContainersSettings.getCheckpointPath(), jobManager);149 return new FlinkContainers(150 jobManager, taskManagers, zookeeper, flinkContainersSettings.getFlinkConfig());151 }152 void createTempDirAndMountToContainer(153 String tempDirPrefix, String containerPath, GenericContainer<?> container) {154 try {155 Path tempDirPath = Files.createTempDirectory(tempDirPrefix);156 File file = tempDirPath.toFile();...

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.containers.wait.strategy.WaitAllStrategy;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;6import java.time.Duration;7import java.util.concurrent.TimeUnit;8public class TestContainer {9 public static void main(String[] args) {10 GenericContainer container = new GenericContainer("postgres:10.4");11 container.withExposedPorts(5432);12 container.withEnv("POSTGRES_PASSWORD", "password");13 container.withEnv("POSTGRES_DB", "testdb");14 container.waitingFor(Wait.forLogMessage(".*database system is ready to accept connections.*\\s", 1));15 container.start();16 System.out.println("Container started");17 System.out.println("Container port: " + container.getMappedPort(5432));18 System.out.println("Container IP: " + container.getContainerIpAddress());19 System.out.println("Container ID: " + container.getContainerId());20 System.out.println("Container DB password: " + container.getEnvMap().get("POSTGRES_PASSWORD"));21 System.out.println("Container DB name: " + container.getEnvMap().get("POSTGRES_DB"));22 System.out.println("Container DB user: " + container.getEnvMap().get("POSTGRES_USER"));23 }24}

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1package com.baeldung.docker.testcontainers;2import org.junit.jupiter.api.Test;3import org.testcontainers.containers.GenericContainer;4import java.io.IOException;5import java.nio.file.Files;6import java.nio.file.Path;7import java.nio.file.Paths;8import static org.junit.jupiter.api.Assertions.assertEquals;9public class TestcontainersConfigTest {10 public void givenDockerImage_whenRunContainerWithConfig_thenContainerRunning() throws IOException, InterruptedException {11 try (GenericContainer container = new GenericContainer("alpine:3.3")12 .withExposedPorts(80)13 .withFileSystemBind("src/test/resources/config", "/etc/config", BindMode.READ_ONLY)14 .withCommand("cat", "/etc/config/config.properties")) {15 container.start();16 String output = container.getLogs();17 assertEquals("property1=value1", output.trim());18 }19 }20}21@Test public void givenDockerImage_whenRunContainerWithCopyFile_thenContainerRunning() throws IOException, InterruptedException { try (GenericContainer container = new GenericContainer("alpine:3.3") .withExposedPorts(80) .withCopyFileToContainer(MountableFile.forClasspathResource("config/config.properties"), "/etc/config/config.properties") .withCommand("cat", "/etc/config/config.properties")) { container.start(); String output = container.getLogs(); assertEquals("property1=value1", output.trim()); } }22@Test public void givenDockerImage_whenRunContainerWithEnv_thenContainerRunning() throws

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import java.util.concurrent.TimeUnit;3import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;4public class GenericContainerExample {5 public static void main(String[] args) {6 GenericContainer container = new GenericContainer("nginx:1.17.6-alpine")7 .withExposedPorts(80)8 .waitingFor(new HttpWaitStrategy().forPort(80).forPath("/"));9 container.start();10 System.out.println("Nginx container started");11 System.out.println("Nginx container IP address: " + container.getContainerIpAddress());12 System.out.println("Nginx container mapped port: " + container.getMappedPort(80));13 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts());14 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings());15 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp"));16 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0));17 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0).getHostPortSpec());18 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0).getHostPort());19 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0).getHostIp());20 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0).getHostIp());21 System.out.println("Nginx container mapped ports: " + container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("80/tcp").get(0).getHostIp());

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer2import org.testcontainers.containers.wait.strategy.Wait3class GenericContainerExample extends Specification {4 def "test generic container"() {5 def container = new GenericContainer('nginx:1.13.8-alpine')6 container.withExposedPorts(80)7 container.withEnv('NGINX_PORT', 80)8 container.waitingFor(Wait.forHttp('/'))9 container.start()10 response.contains('Welcome to nginx!')11 }12}

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1public class GenericContainerExample {2 public void test() {3 GenericContainer container = new GenericContainer("alpine:3.2")4 .withCommand("top")5 .withExposedPorts(80)6 .withEnv("ENV", "DEV")7 .withClasspathResourceMapping("test-resource.txt", "/container/path", BindMode.READ_WRITE)8 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("container")));9 container.start();10 System.out.println(container.getLogs());11 container.stop();12 }13}142017-06-25 15:42:34.460 INFO 14073 --- [ main] o.t.utility.RegistryAuthLocator : Credential helper/store (docker-credential-desktop) does not have credentials for index.docker.io

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1public class GenericContainerUsage {2 public void testGenericContainerUsage() {3 GenericContainer container = new GenericContainer("alpine:3.3")4 .withCommand("sleep", "9999")5 .withExposedPorts(80)6 .withEnv("ENV1", "value1")7 .withEnv("ENV2", "value2")8 .withEnv("ENV3", "value3")9 .withEnv("ENV4", "value4")10 .withEnv("ENV5", "value5")11 .withEnv("ENV6", "value6")12 .withEnv("ENV7", "value7")13 .withEnv("ENV8", "value8")14 .withEnv("ENV9", "value9")15 .withEnv("ENV10", "value10")16 .withEnv("ENV11", "value11")17 .withEnv("ENV12", "value12")18 .withEnv("ENV13", "value13")19 .withEnv("ENV14", "value14")20 .withEnv("ENV15", "value15")21 .withEnv("ENV16", "value16")22 .withEnv("ENV17", "value17")23 .withEnv("ENV18", "value18")24 .withEnv("ENV19", "value19")25 .withEnv("ENV20", "value20")26 .withEnv("ENV21", "value21")27 .withEnv("ENV22", "value22")28 .withEnv("ENV23", "value23")29 .withEnv("ENV24", "value24")30 .withEnv("ENV25", "value25")31 .withEnv("ENV26", "value26")32 .withEnv("ENV27", "value27")33 .withEnv("ENV28", "value28")34 .withEnv("ENV29", "value29")35 .withEnv("ENV30", "value30")36 .withEnv("ENV31", "value31")37 .withEnv("ENV32", "value32")38 .withEnv("ENV33", "value33")39 .withEnv("ENV34", "value34")40 .withEnv("ENV35", "value35")41 .withEnv("ENV36", "value36")42 .withEnv("ENV37", "value37")

Full Screen

Full Screen

configure

Using AI Code Generation

copy

Full Screen

1public class GenericContainerTest {2 public void testGenericContainer() {3 GenericContainer container = new GenericContainer("alpine:latest")4 .withCommand("sleep", "10000")5 .withExposedPorts(80);6 container.start();

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.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful