Skip to content

Commit 02a6a84

Browse files
aivinog1wilkinsona
authored andcommitted
Test the launch script when executed directly
See gh-21388
1 parent b14bd45 commit 02a6a84

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+338
-148
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2012-2020 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 org.springframework.boot.launchscript;
18+
19+
import java.io.File;
20+
import java.time.Duration;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import org.assertj.core.api.Condition;
25+
import org.testcontainers.containers.GenericContainer;
26+
import org.testcontainers.containers.output.ToStringConsumer;
27+
import org.testcontainers.images.builder.ImageFromDockerfile;
28+
import org.testcontainers.utility.MountableFile;
29+
30+
import org.springframework.boot.ansi.AnsiColor;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.hamcrest.Matchers.containsString;
34+
35+
/**
36+
* Abstract base class for testing the launch script.
37+
*
38+
* @author Alexey Vinogradov
39+
*/
40+
abstract class AbstractLaunchScriptIT {
41+
42+
protected static final char ESC = 27;
43+
44+
static List<Object[]> parameters() {
45+
List<Object[]> parameters = new ArrayList<>();
46+
for (File os : new File("src/test/resources/conf").listFiles()) {
47+
for (File version : os.listFiles()) {
48+
parameters.add(new Object[] { os.getName(), version.getName() });
49+
}
50+
}
51+
return parameters;
52+
}
53+
54+
protected Condition<String> coloredString(AnsiColor color, String string) {
55+
String colorString = ESC + "[0;" + color + "m" + string + ESC + "[0m";
56+
return new Condition<String>() {
57+
58+
@Override
59+
public boolean matches(String value) {
60+
return containsString(colorString).matches(value);
61+
}
62+
63+
};
64+
}
65+
66+
protected void doLaunch(String os, String version, String script) throws Exception {
67+
assertThat(doTest(os, version, script)).contains("Launched");
68+
}
69+
70+
protected String doTest(String os, String version, String script) throws Exception {
71+
ToStringConsumer consumer = new ToStringConsumer().withRemoveAnsiCodes(false);
72+
try (LaunchScriptTestContainer container = new LaunchScriptTestContainer(os, version, script)) {
73+
container.withLogConsumer(consumer);
74+
container.start();
75+
while (container.isRunning()) {
76+
Thread.sleep(100);
77+
}
78+
}
79+
return consumer.toUtf8String();
80+
}
81+
82+
private static final class LaunchScriptTestContainer extends GenericContainer<LaunchScriptTestContainer> {
83+
84+
private LaunchScriptTestContainer(String os, String version, String testScript) {
85+
super(new ImageFromDockerfile("spring-boot-launch-script/" + os.toLowerCase() + "-" + version)
86+
.withFileFromFile("Dockerfile",
87+
new File("src/test/resources/conf/" + os + "/" + version + "/Dockerfile"))
88+
.withFileFromFile("spring-boot-launch-script-tests.jar", findApplication())
89+
.withFileFromFile("test-functions.sh", new File("src/test/resources/scripts/test-functions.sh"))
90+
.withFileFromFile("jar/test-functions.sh",
91+
new File("src/test/resources/scripts/jar/test-functions.sh"))
92+
.withFileFromFile("init.d/test-functions.sh",
93+
new File("src/test/resources/scripts/init.d/test-functions.sh")));
94+
withCopyFileToContainer(MountableFile.forHostPath("src/test/resources/scripts/" + testScript),
95+
"/" + testScript);
96+
withCommand("/bin/bash", "-c", "chmod +x " + testScript + " && ./" + testScript);
97+
withStartupTimeout(Duration.ofMinutes(10));
98+
}
99+
100+
private static File findApplication() {
101+
File targetDir = new File("target");
102+
for (File file : targetDir.listFiles()) {
103+
if (file.getName().startsWith("spring-boot-launch-script-tests") && file.getName().endsWith(".jar")
104+
&& !file.getName().endsWith("-sources.jar")) {
105+
return file;
106+
}
107+
}
108+
throw new IllegalStateException(
109+
"Could not find test application in target directory. Have you built it (mvn package)?");
110+
}
111+
112+
}
113+
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2020 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 org.springframework.boot.launchscript;
18+
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.MethodSource;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Integration tests of Spring Boot's launch script with launching via shell.
26+
*
27+
* @author Alexey Vinogradov
28+
*/
29+
class ShellLaunchScriptIT extends AbstractLaunchScriptIT {
30+
31+
@ParameterizedTest(name = "{0} {1}")
32+
@MethodSource("parameters")
33+
void basicLaunch(String os, String version) throws Exception {
34+
doLaunch(os, version, "jar/basic-launch.sh");
35+
}
36+
37+
@ParameterizedTest(name = "{0} {1}")
38+
@MethodSource("parameters")
39+
void launchWithDebugEnv(String os, String version) throws Exception {
40+
final String output = doTest(os, version, "jar/launch-with-debug.sh");
41+
assertThat(output).contains("++ pwd");
42+
}
43+
44+
@ParameterizedTest(name = "{0} {1}")
45+
@MethodSource("parameters")
46+
void launchWithDifferentJarFileEnv(String os, String version) throws Exception {
47+
final String output = doTest(os, version, "jar/launch-with-jarfile.sh");
48+
assertThat(output).contains("app-another.jar");
49+
assertThat(output).doesNotContain("spring-boot-launch-script-tests.jar");
50+
}
51+
52+
@ParameterizedTest(name = "{0} {1}")
53+
@MethodSource("parameters")
54+
void launchWithDifferentAppName(String os, String version) throws Exception {
55+
final String output = doTest(os, version, "jar/launch-with-app-name.sh");
56+
assertThat(output).contains("All tests are passed.");
57+
}
58+
59+
@ParameterizedTest(name = "{0} {1}")
60+
@MethodSource("parameters")
61+
void launchInInitdDir(String os, String version) throws Exception {
62+
final String output = doTest(os, version, "jar/launch-in-init.d-dir.sh");
63+
assertThat(output).contains("Usage: ./some_app {start|stop|force-stop|restart|force-reload|status|run}");
64+
}
65+
66+
}

0 commit comments

Comments
 (0)