From 330f9def83f3ce611dd983d857c96d0e19c3fddf Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 18 Mar 2020 14:53:31 +0800 Subject: [PATCH] DockerTestEngine --- .../controller/request/TestRequest.java | 32 +++++++++++++++++ .../engine/docker/DockerTestEngine.java | 34 +++++++++++++++++++ .../metersphere/service/LoadTestService.java | 2 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/io/metersphere/controller/request/TestRequest.java diff --git a/backend/src/main/java/io/metersphere/controller/request/TestRequest.java b/backend/src/main/java/io/metersphere/controller/request/TestRequest.java new file mode 100644 index 0000000000..bb4cdcbbf0 --- /dev/null +++ b/backend/src/main/java/io/metersphere/controller/request/TestRequest.java @@ -0,0 +1,32 @@ +package io.metersphere.controller.request; + +public class TestRequest { + + int size; + String fileString; + String testId; + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public String getFileString() { + return fileString; + } + + public void setFileString(String fileString) { + this.fileString = fileString; + } + + public String getTestId() { + return testId; + } + + public void setTestId(String testId) { + this.testId = testId; + } +} 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 1665b9f1f0..6b1861b8bb 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -1,7 +1,14 @@ package io.metersphere.engine.docker; +import io.metersphere.commons.exception.MSException; +import io.metersphere.controller.request.TestRequest; import io.metersphere.engine.Engine; import io.metersphere.engine.EngineContext; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.List; public class DockerTestEngine implements Engine { private EngineContext context; @@ -15,11 +22,38 @@ public class DockerTestEngine implements Engine { @Override public void start() { + RestTemplate restTemplate = new RestTemplate(); + String testId = context.getTestId(); + String content = context.getContent(); + + String uri = "http://localhost:8082/jmeter/container/start"; + + TestRequest testRequest = new TestRequest(); + testRequest.setSize(1); + testRequest.setTestId(testId); + testRequest.setFileString(content); + + String taskStatusUri = "http://localhost:8082/jmeter/task/status/" + testId; + List containerList = restTemplate.getForObject(taskStatusUri, List.class); + for (int i = 0; i < containerList.size(); i++) { + HashMap h = (HashMap) containerList.get(i); + if (StringUtils.equals((String)h.get("State"), "running")) { + MSException.throwException("the test is running!"); + } + } + + restTemplate.postForObject(uri, testRequest, String.class); } @Override public void stop() { + RestTemplate restTemplate = new RestTemplate(); + + String testId = context.getTestId(); + + String uri = "http://localhost:8082/jmeter/container/stop" + testId; + restTemplate.getForObject(uri, String.class); } } diff --git a/backend/src/main/java/io/metersphere/service/LoadTestService.java b/backend/src/main/java/io/metersphere/service/LoadTestService.java index a31ec38747..eacc35d222 100644 --- a/backend/src/main/java/io/metersphere/service/LoadTestService.java +++ b/backend/src/main/java/io/metersphere/service/LoadTestService.java @@ -97,7 +97,7 @@ public class LoadTestService { fileMetadata.setSize(file.getSize()); fileMetadata.setCreateTime(System.currentTimeMillis()); fileMetadata.setUpdateTime(System.currentTimeMillis()); - fileMetadata.setType("jmx"); + fileMetadata.setType("JMX"); // TODO engine 选择 fileMetadata.setEngine(EngineType.DOCKER.name()); fileMetadataMapper.insert(fileMetadata);