You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by jo...@apache.org on 2022/06/27 10:29:15 UTC

[incubator-shenyu] branch master updated: add the integrated-test of logging-elasticsearch (#3624)

This is an automated email from the ASF dual-hosted git repository.

jooks pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new a228b0f23 add the integrated-test of logging-elasticsearch (#3624)
a228b0f23 is described below

commit a228b0f23a0fe15f22774f1dbfbbd2f1deb4361c
Author: qinghai777 <80...@users.noreply.github.com>
AuthorDate: Mon Jun 27 18:29:05 2022 +0800

    add the integrated-test of logging-elasticsearch (#3624)
---
 .../shenyu-integrated-test-http/docker-compose.yml |  18 ++++
 .../shenyu-integrated-test-http/pom.xml            |   8 ++
 .../script/services.list                           |   1 +
 .../LoggingElasticSearchPluginTest.java            | 107 +++++++++++++++++++++
 4 files changed, 134 insertions(+)

diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml b/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
index 22fdfb1d3..8f7b9912b 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
@@ -110,6 +110,24 @@ services:
         source: ./config/prometheus.yml
         target: /etc/prometheus/prometheus.yml
 
+  shenyu-elasticsearch:
+    container_name: shenyu-elasticsearch
+    image: elasticsearch:8.2.3
+    restart: always
+    networks:
+      - shenyu
+    ports:
+      - "9200:9200"
+      - "9300:9300"
+    environment:
+      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
+      - discovery.type=single-node
+      - xpack.security.enabled=false
+    healthcheck:
+      test: [ "CMD", "wget", "http://localhost:9200/_cat/health?v" ]
+      timeout: 2s
+      retries: 30
+
 networks:
   shenyu:
     name: shenyu
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
index bf6b19cab..bb12dca5a 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
@@ -170,6 +170,14 @@
         </dependency>
         <!--shenyu metrics plugin end-->
 
+        <!--shenyu logging-elasticsearch plugin start-->
+        <dependency>
+            <groupId>org.apache.shenyu</groupId>
+            <artifactId>shenyu-spring-boot-starter-plugin-logging-elasticsearch</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <!--shenyu logging-elasticsearch plugin end-->
+
         <dependency>
             <groupId>org.apache.shenyu</groupId>
             <artifactId>shenyu-integrated-test-common</artifactId>
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/script/services.list b/shenyu-integrated-test/shenyu-integrated-test-http/script/services.list
index e48dc7e03..5984d7eb0 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/script/services.list
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/script/services.list
@@ -17,3 +17,4 @@
 http://localhost:9095/actuator/health
 http://localhost:8189/test/path/123?name=tom
 http://localhost:9195/actuator/health
+http://localhost:9200/_cat/health?v
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/LoggingElasticSearchPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/LoggingElasticSearchPluginTest.java
new file mode 100644
index 000000000..76c3d516e
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/LoggingElasticSearchPluginTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.shenyu.integrated.test.http.combination;
+
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch.core.SearchResponse;
+import co.elastic.clients.json.jackson.JacksonJsonpMapper;
+import co.elastic.clients.transport.ElasticsearchTransport;
+import co.elastic.clients.transport.endpoints.BooleanResponse;
+import co.elastic.clients.transport.rest_client.RestClientTransport;
+import org.apache.http.HttpHost;
+import org.apache.shenyu.common.dto.ConditionData;
+import org.apache.shenyu.common.enums.OperatorEnum;
+import org.apache.shenyu.common.enums.ParamTypeEnum;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.apache.shenyu.plugin.logging.elasticsearch.entity.ShenyuRequestLog;
+import org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData;
+import org.elasticsearch.client.RestClient;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class LoggingElasticSearchPluginTest extends AbstractPluginDataInit {
+
+    private static final String TEST_LOGGINGELASTICSEARCH_PATH = "/http/post/hi";
+
+    private RestClient restClient;
+
+    private ElasticsearchTransport transport;
+
+    private ElasticsearchClient client;
+
+    @BeforeAll
+    public static void setup() throws IOException {
+        String pluginResult = initPlugin(PluginEnum.LOGGING_ElasticSearch.getName(), "{\"host\":\"shenyu-elasticsearch\", \"port\": \"9200\"}");
+        assertThat(pluginResult, is("success"));
+        String selectorAndRulesResult = initSelectorAndRules(PluginEnum.LOGGING_ElasticSearch.getName(),
+                "", buildSelectorConditionList(), buildRuleLocalDataList());
+        assertThat(selectorAndRulesResult, is("success"));
+    }
+
+    @Test
+    public void testElasticSearchPlugin() throws Exception {
+        restClient = RestClient
+                .builder(new HttpHost("localhost", 9200))
+                .build();
+        transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
+        client = new ElasticsearchClient(transport);
+        String result = HttpHelper.INSTANCE.postGateway(TEST_LOGGINGELASTICSEARCH_PATH, String.class);
+        assertNotNull(result);
+        Thread.sleep(3000);
+        BooleanResponse existsResponse = client.indices().exists(c -> c.index("shenyu-access-logging"));
+        assertThat(String.valueOf(existsResponse.value()), is("true"));
+        SearchResponse<ShenyuRequestLog> searchResponse = client.search(a -> a.index("shenyu-access-logging"), ShenyuRequestLog.class);
+        ShenyuRequestLog shenyuRequestLog = searchResponse.hits().hits()
+                .get(searchResponse.hits().hits().size() - 1).source();
+        assertThat(shenyuRequestLog.getMethod(), is("/http/post/hi"));
+    }
+
+    private static List<ConditionData> buildSelectorConditionList() {
+        ConditionData conditionData = new ConditionData();
+        conditionData.setParamType(ParamTypeEnum.URI.getName());
+        conditionData.setOperator(OperatorEnum.STARTS_WITH.getAlias());
+        conditionData.setParamValue("/http/post/hi");
+        return Collections.singletonList(conditionData);
+    }
+
+    private static List<RuleLocalData> buildRuleLocalDataList() {
+        final RuleLocalData ruleLocalData = new RuleLocalData();
+        ConditionData conditionData = new ConditionData();
+        conditionData.setParamType(ParamTypeEnum.URI.getName());
+        conditionData.setOperator(OperatorEnum.EQ.getAlias());
+        conditionData.setParamValue("/http/post/hi");
+        ruleLocalData.setConditionDataList(Collections.singletonList(conditionData));
+        return Collections.singletonList(ruleLocalData);
+    }
+
+    @AfterAll
+    public static void clean() throws IOException {
+        cleanPluginData(PluginEnum.LOGGING_ElasticSearch.getName());
+    }
+}