Commit 6abd21da7c010257b2ea04962865cc4fea3dedd3

Authored by Sergey Matvienko
1 parent 8722d3f7

testcontainers - copy source dir to tmp, remove container_name in tmp, run, cleanup tmp on stop.

@@ -17,7 +17,6 @@ package org.thingsboard.server.msa; @@ -17,7 +17,6 @@ package org.thingsboard.server.msa;
17 17
18 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.io.FileUtils; 19 import org.apache.commons.io.FileUtils;
20 -import org.junit.Assert;  
21 import org.junit.ClassRule; 20 import org.junit.ClassRule;
22 import org.junit.extensions.cpsuite.ClasspathSuite; 21 import org.junit.extensions.cpsuite.ClasspathSuite;
23 import org.junit.runner.RunWith; 22 import org.junit.runner.RunWith;
@@ -27,20 +26,24 @@ import org.testcontainers.containers.wait.strategy.Wait; @@ -27,20 +26,24 @@ import org.testcontainers.containers.wait.strategy.Wait;
27 import java.io.File; 26 import java.io.File;
28 import java.io.IOException; 27 import java.io.IOException;
29 import java.nio.charset.StandardCharsets; 28 import java.nio.charset.StandardCharsets;
30 -import java.nio.file.Files;  
31 -import java.nio.file.Path;  
32 import java.time.Duration; 29 import java.time.Duration;
  30 +import java.util.UUID;
33 31
34 import static org.hamcrest.CoreMatchers.containsString; 32 import static org.hamcrest.CoreMatchers.containsString;
35 import static org.hamcrest.CoreMatchers.is; 33 import static org.hamcrest.CoreMatchers.is;
36 import static org.hamcrest.CoreMatchers.not; 34 import static org.hamcrest.CoreMatchers.not;
37 import static org.hamcrest.MatcherAssert.assertThat; 35 import static org.hamcrest.MatcherAssert.assertThat;
  36 +import static org.junit.Assert.fail;
