|
| 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 | +} |
0 commit comments