#!/bin/bash set -euo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) REPO_ROOT=$(cd "${SCRIPT_DIR}/.." && pwd) # shellcheck source=/dev/null source "${REPO_ROOT}/docker-runner/common/upgrade.sh" assert_eq() { local expected=$1 local actual=$2 local message=$3 if [ "${expected}" != "${actual}" ]; then echo "FAIL: ${message}" >&2 echo " expected: ${expected}" >&2 echo " actual: ${actual}" >&2 exit 1 fi } make_fake_elf() { local path=$1 local arch=$2 local machine_bytes="" case "${arch}" in amd64) machine_bytes='\076\000' ;; arm64) machine_bytes='\267\000' ;; arm-7) machine_bytes='\050\000' ;; *) echo "FAIL: unsupported fake ELF arch ${arch}" >&2 exit 1 ;; esac printf '\177ELF\002\001\001\000\000\000\000\000\000\000\000\000\002\000%b' \ "${machine_bytes}" > "${path}" } test_extract_versions_from_listing() { local html html=$(cat <<'EOF' 0.6.1 0.5.0 0.4.1 EOF ) local actual actual=$(extract_versions_from_listing <<<"${html}") assert_eq $'0.6.1\n0.5.0\n0.4.1' "${actual}" \ "extract_versions_from_listing should return every version in listing order" } test_pick_latest_version() { local actual actual=$(pick_latest_version <<'EOF' 0.5.0 0.6.1 0.4.9 EOF ) assert_eq "0.6.1" "${actual}" \ "pick_latest_version should return the highest semantic version" } test_extract_version_number_accepts_v_prefix() { local actual actual=$(extract_version_number <<'EOF' act_runner version v0.6.1 EOF ) assert_eq "0.6.1" "${actual}" \ "extract_version_number should accept version output with a leading v" } fake_latest_version() { echo "0.6.2" } fake_invalid_version() { echo "latest" } test_resolve_latest_version_or_fallback_prefers_latest() { local actual actual=$(resolve_latest_version_or_fallback "0.2.13" fake_latest_version) assert_eq "0.6.2" "${actual}" \ "resolve_latest_version_or_fallback should prefer the detected latest version" } test_resolve_latest_version_or_fallback_uses_fallback() { local actual actual=$(resolve_latest_version_or_fallback "0.2.13" fake_invalid_version) assert_eq "0.2.13" "${actual}" \ "resolve_latest_version_or_fallback should fall back when detected version is invalid" } test_detect_binary_arch_from_file() { local temp_root amd64_file arm64_file arm7_file text_file temp_root=$(mktemp -d) amd64_file="${temp_root}/act_runner-amd64" arm64_file="${temp_root}/act_runner-arm64" arm7_file="${temp_root}/act_runner-arm7" text_file="${temp_root}/not-elf" make_fake_elf "${amd64_file}" "amd64" make_fake_elf "${arm64_file}" "arm64" make_fake_elf "${arm7_file}" "arm-7" printf 'not an elf\n' > "${text_file}" assert_eq "amd64" "$(detect_binary_arch_from_file "${amd64_file}")" \ "detect_binary_arch_from_file should recognize amd64 ELF binaries" assert_eq "arm64" "$(detect_binary_arch_from_file "${arm64_file}")" \ "detect_binary_arch_from_file should recognize arm64 ELF binaries" assert_eq "arm-7" "$(detect_binary_arch_from_file "${arm7_file}")" \ "detect_binary_arch_from_file should recognize arm-7 ELF binaries" assert_eq "unknown" "$(detect_binary_arch_from_file "${text_file}")" \ "detect_binary_arch_from_file should reject non-ELF files" rm -rf "${temp_root}" } test_binary_arch_matches_target() { local temp_root amd64_file arm64_file temp_root=$(mktemp -d) amd64_file="${temp_root}/act_runner-amd64" arm64_file="${temp_root}/act_runner-arm64" make_fake_elf "${amd64_file}" "amd64" make_fake_elf "${arm64_file}" "arm64" if ! binary_arch_matches_target "${amd64_file}" "amd64"; then echo "FAIL: binary_arch_matches_target should accept matching architectures" >&2 exit 1 fi if binary_arch_matches_target "${arm64_file}" "amd64"; then echo "FAIL: binary_arch_matches_target should reject mismatched architectures" >&2 exit 1 fi rm -rf "${temp_root}" } test_extract_versions_from_listing test_pick_latest_version test_extract_version_number_accepts_v_prefix test_resolve_latest_version_or_fallback_prefers_latest test_resolve_latest_version_or_fallback_uses_fallback test_detect_binary_arch_from_file test_binary_arch_matches_target echo "upgrade_test.sh: PASS"