Files
fission-console/scripts/smoke_user_journeys.sh
T

119 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE="https://fission.kube5s.ru/console/api"
COUNT=${1:-5}
RUN_ID="journey$(date +%s | tail -c 6)"
ROOT_DIR=$(cd "$(dirname "$0")" && pwd)
NS_CLEANUP_SCRIPT="${ROOT_DIR}/cleanup_test_namespaces.sh"
TMP=$(mktemp -d)
declare -a USERS=()
cleanup() {
rm -rf "$TMP" 2>/dev/null || true
if [ -x "$NS_CLEANUP_SCRIPT" ] && [ ${#USERS[@]} -gt 0 ]; then
KUBECTL_DIRECT=1 "$NS_CLEANUP_SCRIPT" "${USERS[@]}" >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT
json_field() {
local payload="$1"
local key="$2"
printf '%s' "$payload" | python3 -c 'import json,sys
key=sys.argv[1]
try:
data=json.load(sys.stdin)
value=data.get(key, "")
print(value if not isinstance(value, (dict,list)) else json.dumps(value))
except Exception:
print("")' "$key"
}
json_escape() {
python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}
create_call() {
local sub="$1"
local body="$2"
curl -s -w "\n%{http_code}" --max-time 60 -X POST "${BASE}/functions" \
-H "X-Auth-Token: ${sub}" -H 'Content-Type: application/json' -d "$body"
}
invoke_call() {
local sub="$1"
local name="$2"
curl -s --max-time 45 -X POST "${BASE}/functions/${name}/invoke" \
-H "X-Auth-Token: ${sub}" -H 'Content-Type: application/json' -d '{}' || true
}
echo "=== USER JOURNEYS START run=$RUN_ID count=$COUNT ==="
for i in $(seq 1 "$COUNT"); do
USERS+=("${RUN_ID}-u${i}@test.local")
(
U="${RUN_ID}-u${i}@test.local"
FN="journey-${RUN_ID}-${i}"
RESULT="PASS"
AUTH=$(curl -s -w "\n%{http_code}" --max-time 45 -X POST "${BASE}/auth" -H 'Content-Type: application/json' -d "{\"token\":\"${U}\",\"env\":\"test\"}")
AUTH_CODE=$(printf '%s' "$AUTH" | tail -1)
[ "$AUTH_CODE" = "200" ] || RESULT="FAIL auth=${AUTH_CODE}"
BAD=$(create_call "$U" '{"name":"bad-lang","language":"cobol","code":"x"}')
BAD_CODE=$(printf '%s' "$BAD" | tail -1)
[ "$BAD_CODE" = "400" ] || RESULT="FAIL bad_create=${BAD_CODE}"
CODE_TEXT=$(printf 'def main():\n return "journey-ok-%s-%s"\n' "$RUN_ID" "$i")
CODE_JSON=$(printf '%s' "$CODE_TEXT" | json_escape)
GOOD=$(create_call "$U" "{\"name\":\"${FN}\",\"language\":\"python\",\"code\":${CODE_JSON}}")
GOOD_CODE=$(printf '%s' "$GOOD" | tail -1)
[ "$GOOD_CODE" = "201" ] || RESULT="FAIL create=${GOOD_CODE}"
DUP=$(create_call "$U" "{\"name\":\"${FN}\",\"language\":\"python\",\"code\":${CODE_JSON}}")
DUP_CODE=$(printf '%s' "$DUP" | tail -1)
{ [ "$DUP_CODE" = "409" ] || [ "$DUP_CODE" = "502" ]; } || RESULT="FAIL dup=${DUP_CODE}"
INV_STATUS=""
INV_RAW=""
for try in 1 2 3 4 5 6; do
INV=$(invoke_call "$U" "$FN")
INV_STATUS=$(json_field "$INV" status)
INV_RAW=$(json_field "$INV" response_raw)
echo "USER=$i TRY=$try status=$INV_STATUS raw=$INV_RAW"
if [ "$INV_STATUS" = "200" ] && printf '%s' "$INV_RAW" | grep -q "journey-ok-${RUN_ID}-${i}"; then
break
fi
[ "$try" -lt 6 ] && sleep 5
done
[ "$INV_STATUS" = "200" ] || RESULT="FAIL invoke=${INV_STATUS}"
DEL1=$(curl -s -o /dev/null -w "%{http_code}" --max-time 45 -X DELETE "${BASE}/functions/${FN}" -H "X-Auth-Token: ${U}")
[ "$DEL1" = "200" ] || RESULT="FAIL delete1=${DEL1}"
DEL2=$(curl -s -o /dev/null -w "%{http_code}" --max-time 45 -X DELETE "${BASE}/functions/${FN}" -H "X-Auth-Token: ${U}")
[ "$DEL2" = "404" ] || RESULT="FAIL delete2=${DEL2}"
echo "$i:$RESULT" > "${TMP}/user-$i"
) &
done
wait
PASS=0
FAIL=0
for i in $(seq 1 "$COUNT"); do
R=$(cat "${TMP}/user-$i" 2>/dev/null || echo "$i:FAIL missing_result")
STATUS=$(printf '%s' "$R" | cut -d: -f2-)
if [ "$STATUS" = "PASS" ]; then
PASS=$((PASS+1))
echo "USER_PASS $i"
else
FAIL=$((FAIL+1))
echo "USER_FAIL $i $STATUS"
fi
done
echo "USER_JOURNEYS_PASS=$PASS/$COUNT"
echo "=== USER JOURNEYS END ==="
[ "$FAIL" = 0 ]