Procházet zdrojové kódy

feat: api test流水线handler

370774330@qq.com před 5 roky
rodič
revize
acad6fe4ee

+ 5 - 0
core/pom.xml

@@ -27,6 +27,11 @@
             <artifactId>seecoder-devcloud-api</artifactId>
             <version>0.1.0</version>
         </dependency>
+        <dependency>
+            <groupId>cn.seecoder</groupId>
+            <artifactId>seecoder-devcloud-web</artifactId>
+            <version>0.1.0</version>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-configuration-processor</artifactId>

+ 1 - 1
core/src/main/java/cn/seecoder/core/pipeline/PipelineException.java

@@ -19,7 +19,7 @@ public class PipelineException extends Exception{
     public static final String NAMESPACE_CREATE_ERROR = "K8S namespace 创建错误";
     public static final String CONFIG_JSON_ERROR = "获取模板的config json错误";
     public static final String DOCKERFILE_MODIFY_ERROR = "修改模板的dockerfile错误";
-    public static final String POM_READ_ERROR = "获取pom中的jar name错误";
+    public static final String API_TEST_ID_RESOLVE_ERROR = "解析Api test id列表错误";
 
 
 

+ 3 - 0
core/src/main/java/cn/seecoder/core/pipeline/config/HandlerConfigTable.java

@@ -35,6 +35,7 @@ public class HandlerConfigTable {
         handlerConfigMap.put("k8s-service", K8sServiceHandler.class);
         handlerConfigMap.put("k8s-ingress", K8sIngressHandler.class);
         handlerConfigMap.put("k8s-namespace", K8sNamespaceHandler.class);
+        handlerConfigMap.put("api-test", ApiTestHandler.class);
     }
 
     public static Handler transToHandler(String name, JsonNode config) throws Exception {
@@ -54,6 +55,8 @@ public class HandlerConfigTable {
             Field field ;
             field = handler.getClass().getDeclaredField(propertyName);
             field.setAccessible(true);
+            //注意,这种写法无法自动注入List<xx>类,因为泛型信息由于java的机制被删除了
+            // 所以无法 List<xx> 的字段,所以最好还是传入String,在对应的handler自己解析
             field.set(handler, mapper.readValue(propertyValues.traverse(), field.getType()));
         }
         return handler;

+ 63 - 0
core/src/main/java/cn/seecoder/core/pipeline/handler/ApiTestHandler.java

@@ -0,0 +1,63 @@
+package cn.seecoder.core.pipeline.handler;
+
+import cn.seecoder.common.util.SpringUtil;
+import cn.seecoder.core.pipeline.Context;
+import cn.seecoder.core.pipeline.PipelineException;
+import cn.seecoder.web.service.APITest.APITestService;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpStatus;
+
+import java.util.List;
+
+import static cn.seecoder.core.pipeline.PipelineException.API_TEST_ID_RESOLVE_ERROR;
+
+/**
+ * @author PuHong Weng
+ * @date 2021/4/21
+ * @description:
+ * devcloud 构建时自动调取设定好的api测试执行测试
+ */
+@Slf4j
+@Data
+@EqualsAndHashCode(callSuper = false)
+public class ApiTestHandler extends AbstractHandler {
+
+    private final APITestService apiTestService;
+
+    public ApiTestHandler() {
+        this.apiTestService = SpringUtil.getBean(APITestService.class);
+    }
+
+
+    /**
+     * 要自动执行的apiTest的json
+     * 格式 [1,2,3]
+     */
+    private String apiTestId;
+
+
+
+    @Override
+    public void process(Context context) throws PipelineException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        List<Integer> apiTestIds;
+        try {
+            apiTestIds = objectMapper.readValue(apiTestId, new TypeReference<List<Integer>>() {
+            });
+        } catch (JsonProcessingException e) {
+            throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,API_TEST_ID_RESOLVE_ERROR,e);
+        }
+        for (Integer testId : apiTestIds){
+            try {
+                apiTestService.executeAPITest(testId);
+            } catch (InterruptedException e) {
+                log.warn("api test 出现 InterruptedException, 目前暂无特殊处理。 Test id: {}",testId);
+            }
+        }
+    }
+}