Test the launch script when executed directly

See gh-21388
pull/23053/head
Alexey Vinogradov 5 years ago committed by Andy Wilkinson
parent b14bd45399
commit 02a6a84499

@ -0,0 +1,114 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.launchscript;
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.api.Condition;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.utility.MountableFile;
import org.springframework.boot.ansi.AnsiColor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* Abstract base class for testing the launch script.
*
* @author Alexey Vinogradov
*/
abstract class AbstractLaunchScriptIT {
protected static final char ESC = 27;
static List<Object[]> parameters() {
List<Object[]> parameters = new ArrayList<>();
for (File os : new File("src/test/resources/conf").listFiles()) {
for (File version : os.listFiles()) {
parameters.add(new Object[] { os.getName(), version.getName() });
}
}
return parameters;
}
protected Condition<String> coloredString(AnsiColor color, String string) {
String colorString = ESC + "[0;" + color + "m" + string + ESC + "[0m";
return new Condition<String>() {
@Override
public boolean matches(String value) {
return containsString(colorString).matches(value);
}
};
}
protected void doLaunch(String os, String version, String script) throws Exception {
assertThat(doTest(os, version, script)).contains("Launched");
}
protected String doTest(String os, String version, String script) throws Exception {
ToStringConsumer consumer = new ToStringConsumer().withRemoveAnsiCodes(false);
try (LaunchScriptTestContainer container = new LaunchScriptTestContainer(os, version, script)) {
container.withLogConsumer(consumer);
container.start();
while (container.isRunning()) {
Thread.sleep(100);
}
}
return consumer.toUtf8String();
}
private static final class LaunchScriptTestContainer extends GenericContainer<LaunchScriptTestContainer> {
private LaunchScriptTestContainer(String os, String version, String testScript) {
super(new ImageFromDockerfile("spring-boot-launch-script/" + os.toLowerCase() + "-" + version)
.withFileFromFile("Dockerfile",
new File("src/test/resources/conf/" + os + "/" + version + "/Dockerfile"))
.withFileFromFile("spring-boot-launch-script-tests.jar", findApplication())
.withFileFromFile("test-functions.sh", new File("src/test/resources/scripts/test-functions.sh"))
.withFileFromFile("jar/test-functions.sh",
new File("src/test/resources/scripts/jar/test-functions.sh"))
.withFileFromFile("init.d/test-functions.sh",
new File("src/test/resources/scripts/init.d/test-functions.sh")));
withCopyFileToContainer(MountableFile.forHostPath("src/test/resources/scripts/" + testScript),
"/" + testScript);
withCommand("/bin/bash", "-c", "chmod +x " + testScript + " && ./" + testScript);
withStartupTimeout(Duration.ofMinutes(10));
}
private static File findApplication() {
File targetDir = new File("target");
for (File file : targetDir.listFiles()) {
if (file.getName().startsWith("spring-boot-launch-script-tests") && file.getName().endsWith(".jar")
&& !file.getName().endsWith("-sources.jar")) {
return file;
}
}
throw new IllegalStateException(
"Could not find test application in target directory. Have you built it (mvn package)?");
}
}
}

@ -0,0 +1,66 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.launchscript;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests of Spring Boot's launch script with launching via shell.
*
* @author Alexey Vinogradov
*/
class ShellLaunchScriptIT extends AbstractLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters")
void basicLaunch(String os, String version) throws Exception {
doLaunch(os, version, "jar/basic-launch.sh");
}
@ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters")
void launchWithDebugEnv(String os, String version) throws Exception {
final String output = doTest(os, version, "jar/launch-with-debug.sh");
assertThat(output).contains("++ pwd");
}
@ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters")
void launchWithDifferentJarFileEnv(String os, String version) throws Exception {
final String output = doTest(os, version, "jar/launch-with-jarfile.sh");
assertThat(output).contains("app-another.jar");
assertThat(output).doesNotContain("spring-boot-launch-script-tests.jar");
}
@ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters")
void launchWithDifferentAppName(String os, String version) throws Exception {
final String output = doTest(os, version, "jar/launch-with-app-name.sh");
assertThat(output).contains("All tests are passed.");
}
@ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters")
void launchInInitdDir(String os, String version) throws Exception {
final String output = doTest(os, version, "jar/launch-in-init.d-dir.sh");
assertThat(output).contains("Usage: ./some_app {start|stop|force-stop|restart|force-reload|status|run}");
}
}

