Skip to content

Commit

Permalink
🧪 test scripts can be run by non-root user
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Jan 4, 2024
1 parent a10044f commit 4abad07
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 125 deletions.
15 changes: 1 addition & 14 deletions test/fish/alpine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@

set -e

NON_ROOT_USER=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} >/dev/null 2>&1; then
NON_ROOT_USER=${CURRENT_USER}
break
fi
done
if [ "${NON_ROOT_USER}" = "" ]; then
NON_ROOT_USER=root
fi

# Optional: Import test library bundled with the devcontainer CLI
source dev-container-features-test-lib

check "fish" fish -v
echo "Testing with user: ${NON_ROOT_USER}"
check "fisher" su "${NON_ROOT_USER}" -c 'fish -c "fisher -v"'
check fish -c "fisher -v"

# Report result
reportResults
55 changes: 1 addition & 54 deletions test/fish/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,6 @@ set -e

source /etc/os-release

cleanup() {
case "${ID}" in
debian|ubuntu)
rm -rf /var/lib/apt/lists/*
;;
esac
}

# Clean up
cleanup

NON_ROOT_USER=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} >/dev/null 2>&1; then
NON_ROOT_USER=${CURRENT_USER}
break
fi
done
if [ "${NON_ROOT_USER}" = "" ]; then
NON_ROOT_USER=root
fi

apt_get_update() {
case "${ID}" in
debian|ubuntu)
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
;;
esac
}

# Checks if packages are installed and installs them if not
check_packages() {
case "${ID}" in
debian|ubuntu)
if ! dpkg -s "$@" >/dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
;;
alpine)
if ! apk -e info "$@" >/dev/null 2>&1; then
apk add --no-cache "$@"
fi
;;
esac
}

check_packages git
LATEST_FISH_VERSION="$(git ls-remote --tags https://github.com/fish-shell/fish-shell | grep -oP "[0-9]+\\.[0-9]+\\.[0-9]+" | sort -V | tail -n 1)"

# Optional: Import test library bundled with the devcontainer CLI
Expand All @@ -70,8 +18,7 @@ case "${ID}" in
check "fish" fish -v
;;
esac
echo "Testing with user: ${NON_ROOT_USER}"
check "fisher" su "${NON_ROOT_USER}" -c 'fish -c "fisher -v"'
check "fisher" fish -c "fisher -v"

# Report result
reportResults
58 changes: 1 addition & 57 deletions test/homebrew/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,11 @@

set -e

source /etc/os-release

cleanup() {
case "${ID}" in
debian|ubuntu)
rm -rf /var/lib/apt/lists/*
;;
esac
}

# Clean up
cleanup

NON_ROOT_USER=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} >/dev/null 2>&1; then
NON_ROOT_USER=${CURRENT_USER}
break
fi
done
if [ "${NON_ROOT_USER}" = "" ]; then
NON_ROOT_USER=root
fi

apt_get_update() {
case "${ID}" in
debian|ubuntu)
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
;;
esac
}

# Checks if packages are installed and installs them if not
check_packages() {
case "${ID}" in
debian|ubuntu)
if ! dpkg -s "$@" >/dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
;;
alpine)
if ! apk -e info "$@" >/dev/null 2>&1; then
apk add --no-cache "$@"
fi
;;
esac
}

check_packages git

# Optional: Import test library bundled with the devcontainer CLI
source dev-container-features-test-lib

# Feature-specific tests
echo "Testing with user: ${NON_ROOT_USER}"
check "homebrew" su "${NON_ROOT_USER}" -c 'brew --version'
check "homebrew" brew --version

# Report result
reportResults

0 comments on commit 4abad07

Please sign in to comment.