-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_workshop.sh
More file actions
executable file
·250 lines (196 loc) · 5.88 KB
/
Copy pathtest_workshop.sh
File metadata and controls
executable file
·250 lines (196 loc) · 5.88 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
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test directory - use temp dir to avoid polluting the repo
TEST_DIR=$(mktemp -d)
REPO_DIR=$(pwd)
echo -e "${YELLOW}Running workshop tests in: ${TEST_DIR}${NC}"
cleanup() {
echo -e "\n${YELLOW}Cleaning up...${NC}"
cd "$REPO_DIR"
# Stop any running flox services
if [[ -d "$TEST_DIR/.flox" ]]; then
cd "$TEST_DIR"
flox delete --force 2>/dev/null || true
fi
rm -rf "$TEST_DIR"
}
trap cleanup EXIT
pass() {
echo -e "${GREEN}✓ $1${NC}"
}
fail() {
echo -e "${RED}✗ $1${NC}"
exit 1
}
kill_port_3000() {
lsof -ti:3000 | xargs kill -9 2>/dev/null || true
sleep 1
}
wait_for_server() {
local retries=30
local i=0
while [[ $i -lt $retries ]]; do
if curl -s http://localhost:3000/ > /dev/null 2>&1; then
return 0
fi
sleep 1
((i++))
done
return 1
}
# Kill any existing process on port 3000
kill_port_3000
# Copy project files to test directory
cp main.go main_test.go go.mod go.sum quotes.json "$TEST_DIR/"
cd "$TEST_DIR"
echo -e "\n${YELLOW}=== Lab 0: Explore our example app ===${NC}"
# Test that main.go exists
[[ -f main.go ]] && pass "main.go exists" || fail "main.go not found"
# Test that go is NOT available (outside flox)
if command -v go &>/dev/null && [[ -z "${FLOX_ENV:-}" ]]; then
echo -e "${YELLOW} (go is available system-wide, skipping 'not found' test)${NC}"
else
pass "go command not found (as expected)"
fi
echo -e "\n${YELLOW}=== Lab 1: Your first Flox environment ===${NC}"
# Initialize flox
flox init --bare
pass "flox init --bare"
# Install go
flox install go
pass "flox install go"
# Run app with flox and test endpoints
flox activate -- go run main.go quotes.json &
APP_PID=$!
wait_for_server || fail "Server failed to start"
# Test endpoints
curl -s http://localhost:3000/ | grep -q "GET /quotes" && pass "GET / returns endpoints" || fail "GET / failed"
curl -s http://localhost:3000/quotes | grep -q "Steve Jobs" && pass "GET /quotes returns quotes" || fail "GET /quotes failed"
curl -s http://localhost:3000/quotes/0 | grep -q "great work" && pass "GET /quotes/0 returns first quote" || fail "GET /quotes/0 failed"
kill $APP_PID 2>/dev/null || true
wait $APP_PID 2>/dev/null || true
pass "App stopped"
echo -e "\n${YELLOW}=== Lab 2: Running a database ===${NC}"
kill_port_3000
# Install redis
flox install redis
pass "flox install redis"
# Update manifest with redis service
cat >> .flox/env/manifest.toml << 'EOF'
[services.redis]
command = "redis-server --port $REDISPORT"
[vars]
REDISPORT = "6379"
[profile]
common = """
alias load_quotes='redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"'
"""
EOF
pass "Added redis service config"
# Create a helper script to load quotes and test redis
cat > test_redis.sh << 'TESTSCRIPT'
#!/usr/bin/env bash
set -e
wait_for_redis() {
for i in {1..30}; do
redis-cli -p $REDISPORT ping > /dev/null 2>&1 && return 0
sleep 1
done
return 1
}
wait_for_server() {
for i in {1..30}; do
curl -s http://localhost:3000/ > /dev/null 2>&1 && return 0
sleep 1
done
return 1
}
wait_for_redis
redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
echo "Quotes loaded into Redis"
go run main.go redis &
APP_PID=$!
wait_for_server
RESULT=$(curl -s http://localhost:3000/quotes)
kill $APP_PID 2>/dev/null || true
wait $APP_PID 2>/dev/null || true
echo "$RESULT" | grep -q "Steve Jobs"
TESTSCRIPT
chmod +x test_redis.sh
# Activate with services, load quotes and test
flox activate --start-services -- ./test_redis.sh && pass "Redis service works with app" || fail "Redis test failed"
echo -e "\n${YELLOW}=== Lab 3: Reusing environments (composition) ===${NC}"
kill_port_3000
# Replace manifest with includes
cat > .flox/env/manifest.toml << 'EOF'
version = 1
[install]
[vars]
[include]
environments = [
{ remote = "flox/go" },
{ remote = "flox/redis" }
]
EOF
pass "Replaced manifest with includes"
# Create helper script to test composed environment
cat > test_composed.sh << 'TESTSCRIPT'
#!/usr/bin/env bash
set -e
wait_for_redis() {
for i in {1..30}; do
redis-cli -p $REDISPORT ping > /dev/null 2>&1 && return 0
sleep 1
done
return 1
}
wait_for_server() {
for i in {1..30}; do
curl -s http://localhost:3000/ > /dev/null 2>&1 && return 0
sleep 1
done
return 1
}
wait_for_redis
redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
go run main.go redis &
APP_PID=$!
wait_for_server
RESULT=$(curl -s http://localhost:3000/quotes)
kill $APP_PID 2>/dev/null || true
echo "$RESULT" | grep -q "Steve Jobs"
TESTSCRIPT
chmod +x test_composed.sh
# Test with composed environment
flox activate --start-services -- ./test_composed.sh && pass "Composed environment works" || fail "Composed environment failed"
echo -e "\n${YELLOW}=== Lab 4: Prepare for production (build) ===${NC}"
kill_port_3000
# Add build section
cat >> .flox/env/manifest.toml << 'EOF'
[build.quotes-app]
command = """
mkdir -p $out/bin $out/share
cp quotes.json $out/share/
go build -trimpath -o $out/bin/quotes-app main.go
"""
EOF
pass "Added build config"
# Build
flox build
pass "flox build succeeded"
# Test built binary
[[ -x ./result-quotes-app/bin/quotes-app ]] && pass "Binary exists and is executable" || fail "Binary not found"
./result-quotes-app/bin/quotes-app ./result-quotes-app/share/quotes.json &
APP_PID=$!
wait_for_server || fail "Built binary failed to start"
curl -s http://localhost:3000/quotes | grep -q "Steve Jobs" && pass "Built binary serves quotes" || fail "Built binary failed"
kill $APP_PID 2>/dev/null || true
wait $APP_PID 2>/dev/null || true
# Skip publish (requires authentication)
echo -e "${YELLOW} (skipping flox publish - requires authentication)${NC}"
echo -e "\n${GREEN}=== All workshop tests passed! ===${NC}"