From a33ce9d405410b15893ff4b3a98c4ae0118e1f08 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 4 Oct 2023 22:55:17 -0700 Subject: [PATCH] Cache JDK downloads for tests that use ImageFromDockerfile Update `spring-boot-launch-script-tests` and `spring-boot-loader-tests` so that JDK archives are now downloaded by Gradle and cached across builds. Closes gh-37450 --- buildSrc/build.gradle | 1 + ci/tasks/build-project.yml | 1 + .../build.gradle | 24 +++++++ .../AbstractLaunchScriptIntegrationTests.java | 62 +++---------------- .../conf/RedHat/ubi9-9.2-722/Dockerfile | 13 ++-- .../conf/Ubuntu/jammy-20230624/Dockerfile | 16 ++--- .../spring-boot-loader-tests/build.gradle | 26 +++++++- .../boot/loader/LoaderIntegrationTests.java | 9 +-- .../resources/conf/oracle-jdk-17/Dockerfile | 14 +++-- .../conf/oracle-jdk-17/Dockerfile-aarch64 | 8 --- 10 files changed, 91 insertions(+), 83 deletions(-) delete mode 100644 spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile-aarch64 diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 26ceb019699..0f66ad0a6bc 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -32,6 +32,7 @@ dependencies { implementation("com.fasterxml.jackson.core:jackson-databind:2.11.4") implementation("com.gradle:gradle-enterprise-gradle-plugin:3.12.1") implementation("com.tngtech.archunit:archunit:1.0.0") + implementation("de.undercouch.download:de.undercouch.download.gradle.plugin:5.5.0") implementation("commons-codec:commons-codec:1.13") implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}") implementation("org.apache.maven:maven-embedder:3.6.2") diff --git a/ci/tasks/build-project.yml b/ci/tasks/build-project.yml index db3003af6e0..61c86eb759a 100644 --- a/ci/tasks/build-project.yml +++ b/ci/tasks/build-project.yml @@ -7,6 +7,7 @@ outputs: - name: git-repo caches: - path: gradle +- path: build/downloads - path: embedmongo params: BRANCH: diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle index 59f77e797da..646c572ecaf 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/build.gradle @@ -2,10 +2,14 @@ plugins { id "java" id "org.springframework.boot.conventions" id "org.springframework.boot.integration-test" + id "de.undercouch.download" } description = "Spring Boot Launch Script Integration Tests" +def jdkVersion = "8u382+6" +def jdkArch = "aarch64".equalsIgnoreCase(System.getProperty("os.arch")) ? "aarch64" : "amd64" + configurations { app } @@ -38,6 +42,26 @@ task buildApp(type: GradleBuild) { tasks = ["build"] } +task downloadJdk(type: Download) { + def destFolder = new File(rootProject.buildDir, "downloads/jdk/bellsoft") + destFolder.mkdirs() + src "https://download.bell-sw.com/java/${jdkVersion}/bellsoft-jdk${jdkVersion}-linux-${jdkArch}.tar.gz" + dest destFolder + tempAndMove true + overwrite false +} + +task syncJdkDownloads(type: Sync) { + dependsOn downloadJdk + from "${rootProject.buildDir}/downloads/jdk/bellsoft/" + include "bellsoft-jdk${jdkVersion}-linux-${jdkArch}.tar.gz" + into "${project.buildDir}/downloads/jdk/bellsoft/" +} + +processIntTestResources { + dependsOn syncJdkDownloads +} + intTest { dependsOn buildApp enabled = !JavaVersion.current().java9Compatible diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/java/org/springframework/boot/launchscript/AbstractLaunchScriptIntegrationTests.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/java/org/springframework/boot/launchscript/AbstractLaunchScriptIntegrationTests.java index 2b77afeb40f..ad51e204d77 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/java/org/springframework/boot/launchscript/AbstractLaunchScriptIntegrationTests.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/java/org/springframework/boot/launchscript/AbstractLaunchScriptIntegrationTests.java @@ -17,14 +17,9 @@ package org.springframework.boot.launchscript; import java.io.File; -import java.net.URI; -import java.nio.file.Paths; import java.time.Duration; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Predicate; import org.assertj.core.api.Condition; @@ -52,16 +47,6 @@ import static org.hamcrest.Matchers.containsString; */ abstract class AbstractLaunchScriptIntegrationTests { - private static final Map JAVA_DOWNLOAD_URLS; - static { - Map urls = new HashMap<>(); - urls.put(Architecture.AMD64, - URI.create("https://download.bell-sw.com/java/8u382+6/bellsoft-jdk8u382+6-linux-amd64.tar.gz")); - urls.put(Architecture.AARCH64, - URI.create("https://download.bell-sw.com/java/8u382+6/bellsoft-jdk8u382+6-linux-aarch64.tar.gz")); - JAVA_DOWNLOAD_URLS = Collections.unmodifiableMap(urls); - } - protected static final char ESC = 27; private final String scriptsDir; @@ -115,9 +100,7 @@ abstract class AbstractLaunchScriptIntegrationTests { private static final class LaunchScriptTestContainer extends GenericContainer { private LaunchScriptTestContainer(String os, String version, String scriptsDir, String testScript) { - super(new ImageFromDockerfile("spring-boot-launch-script/" + os.toLowerCase() + "-" + version) - .withDockerfile(Paths.get("src/intTest/resources/conf/" + os + "/" + version + "/Dockerfile")) - .withBuildArg("JAVA_DOWNLOAD_URL", getJavaDownloadUrl())); + super(createImage(os, version)); withCopyFileToContainer(MountableFile.forHostPath(findApplication().getAbsolutePath()), "/app.jar"); withCopyFileToContainer( MountableFile.forHostPath("src/intTest/resources/scripts/" + scriptsDir + "test-functions.sh"), @@ -130,14 +113,15 @@ abstract class AbstractLaunchScriptIntegrationTests { withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5))); } - private static String getJavaDownloadUrl() { - Architecture architecture = Architecture.current(); - Assert.notNull(architecture, - () -> String.format("Failed to find current architecture. Value of os.arch is: '%s'", - System.getProperty("os.arch"))); - URI uri = JAVA_DOWNLOAD_URLS.get(architecture); - Assert.notNull(uri, () -> String.format("No JDK download URL for architecture %s found", architecture)); - return uri.toString(); + private static ImageFromDockerfile createImage(String os, String version) { + ImageFromDockerfile image = new ImageFromDockerfile( + "spring-boot-launch-script/" + os.toLowerCase() + "-" + version); + image.withFileFromFile("Dockerfile", + new File("src/intTest/resources/conf/" + os + "/" + version + "/Dockerfile")); + for (File file : new File("build/downloads/jdk/bellsoft").listFiles()) { + image.withFileFromFile("downloads/" + file.getName(), file); + } + return image; } private static File findApplication() { @@ -149,30 +133,4 @@ abstract class AbstractLaunchScriptIntegrationTests { } - private enum Architecture { - - AMD64, AARCH64; - - /** - * Returns the current architecture. - * @return the current architecture or {@code null} - */ - static Architecture current() { - String arch = System.getProperty("os.arch"); - if (arch == null) { - return null; - } - switch (arch) { - case "amd64": - case "x86_64": - return AMD64; - case "aarch64": - return AARCH64; - default: - return null; - } - } - - } - } diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/RedHat/ubi9-9.2-722/Dockerfile b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/RedHat/ubi9-9.2-722/Dockerfile index ebb51b5214a..777f1a3cd93 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/RedHat/ubi9-9.2-722/Dockerfile +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/RedHat/ubi9-9.2-722/Dockerfile @@ -1,7 +1,10 @@ +FROM redhat/ubi9:9.2-722 as prepare +COPY downloads/* /opt/download/ +RUN mkdir -p /opt/jdk && \ + cd /opt/jdk && \ + tar xzf /opt/download/* --strip-components=1 + FROM redhat/ubi9:9.2-722 -ARG JAVA_DOWNLOAD_URL=https://download.bell-sw.com/java/8u382+6/bellsoft-jdk8u382+6-linux-amd64.tar.gz -ENV JAVA_HOME /opt/openjdk +COPY --from=prepare /opt/jdk /opt/jdk +ENV JAVA_HOME /opt/jdk ENV PATH $JAVA_HOME/bin:$PATH -RUN mkdir -p /opt/openjdk && \ - cd /opt/openjdk && \ - curl -L $JAVA_DOWNLOAD_URL | tar zx --strip-components=1 diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/Ubuntu/jammy-20230624/Dockerfile b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/Ubuntu/jammy-20230624/Dockerfile index f4e9645f022..4e9da10f8ff 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/Ubuntu/jammy-20230624/Dockerfile +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/intTest/resources/conf/Ubuntu/jammy-20230624/Dockerfile @@ -1,9 +1,11 @@ +FROM ubuntu:jammy-20230624 as prepare +COPY downloads/* /opt/download/ +RUN mkdir -p /opt/jdk && \ + cd /opt/jdk && \ + tar xzf /opt/download/* --strip-components=1 + FROM ubuntu:jammy-20230624 -RUN apt-get update && \ - apt-get install -y software-properties-common curl && \ - mkdir -p /opt/openjdk -ARG JAVA_DOWNLOAD_URL=https://download.bell-sw.com/java/8u382+6/bellsoft-jdk8u382+6-linux-amd64.tar.gz -ENV JAVA_HOME /opt/openjdk +RUN apt-get update && apt-get install -y software-properties-common curl +COPY --from=prepare /opt/jdk /opt/jdk +ENV JAVA_HOME /opt/jdk ENV PATH $JAVA_HOME/bin:$PATH -RUN cd /opt/openjdk && \ - curl -L $JAVA_DOWNLOAD_URL | tar zx --strip-components=1 diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/build.gradle b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/build.gradle index 7c4095f73b1..c4035df3a5e 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/build.gradle +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/build.gradle @@ -2,10 +2,14 @@ plugins { id "java" id "org.springframework.boot.conventions" id "org.springframework.boot.integration-test" + id "de.undercouch.download" } description = "Spring Boot Loader Integration Tests" +def oracleJdkVersion = "17.0.8" +def oracleJdkArch = "aarch64".equalsIgnoreCase(System.getProperty("os.arch")) ? "aarch64" : "x86" + configurations { app } @@ -39,6 +43,26 @@ task buildApp(type: GradleBuild) { tasks = ["build"] } +task downloadJdk(type: Download) { + def destFolder = new File(rootProject.buildDir, "downloads/jdk/oracle") + destFolder.mkdirs() + src "https://download.oracle.com/java/17/archive/jdk-${oracleJdkVersion}_linux-${oracleJdkArch}_bin.tar.gz" + dest destFolder + tempAndMove true + overwrite false +} + +task syncJdkDownloads(type: Sync) { + dependsOn downloadJdk + from "${rootProject.buildDir}/downloads/jdk/oracle/" + include "jdk-${oracleJdkVersion}_linux-${oracleJdkArch}_bin.tar.gz" + into "${project.buildDir}/downloads/jdk/oracle/" +} + +processIntTestResources { + dependsOn syncJdkDownloads +} + intTest { dependsOn buildApp -} \ No newline at end of file +} diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/java/org/springframework/boot/loader/LoaderIntegrationTests.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/java/org/springframework/boot/loader/LoaderIntegrationTests.java index eba7a2b3673..1560bba14fb 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/java/org/springframework/boot/loader/LoaderIntegrationTests.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/java/org/springframework/boot/loader/LoaderIntegrationTests.java @@ -120,10 +120,11 @@ class LoaderIntegrationTests { } static JavaRuntime oracleJdk17() { - String arch = System.getProperty("os.arch"); - String dockerFile = ("aarch64".equals(arch)) ? "Dockerfile-aarch64" : "Dockerfile"; - ImageFromDockerfile image = new ImageFromDockerfile("spring-boot-loader/oracle-jdk-17") - .withFileFromFile("Dockerfile", new File("src/intTest/resources/conf/oracle-jdk-17/" + dockerFile)); + ImageFromDockerfile image = new ImageFromDockerfile("spring-boot-loader/oracle-jdk"); + image.withFileFromFile("Dockerfile", new File("src/intTest/resources/conf/oracle-jdk-17/Dockerfile")); + for (File file : new File("build/downloads/jdk/oracle").listFiles()) { + image.withFileFromFile("downloads/" + file.getName(), file); + } return new JavaRuntime("Oracle JDK 17", JavaVersion.SEVENTEEN, () -> new GenericContainer<>(image)); } diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile index 2a50709dc5a..0b920d47fde 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile @@ -1,8 +1,10 @@ +FROM ubuntu:jammy-20230624 as prepare +COPY downloads/* /opt/download/ +RUN mkdir -p /opt/jdk && \ + cd /opt/jdk && \ + tar xzf /opt/download/* --strip-components=1 + FROM ubuntu:jammy-20230624 -RUN apt-get update && \ - apt-get install -y software-properties-common curl && \ - mkdir -p /opt/oraclejdk && \ - cd /opt/oraclejdk && \ - curl -L https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz | tar zx --strip-components=1 -ENV JAVA_HOME /opt/oraclejdk +COPY --from=prepare /opt/jdk /opt/jdk +ENV JAVA_HOME /opt/jdk ENV PATH $JAVA_HOME/bin:$PATH diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile-aarch64 b/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile-aarch64 deleted file mode 100644 index 3f8614c7a21..00000000000 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-loader-tests/src/intTest/resources/conf/oracle-jdk-17/Dockerfile-aarch64 +++ /dev/null @@ -1,8 +0,0 @@ -FROM ubuntu:jammy-20230624 -RUN apt-get update && \ - apt-get install -y software-properties-common curl && \ - mkdir -p /opt/oraclejdk && \ - cd /opt/oraclejdk && \ - curl -L https://download.oracle.com/java/17/archive/jdk-17.0.8_linux-aarch64_bin.tar.gz | tar zx --strip-components=1 -ENV JAVA_HOME /opt/oraclejdk -ENV PATH $JAVA_HOME/bin:$PATH