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

[shenyu] branch master updated: beautify e2e log (#4047)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0e766ee9c beautify e2e log (#4047)
0e766ee9c is described below

commit 0e766ee9c94515254e92668e4ead74b4c5b07b35
Author: Luke.Z <10...@users.noreply.github.com>
AuthorDate: Sun Oct 9 10:32:20 2022 +0800

    beautify e2e log (#4047)
---
 .../e2e/testcase/common/function/HttpCheckers.java |  33 ++++---
 .../testcase/common/function/WaitForHelper.java    |   7 ++
 .../shenyu-e2e-case/src/test/resources/log4j2.xml  |   2 +-
 .../shenyu/e2e/engine/ShenYuLogExtension.java      |  57 ++++++++++++
 .../shenyu/e2e/engine/annotation/ShenYuTest.java   |   3 +-
 .../e2e/engine/config/ShenYuEngineConfigure.java   |   8 +-
 .../ShenYuScenarioInvocationContextProvider.java   |   3 +-
 .../specification/ScenarioSpecLogProxy.java        | 101 +++++++++++++++++++++
 8 files changed, 198 insertions(+), 16 deletions(-)

diff --git a/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/HttpCheckers.java b/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/HttpCheckers.java
index 136f1e0f6..cbcd4fa32 100644
--- a/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/HttpCheckers.java
+++ b/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/HttpCheckers.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.e2e.testcase.common.function;
 
 import io.restassured.http.Method;
+import org.junit.jupiter.api.Assertions;
 
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.lessThan;
@@ -32,12 +33,16 @@ public class HttpCheckers {
     
     public static HttpChecker notExists(Method method, String endpoint) {
         return (request) -> {
-            request.request(method, endpoint)
-                    .then()
-                    .log()
-                    .ifValidationFails()
-                    .body("code", lessThan(0))
-                    .body("message", containsString("please check your configuration!"));
+            try {
+                request.request(method, endpoint)
+                        .then()
+                        .log()
+                        .ifValidationFails()
+                        .body("code", lessThan(0))
+                        .body("message", containsString("please check your configuration!"));
+            } catch (AssertionError error) {
+                Assertions.fail("endpoint '" + endpoint + "' already exists, but expected it does not exist.", error);
+            }
         };
     }
     
@@ -47,12 +52,16 @@ public class HttpCheckers {
     
     public static HttpChecker exists(Method method, String endpoint) {
         return (request) -> {
-            request.request(method, endpoint)
-                    .then()
-                    .log()
-                    .ifValidationFails()
-                    .body("code", nullValue())
-                    .body("message", not(containsString("please check your configuration!")));
+            try {
+                request.request(method, endpoint)
+                        .then()
+                        .log()
+                        .ifValidationFails()
+                        .body("code", nullValue())
+                        .body("message", not(containsString("please check your configuration!")));
+            } catch (AssertionError error) {
+                Assertions.fail("endpoint '" + endpoint + "' not exists", error);
+            }
         };
     }
     
diff --git a/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/WaitForHelper.java b/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/WaitForHelper.java
index de073838f..52c107a76 100644
--- a/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/WaitForHelper.java
+++ b/shenyu-e2e/shenyu-e2e-case/src/main/java/org/apache/shenyu/e2e/testcase/common/function/WaitForHelper.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Assertions;
 import org.slf4j.MDC;
 
 import java.time.Duration;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -51,7 +52,10 @@ public class WaitForHelper {
     public Duration timeout = Duration.ofMinutes(3);
     
     public void waitFor(Supplier<RequestSpecification> supplier, Method method, String endpoint, ResponseSpecification expected) throws TimeoutException {
+        final Map<String, String> contextMap = MDC.getCopyOfContextMap();
         Future<?> future = executor.submit(() -> {
+            MDC.setContextMap(contextMap);
+            
             for (int i = 0; i < retryTimes; i++) {
                 try {
                     ValidatableResponse response = supplier.get()
@@ -86,7 +90,10 @@ public class WaitForHelper {
     
     
     public void waitFor(Supplier<RequestSpecification> supplier, HttpChecker checker) throws TimeoutException {
+        final Map<String, String> contextMap = MDC.getCopyOfContextMap();
         Future<?> future = executor.submit(() -> {
+            MDC.setContextMap(contextMap);
+            
             for (int i = 0; i < retryTimes; i++) {
                 try {
                     checker.check(supplier);
diff --git a/shenyu-e2e/shenyu-e2e-case/src/test/resources/log4j2.xml b/shenyu-e2e/shenyu-e2e-case/src/test/resources/log4j2.xml
index 8aaccc555..9383716aa 100644
--- a/shenyu-e2e/shenyu-e2e-case/src/test/resources/log4j2.xml
+++ b/shenyu-e2e/shenyu-e2e-case/src/test/resources/log4j2.xml
@@ -18,7 +18,7 @@
 <Configuration status="INFO">
     <Appenders>
         <Console name="stdout" target="SYSTEM_OUT">
-            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
+            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %X{phase}/%X{operate} %m%n"/>
         </Console>
     </Appenders>
 
diff --git a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/ShenYuLogExtension.java b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/ShenYuLogExtension.java
new file mode 100644
index 000000000..b59d6f9a4
--- /dev/null
+++ b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/ShenYuLogExtension.java
@@ -0,0 +1,57 @@
+/*
+ * 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.e2e.engine;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.InvocationInterceptor;
+import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
+import org.slf4j.MDC;
+
+import java.lang.reflect.Method;
+
+public class ShenYuLogExtension implements InvocationInterceptor {
+    @Override
+    public void interceptBeforeAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
+        MDC.put("phase", "BeforeAll");
+        invocation.proceed();
+    }
+    
+    @Override
+    public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
+        MDC.put("phase", "BeforeAll");
+        invocation.proceed();
+    }
+    
+    @Override
+    public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
+        MDC.put("phase", "Verify");
+        invocation.proceed();
+    }
+    
+    @Override
+    public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
+        MDC.put("phase", "AfterEach");
+        invocation.proceed();
+    }
+    
+    @Override
+    public void interceptAfterAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
+        MDC.put("phase", "AfterAll");
+        invocation.proceed();
+    }
+}
diff --git a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/annotation/ShenYuTest.java b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/annotation/ShenYuTest.java
index aa786b0d3..2ba7124d8 100644
--- a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/annotation/ShenYuTest.java
+++ b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/annotation/ShenYuTest.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.e2e.engine.annotation;
 
 import org.apache.shenyu.e2e.engine.ShenYuExtension;
+import org.apache.shenyu.e2e.engine.ShenYuLogExtension;
 import org.apache.shenyu.e2e.engine.config.ShenYuEngineConfigure.Mode;
 import org.apache.shenyu.e2e.engine.config.ShenYuEngineConfigure.ServiceType;
 import org.junit.jupiter.api.TestInstance;
@@ -31,7 +32,7 @@ import java.lang.annotation.Target;
 
 @Target({ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
-@ExtendWith(ShenYuExtension.class)
+@ExtendWith({ShenYuExtension.class, ShenYuLogExtension.class})
 @TestInstance(Lifecycle.PER_CLASS)
 public @interface ShenYuTest {
     
diff --git a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/config/ShenYuEngineConfigure.java b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/config/ShenYuEngineConfigure.java
index 1d61e48c7..8ef0a7da4 100644
--- a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/config/ShenYuEngineConfigure.java
+++ b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/config/ShenYuEngineConfigure.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.e2e.engine.config;
 
+import com.fasterxml.jackson.annotation.JsonValue;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
@@ -59,7 +60,12 @@ public class ShenYuEngineConfigure {
     }
     
     public enum Mode {
-        HOST, DOCKER
+        HOST, DOCKER;
+        
+        @JsonValue
+        public String value() {
+            return name();
+        }
     }
     
     public enum ServiceType {
diff --git a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/ShenYuScenarioInvocationContextProvider.java b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/ShenYuScenarioInvocationContextProvider.java
index 7c38a5be9..4d6b33096 100644
--- a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/ShenYuScenarioInvocationContextProvider.java
+++ b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/ShenYuScenarioInvocationContextProvider.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.e2e.engine.scenario;
 
 import org.apache.shenyu.e2e.engine.annotation.ShenYuScenario;
 import org.apache.shenyu.e2e.engine.scenario.specification.ScenarioSpec;
+import org.apache.shenyu.e2e.engine.scenario.specification.ScenarioSpecLogProxy;
 import org.junit.jupiter.api.extension.Extension;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
@@ -69,7 +70,7 @@ public class ShenYuScenarioInvocationContextProvider implements TestTemplateInvo
         private final ScenarioSpec scenarioSpec;
         
         public ShenYuTestTemplateInvocationContext(ScenarioSpec scenarioSpec) {
-            this.scenarioSpec = scenarioSpec;
+            this.scenarioSpec = new ScenarioSpecLogProxy(scenarioSpec);
         }
         
         @Override
diff --git a/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/specification/ScenarioSpecLogProxy.java b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/specification/ScenarioSpecLogProxy.java
new file mode 100644
index 000000000..3ee95ca8b
--- /dev/null
+++ b/shenyu-e2e/shenyu-e2e-engine/src/main/java/org/apache/shenyu/e2e/engine/scenario/specification/ScenarioSpecLogProxy.java
@@ -0,0 +1,101 @@
+/*
+ * 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.e2e.engine.scenario.specification;
+
+import lombok.AllArgsConstructor;
+import org.apache.shenyu.e2e.client.admin.model.ResourcesData;
+import org.apache.shenyu.e2e.engine.scenario.function.Checker;
+import org.apache.shenyu.e2e.engine.scenario.function.Deleter;
+import org.apache.shenyu.e2e.engine.scenario.function.Verifier;
+import org.apache.shenyu.e2e.engine.scenario.function.Waiting;
+import org.slf4j.MDC;
+
+import java.util.List;
+
+@AllArgsConstructor
+public class ScenarioSpecLogProxy implements ScenarioSpec {
+    private final ScenarioSpec spec;
+    
+    @Override
+    public BeforeEachSpec getBeforeEachSpec() {
+        return new BeforeEachSpec() {
+            final BeforeEachSpec spec = ScenarioSpecLogProxy.this.spec.getBeforeEachSpec();
+            
+            @Override
+            public Checker getChecker() {
+                MDC.put("operate", "beforeCheck");
+                return spec.getChecker();
+            }
+            
+            @Override
+            public ResourcesData getResources() {
+                MDC.put("operate", "createSelectors");
+                return spec.getResources();
+            }
+            
+            @Override
+            public Waiting getWaiting() {
+                MDC.put("operate", "waitingFor");
+                return spec.getWaiting();
+            }
+        };
+    }
+    
+    @Override
+    public CaseSpec getCaseSpec() {
+        return new CaseSpec() {
+            final CaseSpec spec = ScenarioSpecLogProxy.this.spec.getCaseSpec();
+            
+            @Override
+            public String getName() {
+                return spec.getName();
+            }
+            
+            @Override
+            public List<Verifier> getVerifiers() {
+                MDC.put("operate", "verify");
+                return spec.getVerifiers();
+            }
+        };
+    }
+    
+    @Override
+    public AfterEachSpec getAfterEachSpec() {
+        return new AfterEachSpec() {
+            final AfterEachSpec spec = ScenarioSpecLogProxy.this.spec.getAfterEachSpec();
+            
+            @Override
+            public Deleter getDeleter() {
+                MDC.put("operate", "deleteResource");
+                return spec.getDeleter();
+            }
+            
+            @Override
+            public Checker getPostChecker() {
+                MDC.put("operate", "postCheck");
+                return spec.getPostChecker();
+            }
+        };
+    }
+    
+    @Override
+    public String getName() {
+        return spec.getName();
+    }
+    
+}