Skip to content

Commit 1e65069

Browse files
committed
Merge pull request #25446 from dreis2211
* gh-25446: Split DeploymentIntegrationTests by container Rename DeploymentIntegrationTests Closes gh-25446
2 parents 04e1ad6 + 4ff9e5e commit 1e65069

File tree

7 files changed

+300
-154
lines changed

7 files changed

+300
-154
lines changed

spring-boot-tests/spring-boot-deployment-tests/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
2424
intTestImplementation("org.apache.httpcomponents:httpasyncclient")
2525
intTestImplementation("org.awaitility:awaitility")
26+
intTestImplementation("org.testcontainers:junit-jupiter")
2627
intTestImplementation("org.testcontainers:testcontainers")
2728
intTestImplementation("org.springframework:spring-web")
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import java.io.File;
20+
import java.time.Duration;
21+
import java.util.function.Consumer;
22+
23+
import org.apache.http.impl.client.HttpClients;
24+
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
25+
import org.awaitility.Awaitility;
26+
import org.awaitility.core.ConditionTimeoutException;
27+
import org.junit.jupiter.api.Test;
28+
import org.testcontainers.containers.GenericContainer;
29+
import org.testcontainers.images.builder.ImageFromDockerfile;
30+
31+
import org.springframework.boot.test.web.client.TestRestTemplate;
32+
import org.springframework.boot.web.client.RestTemplateBuilder;
33+
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.ResponseEntity;
35+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Abstract class for deployment integration tests.
41+
*/
42+
abstract class AbstractDeploymentIntegrationTests {
43+
44+
protected static final int DEFAULT_PORT = 8080;
45+
46+
@Test
47+
void home() {
48+
getDeployedApplication().test((rest) -> {
49+
ResponseEntity<String> response = rest.getForEntity("/", String.class);
50+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
51+
assertThat(response.getBody()).isEqualTo("Hello World");
52+
});
53+
}
54+
55+
@Test
56+
void health() {
57+
getDeployedApplication().test((rest) -> {
58+
ResponseEntity<String> response = rest.getForEntity("/actuator/health", String.class);
59+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
60+
assertThat(response.getBody()).isEqualTo("{\"status\":\"UP\"}");
61+
});
62+
}
63+
64+
@Test
65+
void conditionalOnWarShouldBeTrue() {
66+
getDeployedApplication().test((rest) -> {
67+
ResponseEntity<String> response = rest.getForEntity("/actuator/war", String.class);
68+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
69+
assertThat(response.getBody()).isEqualTo("{\"hello\":\"world\"}");
70+
});
71+
}
72+
73+
private DeployedApplication getDeployedApplication() {
74+
return new DeployedApplication(getContainer(), getPort());
75+
}
76+
77+
protected int getPort() {
78+
return DEFAULT_PORT;
79+
}
80+
81+
abstract WarDeploymentContainer getContainer();
82+
83+
static final class DeployedApplication {
84+
85+
private final WarDeploymentContainer container;
86+
87+
private final int port;
88+
89+
DeployedApplication(WarDeploymentContainer container, int port) {
90+
this.container = container;
91+
this.port = port;
92+
}
93+
94+
private void test(Consumer<TestRestTemplate> consumer) {
95+
TestRestTemplate rest = new TestRestTemplate(new RestTemplateBuilder()
96+
.rootUri("http://" + this.container.getHost() + ":" + this.container.getMappedPort(this.port)
97+
+ "/spring-boot")
98+
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClients.custom()
99+
.setRetryHandler(new StandardHttpRequestRetryHandler(10, false)).build())));
100+
try {
101+
Awaitility.await().atMost(Duration.ofMinutes(10)).until(() -> {
102+
try {
103+
consumer.accept(rest);
104+
return true;
105+
}
106+
catch (Throwable ex) {
107+
return false;
108+
}
109+
});
110+
}
111+
catch (ConditionTimeoutException ex) {
112+
System.out.println(this.container.getLogs());
113+
throw ex;
114+
}
115+
}
116+
117+
}
118+
119+
static final class WarDeploymentContainer extends GenericContainer<WarDeploymentContainer> {
120+
121+
WarDeploymentContainer(String baseImage, String deploymentLocation, int port) {
122+
super(new ImageFromDockerfile().withFileFromFile("spring-boot.war", findWarToDeploy())
123+
.withDockerfileFromBuilder((builder) -> builder.from(baseImage)
124+
.add("spring-boot.war", deploymentLocation + "/spring-boot.war").build()));
125+
withExposedPorts(port).withStartupTimeout(Duration.ofMinutes(5)).withStartupAttempts(3);
126+
}
127+
128+
private static File findWarToDeploy() {
129+
File[] candidates = new File("build/libs").listFiles();
130+
assertThat(candidates).hasSize(1);
131+
return candidates[0];
132+
}
133+
134+
}
135+
136+
}

spring-boot-tests/spring-boot-deployment-tests/src/intTest/java/sample/DeploymentIntegrationTests.java

Lines changed: 0 additions & 154 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.testcontainers.junit.jupiter.Container;
20+
import org.testcontainers.junit.jupiter.Testcontainers;
21+
22+
/**
23+
* Deployment integration tests for Open Liberty.
24+
*
25+
* @author Christoph Dreis
26+
*/
27+
@Testcontainers(disabledWithoutDocker = true)
28+
class OpenLibertyDeploymentIntegrationTests extends AbstractDeploymentIntegrationTests {
29+
30+
private static final int PORT = 9080;
31+
32+
@Container
33+
static WarDeploymentContainer container = new WarDeploymentContainer(
34+
"openliberty/open-liberty:20.0.0.9-kernel-java8-openj9-ubi", "/config/dropins", PORT);
35+
36+
@Override
37+
WarDeploymentContainer getContainer() {
38+
return container;
39+
}
40+
41+
@Override
42+
protected int getPort() {
43+
return PORT;
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.testcontainers.junit.jupiter.Container;
20+
import org.testcontainers.junit.jupiter.Testcontainers;
21+
22+
/**
23+
* Deployment integration tests for TomEE.
24+
*
25+
* @author Christoph Dreis
26+
*/
27+
@Testcontainers(disabledWithoutDocker = true)
28+
public class TomEEDeploymentIntegrationTests extends AbstractDeploymentIntegrationTests {
29+
30+
@Container
31+
static WarDeploymentContainer container = new WarDeploymentContainer("tomee:8-jre-8.0.2-webprofile",
32+
"/usr/local/tomee/webapps", DEFAULT_PORT);
33+
34+
@Override
35+
WarDeploymentContainer getContainer() {
36+
return container;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)