Skip to content

Commit

Permalink
set logging automatically into docker-compose sut
Browse files Browse the repository at this point in the history
  • Loading branch information
EduJGURJC committed Dec 19, 2017
1 parent ad6dab2 commit 57b1f2d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
6 changes: 6 additions & 0 deletions elastest-torm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@
<artifactId>pojo-tester</artifactId>
<version>0.7.5</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> envList = new ArrayList<>();
String envVar;
Expand Down Expand Up @@ -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<String, HashMap<String, HashMap<String, HashMap>>> dockerComposeMap = (HashMap) object;
Map<String, HashMap<String, HashMap>> servicesMap = dockerComposeMap
.get("services");
for (HashMap.Entry<String, HashMap<String, HashMap>> service : servicesMap
.entrySet()) {

HashMap<String, HashMap> serviceContent = service.getValue();
String loggingKey = "logging";
if (serviceContent.containsKey(loggingKey)) {
serviceContent.remove(loggingKey);
}
HashMap<String, Object> loggingContent = new HashMap<String, Object>();
loggingContent.put("driver", "syslog");

HashMap<String, Object> loggingOptionsContent = new HashMap<String, Object>();
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();

Expand Down

0 comments on commit 57b1f2d

Please sign in to comment.