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