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

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

Source:DeploymentManager.java Github

copy

Full Screen

...165 if (oauthEnabled) {166 exposedPorts.add(8443);167 }168 KafkaAdminServerContainer container = new KafkaAdminServerContainer()169 .withLabels(Collections.singletonMap("test-ident", Environment.TEST_CONTAINER_LABEL))170 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("systemtests.admin-server"), true))171 .withCreateContainerCmdModifier(cmd -> cmd.withName(name("admin-server")))172 .withNetwork(testNetwork)173 .withExposedPorts(exposedPorts.toArray(Integer[]::new))174 .withEnv(envMap)175 .waitingFor(Wait.forHttp("/health/started").forPort(8080));176 if (oauthEnabled) {177 container178 .withClasspathResourceMapping(pathCert, pathCert, BindMode.READ_ONLY)179 .withClasspathResourceMapping(pathKey, pathKey, BindMode.READ_ONLY);180 }181 Integer configuredDebugPort = Integer.getInteger("debugPort");182 if (configuredDebugPort != null) {183 //container.addExposedPort(configuredDebugPort);184 container.addFixedExposedPort(configuredDebugPort, 5005);185 container.addEnv("JAVA_DEBUG", "true");186 }187 String customLogConfig = System.getProperty("customLogConfig");188 if (customLogConfig != null) {189 container.addFileSystemBind(customLogConfig, "/opt/kafka-admin-api/custom-config/", BindMode.READ_ONLY);190 }191 container.start();192 return container;193 }194 public GenericContainer<?> deployKeycloak() {195 LOGGER.info("Deploying keycloak container");196 String imageName = System.getProperty("keycloak.image");197 GenericContainer<?> container = new GenericContainer<>(imageName)198 .withLabels(Collections.singletonMap("test-ident", Environment.TEST_CONTAINER_LABEL))199 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("systemtests.keycloak"), true))200 .withCreateContainerCmdModifier(cmd -> cmd.withName(name("keycloak")))201 .withNetwork(testNetwork)202 .withNetworkAliases("keycloak")203 .withExposedPorts(8080)204 .withEnv(Map.of("KEYCLOAK_USER", "admin",205 "KEYCLOAK_PASSWORD", "admin",206 "PROXY_ADDRESS_FORWARDING", "true"))207 .withCopyFileToContainer(MountableFile.forClasspathResource("/keycloak/scripts/keycloak-ssl.cli"),208 "/opt/jboss/keycloak/keycloak-ssl.cli")209 .withCopyFileToContainer(MountableFile.forClasspathResource("/certs/keycloak.server.keystore.p12"),210 "/opt/jboss/keycloak/standalone/configuration/certs/keycloak.server.keystore.p12")211 .withCommand("-Dkeycloak.profile.feature.upload_scripts=enabled")212 .waitingFor(Wait.forHttp("/auth/realms/demo").withStartupTimeout(Duration.ofMinutes(5)));213 LOGGER.info("Deploying keycloak_import container");214 new GenericContainer<>(imageName)215 .withCreateContainerCmdModifier(cmd -> cmd.withName(name("keycloak-import")))216 .withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint(List.of("")))217 .withNetwork(testNetwork)218 .withEnv(Map.of("KEYCLOAK_HOST", "keycloak"))219 .withCopyFileToContainer(MountableFile.forClasspathResource("/keycloak-import/realms/authz-realm.json"),220 "/opt/jboss/realms/authz-realm.json")221 .withCopyFileToContainer(MountableFile.forClasspathResource("/keycloak-import/realms/demo-realm.json"),222 "/opt/jboss/realms/demo-realm.json")223 .withCopyFileToContainer(MountableFile.forClasspathResource("/keycloak-import/start.sh", 0755),224 "/opt/jboss/start.sh")225 .withCommand("/opt/jboss/start.sh")226 .start();227 LOGGER.info("Waiting for keycloak container");228 container.start();229 return container;230 }231 private KafkaContainer<?> deployKafka() {232 LOGGER.info("Deploying Kafka container");233 Map<String, String> env = new HashMap<>();234 try (InputStream stream = getClass().getResourceAsStream("/kafka-oauth/env.properties")) {235 Properties envProps = new Properties();236 envProps.load(stream);237 envProps.keySet()238 .stream()239 .map(Object::toString)240 .forEach(key -> env.put(key, envProps.getProperty(key)));241 } catch (IOException e) {242 throw new UncheckedIOException(e);243 }244 String imageTag = System.getProperty("strimzi-kafka.tag");245 var container = new KeycloakSecuredKafkaContainer(imageTag)246 .withLabels(Collections.singletonMap("test-ident", Environment.TEST_CONTAINER_LABEL))247 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("systemtests.oauth-kafka"), true))248 .withCreateContainerCmdModifier(cmd -> cmd.withName(name("oauth-kafka")))249 .withEnv(env)250 .withNetwork(testNetwork)251 .withClasspathResourceMapping("/certs/cluster.keystore.p12", "/opt/kafka/certs/cluster.keystore.p12", BindMode.READ_ONLY)252 .withClasspathResourceMapping("/certs/cluster.truststore.p12", "/opt/kafka/certs/cluster.truststore.p12", BindMode.READ_ONLY)253 .withClasspathResourceMapping("/kafka-oauth/config/", "/opt/kafka/config/strimzi/", BindMode.READ_ONLY)254 .withCopyFileToContainer(MountableFile.forClasspathResource("/kafka-oauth/scripts/functions.sh"), "/opt/kafka/functions.sh")255 .withCopyFileToContainer(MountableFile.forClasspathResource("/kafka-oauth/scripts/simple_kafka_config.sh", 0755), "/opt/kafka/simple_kafka_config.sh")256 .withCopyFileToContainer(MountableFile.forClasspathResource("/kafka-oauth/scripts/start.sh", 0755), "/opt/kafka/start.sh")257 .withCommand("/opt/kafka/start.sh");258 container.start();259 return container;260 }261 private KafkaContainer<?> deployStrimziKafka() {262 LOGGER.info("Deploying Strimzi Kafka container");263 class StrimziPlainKafkaContainer extends StrimziKafkaContainer264 implements KafkaContainer<StrimziKafkaContainer> {265 StrimziPlainKafkaContainer(String version) {266 super(version);267 }268 }269 String imageTag = System.getProperty("strimzi-kafka.tag");270 var container = new StrimziPlainKafkaContainer(imageTag)271 .withLabels(Collections.singletonMap("test-ident", Environment.TEST_CONTAINER_LABEL))272 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("systemtests.plain-kafka"), true))273 .withCreateContainerCmdModifier(cmd -> cmd.withName(name("plain-kafka")))274 .withKafkaConfigurationMap(Map.of("auto.create.topics.enable", "false"))275 .withNetwork(testNetwork);276 container.start();277 return (KafkaContainer<?>) container;278 }279 private String encodeTLSConfig(String fileName) {280 String rawContent;281 try (InputStream stream = getClass().getResourceAsStream(fileName)) {282 rawContent = new BufferedReader(new InputStreamReader(stream))283 .lines().collect(Collectors.joining("\n"));284 return Base64.getEncoder().encodeToString(rawContent.getBytes(StandardCharsets.UTF_8));285 } catch (IOException e) {...

Full Screen

Full Screen

Source:Environment.java Github

copy

Full Screen

...67 private Network network = Network.builder()68 .createNetworkCmdModifier(createNetworkCmd ->69 createNetworkCmd70 .withName(PRODUCT_TEST_LAUNCHER_NETWORK)71 .withLabels(ImmutableMap.of(PRODUCT_TEST_LAUNCHER_STARTED_LABEL_NAME, PRODUCT_TEST_LAUNCHER_STARTED_LABEL_VALUE)))72 .build();73 private Map<String, DockerContainer> containers = new HashMap<>();74 public Builder addContainer(String name, DockerContainer container)75 {76 requireNonNull(name, "name is null");77 checkState(!containers.containsKey(name), "Container with name %s is already registered", name);78 containers.put(name, requireNonNull(container, "container is null"));79 String containerName = "ptl-" + name;80 container81 .withLogConsumer(new PrintingLogConsumer(format("%-20s| ", containerName)))82 .withNetwork(network)83 .withNetworkAliases(name)84 .withLabel(PRODUCT_TEST_LAUNCHER_STARTED_LABEL_NAME, PRODUCT_TEST_LAUNCHER_STARTED_LABEL_VALUE)85 .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd...

Full Screen

Full Screen

Source:ContainerLabelTest.java Github

copy

Full Screen

...11 // multiple_labels {12 private Map<String, String> mapOfLabels = new HashMap<>();13 // populate map, e.g. mapOfLabels.put("key1", "value1");14 public GenericContainer containerWithMultipleLabels = new GenericContainer(DockerImageName.parse("alpine:3.14"))15 .withLabels(mapOfLabels);16 // }17}...

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.containers.output.ToStringConsumer;5import org.testcontainers.containers.output.WaitingConsumer;6import java.util.concurrent.TimeUnit;7public class Test {8 public static void main(String[] args){9 GenericContainer container = new GenericContainer("alpine:latest");10 container.withCommand("sh", "-c", "echo hello && sleep 10 && echo world");11 container.withLabels("key1", "value1", "key2", "value2");12 container.start();13 System.out.println("Container started");14 System.out.println("Container labels: " + container.getLabels());15 System.out.println("Container label key1: " + container.getLabels().get("key1"));16 System.out.println("Container label key2: " + container.getLabels().get("key2"));17 container.stop();18 }19}20Container labels: {key1=value1, key2=value2}21import org.testcontainers.containers.GenericContainer;22import org.testcontainers.containers.output.OutputFrame;23import org.testcontainers.containers.output.Slf4jLogConsumer;24import org.testcontainers.containers.output.ToStringConsumer;25import org.testcontainers.containers.output.WaitingConsumer;26import java.util.concurrent.TimeUnit;27public class Test {28 public static void main(String[] args){29 GenericContainer container = new GenericContainer("alpine:latest");30 container.withCommand("sh", "-c", "echo hello && sleep 10 && echo world");31 container.withLabel("key1", "value1");32 container.withLabel("key2", "value2");33 container.start();34 System.out.println("Container started");35 System.out.println("Container labels: " + container.getLabels());36 System.out.println("Container label key1: " + container.getLabels().get("key1"));37 System.out.println("Container label key2: " + container.getLabels().get("key2"));38 container.stop();39 }40}41Container labels: {key1=value1, key2=value2}

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5public class TestContainers {6 private static final Logger LOGGER = LoggerFactory.getLogger(TestContainers.class);7 public static void main(String[] args) {8 try (GenericContainer container = new GenericContainer("alpine:3.7").withCommand("sleep", "1000")) {9 container.withLogConsumer(new Slf4jLogConsumer(LOGGER).withPrefix("container"));10 container.setLabel("key1", "value1");11 container.setLabel("key2", "value2");12 container.start();13 container.followOutput(new Slf4jLogConsumer(LOGGER));14 }15 }16}17import org.testcontainers.containers.GenericContainer;18import org.testcontainers.containers.output.Slf4jLogConsumer;19import org.slf4j.Logger;20import org.slf4j.LoggerFactory;21import java.util.HashMap;22import java.util.Map;23public class TestContainers {24 private static final Logger LOGGER = LoggerFactory.getLogger(TestContainers.class);25 public static void main(String[] args) {26 try (GenericContainer container = new GenericContainer("alpine:3.7").withCommand("sleep", "1000")) {27 container.withLogConsumer(new Slf4jLogConsumer(LOGGER).withPrefix("container"));28 Map<String, String> labels = new HashMap<>();29 labels.put("key1", "value1");30 labels.put("key2", "value2");31 container.withLabels(labels);32 container.start();33 container.followOutput(new Slf4jLogConsumer(LOGGER));34 }35 }36}3717:01:16.807 [main] INFO o.t.utility.RegistryAuthLocator - Credential helper/store (docker-credential-desktop) does not have credentials for index.docker.io3817:01:16.809 [main] INFO o.t.utility.RegistryAuthLocator - Credential helper/store (docker-credential-desktop) does not have credentials for docker.io

Full Screen

Full Screen

withLabels

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.WaitStrategy;4public class TestContainer {5 public static void main(String[] args) {6 try (GenericContainer container = new GenericContainer("nginx:1.15.8-alpine")7 .withExposedPorts(80)8 .withCommand("nginx", "-g", "daemon off;")9 .withEnv("NGINX_PORT", "80")10 .withEnv("NGINX_VERSION", "1.15.8")11 .withEnv("NGINX_VHOST_PRESET", "php")12 .withEnv("NGINX_VHOST_PRESET_PORT", "9000")13 .withEnv("NGINX_VHOST_PRESET_DOCROOT", "/var/www")14 .withEnv("NGINX_VHOST_PRESET_SERVERNAME", "localhost")15 .withEnv("NGINX_VHOST_PRESET_SSL", "false")16 .withEnv("NGINX_VHOST_PRESET_INDEX", "index.php")17 .withEnv("NGINX_VHOST_PRESET_PHP", "true")18 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_HOST", "php")19 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PORT", "9000")20 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_STATUS", "false")21 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_STATUS_PATH", "/status")22 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_STATUS_AUTH", "false")23 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_STATUS_AUTH_USER", "admin")24 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_STATUS_AUTH_PASS", "admin")25 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PM", "dynamic")26 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PM_MAX_CHILDREN", "5")27 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PM_START_SERVERS", "2")28 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PM_MIN_SPARE_SERVERS", "1")29 .withEnv("NGINX_VHOST_PRESET_PHP_FPM_PM_MAX_SPARE

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import java.time.Duration;5import java.util.concurrent.TimeUnit;6public class WithLabels {7 public static void main(String[] args) throws Exception {8 GenericContainer container = new GenericContainer("alpine:3.7")9 .withCommand("sh", "-c", "while true; do echo 'Hello World'; sleep 1; done")10 .withLabels("label1", "value1", "label2", "value2")11 .withStartupTimeout(Duration.ofSeconds(5));12 container.start();13 String containerId = container.getContainerId();14 container.followOutput(new ToStringConsumer(), OutputFrame.OutputType.STDOUT);15 System.out.println("Container ID: " + containerId);16 System.out.println("Container Labels: " + container.getLabels());17 container.stop();18 }19}20Container Labels: {label1=value1, labe

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1package org.testcontainers;2import org.testcontainers.containers.GenericContainer;3public class Example1 {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer("alpine:3.9")6 .withCommand("tail", "-f", "/dev/null")7 .withLabels(8 );9 container.start();10 System.out.println(container.getLabels());11 }12}13package org.testcontainers;14import org.testcontainers.containers.GenericContainer;15public class Example2 {16 public static void main(String[] args) {17 GenericContainer container = new GenericContainer("alpine:3.9")18 .withCommand("tail", "-f", "/dev/null")19 .withLabel("foo", "bar");20 container.start();21 System.out.println(container.getLabels());22 }23}24package org.testcontainers;25import org.testcontainers.containers.GenericContainer;26import java.util.HashMap;27import java.util.Map;28public class Example3 {29 public static void main(String[] args) {30 Map<String, String> labels = new HashMap<>();31 labels.put("foo", "bar");32 labels.put("foo2", "bar2");33 GenericContainer container = new GenericContainer("alpine:3.9")34 .withCommand("tail", "-f", "/dev/null")35 .withLabels(labels);36 container.start();37 System.out.println(container.getLabels());38 }39}40package org.testcontainers;41import org.testcontainers.containers.GenericContainer;42public class Example4 {43 public static void main(String[] args) {44 GenericContainer container = new GenericContainer("alpine:3.9")45 .withCommand("tail", "-f", "/dev/null")46 .withLabel("foo", "bar");47 container.start();48 System.out.println(container.getLabels());49 }50}51package org.testcontainers;52import org.testcontainers.containers.GenericContainer;53import java

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2public class TestContainersExample {3 public static void main(String[] args) {4 try (GenericContainer container = new GenericContainer("ubuntu:18.04")5 .withCommand("top")6 .withLabels(java.util.Map.of("key1","value1","key2","value2"))) {7 container.start();8 System.out.println(container.getLabels());9 }10 }11}12import org.testcontainers.containers.GenericContainer;13public class TestContainersExample {14 public static void main(String[] args) {15 try (GenericContainer container = new GenericContainer("ubuntu:18.04")16 .withCommand("top")17 .withLabel("key1","value1")18 .withLabel("key2","value2")) {19 container.start();20 System.out.println(container.getLabels());21 }22 }23}24import org.testcontainers.containers.GenericContainer;25public class TestContainersExample {26 public static void main(String[] args) {27 try (GenericContainer container = new GenericContainer("ubuntu:18.04")28 .withCommand("top")29 .withLabel("key1","value1")30 .withLabel("key2","value2")) {31 container.start();32 System.out.println(container.getLabels());33 }34 }35}36import org.testcontainers.containers.GenericContainer;37public class TestContainersExample {38 public static void main(String[] args) {39 try (GenericContainer container = new GenericContainer("ubuntu:18.04")40 .withCommand("top")41 .withLabel("key1","value1")42 .withLabel("key2","value2")) {43 container.start();44 System.out.println(container.getLabels());45 }46 }47}48import org.testcontainers.containers.GenericContainer;49public class TestContainersExample {50 public static void main(String[] args) {51 try (GenericContainer container = new GenericContainer("ubuntu:18.04")52 .withCommand("

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.Label;3public class TestContainers {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer("redis:latest")6 .withExposedPorts(6379)7 .withLabels(new Label("label1", "value1"), new Label("label2", "value2"));8 container.start();9 System.out.println("Container started with labels: " + container.getLabels());10 container.stop();11 }12}13Container started with labels: {label1=value1, label2=value2}

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2public class ContainerLabels {3 public static void main(String[] args) {4 GenericContainer container = new GenericContainer("alpine:3.10")5 .withLabels("org.testcontainers", "rocks");6 container.start();7 }8}9import org.testcontainers.containers.GenericContainer;10public class ContainerLabels {11 public static void main(String[] args) {12 GenericContainer container = new GenericContainer("alpine:3.10")13 .withLabel("org.testcontainers", "rocks");14 container.start();15 }16}17import org.testcontainers.containers.GenericContainer;18public class ContainerLabels {19 public static void main(String[] args) {20 GenericContainer container = new GenericContainer("alpine:3.10")21 .withEnv("TEST", "TEST_VALUE");22 container.start();23 }24}25import org.testcontainers.containers.GenericContainer;26public class ContainerLabels {27 public static void main(String[] args) {28 GenericContainer container = new GenericContainer("alpine:3.10")29 .withExposedPorts(8080);30 container.start();31 }32}33import org.testcontainers.containers.GenericContainer;34public class ContainerLabels {35 public static void main(String[] args) {36 GenericContainer container = new GenericContainer("alpine:3.10")37 .withExposedPorts(8080, 8081);38 container.start();39 }40}41import org.testcontainers.containers.GenericContainer;42public class ContainerLabels {43 public static void main(String[] args) {

Full Screen

Full Screen

withLabels

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import java.util.Map;3public class GenericContainerWithLabels {4 public static void main(String[] args) {5 try (GenericContainer container = new GenericContainer("alpine:3.7")6 .withLabels(Map.of("key1", "value1", "key2", "value2"))) {7 container.start();8 System.out.println(container.getLabels());9 }10 }11}12{key1=value1, key2=value2}

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