#!/usr/bin/env bash # A fake curl for PipelineTests: the registry part of .gitea/scripts/pulizia-docker.sh and .gitea/scripts/artefatti.sh # talk to the packages API of Gitea 28 through it. No network. It knows only the options those scripts use and answers # like Gitea: # GET ?page=&limit=50 the page $FAKE_CURL_WORLD/page-.json, or [] after the last one (200) # DELETE / 204, and the version is written to $FAKE_CURL_WORLD/deleted # GET ?page=&limit=50 the page $FAKE_CURL_WORLD/ci-page-.json, or [] after the last one (200) # DELETE / 204, and the version is written to $FAKE_CURL_WORLD/deleted-ci # PUT // with --upload-file: 201, the file copied to $FAKE_CURL_WORLD/uploaded//; # 409 if that file is already there (as Gitea answers a second upload) # DELETE // 204, and the uploaded file goes # where is the content of $FAKE_CURL_WORLD/base (…/api/v1/packages//container/febe), of # $FAKE_CURL_WORLD/base-ci (…/api/v1/packages//generic/febe-ci) and of $FAKE_CURL_WORLD/upload # (…/api/packages//generic/febe-ci); a base whose file is missing is unknown. Without the header # «Authorization: token » it answers 401; with $FAKE_CURL_WORLD/refuse it answers # 403 to every call; any other address is 404. Every call goes to $FAKE_CURL_WORLD/calls as «METHOD URL», never with # its headers. Any other option is an error (exit 3). set -uo pipefail world="${FAKE_CURL_WORLD:?}" token="$(cat "$world/token" 2> /dev/null)" method=GET output="" write="" url="" authorized=0 upload="" while (($#)); do case "$1" in -s | -S | -sS) ;; --max-time) shift ;; -X) method="$2"; shift ;; -H) [[ -n "$token" && "$2" == "Authorization: token $token" ]] && authorized=1; shift ;; -o) output="$2"; shift ;; -w) write="$2"; shift ;; --upload-file) upload="$2"; shift ;; -*) echo "fake curl: unexpected option $1" >&2; exit 3 ;; *) [[ -z "$url" ]] || { echo "fake curl: a second address $1" >&2; exit 3; } url="$1" ;; esac shift done printf '%s %s\n' "$method" "$url" >> "$world/calls" [[ -z "$write" || "$write" == '%{http_code}' ]] || { echo "fake curl: unexpected -w $write" >&2; exit 3; } [[ -z "$upload" || "$method" == PUT ]] || { echo "fake curl: --upload-file without -X PUT" >&2; exit 3; } answer() { local code="$1" body="${2:-}" if [[ -n "$output" ]]; then printf '%s' "$body" > "$output" else printf '%s' "$body" fi [[ -z "$write" ]] || printf '%s' "$code" exit 0 } [[ -f "$world/refuse" ]] && answer 403 '{"message":"token does not have at least one of required scope(s): [write:package]"}' ((authorized)) || answer 401 '{"message":"token is required"}' known() { [[ -f "$world/$1" ]] && cat "$world/$1"; } base="$(known base)" || base="//nessuna-base" ci="$(known base-ci)" || ci="//nessuna-base" send="$(known upload)" || send="//nessuna-base" page() { local prefix="$1" number="${url#*\?page=}" number="${number%%&*}" if [[ -f "$world/${prefix}page-$number.json" ]]; then answer 200 "$(cat "$world/${prefix}page-$number.json")" fi answer 200 "[]" } case "$method $url" in "GET $base?page="*"&limit=50") page "" ;; "DELETE $base/"*) echo "${url#"$base/"}" >> "$world/deleted" answer 204 ;; "GET $ci?page="*"&limit=50") page "ci-" ;; "DELETE $ci/"*) echo "${url#"$ci/"}" >> "$world/deleted-ci" answer 204 ;; "PUT $send/"*/*) [[ -f "$upload" ]] || { echo "fake curl: no file to upload: $upload" >&2; exit 3; } target="$world/uploaded/${url#"$send/"}" [[ -f "$target" ]] && answer 409 '{"message":"package file already exists"}' mkdir -p "$(dirname "$target")" cp "$upload" "$target" answer 201 ;; "DELETE $send/"*/*) target="$world/uploaded/${url#"$send/"}" [[ -f "$target" ]] || answer 404 '{"message":"The target couldn'"'"'t be found."}' rm -f "$target" answer 204 ;; *) answer 404 '{"message":"The target couldn'"'"'t be found."}' ;; esac