Best Testcontainers-java code snippet using org.testcontainers.utility.TestcontainersConfiguration.getImage
Source:TestcontainersConfiguration.java
...82 this.environment = environment;83 }84 @Deprecated85 public String getAmbassadorContainerImage() {86 return getImage(AMBASSADOR_IMAGE).asCanonicalNameString();87 }88 @Deprecated89 public String getSocatContainerImage() {90 return getImage(SOCAT_IMAGE).asCanonicalNameString();91 }92 @Deprecated93 public String getVncRecordedContainerImage() {94 return getImage(VNC_RECORDER_IMAGE).asCanonicalNameString();95 }96 @Deprecated97 public String getDockerComposeContainerImage() {98 return getImage(COMPOSE_IMAGE).asCanonicalNameString();99 }100 @Deprecated101 public String getTinyImage() {102 return getImage(ALPINE_IMAGE).asCanonicalNameString();103 }104 public boolean isRyukPrivileged() {105 return Boolean106 .parseBoolean(getEnvVarOrProperty("ryuk.container.privileged", "false"));107 }108 @Deprecated109 public String getRyukImage() {110 return getImage(RYUK_IMAGE).asCanonicalNameString();111 }112 @Deprecated113 public String getSSHdImage() {114 return getImage(SSHD_IMAGE).asCanonicalNameString();115 }116 public Integer getRyukTimeout() {117 return Integer.parseInt(getEnvVarOrProperty("ryuk.container.timeout", "30"));118 }119 @Deprecated120 public String getKafkaImage() {121 return getImage(KAFKA_IMAGE).asCanonicalNameString();122 }123 @Deprecated124 public String getOracleImage() {125 return getEnvVarOrProperty("oracle.container.image", null);126 }127 @Deprecated128 public String getPulsarImage() {129 return getImage(PULSAR_IMAGE).asCanonicalNameString();130 }131 @Deprecated132 public String getLocalStackImage() {133 return getImage(LOCALSTACK_IMAGE).asCanonicalNameString();134 }135 public boolean isDisableChecks() {136 return Boolean.parseBoolean(getEnvVarOrUserProperty("checks.disable", "false"));137 }138 @UnstableAPI139 public boolean environmentSupportsReuse() {140 // specifically not supported as an environment variable or classpath property141 return Boolean.parseBoolean(getEnvVarOrUserProperty("testcontainers.reuse.enable", "false"));142 }143 public String getDockerClientStrategyClassName() {144 return getEnvVarOrUserProperty("docker.client.strategy", null);145 }146 public String getTransportType() {147 return getEnvVarOrProperty("transport.type", "okhttp");148 }149 public Integer getImagePullPauseTimeout() {150 return Integer.parseInt(getEnvVarOrProperty("pull.pause.timeout", "30"));151 }152 public String getImageSubstitutorClassName() {153 return getEnvVarOrProperty("image.substitutor", null);154 }155 @Nullable156 @Contract("_, !null, _ -> !null")157 private String getConfigurable(@NotNull final String propertyName, @Nullable final String defaultValue, Properties... propertiesSources) {158 String envVarName = propertyName.replaceAll("\\.", "_").toUpperCase();159 if (!envVarName.startsWith("TESTCONTAINERS_")) {160 envVarName = "TESTCONTAINERS_" + envVarName;161 }162 if (environment.containsKey(envVarName)) {163 return environment.get(envVarName);164 }165 for (final Properties properties : propertiesSources) {166 if (properties.get(propertyName) != null) {167 return (String) properties.get(propertyName);168 }169 }170 return defaultValue;171 }172 /**173 * Gets a configured setting from an environment variable (if present) or a configuration file property otherwise.174 * The configuration file will be the <code>.testcontainers.properties</code> file in the user's home directory or175 * a <code>testcontainers.properties</code> found on the classpath.176 *177 * @param propertyName name of configuration file property (dot-separated lower case)178 * @return the found value, or null if not set179 */180 @Nullable181 @Contract("_, !null -> !null")182 public String getEnvVarOrProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {183 return getConfigurable(propertyName, defaultValue, userProperties, classpathProperties);184 }185 /**186 * Gets a configured setting from an environment variable (if present) or a configuration file property otherwise.187 * The configuration file will be the <code>.testcontainers.properties</code> file in the user's home directory.188 *189 * @param propertyName name of configuration file property (dot-separated lower case)190 * @return the found value, or null if not set191 */192 @Nullable193 @Contract("_, !null -> !null")194 public String getEnvVarOrUserProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {195 return getConfigurable(propertyName, defaultValue, userProperties);196 }197 /**198 * Gets a configured setting from a the user's configuration file.199 * The configuration file will be the <code>.testcontainers.properties</code> file in the user's home directory.200 *201 * @param propertyName name of configuration file property (dot-separated lower case)202 * @return the found value, or null if not set203 */204 @Nullable205 @Contract("_, !null -> !null")206 public String getUserProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {207 return getConfigurable(propertyName, defaultValue);208 }209 /**210 * @return properties values available from user properties and classpath properties. Values set by environment211 * variable are NOT included.212 * @deprecated usages should be removed ASAP. See {@link TestcontainersConfiguration#getEnvVarOrProperty(String, String)},213 * {@link TestcontainersConfiguration#getEnvVarOrUserProperty(String, String)} or {@link TestcontainersConfiguration#getUserProperty(String, String)}214 * for suitable replacements.215 */216 @Deprecated217 public Properties getProperties() {218 return Stream.of(userProperties, classpathProperties)219 .reduce(new Properties(), (a, b) -> {220 a.putAll(b);221 return a;222 });223 }224 @Deprecated225 public boolean updateGlobalConfig(@NonNull String prop, @NonNull String value) {226 return updateUserConfig(prop, value);227 }228 @Synchronized229 public boolean updateUserConfig(@NonNull String prop, @NonNull String value) {230 try {231 if (value.equals(userProperties.get(prop))) {232 return false;233 }234 userProperties.setProperty(prop, value);235 USER_CONFIG_FILE.createNewFile();236 try (OutputStream outputStream = new FileOutputStream(USER_CONFIG_FILE)) {237 userProperties.store(outputStream, "Modified by Testcontainers");238 }239 // Update internal state only if environment config was successfully updated240 userProperties.setProperty(prop, value);241 return true;242 } catch (Exception e) {243 log.debug("Can't store environment property {} in {}", prop, USER_CONFIG_FILE);244 return false;245 }246 }247 @SneakyThrows(MalformedURLException.class)248 private static TestcontainersConfiguration loadConfiguration() {249 return new TestcontainersConfiguration(250 readProperties(USER_CONFIG_FILE.toURI().toURL()),251 ClasspathScanner.scanFor(PROPERTIES_FILE_NAME)252 .map(TestcontainersConfiguration::readProperties)253 .reduce(new Properties(), (a, b) -> {254 // first-write-wins merging - URLs appearing first on the classpath alphabetically will take priority.255 // Note that this means that file: URLs will always take priority over jar: URLs.256 b.putAll(a);257 return b;258 }),259 System.getenv());260 }261 private static Properties readProperties(URL url) {262 log.debug("Testcontainers configuration overrides will be loaded from {}", url);263 Properties properties = new Properties();264 try (InputStream inputStream = url.openStream()) {265 properties.load(inputStream);266 } catch (FileNotFoundException e) {267 log.warn("Attempted to read Testcontainers configuration file at {} but the file was not found. Exception message: {}", url, ExceptionUtils.getRootCauseMessage(e));268 } catch (IOException e) {269 log.warn("Attempted to read Testcontainers configuration file at {} but could it not be loaded. Exception message: {}", url, ExceptionUtils.getRootCauseMessage(e));270 }271 return properties;272 }273 private DockerImageName getImage(final String defaultValue) {274 return getConfiguredSubstituteImage(DockerImageName.parse(defaultValue));275 }276 DockerImageName getConfiguredSubstituteImage(DockerImageName original) {277 for (final Map.Entry<DockerImageName, String> entry : CONTAINER_MAPPING.entrySet()) {278 if (original.isCompatibleWith(entry.getKey())) {279 return280 Optional.ofNullable(entry.getValue())281 .map(propertyName -> getEnvVarOrProperty(propertyName, null))282 .map(String::valueOf)283 .map(String::trim)284 .map(DockerImageName::parse)285 .orElse(original)286 .asCompatibleSubstituteFor(original);287 }...
getImage
Using AI Code Generation
1import org.testcontainers.utility.TestcontainersConfiguration;2public class TestcontainersConfig {3 public static void main(String[] args) {4 System.out.println(TestcontainersConfiguration.getInstance().getDockerClient());5 System.out.println(TestcontainersConfiguration.getInstance().getDockerHostIpAddress());6 System.out.println(TestcontainersConfiguration.getInstance().getDockerServerIpAddress());7 System.out.println(TestcontainersConfiguration.getInstance().getDockerServer
getImage
Using AI Code Generation
1def imageName = TestcontainersConfiguration.getInstance().getImage("redis")2def imageNameAndTag = imageName.split(":")3def image = docker.image(imageNameAndTag[0]).withTag(imageNameAndTag[1])4image.pull()5image.pull()6def imagePullPolicy = TestcontainersConfiguration.getInstance().getImagePullPolicy()7def imagePullPolicyName = imagePullPolicy.name().toLowerCase()8image.pull(imagePullPolicyName)9def dockerClientStrategy = TestcontainersConfiguration.getInstance().getDockerClientStrategy()10def dockerClientStrategyName = dockerClientStrategy.name().toLowerCase()11image.pull(dockerClientStrategyName)12def dockerClient = TestcontainersConfiguration.getInstance().getDockerClient()13image.pull(dockerClient)14def dockerClient = TestcontainersConfiguration.getInstance().getDockerClient()15image.pull(dockerClient)16def dockerClientConfig = TestcontainersConfiguration.getInstance().getDockerClientConfig()17image.pull(dockerClientConfig)
getImage
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer2import org.testcontainers.utility.TestcontainersConfiguration3def imageName = TestcontainersConfiguration.getInstance().getImage("alpine:3.10")4def container = new GenericContainer(imageName)5container.withCommand("echo", "Hello World")6container.start()7assert container.getLogs().contains("Hello World")8container.stop()9container.close()10import org.testcontainers.containers.GenericContainer11import org.testcontainers.utility.TestcontainersConfiguration12def imageName = TestcontainersConfiguration.getInstance().getImage("alpine:3.10")13def container = new GenericContainer(imageName)14container.withCommand("echo", "Hello World")15container.start()16assert container.getLogs().contains("Hello World")17container.stop()18container.close()19import org.testcontainers.containers.GenericContainer20import org.testcontainers.utility.TestcontainersConfiguration21val imageName = TestcontainersConfiguration.getInstance().getImage("alpine:3.10")22val container = GenericContainer(imageName)23container.withCommand("echo", "Hello World")24container.start()
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!!