Commit 05c2919f0d570ff60a4f84237d0b25e8efaa6e02

Authored by Igor Kulikov
Committed by GitHub
2 parents 7ca2b3db 6abd21da

Merge pull request #5052 from smatvienko-tb/dependency-upgrade-test-scope-testcontainers

[3.3.1] Testcontainers - dependency upgrade
@@ -63,6 +63,21 @@ @@ -63,6 +63,21 @@
63 <scope>test</scope> 63 <scope>test</scope>
64 </dependency> 64 </dependency>
65 <dependency> 65 <dependency>
  66 + <groupId>org.springframework.boot</groupId>
  67 + <artifactId>spring-boot-starter-test</artifactId>
  68 + <scope>test</scope>
  69 + </dependency>
  70 + <dependency>
  71 + <groupId>org.junit.vintage</groupId>
  72 + <artifactId>junit-vintage-engine</artifactId>
  73 + <scope>test</scope>
  74 + </dependency>
  75 + <dependency>
  76 + <groupId>org.awaitility</groupId>
  77 + <artifactId>awaitility</artifactId>
  78 + <scope>test</scope>
  79 + </dependency>
  80 + <dependency>
66 <groupId>ch.qos.logback</groupId> 81 <groupId>ch.qos.logback</groupId>
67 <artifactId>logback-classic</artifactId> 82 <artifactId>logback-classic</artifactId>
68 </dependency> 83 </dependency>
@@ -16,26 +16,34 @@ @@ -16,26 +16,34 @@
16 package org.thingsboard.server.msa; 16 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.junit.ClassRule; 20 import org.junit.ClassRule;
20 import org.junit.extensions.cpsuite.ClasspathSuite; 21 import org.junit.extensions.cpsuite.ClasspathSuite;
21 -import org.junit.rules.ExternalResource;  
22 import org.junit.runner.RunWith; 22 import org.junit.runner.RunWith;
23 import org.testcontainers.containers.DockerComposeContainer; 23 import org.testcontainers.containers.DockerComposeContainer;
24 import org.testcontainers.containers.wait.strategy.Wait; 24 import org.testcontainers.containers.wait.strategy.Wait;
25 -import org.testcontainers.utility.Base58;  
26 25
27 import java.io.File; 26 import java.io.File;
  27 +import java.io.IOException;
  28 +import java.nio.charset.StandardCharsets;
28 import java.time.Duration; 29 import java.time.Duration;
29 -import java.util.Arrays;  
30 -import java.util.HashMap;  
31 -import java.util.List;  
32 -import java.util.Map; 30 +import java.util.UUID;
  31 +
  32 +import static org.hamcrest.CoreMatchers.containsString;
  33 +import static org.hamcrest.CoreMatchers.is;
  34 +import static org.hamcrest.CoreMatchers.not;
  35 +import static org.hamcrest.MatcherAssert.assertThat;
  36 +import static org.junit.Assert.fail;
