diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java
index a607f3a13f2..2ff5cc6ea38 100644
--- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java
+++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java
@@ -383,6 +383,8 @@ private String constructDatabaseUrl(JdbcProtocol jdbcProtocol, String host, Stri
case "oracle" -> "jdbc:oracle:thin:@" + host + ":" + port
+ "/" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase());
case "dm" -> "jdbc:dm://" + host + ":" + port;
+ case "testcontainers" -> "jdbc:tc:" + host + ":" + port
+ + ":///" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + "?user=root&password=root";
default -> throw new IllegalArgumentException("Not support database platform: " + jdbcProtocol.getPlatform());
};
}
diff --git a/hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml
index 79740608210..423ab0c21cb 100644
--- a/hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml
+++ b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml
@@ -31,6 +31,8 @@
17
17
UTF-8
+ 8.0.30
+ 42.5.5
@@ -53,10 +55,46 @@
${hertzbeat.version}
test
+
+
+
+ org.testcontainers
+ junit-jupiter
+ test
+
+
org.testcontainers
- testcontainers
+ jdbc
test
+
+
+ org.testcontainers
+ mysql
+ test
+
+
+ org.testcontainers
+ postgresql
+ test
+
+
+
+
+ mysql
+ mysql-connector-java
+ ${mysql.version}
+ test
+ true
+
+
+ org.postgresql
+ postgresql
+ ${postgresql.version}
+ test
+ true
+
+
-
\ No newline at end of file
+
diff --git a/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java
new file mode 100644
index 00000000000..03427d11f88
--- /dev/null
+++ b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/DatabaseImagesEnum.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.collector.collect.basic.database;
+
+/**
+ * Database type (image mame) and image tag enumeration class
+ */
+public enum DatabaseImagesEnum {
+ MYSQL("mysql", "8.0.36"),
+ POSTGRESQL("postgresql", "15");
+
+ private final String imageName;
+ private final String defaultTag;
+
+ DatabaseImagesEnum(String imageName, String defaultTag) {
+ this.imageName = imageName;
+ this.defaultTag = defaultTag;
+ }
+
+ public String getImageName() {
+ return imageName;
+ }
+
+ public String getDefaultTag() {
+ return defaultTag;
+ }
+
+ public String getFullImageName() {
+ return imageName + ":" + defaultTag;
+ }
+
+ public static DatabaseImagesEnum fromImageName(String imageName) {
+ for (DatabaseImagesEnum value : values()) {
+ if (value.getImageName().equalsIgnoreCase(imageName)) {
+ return value;
+ }
+ }
+ throw new IllegalArgumentException("Unknown database image name: " + imageName);
+ }
+
+ public static DatabaseImagesEnum fromFullImageName(String fullImageName) {
+ for (DatabaseImagesEnum value : values()) {
+ if (value.getFullImageName().equalsIgnoreCase(fullImageName)) {
+ return value;
+ }
+ }
+ throw new IllegalArgumentException("Unknown full database image name: " + fullImageName);
+ }
+
+ @Override
+ public String toString() {
+ return "DatabaseImageNameEnum{" +
+ "imageName='" + imageName + '\'' +
+ ", defaultTag='" + defaultTag + '\'' +
+ '}';
+ }
+}
diff --git a/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java
new file mode 100644
index 00000000000..69f8492f3cc
--- /dev/null
+++ b/hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.collector.collect.basic.database;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.collector.collect.AbstractCollectE2eTest;
+import org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect;
+import org.apache.hertzbeat.collector.util.CollectUtil;
+import org.apache.hertzbeat.common.entity.job.Configmap;
+import org.apache.hertzbeat.common.entity.job.Job;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.JdbcProtocol;
+import org.apache.hertzbeat.common.entity.job.protocol.Protocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+/**
+ * E2e test monitored by Jdbc Common
+ */
+@Slf4j
+@ExtendWith(MockitoExtension.class)
+public class JdbcCommonCollectE2eTest extends AbstractCollectE2eTest {
+ private static final DatabaseImagesEnum databaseImage = DatabaseImagesEnum.fromImageName("postgresql");
+
+ private static final String DATABASE_IMAGE_NAME = databaseImage.getImageName();
+
+ private static final String DATABASE_IMAGE_TAG = databaseImage.getDefaultTag();
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ super.setUp();
+ collect = new JdbcCommonCollect();
+ metrics = new Metrics();
+ }
+
+ @Override
+ protected CollectRep.MetricsData.Builder collectMetrics(Metrics metricsDef) {
+ JdbcProtocol jdbcProtocol = (JdbcProtocol) buildProtocol(metricsDef);
+ metrics.setJdbc(jdbcProtocol);
+ CollectRep.MetricsData.Builder metricsData = CollectRep.MetricsData.newBuilder();
+ metricsData.setApp(DATABASE_IMAGE_NAME);
+ metrics.setAliasFields(metricsDef.getAliasFields());
+ return collectMetricsData(metrics, metricsDef, metricsData);
+ }
+
+ @Override
+ protected Protocol buildProtocol(Metrics metricsDef) {
+ JdbcProtocol jdbcProtocol = metricsDef.getJdbc();
+ jdbcProtocol.setHost(DATABASE_IMAGE_NAME);
+ jdbcProtocol.setPort(DATABASE_IMAGE_TAG);
+ jdbcProtocol.setDatabase("test");
+ jdbcProtocol.setPlatform("testcontainers");
+ return jdbcProtocol;
+ }
+
+ @Test
+ public void testWithJdbcTcUrl() {
+ Job dockerJob = appService.getAppDefine(DATABASE_IMAGE_NAME);
+ List