diff --git a/elastest-torm/pom.xml b/elastest-torm/pom.xml index 2adb7e213..dbb6e71f6 100644 --- a/elastest-torm/pom.xml +++ b/elastest-torm/pom.xml @@ -237,6 +237,12 @@ pojo-tester 0.7.5 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.9.3 + diff --git a/elastest-torm/src/main/java/io/elastest/etm/service/TJobExecOrchestratorService.java b/elastest-torm/src/main/java/io/elastest/etm/service/TJobExecOrchestratorService.java index 6c00604b4..e12164a9c 100644 --- a/elastest-torm/src/main/java/io/elastest/etm/service/TJobExecOrchestratorService.java +++ b/elastest-torm/src/main/java/io/elastest/etm/service/TJobExecOrchestratorService.java @@ -1,6 +1,7 @@ package io.elastest.etm.service; import java.io.IOException; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -18,8 +19,11 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestClientException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.elastest.epm.client.json.DockerComposeCreateProject; import io.elastest.epm.client.json.DockerContainerInfo.DockerContainer; @@ -470,11 +474,9 @@ public void startSutByDockerCompose(DockerExecution dockerExec) String containerPrefix = composeProjectName.replaceAll("_", ""); // TMP replace sut exec and logstash sut tcp - String dockerComposeYml = sut.getSpecification() - .replaceAll("ETSUTREPLACE", composeProjectName) - .replaceAll("LOGSTASHSUTTCP", "tcp://" - + dockerService.getLogstashHost(dockerExec) + ":5001"); - + String dockerComposeYml = sut.getSpecification(); + dockerComposeYml = setLoggingToDockerComposeYml(dockerComposeYml, + composeProjectName, dockerExec); // Environment variables (optional) ArrayList envList = new ArrayList<>(); String envVar; @@ -530,6 +532,55 @@ public void startSutByDockerCompose(DockerExecution dockerExec) } + public String setLoggingToDockerComposeYml(String dockerComposeYml, + String composeProjectName, DockerExecution dockerExec) + throws Exception { + YAMLFactory yf = new YAMLFactory(); + ObjectMapper mapper = new ObjectMapper(yf); + Object object; + try { + object = mapper.readValue(dockerComposeYml, Object.class); + + Map>> dockerComposeMap = (HashMap) object; + Map> servicesMap = dockerComposeMap + .get("services"); + for (HashMap.Entry> service : servicesMap + .entrySet()) { + + HashMap serviceContent = service.getValue(); + String loggingKey = "logging"; + if (serviceContent.containsKey(loggingKey)) { + serviceContent.remove(loggingKey); + } + HashMap loggingContent = new HashMap(); + loggingContent.put("driver", "syslog"); + + HashMap loggingOptionsContent = new HashMap(); + loggingOptionsContent.put("syslog-address", "tcp://" + + dockerService.getLogstashHost(dockerExec) + ":5001"); + loggingOptionsContent.put("tag", + composeProjectName + "_" + service.getKey() + "_exec"); + + loggingContent.put("options", loggingOptionsContent); + + serviceContent.put(loggingKey, loggingContent); + } + + StringWriter writer = new StringWriter(); + + yf.createGenerator(writer).writeObject(object); + dockerComposeYml = writer.toString(); + + writer.close(); + } catch (Exception e) { + logger.error(e.getMessage()); + throw new Exception( + "Error on setting logging into docker compose yml"); + } + + return dockerComposeYml; + } + public void endSutExec(DockerExecution dockerExec) throws Exception { SutSpecification sut = dockerExec.gettJobexec().getTjob().getSut();