|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Import test library for `check` command |
| 6 | +source dev-container-features-test-lib |
| 7 | + |
| 8 | +# Extension-specific tests |
| 9 | +check "az.resources" pwsh -Command "(Get-Module -ListAvailable -Name Az.Resources).Version.ToString()" |
| 10 | +check "az.storage" pwsh -Command "(Get-Module -ListAvailable -Name Az.Storage).Version.ToString()" |
| 11 | +check "profile" pwsh -Command "(Get-Variable $env:ProfileLoaded).Value" |
| 12 | + |
| 13 | +check "Powershell version as installed by feature" bash -c "pwsh --version" |
| 14 | + |
| 15 | +. /etc/os-release |
| 16 | +architecture="$(dpkg --print-architecture)" |
| 17 | + |
| 18 | +get_previous_version() { |
| 19 | + repo_url=$1 |
| 20 | + curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name' |
| 21 | +} |
| 22 | + |
| 23 | +install_prev_pwsh() { |
| 24 | + echo -e "\n(!) Failed to fetch the latest artifacts for powershell v${POWERSHELL_VERSION}..." |
| 25 | + previous_version=$(get_previous_version "https://api.github.com/repos/PowerShell/PowerShell/releases") |
| 26 | + echo -e "\nAttempting to install ${previous_version}" |
| 27 | + POWERSHELL_VERSION="${previous_version#v}" |
| 28 | + install_pwsh "${POWERSHELL_VERSION}" |
| 29 | +} |
| 30 | + |
| 31 | +install_pwsh() { |
| 32 | + POWERSHELL_VERSION=$1 |
| 33 | + powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz" |
| 34 | + powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)" |
| 35 | + sudo mkdir -p /tmp/pwsh "${powershell_target_path}" |
| 36 | + cd /tmp/pwsh |
| 37 | + sudo curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}" |
| 38 | +} |
| 39 | + |
| 40 | +apt_get_update() |
| 41 | +{ |
| 42 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 43 | + echo "Running apt-get update..." |
| 44 | + sudo apt-get update -y |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +check_packages() { |
| 49 | + if ! dpkg -s "$@" > /dev/null 2>&1; then |
| 50 | + sudo chmod +x /var/lib/apt/lists/ |
| 51 | + sudo mkdir -p /var/lib/apt/lists/partial |
| 52 | + sudo chmod +rx /var/lib/dpkg/lock-frontend |
| 53 | + apt_get_update |
| 54 | + sudo apt-get -y install --no-install-recommends "$@" |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +install_using_github() { |
| 59 | + # Fall back on direct download if no apt package exists in microsoft pool |
| 60 | + check_packages curl ca-certificates gnupg2 dirmngr libc6 libgcc1 libgssapi-krb5-2 libstdc++6 libunwind8 libuuid1 zlib1g libicu[0-9][0-9] |
| 61 | + if ! type git > /dev/null 2>&1; then |
| 62 | + check_packages git |
| 63 | + fi |
| 64 | + if [ "${architecture}" = "amd64" ]; then |
| 65 | + architecture="x64" |
| 66 | + fi |
| 67 | + |
| 68 | + echo -e "\nTrying to install a non-existing version for Powershell..." |
| 69 | + |
| 70 | + POWERSHELL_VERSION="1.2.XYZ" |
| 71 | + install_pwsh "${POWERSHELL_VERSION}" |
| 72 | + |
| 73 | + if grep -q "Not Found" "${powershell_filename}"; then |
| 74 | + install_prev_pwsh |
| 75 | + fi |
| 76 | + |
| 77 | + echo -e "\n" $POWERSHELL_VERSION "=powershell_version\n"; |
| 78 | + # Ugly - but only way to get sha256 is to parse release HTML. Remove newlines and tags, then look for filename followed by 64 hex characters. |
| 79 | + sudo curl -sSL -o "release.html" "https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION}" |
| 80 | + powershell_archive_sha256="$(cat release.html | tr '\n' ' ' | sed 's|<[^>]*>||g' | grep -oP "${powershell_filename}\s+\K[0-9a-fA-F]{64}" || echo '')" |
| 81 | + if [ -z "${powershell_archive_sha256}" ]; then |
| 82 | + echo "(!) WARNING: Failed to retrieve SHA256 for archive. Skipping validaiton." |
| 83 | + else |
| 84 | + echo "SHA256: ${powershell_archive_sha256}" |
| 85 | + echo "${powershell_archive_sha256} *${powershell_filename}" | sha256sum -c - |
| 86 | + fi |
| 87 | + sudo tar xf "${powershell_filename}" -C "${powershell_target_path}" |
| 88 | + sudo ln -s "${powershell_target_path}/pwsh" /usr/local/bin/pwsh |
| 89 | + sudo rm -rf /tmp/pwsh |
| 90 | +} |
| 91 | + |
| 92 | +install_using_github |
| 93 | + |
| 94 | +check "Powershell version as installed by test" bash -c "pwsh --version" |
| 95 | + |
| 96 | +# Report result |
| 97 | +reportResults |
0 commit comments