Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 \"**/*.*\"",
Expand Down
5 changes: 4 additions & 1 deletion src/usr/local/buildpack/utils/cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

Expand Down
62 changes: 45 additions & 17 deletions src/usr/local/buildpack/utils/java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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&lts=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}"
}
5 changes: 5 additions & 0 deletions test/bash/cache.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions test/bash/java.bats
Original file line number Diff line number Diff line change
@@ -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]+'
}
1 change: 1 addition & 0 deletions test/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down