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

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

Source:FlinkTestcontainersConfigurator.java Github

copy

Full Screen

1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.flink.connector.testframe.container;19import org.apache.flink.configuration.Configuration;20import org.apache.flink.configuration.RestOptions;21import org.apache.flink.configuration.TaskManagerOptions;22import org.slf4j.Logger;23import org.testcontainers.containers.GenericContainer;24import org.testcontainers.containers.output.Slf4jLogConsumer;25import org.testcontainers.images.builder.ImageFromDockerfile;26import org.testcontainers.utility.DockerImageName;27import java.io.File;28import java.io.IOException;29import java.nio.file.Files;30import java.nio.file.Path;31import java.util.ArrayList;32import java.util.List;33/** Orchestrates configuration of Flink containers within Testcontainers framework. */34class FlinkTestcontainersConfigurator {35 private final TestcontainersSettings testcontainersSettings;36 private final FlinkContainersSettings flinkContainersSettings;37 FlinkTestcontainersConfigurator(38 FlinkContainersSettings flinkContainersSettings,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();157 file.setReadable(true, false);158 file.setWritable(true, false);159 file.setExecutable(true, false);160 container.withFileSystemBind(tempDirPath.toAbsolutePath().toString(), containerPath);161 } catch (IOException e) {162 throw new IllegalStateException("Failed to create temporary recovery directory", e);163 }164 }165}...

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1[INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ testcontainers ---2[INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ testcontainers ---3[INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ testcontainers ---4[INFO] [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ testcontainers ---5[INFO] [INFO] --- maven-failsafe-plugin:2.22.2:verify (default) @ testcontainers ---6[INFO] [INFO] --- maven-failsafe-plugin:2.22.2:verify (default) @

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1 public void failed(String reason) {2 if (reason == null) {3 throw new IllegalArgumentException("Reason may not be null");4 } else {5 this.state = ContainerState.FAILED;6 this.stopLatch.countDown();7 this.logger().error("Container {} failed because of {}", this, reason);8 }9 }10 public void failed(Throwable cause) {11 if (cause == null) {12 throw new IllegalArgumentException("Cause may not be null");13 } else {14 this.state = ContainerState.FAILED;15 this.stopLatch.countDown();16 this.logger().error("Container {} failed because of {}", this, cause.getMessage(), cause);17 }18 }19 public void failed() {20 this.state = ContainerState.FAILED;21 this.stopLatch.countDown();22 this.logger().error("Container {} failed", this);23 }24 public void failed(String reason, Throwable cause) {25 if (reason == null) {26 throw new IllegalArgumentException("Reason may not be null");27 } else if (cause == nullString reason) {28 if (reaso IllegalArgumentException("Cause may not be null");29 } else {30 this.state = ContainerState.FAILED;31 n this.stopLatch.countDown();32 this.logger().error("Container {} failed because of {}", this, reason, cause);33 }34 }35 public void failed(String reason, Throwable cause, boolean logExceptionStackTrace) {36 if (reason == null) {37 throw new IllegalArgumentException("Reason may not be null");38 } else if (cause == null) {39 throw new IllegalArgumentException("Cause may not be null");40 } else {41 this.state = ContainerState.FAILED;42 this.stopLatch.countDown();43 if (logExceptionStackTrace) {44 this.logger().error("Container {} failed because of {}", this, reason, cause);45 } else {46 this.logger().error("Container {} failed because of {}", this, reason, cause.getMessage());47 }48 }49 }50 public void failed(Throwable cause, boolean logExceptionStackTrace

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1 public void failed() {2 throw new == null) {3 throw new IllegalArgumentException("Reason may not be null");4 } else {5 this.state = ContainerState.FAILED;6 this.stopLatch.countDown();7 this.logger().error("Container {} failed because of {}", this, reason);8 }9 }10 public void failed(Throwable cause) {11 if (cause == null) {12 throw new IllegalArgumentException("Cause may not be null");13 } else {14 this.state = ContainerState.FAILED;15 this.stopLatch.countDown();16 this.logger().error("Container {} failed because of {}", this, cause.getMessage(), cause);17 }18 }19;

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1 public static final String[] CMD = new String[] {2 "while true do echo 'Hello, World!'; sleep 1; done"3 };4 public static final String[] CMD = new String[] {5 "echo", public void failed() {6 };7 this.state = ContainerState.FAILED;8 this.stopLatch.countDown();9 this.logger().error("Container {} failed", this);10 }

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1 public void failed(String reason, Throwable cause) {2 if (reason == null) {3 } else if (cause == null) {4 throw new IllegalArgumentException("Cause may not be null");5 } else {6 this.state = ContainerState.FAILED;7 this.stopLatch.countDown();8 this.logger().error("Container {} failed because of {}", this, reason, cause);9 }10 }11 public void failed(String reason, Throwable cause, boolean logExceptionStackTrace) {12 if (reason == null) {13 throw new IllegalArgumentException("Reason may not be null");14 } else if (cause == null) {15 throw new IllegalArgumentException("Cause may not be null");16 } else {17 this.state = ContainerState.FAILED;18 this.stopLatch.countDown();19 if (logExceptionStackTrace) {20 this.logger().error("Container {} failed because of {}", this, reason, cause);21 } else {22 this.logger().error("Container {} failed because of {}", this, reason, cause.getMessage());23 }24 }25 }26 public void failed(Throwable cause, boolean logExceptionStackTrace

Full Screen

Full Screen

failed

Using AI Code Generation

copy

Full Screen

1 public void failed() {2 throw new UnsupportedOperationException();3 }4 public void test() {5 DockerComposeContainer composeContainer = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))6 .withExposedService("redis_1", 6379)7 .withExposedService("redis_2", 6379)8 .withExposedService("redis_3", 6379)9 .withLocalCompose(true);10 composeContainer.start();11 GenericContainer redis = new GenericContainer<>("redis:3.2.8")12 .withExposedPorts(6379)13 .withCommand("redis-server --appendonly yes")14 .withCreateContainerCmdModifier(cmd -> cmd.withName("redis_1"))15 .withNetworkAliases("redis_1")16 .withNetwork(composeContainer.getNetwork());17 redis.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