From dfb17365d5ae2df723e352302a69dbe89ceaf99b Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 30 Aug 2022 12:54:44 +0200 Subject: [PATCH] fix(java): handle modern versions --- package.json | 1 + src/usr/local/buildpack/utils/cache.sh | 5 +- src/usr/local/buildpack/utils/java.sh | 62 +++++++++++++----- test/bash/cache.bats | 5 ++ test/bash/java.bats | 90 ++++++++++++++++++++++++++ test/java/Dockerfile | 1 + 6 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 test/bash/java.bats diff --git a/package.json b/package.json index 3dbeaa538e..2ecd1f5b5b 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "scripts": { "test:bats": "node_modules/bats/bin/bats --timing --verbose-run test/bash/ test/bash/v2 test/bash/tools", + "bats": "node_modules/bats/bin/bats --timing --verbose-run", "build": "release:prepare", "lint": "prettier -c -u \"**/*.*\"", "lint-fix": "prettier -w -u \"**/*.*\"", diff --git a/src/usr/local/buildpack/utils/cache.sh b/src/usr/local/buildpack/utils/cache.sh index 88efc9ed33..a20f8c4db5 100644 --- a/src/usr/local/buildpack/utils/cache.sh +++ b/src/usr/local/buildpack/utils/cache.sh @@ -64,7 +64,10 @@ function download_file () { name=${2:-$(basename "${url}")} local temp_folder=${BUILDPACK_CACHE_DIR:-${TEMP_DIR}} - curl --retry 3 --create-dirs -sSfLo "${temp_folder}/${name}" "${url}" + if ! curl --retry 3 --create-dirs -sSfLo "${temp_folder}/${name}" "${url}" ; then + echo "Download failed: ${url}" >&2 + exit 1 + fi; echo "${temp_folder}/${name}" } diff --git a/src/usr/local/buildpack/utils/java.sh b/src/usr/local/buildpack/utils/java.sh index c9af0942e4..aa821bb336 100644 --- a/src/usr/local/buildpack/utils/java.sh +++ b/src/usr/local/buildpack/utils/java.sh @@ -38,23 +38,39 @@ EOM } function get_java_install_url () { - local arch=x64 - local url - local baseUrl=https://api.adoptium.net/v3/assets/version - local apiArgs='heap_size=normal&os=linux&page=0&page_size=1&project=jdk' + local arch + local json + local base_url=https://api.adoptium.net/v3/assets/version + local api_args='heap_size=normal&os=linux&page=0&page_size=1&project=jdk' local version=${1} - local type=${2} + local type=${2:-jre} + local patched_version + + if [ -z "${version}" ]; then + echo "Missing Java version" + exit 1 + fi + + # https://github.com/adoptium/api.adoptium.net/issues/468 + arch=$(uname -m) + # https://github.com/adoptium/api.adoptium.net/issues/492 + patched_version=$(patch_java_version "$version") - url=$(curl -sSLf -H 'accept: application/json' "${baseUrl}/${version}?architecture=${arch}&image_type=${type}&${apiArgs}" \ - | jq --raw-output '.[0].binaries[0].package.link') - echo "${url}" + if ! json=$(curl -sSLf -H 'accept: application/json' "${base_url}/${version}?architecture=${arch}&image_type=${type}&${api_args}" 2>&1) && [ "$patched_version" != "$version" ]; then + if ! json=$(curl -sSLf -H 'accept: application/json' "${base_url}/${patched_version}?architecture=${arch}&image_type=${type}&${api_args}" 2>&1); then + echo "Invalid java version: $version" >&2 + exit 1 + fi + fi + + echo "${json}" | jq --raw-output '.[0].binaries[0].package.link' } function install_java () { local versioned_tool_path local file local url - local type=${1} + local type=${1:-jre} local ssl_dir ssl_dir=$(get_ssl_path) @@ -115,13 +131,25 @@ function prepare_java () { } function get_latest_java_version () { - local arch=x64 - local url - local baseUrl=https://api.adoptium.net/v3/info/release_versions - local apiArgs='heap_size=normal&os=linux&page=0&page_size=1&project=jdk&release_type=ga' - local type=${1} + local arch + local version + local base_url=https://api.adoptium.net/v3/info/release_versions + local api_args='heap_size=normal&os=linux&page=0&page_size=1&project=jdk&release_type=ga<s=true' + local type=${1:-jre} + + # https://github.com/adoptium/api.adoptium.net/issues/468 + arch=$(uname -m) + curl -sSLf -H 'accept: application/json' "${base_url}?architecture=${arch}&image_type=${type}&${api_args}" \ + | jq --raw-output '.versions[0].semver' +} - url=$(curl -sSLf -H 'accept: application/json' "${baseUrl}?architecture=${arch}&image_type=${type}&${apiArgs}" \ - | jq --raw-output '.versions[0].semver') - echo "${url}" +# https://github.com/adoptium/api.adoptium.net/issues/492 +function patch_java_version () { + local version=${1} + if [[ "${version}" =~ ^((0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*))\+([1-9])([0-9][0-9])$ ]]; then + local build=${BASH_REMATCH[5]} + local meta=${BASH_REMATCH[6]} + version="${BASH_REMATCH[1]}.$build+$((meta + 0))" + fi + echo "${version}" } diff --git a/test/bash/cache.bats b/test/bash/cache.bats index ce5f72fce9..b48043e007 100644 --- a/test/bash/cache.bats +++ b/test/bash/cache.bats @@ -148,6 +148,11 @@ teardown() { run download_file assert_failure + assert_output "param url is set but empty" + + run download_file "${file}zzz" + assert_failure + assert_line "Download failed: ${file}zzz" run download_file "${file}" assert_success diff --git a/test/bash/java.bats b/test/bash/java.bats new file mode 100644 index 0000000000..f61b113bea --- /dev/null +++ b/test/bash/java.bats @@ -0,0 +1,90 @@ +# shellcheck disable=SC2034,SC2148 + +setup() { + load '../../node_modules/bats-support/load' + load '../../node_modules/bats-assert/load' + + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" + TEST_ROOT_DIR=$(mktemp -u) + + load "$TEST_DIR/../../src/usr/local/buildpack/util.sh" + + # load v2 overwrites + load "$TEST_DIR/../../src/usr/local/buildpack/utils/v2/overrides.sh" + + # load test overwrites + load "$TEST_DIR/util.sh" + + # set directories for test + ROOT_DIR="${TEST_ROOT_DIR}/root" + BIN_DIR="${TEST_ROOT_DIR}/bin" + USER_HOME="${TEST_ROOT_DIR}/user" + ENV_FILE="${TEST_ROOT_DIR}/env" + + setup_directories + + # copy buildpack files + cp -r "$TEST_DIR/../../src/usr/local/buildpack" "${ROOT_DIR}/buildpack" + + # set default test user is root + TEST_ROOT_USER=1000 + # load helm + load "$TEST_DIR/../../src/usr/local/buildpack/utils/java.sh" +} + +teardown() { + rm -rf "${TEST_ROOT_DIR}" +} + +@test "patch_java_version" { + run patch_java_version 18.0.2+101 + assert_success + assert_output "18.0.2.1+1" + + run patch_java_version 18.0.2+9 + assert_success + assert_output "18.0.2+9" + + run patch_java_version 11.0.14+101 + assert_success + assert_output "11.0.14.1+1" + + run patch_java_version 8.0.345+1 + assert_success + assert_output "8.0.345+1" +} + + +@test "get_java_install_url" { + + run get_java_install_url 18.0.2+9 jre + assert_success + assert_output "https://github.com/adoptium/temurin18-binaries/releases/download/jdk-18.0.2%2B9/OpenJDK18U-jre_x64_linux_hotspot_18.0.2_9.tar.gz" + + run get_java_install_url 8.0.345+1 jre + assert_success + assert_output "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u345-b01/OpenJDK8U-jre_x64_linux_hotspot_8u345b01.tar.gz" + + run get_java_install_url 18.0.2+101 jre + assert_success + assert_output "https://github.com/adoptium/temurin18-binaries/releases/download/jdk-18.0.2.1%2B1/OpenJDK18U-jre_x64_linux_hotspot_18.0.2.1_1.tar.gz" + + run get_java_install_url 11.0.14+101 jre + assert_success + assert_output "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jre_x64_linux_hotspot_11.0.14.1_1.tar.gz" + + run get_java_install_url 11.0.14+102 jre + assert_failure + assert_output "Invalid java version: 11.0.14+102" + + run get_java_install_url + assert_failure + assert_output "Missing Java version" +} + + +@test "get_latest_java_version" { + run get_latest_java_version + assert_success + assert_output --regexp '^[0-9]+\.[0-9]+\.[0-9]+\+[0-9]+' +} diff --git a/test/java/Dockerfile b/test/java/Dockerfile index 7c1498078d..d2325035d6 100644 --- a/test/java/Dockerfile +++ b/test/java/Dockerfile @@ -13,6 +13,7 @@ WORKDIR /test FROM base as build ARG APT_HTTP_PROXY +ARG BUILDPACK_DEBUG # TODO: only lts # renovate: datasource=adoptium-java packageName=java-jre