@ -16,25 +16,15 @@
package org.springframework.boot.launchscript; package org.springframework.boot.launchscript;
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.utility.MountableFile;
import org.springframework.boot.ansi.AnsiColor; import org.springframework.boot.ansi.AnsiColor;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
/** /**
* Integration tests for Spring Boot's launch script on OSs that use SysVinit. * Integration tests for Spring Boot's launch script on OSs that use SysVinit.
@ -42,14 +32,12 @@ import static org.hamcrest.Matchers.containsString;
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Ali Shahbour * @author Ali Shahbour
*/ */
class SysVinitLaunchScriptIT { class SysVinitLaunchScriptIT extends AbstractLaunchScriptIT {
private static final char ESC = 27;
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void statusWhenStopped(String os, String version) throws Exception { void statusWhenStopped(String os, String version) throws Exception {
String output = doTest(os, version, "status-when-stopped.sh"); String output = doTest(os, version, "init.d/status-when-stopped.sh");
assertThat(output).contains("Status: 3"); assertThat(output).contains("Status: 3");
assertThat(output).has(coloredString(AnsiColor.RED, "Not running")); assertThat(output).has(coloredString(AnsiColor.RED, "Not running"));
} }
@ -57,7 +45,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void statusWhenStarted(String os, String version) throws Exception { void statusWhenStarted(String os, String version) throws Exception {
String output = doTest(os, version, "status-when-started.sh"); String output = doTest(os, version, "init.d/status-when-started.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]"));
} }
@ -65,7 +53,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void statusWhenKilled(String os, String version) throws Exception { void statusWhenKilled(String os, String version) throws Exception {
String output = doTest(os, version, "status-when-killed.sh"); String output = doTest(os, version, "init.d/status-when-killed.sh");
assertThat(output).contains("Status: 1"); assertThat(output).contains("Status: 1");
assertThat(output) assertThat(output)
.has(coloredString(AnsiColor.RED, "Not running (process " + extractPid(output) + " not found)")); .has(coloredString(AnsiColor.RED, "Not running (process " + extractPid(output) + " not found)"));
@ -74,7 +62,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void stopWhenStopped(String os, String version) throws Exception { void stopWhenStopped(String os, String version) throws Exception {
String output = doTest(os, version, "stop-when-stopped.sh"); String output = doTest(os, version, "init.d/stop-when-stopped.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)")); assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)"));
} }
@ -82,7 +70,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void forceStopWhenStopped(String os, String version) throws Exception { void forceStopWhenStopped(String os, String version) throws Exception {
String output = doTest(os, version, "force-stop-when-stopped.sh"); String output = doTest(os, version, "init.d/force-stop-when-stopped.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)")); assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)"));
} }
@ -90,7 +78,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void startWhenStarted(String os, String version) throws Exception { void startWhenStarted(String os, String version) throws Exception {
String output = doTest(os, version, "start-when-started.sh"); String output = doTest(os, version, "init.d/start-when-started.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.YELLOW, "Already running [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.YELLOW, "Already running [" + extractPid(output) + "]"));
} }
@ -98,7 +86,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void restartWhenStopped(String os, String version) throws Exception { void restartWhenStopped(String os, String version) throws Exception {
String output = doTest(os, version, "restart-when-stopped.sh"); String output = doTest(os, version, "init.d/restart-when-stopped.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)")); assertThat(output).has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)"));
assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]"));
@ -107,7 +95,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void restartWhenStarted(String os, String version) throws Exception { void restartWhenStarted(String os, String version) throws Exception {
String output = doTest(os, version, "restart-when-started.sh"); String output = doTest(os, version, "init.d/restart-when-started.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extract("PID1", output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extract("PID1", output) + "]"));
assertThat(output).has(coloredString(AnsiColor.GREEN, "Stopped [" + extract("PID1", output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Stopped [" + extract("PID1", output) + "]"));
@ -117,7 +105,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void startWhenStopped(String os, String version) throws Exception { void startWhenStopped(String os, String version) throws Exception {
String output = doTest(os, version, "start-when-stopped.sh"); String output = doTest(os, version, "init.d/start-when-stopped.sh");
assertThat(output).contains("Status: 0"); assertThat(output).contains("Status: 0");
assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]"));
} }
@ -125,14 +113,14 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void basicLaunch(String os, String version) throws Exception { void basicLaunch(String os, String version) throws Exception {
String output = doTest(os, version, "basic-launch.sh"); String output = doTest(os, version, "init.d/basic-launch.sh");
assertThat(output).doesNotContain("PID_FOLDER"); assertThat(output).doesNotContain("PID_FOLDER");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithMissingLogFolderGeneratesAWarning(String os, String version) throws Exception { void launchWithMissingLogFolderGeneratesAWarning(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-missing-log-folder.sh"); String output = doTest(os, version, "init.d/launch-with-missing-log-folder.sh");
assertThat(output).has( assertThat(output).has(
coloredString(AnsiColor.YELLOW, "LOG_FOLDER /does/not/exist does not exist. Falling back to /tmp")); coloredString(AnsiColor.YELLOW, "LOG_FOLDER /does/not/exist does not exist. Falling back to /tmp"));
} }
@ -140,7 +128,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithMissingPidFolderGeneratesAWarning(String os, String version) throws Exception { void launchWithMissingPidFolderGeneratesAWarning(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-missing-pid-folder.sh"); String output = doTest(os, version, "init.d/launch-with-missing-pid-folder.sh");
assertThat(output).has( assertThat(output).has(
coloredString(AnsiColor.YELLOW, "PID_FOLDER /does/not/exist does not exist. Falling back to /tmp")); coloredString(AnsiColor.YELLOW, "PID_FOLDER /does/not/exist does not exist. Falling back to /tmp"));
} }
@ -148,43 +136,43 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithSingleCommandLineArgument(String os, String version) throws Exception { void launchWithSingleCommandLineArgument(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-single-command-line-argument.sh"); doLaunch(os, version, "init.d/launch-with-single-command-line-argument.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithMultipleCommandLineArguments(String os, String version) throws Exception { void launchWithMultipleCommandLineArguments(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-multiple-command-line-arguments.sh"); doLaunch(os, version, "init.d/launch-with-multiple-command-line-arguments.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithSingleRunArg(String os, String version) throws Exception { void launchWithSingleRunArg(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-single-run-arg.sh"); doLaunch(os, version, "init.d/launch-with-single-run-arg.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithMultipleRunArgs(String os, String version) throws Exception { void launchWithMultipleRunArgs(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-multiple-run-args.sh"); doLaunch(os, version, "init.d/launch-with-multiple-run-args.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithSingleJavaOpt(String os, String version) throws Exception { void launchWithSingleJavaOpt(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-single-java-opt.sh"); doLaunch(os, version, "init.d/launch-with-single-java-opt.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithDoubleLinkSingleJavaOpt(String os, String version) throws Exception { void launchWithDoubleLinkSingleJavaOpt(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-double-link-single-java-opt.sh"); doLaunch(os, version, "init.d/launch-with-double-link-single-java-opt.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithMultipleJavaOpts(String os, String version) throws Exception { void launchWithMultipleJavaOpts(String os, String version) throws Exception {
doLaunch(os, version, "launch-with-multiple-java-opts.sh"); doLaunch(os, version, "init.d/launch-with-multiple-java-opts.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@ -192,13 +180,13 @@ class SysVinitLaunchScriptIT {
void launchWithUseOfStartStopDaemonDisabled(String os, String version) throws Exception { void launchWithUseOfStartStopDaemonDisabled(String os, String version) throws Exception {
// CentOS doesn't have start-stop-daemon // CentOS doesn't have start-stop-daemon
Assumptions.assumeFalse(os.equals("CentOS")); Assumptions.assumeFalse(os.equals("CentOS"));
doLaunch(os, version, "launch-with-use-of-start-stop-daemon-disabled.sh"); doLaunch(os, version, "init.d/launch-with-use-of-start-stop-daemon-disabled.sh");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithRelativePidFolder(String os, String version) throws Exception { void launchWithRelativePidFolder(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-relative-pid-folder.sh"); String output = doTest(os, version, "init.d/launch-with-relative-pid-folder.sh");
assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Started [" + extractPid(output) + "]"));
assertThat(output).has(coloredString(AnsiColor.GREEN, "Running [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Running [" + extractPid(output) + "]"));
assertThat(output).has(coloredString(AnsiColor.GREEN, "Stopped [" + extractPid(output) + "]")); assertThat(output).has(coloredString(AnsiColor.GREEN, "Stopped [" + extractPid(output) + "]"));
@ -207,56 +195,56 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void pidFolderOwnership(String os, String version) throws Exception { void pidFolderOwnership(String os, String version) throws Exception {
String output = doTest(os, version, "pid-folder-ownership.sh"); String output = doTest(os, version, "init.d/pid-folder-ownership.sh");
assertThat(output).contains("phil root"); assertThat(output).contains("phil root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void pidFileOwnership(String os, String version) throws Exception { void pidFileOwnership(String os, String version) throws Exception {
String output = doTest(os, version, "pid-file-ownership.sh"); String output = doTest(os, version, "init.d/pid-file-ownership.sh");
assertThat(output).contains("phil root"); assertThat(output).contains("phil root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void logFileOwnership(String os, String version) throws Exception { void logFileOwnership(String os, String version) throws Exception {
String output = doTest(os, version, "log-file-ownership.sh"); String output = doTest(os, version, "init.d/log-file-ownership.sh");
assertThat(output).contains("phil root"); assertThat(output).contains("phil root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void logFileOwnershipIsChangedWhenCreated(String os, String version) throws Exception { void logFileOwnershipIsChangedWhenCreated(String os, String version) throws Exception {
String output = doTest(os, version, "log-file-ownership-is-changed-when-created.sh"); String output = doTest(os, version, "init.d/log-file-ownership-is-changed-when-created.sh");
assertThat(output).contains("andy root"); assertThat(output).contains("andy root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void logFileOwnershipIsUnchangedWhenExists(String os, String version) throws Exception { void logFileOwnershipIsUnchangedWhenExists(String os, String version) throws Exception {
String output = doTest(os, version, "log-file-ownership-is-unchanged-when-exists.sh"); String output = doTest(os, version, "init.d/log-file-ownership-is-unchanged-when-exists.sh");
assertThat(output).contains("root root"); assertThat(output).contains("root root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithRelativeLogFolder(String os, String version) throws Exception { void launchWithRelativeLogFolder(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-relative-log-folder.sh"); String output = doTest(os, version, "init.d/launch-with-relative-log-folder.sh");
assertThat(output).contains("Log written"); assertThat(output).contains("Log written");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void launchWithRunAsUser(String os, String version) throws Exception { void launchWithRunAsUser(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-run-as-user.sh"); String output = doTest(os, version, "init.d/launch-with-run-as-user.sh");
assertThat(output).contains("wagner root"); assertThat(output).contains("wagner root");
} }
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void whenRunAsUserDoesNotExistLaunchFailsWithInvalidArgument(String os, String version) throws Exception { void whenRunAsUserDoesNotExistLaunchFailsWithInvalidArgument(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-run-as-invalid-user.sh"); String output = doTest(os, version, "init.d/launch-with-run-as-invalid-user.sh");
assertThat(output).contains("Status: 2"); assertThat(output).contains("Status: 2");
assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'johndoe': no such user")); assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'johndoe': no such user"));
} }
@ -264,7 +252,7 @@ class SysVinitLaunchScriptIT {
@ParameterizedTest(name = "{0} {1}") @ParameterizedTest(name = "{0} {1}")
@MethodSource("parameters") @MethodSource("parameters")
void whenJarOwnerAndRunAsUserAreBothSpecifiedRunAsUserTakesPrecedence(String os, String version) throws Exception { void whenJarOwnerAndRunAsUserAreBothSpecifiedRunAsUserTakesPrecedence(String os, String version) throws Exception {
String output = doTest(os, version, "launch-with-run-as-user-preferred-to-jar-owner.sh"); String output = doTest(os, version, "init.d/launch-with-run-as-user-preferred-to-jar-owner.sh");
assertThat(output).contains("wagner root"); assertThat(output).contains("wagner root");
} }
@ -272,49 +260,11 @@ class SysVinitLaunchScriptIT {
@MethodSource("parameters") @MethodSource("parameters")
void whenLaunchedUsingNonRootUserWithRunAsUserSpecifiedLaunchFailsWithInsufficientPrivilege(String os, void whenLaunchedUsingNonRootUserWithRunAsUserSpecifiedLaunchFailsWithInsufficientPrivilege(String os,
String version) throws Exception { String version) throws Exception {
String output = doTest(os, version, "launch-with-run-as-user-root-required.sh"); String output = doTest(os, version, "init.d/launch-with-run-as-user-root-required.sh");
assertThat(output).contains("Status: 4"); assertThat(output).contains("Status: 4");
assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'wagner': current user is not root")); assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'wagner': current user is not root"));
} }
static List<Object[]> parameters() {
List<Object[]> parameters = new ArrayList<>();
for (File os : new File("src/test/resources/conf").listFiles()) {
for (File version : os.listFiles()) {
parameters.add(new Object[] { os.getName(), version.getName() });
}
}
return parameters;
}
private void doLaunch(String os, String version, String script) throws Exception {
assertThat(doTest(os, version, script)).contains("Launched");
}
private String doTest(String os, String version, String script) throws Exception {
ToStringConsumer consumer = new ToStringConsumer().withRemoveAnsiCodes(false);
try (LaunchScriptTestContainer container = new LaunchScriptTestContainer(os, version, script)) {
container.withLogConsumer(consumer);
container.start();
while (container.isRunning()) {
Thread.sleep(100);
}
}
return consumer.toUtf8String();
}
private Condition<String> coloredString(AnsiColor color, String string) {
String colorString = ESC + "[0;" + color + "m" + string + ESC + "[0m";
return new Condition<String>() {
@Override
public boolean matches(String value) {
return containsString(colorString).matches(value);
}
};
}
private String extractPid(String output) { private String extractPid(String output) {
return extract("PID", output); return extract("PID", output);
} }
@ -328,32 +278,4 @@ class SysVinitLaunchScriptIT {
throw new IllegalArgumentException("Failed to extract " + label + " from output: " + output); throw new IllegalArgumentException("Failed to extract " + label + " from output: " + output);
} }
private static final class LaunchScriptTestContainer extends GenericContainer<LaunchScriptTestContainer> {
private LaunchScriptTestContainer(String os, String version, String testScript) {
super(new ImageFromDockerfile("spring-boot-launch-script/" + os.toLowerCase() + "-" + version)
.withFileFromFile("Dockerfile",
new File("src/test/resources/conf/" + os + "/" + version + "/Dockerfile"))
.withFileFromFile("spring-boot-launch-script-tests.jar", findApplication())
.withFileFromFile("test-functions.sh", new File("src/test/resources/scripts/test-functions.sh")));
withCopyFileToContainer(MountableFile.forHostPath("src/test/resources/scripts/" + testScript),
"/" + testScript);
withCommand("/bin/bash", "-c", "chmod +x " + testScript + " && ./" + testScript);
withStartupTimeout(Duration.ofMinutes(10));
}
private static File findApplication() {
File targetDir = new File("target");
for (File file : targetDir.listFiles()) {
if (file.getName().startsWith("spring-boot-launch-script-tests") && file.getName().endsWith(".jar")
&& !file.getName().endsWith("-sources.jar")) {
return file;
}
}
throw new IllegalStateException(
"Could not find test application in target directory. Have you built it (mvn package)?");
}
}
} }

@ -8,4 +8,6 @@ RUN yum install -y wget && \
yum --nogpg localinstall -y jdk.rpm && \ yum --nogpg localinstall -y jdk.rpm && \
rm -f jdk.rpm rm -f jdk.rpm
ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar
ADD test-functions.sh /test-functions.sh ADD test-functions.sh /test-functions.sh
ADD init.d/test-functions.sh /init.d/test-functions.sh
ADD jar/test-functions.sh /jar/test-functions.sh

@ -7,4 +7,6 @@ RUN apt-get update && \
ENV JAVA_HOME /opt/openjdk ENV JAVA_HOME /opt/openjdk
ENV PATH $JAVA_HOME/bin:$PATH ENV PATH $JAVA_HOME/bin:$PATH
ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar
ADD test-functions.sh /test-functions.sh ADD test-functions.sh /test-functions.sh
ADD init.d/test-functions.sh /init.d/test-functions.sh
ADD jar/test-functions.sh /jar/test-functions.sh

@ -7,4 +7,6 @@ RUN apt-get update && \
ENV JAVA_HOME /opt/openjdk ENV JAVA_HOME /opt/openjdk
ENV PATH $JAVA_HOME/bin:$PATH ENV PATH $JAVA_HOME/bin:$PATH
ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar ADD spring-boot-launch-script-tests.jar /spring-boot-launch-script-tests.jar
ADD test-functions.sh /test-functions.sh ADD test-functions.sh /test-functions.sh
ADD init.d/test-functions.sh /init.d/test-functions.sh
ADD jar/test-functions.sh /jar/test-functions.sh

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
await_app await_app

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
force_stop_service force_stop_service
echo "Status: $?" echo "Status: $?"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_double_link_service install_double_link_service
echo 'JAVA_OPTS=-Dserver.port=8081' > /test-service/spring-boot-app.conf echo 'JAVA_OPTS=-Dserver.port=8081' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'LOG_FOLDER=/does/not/exist' > /test-service/spring-boot-app.conf echo 'LOG_FOLDER=/does/not/exist' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'PID_FOLDER=/does/not/exist' > /test-service/spring-boot-app.conf echo 'PID_FOLDER=/does/not/exist' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service --server.port=8081 --server.servlet.context-path=/test start_service --server.port=8081 --server.servlet.context-path=/test
await_app http://127.0.0.1:8081/test/ await_app http://127.0.0.1:8081/test/

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'JAVA_OPTS="-Dserver.port=8081 -Dserver.servlet.context-path=/test"' > /test-service/spring-boot-app.conf echo 'JAVA_OPTS="-Dserver.port=8081 -Dserver.servlet.context-path=/test"' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'RUN_ARGS="--server.port=8081 --server.servlet.context-path=/test"' > /test-service/spring-boot-app.conf echo 'RUN_ARGS="--server.port=8081 --server.servlet.context-path=/test"' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
mkdir ./pid mkdir ./pid
install_service install_service
echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
mkdir /test-service/pid mkdir /test-service/pid
echo 'PID_FOLDER=pid' > /test-service/spring-boot-app.conf echo 'PID_FOLDER=pid' > /test-service/spring-boot-app.conf

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'RUN_AS_USER=johndoe' > /test-service/spring-boot-app.conf echo 'RUN_AS_USER=johndoe' > /test-service/spring-boot-app.conf

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service --server.port=8081 start_service --server.port=8081
await_app http://127.0.0.1:8081/ await_app http://127.0.0.1:8081/

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'JAVA_OPTS=-Dserver.port=8081' > /test-service/spring-boot-app.conf echo 'JAVA_OPTS=-Dserver.port=8081' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'RUN_ARGS=--server.port=8081' > /test-service/spring-boot-app.conf echo 'RUN_ARGS=--server.port=8081' > /test-service/spring-boot-app.conf
start_service start_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
chmod -x $(type -p start-stop-daemon) chmod -x $(type -p start-stop-daemon)
install_service install_service
echo 'USE_START_STOP_DAEMON=false' > /test-service/spring-boot-app.conf echo 'USE_START_STOP_DAEMON=false' > /test-service/spring-boot-app.conf

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf
mkdir -p /test-service/log mkdir -p /test-service/log

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf
mkdir -p /test-service/log mkdir -p /test-service/log

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
echo "PID1: $(cat /var/run/spring-boot-app/spring-boot-app.pid)" echo "PID1: $(cat /var/run/spring-boot-app/spring-boot-app.pid)"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
restart_service restart_service
echo "Status: $?" echo "Status: $?"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
echo "PID: $(cat /var/run/spring-boot-app/spring-boot-app.pid)" echo "PID: $(cat /var/run/spring-boot-app/spring-boot-app.pid)"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
echo "Status: $?" echo "Status: $?"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
pid=$(cat /var/run/spring-boot-app/spring-boot-app.pid) pid=$(cat /var/run/spring-boot-app/spring-boot-app.pid)

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
start_service start_service
status_service status_service

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
status_service status_service
echo "Status: $?" echo "Status: $?"

@ -1,4 +1,5 @@
source ./test-functions.sh source ./test-functions.sh
source ./init.d/test-functions.sh
install_service install_service
stop_service stop_service
echo "Status: $?" echo "Status: $?"

@ -0,0 +1,36 @@
source ../test-functions.sh
install_service() {
mkdir /test-service
mv /spring-boot-launch-script-tests.jar /test-service/spring-boot-app.jar
chmod +x /test-service/spring-boot-app.jar
ln -s /test-service/spring-boot-app.jar /etc/init.d/spring-boot-app
}
install_double_link_service() {
mkdir /test-service
mv /spring-boot-launch-script-tests.jar /test-service/
chmod +x /test-service/spring-boot-launch-script-tests.jar
ln -s /test-service/spring-boot-launch-script-tests.jar /test-service/spring-boot-app.jar
ln -s /test-service/spring-boot-app.jar /etc/init.d/spring-boot-app
}
start_service() {
service spring-boot-app start $@
}
restart_service() {
service spring-boot-app restart
}
status_service() {
service spring-boot-app status
}
stop_service() {
service spring-boot-app stop
}
force_stop_service() {
service spring-boot-app force-stop
}

@ -0,0 +1,4 @@
source ./test-functions.sh
source ./jar/test-functions.sh
launch_jar & await_app
curl -s http://127.0.0.1:8080/

@ -0,0 +1,5 @@
source ./test-functions.sh
source ./jar/test-functions.sh
cd "init.d" || exit 1
ln -s ../spring-boot-launch-script-tests.jar some_app
./some_app

@ -0,0 +1,27 @@
source ./test-functions.sh
source ./jar/test-functions.sh
export APP_NAME="my-new-app"
export MODE=service
./spring-boot-launch-script-tests.jar start
TEST_LOG_FILE="/var/log/$APP_NAME.log"
if [[ (! (-e "$TEST_LOG_FILE")) || (! (-f "$TEST_LOG_FILE")) ]]; then
echo Log file "$TEST_LOG_FILE" doesn\'t exists.
exit 2
else
echo Test for a log file is passed.
fi
TEST_PID_FOLDER="/var/run/$APP_NAME"
if [[ (! (-e "$TEST_PID_FOLDER")) || (! (-d "$TEST_PID_FOLDER")) ]]; then
echo PID folder "$TEST_PID_FOLDER" doesn\'t exists.
exit 2
else
echo Test for a PID folder is passed.
fi
TEST_PID_FILE="$TEST_PID_FOLDER/$APP_NAME.pid"
if [[ (! (-e "$TEST_PID_FILE")) || (! (-f "$TEST_PID_FILE"))]]; then
echo PID file "$TEST_PID_FILE" doesn\'t exists.
exit 2
else
echo Test for a PID file is passed.
fi
echo All tests are passed.

@ -0,0 +1,5 @@
export DEBUG=true
source ./test-functions.sh
source ./jar/test-functions.sh
launch_jar & await_app
curl -s http://127.0.0.1:8080/

@ -0,0 +1,6 @@
source ./test-functions.sh
source ./jar/test-functions.sh
cp spring-boot-launch-script-tests.jar app-another.jar
export JARFILE=app-another.jar
launch_jar & await_app
curl -s http://127.0.0.1:8080/

@ -1,38 +1,3 @@
install_service() {
mkdir /test-service
mv /spring-boot-launch-script-tests.jar /test-service/spring-boot-app.jar
chmod +x /test-service/spring-boot-app.jar
ln -s /test-service/spring-boot-app.jar /etc/init.d/spring-boot-app
}
install_double_link_service() {
mkdir /test-service
mv /spring-boot-launch-script-tests.jar /test-service/
chmod +x /test-service/spring-boot-launch-script-tests.jar
ln -s /test-service/spring-boot-launch-script-tests.jar /test-service/spring-boot-app.jar
ln -s /test-service/spring-boot-app.jar /etc/init.d/spring-boot-app
}
start_service() {
service spring-boot-app start $@
}
restart_service() {
service spring-boot-app restart
}
status_service() {
service spring-boot-app status
}
stop_service() {
service spring-boot-app stop
}
force_stop_service() {
service spring-boot-app force-stop
}
await_app() { await_app() {
if [ -z $1 ] if [ -z $1 ]
then then

Loading…
Cancel
Save