Skip to content

Commit

Permalink
DBZ-7711 Adjust fakeDNS startup conditional for Max OS
Browse files Browse the repository at this point in the history
  • Loading branch information
obabec authored and jcechace committed Apr 11, 2024
1 parent 8039211 commit 7448543
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ private MongoDbContainer(Builder builder) {
this.authEnabled = builder.authEnabled;
this.configAddress = builder.configAddress;

if (DockerUtils.isDockerDesktop()) {
if (DockerUtils.isContainerVM()) {
this.port = portResolver.resolveFreePort();
addFixedExposedPort(port, port);
}
else {
this.port = builder.port;
}

DockerUtils.logDockerDesktopBanner(LOGGER, List.of(name), builder.skipDockerDesktopLogWarning);
DockerUtils.logContainerVMBanner(LOGGER, List.of(name), builder.skipDockerDesktopLogWarning);

withNetwork(builder.network);
withNetworkAliases(name);
Expand Down Expand Up @@ -225,7 +225,7 @@ public Address getClientAddress() {
checkStarted();

// Technically we only need to do this for Mac
if (DockerUtils.isDockerDesktop()) {
if (DockerUtils.isContainerVM()) {
return getNamedAddress();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package io.debezium.testing.testcontainers;

import static io.debezium.testing.testcontainers.util.DockerUtils.logDockerDesktopBanner;
import static io.debezium.testing.testcontainers.util.DockerUtils.logContainerVMBanner;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -174,7 +174,7 @@ private MongoDbReplicaSet(Builder builder) {
.build());
}

logDockerDesktopBanner(LOGGER, getHostNames(), builder.skipDockerDesktopLogWarning);
logContainerVMBanner(LOGGER, getHostNames(), builder.skipDockerDesktopLogWarning);
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import static io.debezium.testing.testcontainers.MongoDbContainer.router;
import static io.debezium.testing.testcontainers.MongoDbReplicaSet.configServerReplicaSet;
import static io.debezium.testing.testcontainers.MongoDbReplicaSet.replicaSet;
import static io.debezium.testing.testcontainers.util.DockerUtils.logDockerDesktopBanner;
import static io.debezium.testing.testcontainers.util.DockerUtils.logContainerVMBanner;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -119,7 +118,7 @@ private MongoDbShardedCluster(Builder builder) {
this.configServers = createConfigServers();
this.routers = createRouters();

logDockerDesktopBanner(LOGGER, getHostNames(), builder.skipDockerDesktopLogWarning);
logContainerVMBanner(LOGGER, getHostNames(), builder.skipDockerDesktopLogWarning);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.testcontainers.DockerClientFactory;

public class DockerUtils {

public static final String DOCKER_DESKTOP_LOG_SKIP_PROPERTY = "docker.desktop.log.skip";
public static final String DOCKER_DESKTOP_DISABLE_FAKE_DNS = "docker.desktop.disable.fake.dns";
public static final String CONTAINER_VM_LOG_SKIP = "container.vm.log.skip";
public static final String CONTAINER_VM_FAKE_DNS = "container.vm.fake.dns";

public static boolean isDockerDesktop() {
var info = DockerClientFactory.instance().getInfo();
return "docker-desktop".equals(info.getName());
/**
* Checks if system is configured to deploy fake DNS.
*/
public static boolean isContainerVM() {
String property = System.getProperty(CONTAINER_VM_FAKE_DNS, "auto");
return property.equals("true") || (property.equals("auto") && isMac());
}

/**
* Check if running OS is Mac.
*/
public static boolean isMac() {
return System.getProperty("os.name").contains("Mac");
}

/**
Expand All @@ -29,19 +38,19 @@ public static boolean isDockerDesktop() {
* <ul>
* <li>Containers are not running under Docker Desktop</li>
* <li>Skip parameter is {@code true}</li>
* <li>{@link #DOCKER_DESKTOP_LOG_SKIP_PROPERTY} property is {@code true}</li>
* <li>{@link #CONTAINER_VM_LOG_SKIP} property is {@code true}</li>
* <li>{@link FakeDns} is installed within JVM</li>
* </ul>
*
* @param logger logger used to print the banner
* @param hosts list of container hostnames
* @param skip if true the operation is skipped
*/
public static void logDockerDesktopBanner(Logger logger, Collection<String> hosts, boolean skip) {
var prop = System.getProperty(DOCKER_DESKTOP_LOG_SKIP_PROPERTY, "false");
public static void logContainerVMBanner(Logger logger, Collection<String> hosts, boolean skip) {
var prop = System.getProperty(CONTAINER_VM_LOG_SKIP, "false");
var propertySkip = Boolean.parseBoolean(prop);

if (propertySkip || skip || !isDockerDesktop() || FakeDns.getInstance().isInstalled()) {
if (propertySkip || skip || !isContainerVM() || FakeDns.getInstance().isInstalled()) {
return;
}

Expand All @@ -53,7 +62,7 @@ public static void logDockerDesktopBanner(Logger logger, Collection<String> host
.collect(Collectors.joining(newLine));

var banner = new StringBuilder()
.append("\n>>> DOCKER DESKTOP DETECTED <<<")
.append("\n>>> VM BASED CONTAINER RUNTIME DETECTED <<<")
.append(doubleNewLine)
.append("Requires the following entries in /etc/hosts or equivalent of your platform")
.append(doubleNewLine)
Expand All @@ -68,9 +77,7 @@ public static void logDockerDesktopBanner(Logger logger, Collection<String> host
* Enables {@link FakeDns} on docker desktop (if
*/
public static void enableFakeDnsIfRequired() {
var property = System.getProperty(DOCKER_DESKTOP_DISABLE_FAKE_DNS, "false");

if (isDockerDesktop() && !Boolean.parseBoolean(property)) {
if (isContainerVM()) {
FakeDns.getInstance().install();
}
}
Expand Down

0 comments on commit 7448543

Please sign in to comment.