Skip to content

Commit f147da0

Browse files
committed
local testing invoke:all
1 parent e30851b commit f147da0

2 files changed

Lines changed: 263 additions & 0 deletions

File tree

File renamed without changes.

‎invoke-local.sh‎

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
#!/bin/bash
2+
#
3+
# invoke-local.sh - Test all function templates locally
4+
#
5+
# This script replicates the invoke-all.yaml GitHub workflow for local testing.
6+
# It discovers all language/template combinations, builds, runs, and invokes each one.
7+
#
8+
# Usage: ./invoke-local.sh
9+
#
10+
# Environment variables:
11+
# FUNC_BIN - Path to func binary (default: func)
12+
# FUNC_REGISTRY - Container registry to use (default: quay.io/test)
13+
#
14+
# Requirements:
15+
# - func CLI installed
16+
# - hugo (for go/blog template)
17+
# - npm (for typescript templates)
18+
# - cargo (for rust templates)
19+
20+
set -euo pipefail
21+
22+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23+
WORKDIR=$(mktemp -d)
24+
HOST_ENABLED_LANGUAGES=("go" "python")
25+
REGISTRY="${FUNC_REGISTRY:-quay.io/test}"
26+
FUNC_BIN="${FUNC_BIN:-func}"
27+
28+
# Colors for output
29+
RED='\033[0;31m'
30+
GREEN='\033[0;32m'
31+
YELLOW='\033[1;33m'
32+
NC='\033[0m' # No Color
33+
34+
# Track results
35+
declare -a PASSED=()
36+
declare -a FAILED=()
37+
declare -a FAILED_DIRS=()
38+
39+
log_info() {
40+
echo -e "${GREEN}[INFO]${NC} $1"
41+
}
42+
43+
log_warn() {
44+
echo -e "${YELLOW}[WARN]${NC} $1"
45+
}
46+
47+
log_error() {
48+
echo -e "${RED}[ERROR]${NC} $1"
49+
}
50+
51+
cleanup_on_success() {
52+
local dir=$1
53+
if [[ -d "$dir" ]]; then
54+
rm -rf "$dir"
55+
log_info "Cleaned up: $dir"
56+
fi
57+
}
58+
59+
is_host_enabled() {
60+
local lang=$1
61+
for enabled in "${HOST_ENABLED_LANGUAGES[@]}"; do
62+
if [[ "$lang" == "$enabled" ]]; then
63+
return 0
64+
fi
65+
done
66+
return 1
67+
}
68+
69+
run_prerequisites() {
70+
local language=$1
71+
local template=$2
72+
73+
log_info "Running prerequisites for $language/$template"
74+
75+
case "$language/$template" in
76+
go/blog)
77+
log_info "Building hugo static files"
78+
make
79+
;;
80+
typescript/*)
81+
log_info "Running npm install"
82+
npm install
83+
;;
84+
rust/*)
85+
log_info "Running cargo build"
86+
cargo build
87+
;;
88+
*)
89+
log_info "No prerequisites needed"
90+
;;
91+
esac
92+
}
93+
94+
test_template() {
95+
local language=$1
96+
local template=$2
97+
local func_name="$language-$template"
98+
local func_dir="$WORKDIR/$func_name"
99+
local run_pid=""
100+
101+
log_info "=========================================="
102+
log_info "Testing: $language/$template"
103+
log_info "=========================================="
104+
105+
# Determine builder
106+
local builder="pack"
107+
if is_host_enabled "$language"; then
108+
builder="host"
109+
fi
110+
log_info "Using builder: $builder"
111+
112+
# Create function using func create with local repository (file:// protocol)
113+
local repo_uri="file://$SCRIPT_DIR"
114+
log_info "Creating function with: $FUNC_BIN create $func_name -r $repo_uri -l $language -t $template"
115+
cd "$WORKDIR"
116+
117+
if ! $FUNC_BIN create "$func_name" -r "$repo_uri" -l "$language" -t "$template"; then
118+
log_error "func create failed for $language/$template"
119+
return 1
120+
fi
121+
122+
cd "$func_dir"
123+
124+
if [[ ! -f "func.yaml" ]]; then
125+
log_error "No func.yaml found after func create"
126+
return 1
127+
fi
128+
129+
# Run prerequisites
130+
run_prerequisites "$language" "$template"
131+
132+
# Build
133+
log_info "Building function"
134+
if ! FUNC_REGISTRY="$REGISTRY" $FUNC_BIN build --builder="$builder"; then
135+
log_error "Build failed for $language/$template"
136+
return 1
137+
fi
138+
139+
# Run in background
140+
log_info "Starting function"
141+
$FUNC_BIN run --build=false &
142+
run_pid=$!
143+
144+
# Check if process started
145+
sleep 2
146+
if ! ps -p $run_pid > /dev/null 2>&1; then
147+
log_error "Failed to start function"
148+
return 1
149+
fi
150+
151+
# Wait for function to be ready
152+
log_info "Waiting for function to be ready..."
153+
sleep 10
154+
155+
# Invoke with retries
156+
local max_retries=5
157+
local retry_count=0
158+
local success=false
159+
160+
while [[ $retry_count -lt $max_retries ]]; do
161+
log_info "Invoke attempt $((retry_count + 1)) of $max_retries"
162+
if $FUNC_BIN invoke --request-type=GET 2>/dev/null; then
163+
log_info "Invoke succeeded!"
164+
success=true
165+
break
166+
else
167+
log_warn "Invoke failed, retrying..."
168+
retry_count=$((retry_count + 1))
169+
sleep 5
170+
fi
171+
done
172+
173+
# Cleanup: kill the running function
174+
if [[ -n "$run_pid" ]] && ps -p $run_pid > /dev/null 2>&1; then
175+
kill $run_pid 2>/dev/null || true
176+
wait $run_pid 2>/dev/null || true
177+
fi
178+
179+
if [[ "$success" == "true" ]]; then
180+
return 0
181+
else
182+
return 1
183+
fi
184+
}
185+
186+
print_summary() {
187+
echo ""
188+
echo "=========================================="
189+
echo " SUMMARY"
190+
echo "=========================================="
191+
echo ""
192+
193+
if [[ ${#PASSED[@]} -gt 0 ]]; then
194+
echo -e "${GREEN}PASSED (${#PASSED[@]}):${NC}"
195+
for item in "${PASSED[@]}"; do
196+
echo " - $item"
197+
done
198+
fi
199+
200+
echo ""
201+
202+
if [[ ${#FAILED[@]} -gt 0 ]]; then
203+
echo -e "${RED}FAILED (${#FAILED[@]}):${NC}"
204+
for i in "${!FAILED[@]}"; do
205+
echo " - ${FAILED[$i]}"
206+
echo " Preserved at: ${FAILED_DIRS[$i]}"
207+
done
208+
fi
209+
210+
echo ""
211+
echo "Total: $((${#PASSED[@]} + ${#FAILED[@]})) | Passed: ${#PASSED[@]} | Failed: ${#FAILED[@]}"
212+
213+
if [[ ${#FAILED[@]} -gt 0 ]]; then
214+
return 1
215+
fi
216+
return 0
217+
}
218+
219+
main() {
220+
log_info "Starting local template testing"
221+
log_info "Script directory: $SCRIPT_DIR"
222+
log_info "Work directory: $WORKDIR"
223+
echo ""
224+
225+
# Find all language directories
226+
for lang_dir in "$SCRIPT_DIR"/*/; do
227+
# Skip hidden directories and non-directories
228+
[[ ! -d "$lang_dir" ]] && continue
229+
[[ "$(basename "$lang_dir")" == .* ]] && continue
230+
231+
local language=$(basename "$lang_dir")
232+
233+
# Skip non-language directories
234+
if [[ "$language" == "docs" ]] || [[ "$language" == ".github" ]]; then
235+
continue
236+
fi
237+
238+
# Find all templates in this language
239+
for template_dir in "$lang_dir"/*/; do
240+
[[ ! -d "$template_dir" ]] && continue
241+
242+
local template=$(basename "$template_dir")
243+
local test_name="$language/$template"
244+
local func_dir="$WORKDIR/$language-$template"
245+
246+
if test_template "$language" "$template"; then
247+
PASSED+=("$test_name")
248+
cleanup_on_success "$func_dir"
249+
else
250+
FAILED+=("$test_name")
251+
FAILED_DIRS+=("$func_dir")
252+
log_error "FAILED: $test_name (preserved at $func_dir)"
253+
fi
254+
255+
echo ""
256+
done
257+
done
258+
259+
print_summary
260+
}
261+
262+
# Run main
263+
main "$@"

0 commit comments

Comments
 (0)