38 37
39 @RunWith(ClasspathSuite.class) 38 @RunWith(ClasspathSuite.class)
40 @ClasspathSuite.ClassnameFilters({"org.thingsboard.server.msa.*Test"}) 39 @ClasspathSuite.ClassnameFilters({"org.thingsboard.server.msa.*Test"})
41 @Slf4j 40 @Slf4j
42 public class ContainerTestSuite { 41 public class ContainerTestSuite {
43 42
  43 + private static final String SOURCE_DIR = "./../../docker/";
  44 + private static final String TB_CORE_LOG_REGEXP = ".*Starting polling for events.*";
  45 + private static final String TRANSPORTS_LOG_REGEXP = ".*Going to recalculate partitions.*";
  46 +
44 private static DockerComposeContainer<?> testContainer; 47 private static DockerComposeContainer<?> testContainer;
45 48
46 @ClassRule 49 @ClassRule
@@ -51,34 +54,57 @@ public class ContainerTestSuite { @@ -51,34 +54,57 @@ public class ContainerTestSuite {
51 if (testContainer == null) { 54 if (testContainer == null) {
52 boolean skipTailChildContainers = Boolean.valueOf(System.getProperty("blackBoxTests.skipTailChildContainers")); 55 boolean skipTailChildContainers = Boolean.valueOf(System.getProperty("blackBoxTests.skipTailChildContainers"));
53 try { 56 try {
54 - String tbCoreLogRegexp = ".*Starting polling for events.*";  
55 - String transportsLogRegexp = ".*Going to recalculate partitions.*"; 57 + final String targetDir = FileUtils.getTempDirectoryPath() + "/" + "ContainerTestSuite-" + UUID.randomUUID() + "/";
  58 + log.info("targetDir {}", targetDir);
  59 + FileUtils.copyDirectory(new File(SOURCE_DIR), new File(targetDir));
  60 + replaceInFile(targetDir + "docker-compose.yml", " container_name: \"${LOAD_BALANCER_NAME}\"", "", "container_name");
  61 +
  62 + class DockerComposeContainerImpl<SELF extends DockerComposeContainer<SELF>> extends DockerComposeContainer<SELF> {
  63 + public DockerComposeContainerImpl(File... composeFiles) {
  64 + super(composeFiles);
  65 + }
  66 +
  67 + @Override
  68 + public void stop() {
  69 + super.stop();
  70 + tryDeleteDir(targetDir);
  71 + }
  72 + }
56 73
57 - testContainer = new DockerComposeContainer<>(  
58 - new File(removeContainerName("./../../docker/docker-compose.yml")),  
59 - new File("./../../docker/docker-compose.postgres.yml"),  
60 - new File("./../../docker/docker-compose.postgres.volumes.yml"),  
61 - new File("./../../docker/docker-compose.kafka.yml")) 74 + testContainer = new DockerComposeContainerImpl<>(
  75 + new File(targetDir + "docker-compose.yml"),
  76 + new File(targetDir + "docker-compose.postgres.yml"),
  77 + new File(targetDir + "docker-compose.postgres.volumes.yml"),
  78 + new File(targetDir + "docker-compose.kafka.yml"))
62 .withPull(false) 79 .withPull(false)
63 .withLocalCompose(true) 80 .withLocalCompose(true)
64 .withTailChildContainers(!skipTailChildContainers) 81 .withTailChildContainers(!skipTailChildContainers)
65 .withEnv(installTb.getEnv()) 82 .withEnv(installTb.getEnv())
66 .withEnv("LOAD_BALANCER_NAME", "") 83 .withEnv("LOAD_BALANCER_NAME", "")
67 .withExposedService("haproxy", 80, Wait.forHttp("/swagger-ui.html").withStartupTimeout(Duration.ofSeconds(400))) 84 .withExposedService("haproxy", 80, Wait.forHttp("/swagger-ui.html").withStartupTimeout(Duration.ofSeconds(400)))
68 - .waitingFor("tb-core1", Wait.forLogMessage(tbCoreLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
69 - .waitingFor("tb-core2", Wait.forLogMessage(tbCoreLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
70 - .waitingFor("tb-http-transport1", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
71 - .waitingFor("tb-http-transport2", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
72 - .waitingFor("tb-mqtt-transport1", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
73 - .waitingFor("tb-mqtt-transport2", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400))); 85 + .waitingFor("tb-core1", Wait.forLogMessage(TB_CORE_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))
  86 + .waitingFor("tb-core2", Wait.forLogMessage(TB_CORE_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))
  87 + .waitingFor("tb-http-transport1", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))
  88 + .waitingFor("tb-http-transport2", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))
  89 + .waitingFor("tb-mqtt-transport1", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))
  90 + .waitingFor("tb-mqtt-transport2", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)));
74 } catch (Exception e) { 91 } catch (Exception e) {
75 log.error("Failed to create test container", e); 92 log.error("Failed to create test container", e);
76 - throw e; 93 + fail("Failed to create test container");
77 } 94 }
78 } 95 }
79 return testContainer; 96 return testContainer;
80 } 97 }
81 98
  99 + private static void tryDeleteDir(String targetDir) {
  100 + try {
  101 + log.info("Trying to delete temp dir {}", targetDir);
  102 + FileUtils.deleteDirectory(new File(targetDir));
  103 + } catch (IOException e) {
  104 + log.error("Can't delete temp directory " + targetDir, e);
  105 + }
  106 + }
  107 +
82 /** 108 /**
83 * This workaround is actual until issue will be resolved: 109 * This workaround is actual until issue will be resolved:
84 * Support container_name in docker-compose file #2472 https://github.com/testcontainers/testcontainers-java/issues/2472 110 * Support container_name in docker-compose file #2472 https://github.com/testcontainers/testcontainers-java/issues/2472
@@ -86,23 +112,20 @@ public class ContainerTestSuite { @@ -86,23 +112,20 @@ public class ContainerTestSuite {
86 * This has been introduced in #1151 as a quick fix for unintuitive feedback. https://github.com/testcontainers/testcontainers-java/issues/1151 112 * This has been introduced in #1151 as a quick fix for unintuitive feedback. https://github.com/testcontainers/testcontainers-java/issues/1151
87 * Using the latest testcontainers and waiting for the fix... 113 * Using the latest testcontainers and waiting for the fix...
88 * */ 114 * */
89 - private static String removeContainerName(String sourceFilename) {  
90 - String outputFilename = null; 115 + private static void replaceInFile(String sourceFilename, String target, String replacement, String verifyPhrase) {
91 try { 116 try {
92 - String sourceContent = FileUtils.readFileToString(new File(sourceFilename), StandardCharsets.UTF_8);  
93 - String outputContent = sourceContent.replace("container_name: \"${LOAD_BALANCER_NAME}\"", "");  
94 - assertThat(outputContent, (not(containsString("container_name"))));  
95 -  
96 - Path tempFile = Files.createTempFile("docker-compose", ".yml"); // the file looks like /tmp/docker-compose713972234379430232.yml  
97 - log.info("tempFile is {}", tempFile.toFile().getAbsolutePath()); 117 + File file = new File(sourceFilename);
  118 + String sourceContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
98 119
99 - FileUtils.writeStringToFile(tempFile.toFile(), outputContent, StandardCharsets.UTF_8);  
100 - outputFilename = tempFile.toFile().getAbsolutePath();  
101 - assertThat(FileUtils.readFileToString(new File(outputFilename), StandardCharsets.UTF_8), is(outputContent)); 120 + String outputContent = sourceContent.replace(target, replacement);
  121 + assertThat(outputContent, (not(containsString(target))));
  122 + assertThat(outputContent, (not(containsString(verifyPhrase))));
102 123
  124 + FileUtils.writeStringToFile(file, outputContent, StandardCharsets.UTF_8);
  125 + assertThat(FileUtils.readFileToString(file, StandardCharsets.UTF_8), is(outputContent));
103 } catch (IOException e) { 126 } catch (IOException e) {
104 - Assert.fail("failed to create tmp file " + e.getMessage()); 127 + log.error("failed to update file " + sourceFilename, e);
  128 + fail("failed to update file");
105 } 129 }
106 - return outputFilename;  
107 } 130 }
108 } 131 }