From ade105cf3b61d0bac403cdb0bf6158408b8381d6 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 20 Apr 2020 17:18:28 +0800 Subject: [PATCH] log --- .../java/io/metersphere/engine/Engine.java | 4 ++++ .../engine/docker/DockerTestEngine.java | 17 ++++++++++++++++ .../kubernetes/KubernetesTestEngine.java | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/backend/src/main/java/io/metersphere/engine/Engine.java b/backend/src/main/java/io/metersphere/engine/Engine.java index 4a616e8a00..a7bb071a31 100644 --- a/backend/src/main/java/io/metersphere/engine/Engine.java +++ b/backend/src/main/java/io/metersphere/engine/Engine.java @@ -1,5 +1,7 @@ package io.metersphere.engine; +import java.util.Map; + public interface Engine { Long getStartTime(); @@ -8,4 +10,6 @@ public interface Engine { void start(); void stop(); + + Map log(); } diff --git a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java index 1c208ffc49..a898fb7027 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -15,7 +15,9 @@ import io.metersphere.engine.docker.request.TestRequest; import io.metersphere.i18n.Translator; import org.springframework.web.client.RestTemplate; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class DockerTestEngine extends AbstractEngine { @@ -98,7 +100,22 @@ public class DockerTestEngine extends AbstractEngine { String uri = String.format(BASE_URL + "/jmeter/container/stop/" + testId, ip, port); restTemplate.postForObject(uri, request, String.class); }); + } + @Override + public Map log() { + String testId = loadTest.getId(); + Map logs = new HashMap<>(); + BaseRequest request = new BaseRequest(); + this.resourceList.forEach(r -> { + NodeDTO node = JSON.parseObject(r.getConfiguration(), NodeDTO.class); + String ip = node.getIp(); + Integer port = node.getPort(); + String uri = String.format(BASE_URL + "/jmeter/container/log/" + testId, ip, port); + String log = restTemplate.postForObject(uri, request, String.class); + logs.put(node.getIp(), log); + }); + return logs; } } diff --git a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java index e1e8a3f68d..7f74c6dfab 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -19,6 +19,7 @@ import io.metersphere.i18n.Translator; import org.apache.commons.collections.MapUtils; import java.util.HashMap; +import java.util.Map; public class KubernetesTestEngine extends AbstractEngine { @@ -122,4 +123,23 @@ public class KubernetesTestEngine extends AbstractEngine { }); } + + @Override + public Map log() { + Map logs = new HashMap<>(); + resourceList.forEach(r -> { + try { + String configuration = r.getConfiguration(); + ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class); + KubernetesProvider provider = new KubernetesProvider(JSON.toJSONString(clientCredential)); + provider.confirmNamespace(loadTest.getProjectId()); + String joblog = provider.getKubernetesClient().batch().jobs().inNamespace(loadTest.getProjectId()).withName("job-" + loadTest.getId()).getLog(); + logs.put(clientCredential.getMasterUrl(), joblog); + } catch (Exception e) { + MSException.throwException(e); + } + + }); + return logs; + } }