33 37
34 @RunWith(ClasspathSuite.class) 38 @RunWith(ClasspathSuite.class)
35 @ClasspathSuite.ClassnameFilters({"org.thingsboard.server.msa.*Test"}) 39 @ClasspathSuite.ClassnameFilters({"org.thingsboard.server.msa.*Test"})
36 @Slf4j 40 @Slf4j
37 public class ContainerTestSuite { 41 public class ContainerTestSuite {
38 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 +
39 private static DockerComposeContainer<?> testContainer; 47 private static DockerComposeContainer<?> testContainer;
40 48
41 @ClassRule 49 @ClassRule
@@ -46,31 +54,78 @@ public class ContainerTestSuite { @@ -46,31 +54,78 @@ public class ContainerTestSuite {
46 if (testContainer == null) { 54 if (testContainer == null) {
47 boolean skipTailChildContainers = Boolean.valueOf(System.getProperty("blackBoxTests.skipTailChildContainers")); 55 boolean skipTailChildContainers = Boolean.valueOf(System.getProperty("blackBoxTests.skipTailChildContainers"));
48 try { 56 try {
49 - String tbCoreLogRegexp = ".*Starting polling for events.*";  
50 - 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 + }
51 66
52 - testContainer = new DockerComposeContainer<>(  
53 - new File("./../../docker/docker-compose.yml"),  
54 - new File("./../../docker/docker-compose.postgres.yml"),  
55 - new File("./../../docker/docker-compose.postgres.volumes.yml"),  
56 - new File("./../../docker/docker-compose.kafka.yml")) 67 + @Override
  68 + public void stop() {
  69 + super.stop();
  70 + tryDeleteDir(targetDir);
  71 + }
  72 + }
  73 +
  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"))
57 .withPull(false) 79 .withPull(false)
58 .withLocalCompose(true) 80 .withLocalCompose(true)
59 .withTailChildContainers(!skipTailChildContainers) 81 .withTailChildContainers(!skipTailChildContainers)
60 .withEnv(installTb.getEnv()) 82 .withEnv(installTb.getEnv())
61 .withEnv("LOAD_BALANCER_NAME", "") 83 .withEnv("LOAD_BALANCER_NAME", "")
62 .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)))
63 - .waitingFor("tb-core1", Wait.forLogMessage(tbCoreLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
64 - .waitingFor("tb-core2", Wait.forLogMessage(tbCoreLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
65 - .waitingFor("tb-http-transport1", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
66 - .waitingFor("tb-http-transport2", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
67 - .waitingFor("tb-mqtt-transport1", Wait.forLogMessage(transportsLogRegexp, 1).withStartupTimeout(Duration.ofSeconds(400)))  
68 - .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)));
69 } catch (Exception e) { 91 } catch (Exception e) {
70 log.error("Failed to create test container", e); 92 log.error("Failed to create test container", e);
71 - throw e; 93 + fail("Failed to create test container");
72 } 94 }
73 } 95 }
74 return testContainer; 96 return testContainer;
75 } 97 }
  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 +
  108 + /**
  109 + * This workaround is actual until issue will be resolved:
  110 + * Support container_name in docker-compose file #2472 https://github.com/testcontainers/testcontainers-java/issues/2472
  111 + * docker-compose files which contain container_name are not supported and the creation of DockerComposeContainer fails due to IllegalStateException.
  112 + * This has been introduced in #1151 as a quick fix for unintuitive feedback. https://github.com/testcontainers/testcontainers-java/issues/1151
  113 + * Using the latest testcontainers and waiting for the fix...
  114 + * */
  115 + private static void replaceInFile(String sourceFilename, String target, String replacement, String verifyPhrase) {
  116 + try {
  117 + File file = new File(sourceFilename);
  118 + String sourceContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
  119 +
  120 + String outputContent = sourceContent.replace(target, replacement);
  121 + assertThat(outputContent, (not(containsString(target))));
  122 + assertThat(outputContent, (not(containsString(verifyPhrase))));
  123 +
  124 + FileUtils.writeStringToFile(file, outputContent, StandardCharsets.UTF_8);
  125 + assertThat(FileUtils.readFileToString(file, StandardCharsets.UTF_8), is(outputContent));
  126 + } catch (IOException e) {
  127 + log.error("failed to update file " + sourceFilename, e);
  128 + fail("failed to update file");
  129 + }
  130 + }
76 } 131 }
@@ -123,8 +123,8 @@ @@ -123,8 +123,8 @@
123 <spring-test-dbunit.version>1.3.0</spring-test-dbunit.version> <!-- 2016 --> 123 <spring-test-dbunit.version>1.3.0</spring-test-dbunit.version> <!-- 2016 -->
124 <takari-cpsuite.version>1.2.7</takari-cpsuite.version> <!-- 2015 --> 124 <takari-cpsuite.version>1.2.7</takari-cpsuite.version> <!-- 2015 -->
125 <!-- BLACKBOX TEST SCOPE --> 125 <!-- BLACKBOX TEST SCOPE -->
126 - <testcontainers.version>1.11.4</testcontainers.version>  
127 - <zeroturnaround.version>1.10</zeroturnaround.version> 126 + <testcontainers.version>1.16.0</testcontainers.version>
  127 + <zeroturnaround.version>1.12</zeroturnaround.version>
128 </properties> 128 </properties>
129 129
130 <modules> 130 <modules>
@@ -1736,6 +1736,12 @@ @@ -1736,6 +1736,12 @@
1736 <artifactId>testcontainers</artifactId> 1736 <artifactId>testcontainers</artifactId>
1737 <version>${testcontainers.version}</version> 1737 <version>${testcontainers.version}</version>
1738 <scope>test</scope> 1738 <scope>test</scope>
  1739 + <exclusions>
  1740 + <exclusion>
  1741 + <groupId>junit</groupId>
  1742 + <artifactId>junit</artifactId>
  1743 + </exclusion>
  1744 + </exclusions>
1739 </dependency> 1745 </dependency>
1740 <dependency> 1746 <dependency>
1741 <groupId>org.zeroturnaround</groupId> 1747 <groupId>org.zeroturnaround</groupId>