ldc-web-images-docker/PHP8.5/utils.sh
2026-09-23 16:27:23 +02:00

87 lines
No EOL
2.1 KiB
Bash

# Variables.
IMAGE="${1:-image-name:latest}"
PASS=0
FAIL=0
ERRORS=()
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
# Functions to test element in target image.
# ex: run <label> <command>
run() {
local label="$1";
local labelColored="${NC} ${label}"
local command="$2"
local user="${3:-root}"
if docker run --rm --user "$user" --entrypoint /bin/sh "$IMAGE" -c "$command" > /dev/null 2>&1; then
echo -e " ${GREEN}✔ ${labelColored}"
(( PASS++ )) || true
else
echo -e " ${RED}✘ ${labelColored}"
ERRORS+=("$label")
(( FAIL++ )) || true
fi
}
# Functions to test element in target image with output.
# ex: run_output <label> <command>
run_output() {
local label="$1";
local labelColored="${NC} ${label}"
local command="$2"
local user="${3:-root}"
local out
if out=$(docker run --rm --user "$user" --entrypoint /bin/sh "$IMAGE" -c "$command" 2>&1); then
echo -e " ${GREEN}✔ ${labelColored}${CYAN} → $(echo "$out" | head -1)${NC}"
(( PASS++ )) || true
else
echo -e " ${RED}✘ ${labelColored}"
echo -e " ${YELLOW}↳ $out${NC}"
ERRORS+=("$label")
(( FAIL++ )) || true
fi
}
# Functions to display section.
# ex: section <label>
section() {
echo ""
echo -e "${CYAN}▶ $1${NC}"
}
# Functions to display a starting section of tests.
start_tests() {
# valid image
echo ""
echo -e " Image: ${YELLOW}${IMAGE}${NC}"
if ! docker image inspect "$IMAGE" > /dev/null 2>&1; then
echo -e "${RED}✘ Image not found: ${IMAGE}${NC}"
echo ""
echo " To create a new docker image: docker build -t ${IMAGE} ."
exit 1
fi
}
# Functions to display an ending section of tests with results.
end_tests() {
TOTAL=$(( PASS + FAIL ))
echo ""
echo -e "${GREEN}▶ ${PASS}/${TOTAL} tests passed${NC} $([ $FAIL -gt 0 ] || echo "")\n"
if [ ${#ERRORS[@]} -gt 0 ]; then
echo -e "${RED}▶ ${FAIL} tests failed:${NC}"
for e in "${ERRORS[@]}"; do
echo -e " ${RED}✘ ${NC} $e"
done
echo ""
exit 1
fi
exit 0
}