You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2021/02/17 16:14:49 UTC
[camel-kafka-connector] branch camel-master updated: Avoid using
static hostnames in tests
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch camel-master
in repository https://gitbox.apache.org/repos/asf/camel-kafka-connector.git
The following commit(s) were added to refs/heads/camel-master by this push:
new eb81f69 Avoid using static hostnames in tests
eb81f69 is described below
commit eb81f6996c3d7ac3c4d0d4d34dc48082f1c215dc
Author: Otavio Rodolfo Piske <op...@redhat.com>
AuthorDate: Wed Feb 17 16:39:28 2021 +0100
Avoid using static hostnames in tests
---
.../services/kafka/EmbeddedKafkaService.java | 5 +++-
.../DefaultKafkaConnectPropertyFactory.java | 10 +++++++-
.../kafkaconnector/common/utils/NetworkUtils.java | 27 +++++++++++++++++++++-
.../cxf/sink/CamelSinkCXFITCase.java | 8 +++----
.../http/sink/CamelSinkHTTPITCase.java | 2 +-
.../syslog/sink/CamelSinkSyslogITCase.java | 4 ++--
.../syslog/source/CamelSourceSyslogITCase.java | 4 ++--
7 files changed, 48 insertions(+), 12 deletions(-)
diff --git a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafka/EmbeddedKafkaService.java b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafka/EmbeddedKafkaService.java
index a7e50bd..d5d4dd2 100644
--- a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafka/EmbeddedKafkaService.java
+++ b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafka/EmbeddedKafkaService.java
@@ -23,6 +23,7 @@ import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.camel.kafkaconnector.common.PluginPathHelper;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
import org.apache.camel.test.infra.kafka.services.KafkaService;
import org.apache.kafka.connect.runtime.WorkerConfig;
import org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster;
@@ -50,8 +51,10 @@ public class EmbeddedKafkaService implements KafkaService {
Map<String, String> workerProps = new HashMap<>();
workerProps.put(WorkerConfig.OFFSET_COMMIT_INTERVAL_MS_CONFIG, String.valueOf(OFFSET_COMMIT_INTERVAL_MS));
- workerProps.put(WorkerConfig.LISTENERS_CONFIG, "http://localhost:9999");
+ String address = NetworkUtils.getAddress("http");
+ LOG.info("Using the following address for the listener configuration: {}", address);
+ workerProps.put(WorkerConfig.LISTENERS_CONFIG, address);
String pluginPaths = PluginPathHelper.getInstance().pluginPaths();
diff --git a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafkaconnect/DefaultKafkaConnectPropertyFactory.java b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafkaconnect/DefaultKafkaConnectPropertyFactory.java
index 39b9351..98a69cd 100644
--- a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafkaconnect/DefaultKafkaConnectPropertyFactory.java
+++ b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/services/kafkaconnect/DefaultKafkaConnectPropertyFactory.java
@@ -20,7 +20,10 @@ package org.apache.camel.kafkaconnector.common.services.kafkaconnect;
import java.util.Properties;
import org.apache.camel.kafkaconnector.common.PluginPathHelper;
+import org.apache.camel.kafkaconnector.common.utils.NetworkUtils;
import org.apache.kafka.connect.runtime.standalone.StandaloneConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
@@ -28,6 +31,8 @@ import org.apache.kafka.connect.runtime.standalone.StandaloneConfig;
* used for the standalone CLI connect runtime.
*/
class DefaultKafkaConnectPropertyFactory implements KafkaConnectPropertyFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(DefaultKafkaConnectPropertyFactory.class);
+
private final String bootstrapServer;
/**
@@ -48,7 +53,10 @@ class DefaultKafkaConnectPropertyFactory implements KafkaConnectPropertyFactory
props.put(StandaloneConfig.VALUE_CONVERTER_CLASS_CONFIG, "org.apache.kafka.connect.json.JsonConverter");
props.put(StandaloneConfig.OFFSET_STORAGE_FILE_FILENAME_CONFIG, this.getClass().getResource("/").getPath() + "connect.offsets");
props.put(StandaloneConfig.OFFSET_COMMIT_INTERVAL_MS_CONFIG, "10000");
- props.put(StandaloneConfig.LISTENERS_CONFIG, "http://localhost:9999");
+
+ String address = NetworkUtils.getAddress("http");
+ LOG.info("Using the following address for the listener configuration: {}", address);
+ props.put(StandaloneConfig.LISTENERS_CONFIG, address);
String pluginPaths = PluginPathHelper.getInstance().pluginPaths();
props.put(StandaloneConfig.PLUGIN_PATH_CONFIG, pluginPaths);
diff --git a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/utils/NetworkUtils.java b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/utils/NetworkUtils.java
index 79f9c60..80e8ca5 100644
--- a/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/utils/NetworkUtils.java
+++ b/tests/itests-common/src/test/java/org/apache/camel/kafkaconnector/common/utils/NetworkUtils.java
@@ -19,6 +19,7 @@ package org.apache.camel.kafkaconnector.common.utils;
import java.io.IOException;
import java.net.ConnectException;
import java.net.DatagramSocket;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
@@ -30,6 +31,7 @@ import org.slf4j.LoggerFactory;
public final class NetworkUtils {
public static final int DEFAULT_STARTING_PORT = 49152;
public static final int DEFAULT_ENDING_PORT = 65535;
+ private static String hostname;
private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class);
@@ -38,7 +40,7 @@ public final class NetworkUtils {
}
public static int getFreePort() {
- return getFreePort("localhost");
+ return getFreePort(getHostname());
}
public static int getFreePort(String host) {
@@ -109,4 +111,27 @@ public final class NetworkUtils {
UDP,
TCP
}
+
+ public static String getHostname() {
+ if (hostname == null) {
+ try {
+ hostname = InetAddress.getLocalHost().getCanonicalHostName();
+ } catch (UnknownHostException e) {
+ LOG.warn("Will default to 'localhost' because the code could not get the local hostname: {}",
+ e.getMessage(), e);
+
+ hostname = "localhost";
+ }
+ }
+
+ return hostname;
+ }
+
+ public static String getAddress(String protocol) {
+ return String.format("%s://%s:%d", protocol, NetworkUtils.getHostname(), NetworkUtils.getFreePort());
+ }
+
+ public static String getAddress(String protocol, int port) {
+ return String.format("%s://%s:%d", protocol, NetworkUtils.getHostname(), port);
+ }
}
diff --git a/tests/itests-cxf/src/test/java/org/apache/camel/kafkaconnector/cxf/sink/CamelSinkCXFITCase.java b/tests/itests-cxf/src/test/java/org/apache/camel/kafkaconnector/cxf/sink/CamelSinkCXFITCase.java
index 6220924..830f804 100644
--- a/tests/itests-cxf/src/test/java/org/apache/camel/kafkaconnector/cxf/sink/CamelSinkCXFITCase.java
+++ b/tests/itests-cxf/src/test/java/org/apache/camel/kafkaconnector/cxf/sink/CamelSinkCXFITCase.java
@@ -63,8 +63,8 @@ public class CamelSinkCXFITCase extends AbstractKafkaTest {
protected Server server;
protected EndpointImpl endpoint;
- private final int simplePort = NetworkUtils.getFreePort("localhost");
- private final int jaxwsPort = NetworkUtils.getFreePort("localhost");
+ private final int simplePort = NetworkUtils.getFreePort();
+ private final int jaxwsPort = NetworkUtils.getFreePort();
private final int expect = 10;
@@ -74,11 +74,11 @@ public class CamelSinkCXFITCase extends AbstractKafkaTest {
}
protected String getSimpleServerAddress() {
- return "http://localhost:" + simplePort + "/" + getClass().getSimpleName() + "/simpletest";
+ return "http://" + NetworkUtils.getHostname() + ":" + simplePort + "/" + getClass().getSimpleName() + "/simpletest";
}
protected String getJaxWsServerAddress() {
- return "http://localhost:" + jaxwsPort + "/" + getClass().getSimpleName() + "/jaxwstest";
+ return "http://" + NetworkUtils.getHostname() + ":" + jaxwsPort + "/" + getClass().getSimpleName() + "/jaxwstest";
}
@BeforeEach
diff --git a/tests/itests-http/src/test/java/org/apache/camel/kafkaconnector/http/sink/CamelSinkHTTPITCase.java b/tests/itests-http/src/test/java/org/apache/camel/kafkaconnector/http/sink/CamelSinkHTTPITCase.java
index 33bd066..3e2d611 100644
--- a/tests/itests-http/src/test/java/org/apache/camel/kafkaconnector/http/sink/CamelSinkHTTPITCase.java
+++ b/tests/itests-http/src/test/java/org/apache/camel/kafkaconnector/http/sink/CamelSinkHTTPITCase.java
@@ -44,7 +44,7 @@ import static org.junit.jupiter.api.Assertions.fail;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CamelSinkHTTPITCase extends CamelSinkTestSupport {
private static final Logger LOG = LoggerFactory.getLogger(CamelSinkHTTPITCase.class);
- private static final int HTTP_PORT = NetworkUtils.getFreePort("localhost");
+ private static final int HTTP_PORT = NetworkUtils.getFreePort();
private HttpServer localServer;
diff --git a/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/sink/CamelSinkSyslogITCase.java b/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/sink/CamelSinkSyslogITCase.java
index ab04b7d..b5bc282 100644
--- a/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/sink/CamelSinkSyslogITCase.java
+++ b/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/sink/CamelSinkSyslogITCase.java
@@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.fail;
* messages
*/
public class CamelSinkSyslogITCase extends CamelSinkTestSupport {
- private static final String HOST = "localhost";
+ private static final String HOST = NetworkUtils.getHostname();
private static final String PROTOCOL = "udp";
private static final int FREE_PORT = NetworkUtils.getFreePort(HOST, NetworkUtils.Protocol.UDP);
private static final String TEST_TXT = "<13>1 2020-05-14T14:47:01.198+02:00 nathannever myapp - - [timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"11266\"] FOO BAR!";
@@ -103,7 +103,7 @@ public class CamelSinkSyslogITCase extends CamelSinkTestSupport {
ConnectorPropertyFactory connectorPropertyFactory = CamelSyslogPropertyFactory
.basic()
.withTopics(topicName)
- .withHost("localhost")
+ .withHost(HOST)
.withPort(FREE_PORT)
.withProtocol("udp");
diff --git a/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/source/CamelSourceSyslogITCase.java b/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/source/CamelSourceSyslogITCase.java
index 8dacc0b..e6c63f9 100644
--- a/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/source/CamelSourceSyslogITCase.java
+++ b/tests/itests-syslog/src/test/java/org/apache/camel/kafkaconnector/syslog/source/CamelSourceSyslogITCase.java
@@ -43,12 +43,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
@EnabledIfSystemProperty(named = "enable.flaky.tests", matches = "true",
disabledReason = "Quickly spawning multiple Jetty Servers doesn't work well on Github Actions")
public class CamelSourceSyslogITCase extends CamelSourceTestSupport {
- private static final String HOST = "localhost";
+ private static final String HOST = NetworkUtils.getHostname();
private static final String PROTOCOL = "udp";
private static final int FREE_PORT = NetworkUtils.getFreePort(HOST, NetworkUtils.Protocol.UDP);
@RegisterExtension
- public static SyslogService service = SyslogService.sourceSyslogServiceFactory(PROTOCOL, "localhost", FREE_PORT);
+ public static SyslogService service = SyslogService.sourceSyslogServiceFactory(PROTOCOL, HOST, FREE_PORT);
private final int expect = 1;
private String topicName;