Skip to content

Commit

Permalink
Merge pull request #61 from delilahw/fix-install-script-and-tests
Browse files Browse the repository at this point in the history
fix(keyring): Fix install script and add tests
  • Loading branch information
markphip authored May 17, 2024
2 parents 6900f62 + d313a46 commit 9ed0329
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/artifacts-helper/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Azure Artifacts Credential Helper",
"id": "artifacts-helper",
"version": "1.0.8",
"version": "1.0.9",
"description": "Configures Codespace to authenticate with Azure Artifact feeds",
"options": {
"nugetURIPrefixes": {
Expand Down Expand Up @@ -51,13 +51,14 @@
}
},
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
"ghcr.io/devcontainers/features/common-utils",
"ghcr.io/devcontainers/features/python"
],
"customizations": {
"vscode": {
"extensions": [
"ms-codespaces-tools.ado-codespaces-auth"
]
}
}
}
}
13 changes: 5 additions & 8 deletions src/artifacts-helper/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ cp ./scripts/run-rush-pnpm.sh /usr/local/bin/run-rush-pnpm.sh
chmod +rx /usr/local/bin/run-rush-pnpm.sh


if [ "${INSTALL_PIP_HELPER}" = "true" ]; then
USER="${_REMOTE_USER}" /tmp/install-python-keyring.sh
rm /tmp/install-python-keyring.sh
fi

if command -v sudo >/dev/null 2>&1; then
if [ "root" != "$_REMOTE_USER" ]; then
if [ "${ALIAS_DOTNET}" = "true" ]; then
Expand Down Expand Up @@ -104,10 +109,6 @@ if command -v sudo >/dev/null 2>&1; then
fi
sudo -u ${_REMOTE_USER} bash -c "/tmp/install-provider.sh ${USENET6}"
rm /tmp/install-provider.sh
if [ "${INSTALL_PIP_HELPER}" = "true" ]; then
sudo -u ${_REMOTE_USER} bash -c "/tmp/install-python-keyring.sh"
rm /tmp/install-python-keyring.sh
fi
exit 0
fi
fi
Expand Down Expand Up @@ -145,10 +146,6 @@ if [ "${ALIAS_RUSH}" = "true" ]; then
sudo -u ${_REMOTE_USER} bash -c "echo 'alias rush-pnpm=/usr/local/bin/run-rush-pnpm.sh' >> /etc/zsh/zshrc || true
fi

if [ "${INSTALL_PIP_HELPER}" = "true" ]; then
bash -c "/tmp/install-python-keyring.sh"
rm /tmp/install-python-keyring.sh
fi
rm /tmp/install-provider.sh

exit 0
73 changes: 62 additions & 11 deletions src/artifacts-helper/scripts/install-python-keyring.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

WHEEL_URL='https://github.com/microsoft/codespace-features/releases/download/latest/codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl'
WHEEL_DEST_FILENAME='codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl'

cd /tmp
wget https://github.com/microsoft/codespace-features/releases/download/latest/codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl
if python3 -m pip &> /dev/null; then
python3 -m pip install codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl
elif command -v pip3 &> /dev/null; then
pip3 install codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl
elif python -m pip &> /dev/null; then
python -m pip install codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl
elif command -v pip &> /dev/null; then
pip install codespaces_artifacts_helper_keyring-0.1.0-py3-none-any.whl

while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --help)
echo "Usage: $(basename "$0") [(-u | --user) <user>] [-h | --help]
Options:
-h --help Show this screen.
-u USER, --user USER Install for another user by specifying their name.
Alternatively, set the USER environment variable.
"
exit
;;

-u | --user)
USER="$2"
shift
;;

*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done

# Find the path to the Python executable outside of sudo, to account for Python
# not being present in the secure_path.
if command -v python3 &>/dev/null; then
PYTHON_SRC=$(which python3)
elif command -v python &>/dev/null; then
PYTHON_SRC=$(which python)
else
echo "pip installation not detected. Artifacts Helper keyring not installed."
echo "Python not found. Artifacts Helper keyring not installed. whoami=$(whoami), PATH=$PATH"
exit 1
fi

sudo_if_user() {
COMMAND="$*"
if [[ $USER ]]; then
if ! command -v sudo >/dev/null 2>&1; then
echo "The --user option was specified, but sudo could not be found."
exit 1
fi
sudo -u "$USER" bash -c "$COMMAND"
else
$COMMAND
fi
}

install_user_package() {
sudo_if_user "${PYTHON_SRC}" -m pip install --user --upgrade --no-cache-dir "$1"
}

wget "$WHEEL_URL" -O "$WHEEL_DEST_FILENAME"
chmod a=r "$WHEEL_DEST_FILENAME" # Usually not needed, but helpful just in case
install_user_package "$WHEEL_DEST_FILENAME"
rm "$WHEEL_DEST_FILENAME"
4 changes: 2 additions & 2 deletions test/_global/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"microsoft-git": {
"version": "2.39.2.vfs.0.0"
},
"artifacts-helper": {"python" : "true"},
"artifacts-helper": {},
"external-repository": {
"gitProvider": "azuredevops",
"cloneUrl": "https://github.com/devcontainers/features",
Expand Down Expand Up @@ -33,7 +33,7 @@
"options": "--single-branch --no-src",
"scalar": "true"
},
"artifacts-helper": {"python" : "true"}
"artifacts-helper": {}
},
"remoteEnv": {
"EXT_GIT_PAT": "dummypat"
Expand Down
14 changes: 14 additions & 0 deletions test/artifacts-helper/check_python_and_keyring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

# Import test library bundled with the devcontainer CLI
source dev-container-features-test-lib || exit 1

# Confidence check for prerequisites
check 'pip should be installed' python3 -m pip --version

check 'keyring should be installed' python3 -m pip show keyring
check 'codespaces_artifacts_helper_keyring should be installed' python3 -m pip show codespaces_artifacts_helper_keyring

# Report results
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults
4 changes: 4 additions & 0 deletions test/artifacts-helper/python312_and_keyring_debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

set -e
./check_python_and_keyring.sh
4 changes: 4 additions & 0 deletions test/artifacts-helper/python38_and_keyring_debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

set -e
./check_python_and_keyring.sh
4 changes: 4 additions & 0 deletions test/artifacts-helper/python38_and_keyring_ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

set -e
./check_python_and_keyring.sh
13 changes: 13 additions & 0 deletions test/artifacts-helper/python_and_no_keyring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Import test library bundled with the devcontainer CLI
source dev-container-features-test-lib || exit 1

# Confidence check for prerequisites
check 'pip should be installed' bash -c 'python3 -m pip --version'

check 'codespaces_artifacts_helper_keyring should not be installed' bash -c '! python3 -m pip show codespaces_artifacts_helper_keyring'

# Report results
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults
46 changes: 46 additions & 0 deletions test/artifacts-helper/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"python38_and_keyring_debian": {
"image": "mcr.microsoft.com/devcontainers/base:debian",
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.8"
},
"artifacts-helper": {
"python": true
}
}
},
"python38_and_keyring_ubuntu": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.8"
},
"artifacts-helper": {
"python": true
}
}
},
"python312_and_keyring_debian": {
"image": "mcr.microsoft.com/devcontainers/base:debian",
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.12"
},
"artifacts-helper": {
"python": true
}
}
},
"python_and_no_keyring": {
"image": "mcr.microsoft.com/devcontainers/base:debian",
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.8"
},
"artifacts-helper": {
"python": false
}
}
}
}

0 comments on commit 9ed0329

Please sign in to comment.