-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest-docker.sh
More file actions
executable file
·234 lines (188 loc) · 5.97 KB
/
test-docker.sh
File metadata and controls
executable file
·234 lines (188 loc) · 5.97 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
#!/bin/bash
# Test script for Docker permission handling and healthcheck
# Builds base and production images, then tests various runtime configurations
set -e
BASE_IMAGE_NAME="databasement-php-test"
PROD_IMAGE_NAME="databasement-test"
CONTAINER_NAME="databasement-test-container"
PORT=2227
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Track temp directories for cleanup
TEMP_DIRS=()
cleanup() {
echo -e "\n${YELLOW}Cleaning up...${NC}"
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
# Clean up all temp directories
for dir in "${TEMP_DIRS[@]}"; do
if [ -d "$dir" ]; then
rm -rf "$dir" 2>/dev/null || true
fi
done
}
trap cleanup EXIT
# Create a temp data directory with correct ownership
# Usage: create_data_dir <uid> <gid>
create_data_dir() {
local uid=$1
local gid=$2
local dir
dir=$(mktemp -d)
TEMP_DIRS+=("$dir")
# Use docker to chown since the UID might not exist on host
docker run --rm -v "$dir:/data" "$BASE_IMAGE_NAME:latest" chown "$uid:$gid" /data
echo "$dir"
}
pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
}
fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
echo -e "${YELLOW}Container logs:${NC}"
docker logs "$CONTAINER_NAME" 2>&1 | tail -30
exit 1
}
info() {
echo -e "${BLUE}→${NC} $1"
}
wait_for_healthy() {
local max_attempts=30
local attempt=1
info "Waiting for container to be healthy..."
while [ $attempt -le $max_attempts ]; do
if curl -sf "http://localhost:$PORT/health" > /dev/null 2>&1; then
pass "Healthcheck passed (attempt $attempt)"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
fail "Healthcheck failed after $max_attempts attempts"
}
verify_frankenphp() {
local max_attempts=15
local attempt=1
info "Waiting for FrankenPHP to be ready..."
while [ $attempt -le $max_attempts ]; do
local logs
logs=$(docker logs "$CONTAINER_NAME" 2>&1)
if echo "$logs" | grep -q "FrankenPHP started"; then
pass "FrankenPHP started successfully"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
fail "FrankenPHP did not start (missing 'FrankenPHP started' message)"
}
echo "=========================================="
echo "Docker Build & Permission Tests"
echo "=========================================="
echo ""
# Step 1: Build base image
echo -e "${YELLOW}Step 1: Building base image from docker/php/Dockerfile...${NC}"
docker build -f docker/php/Dockerfile -t "$BASE_IMAGE_NAME:latest" docker/php/ --quiet
pass "Base image built: $BASE_IMAGE_NAME:latest"
echo ""
# Step 2: Build production image
echo -e "${YELLOW}Step 2: Building production image from Dockerfile...${NC}"
docker build --build-arg BASE_IMAGE="$BASE_IMAGE_NAME:latest" -t "$PROD_IMAGE_NAME:latest" . --quiet
pass "Production image built: $PROD_IMAGE_NAME:latest"
echo ""
# Test 1: Default settings (root start, PUID=1000 PGID=1000)
echo "=========================================="
echo "Test 1: Default settings (PUID=1000 PGID=1000)"
echo "=========================================="
TEST_DATA_DIR=$(create_data_dir 1000 1000)
info "Created data dir: $TEST_DATA_DIR (owned by 1000:1000)"
info "Starting container with default settings..."
docker run -d \
--name "$CONTAINER_NAME" \
-p "$PORT:2226" \
-v "$TEST_DATA_DIR:/data" \
-e ENABLE_QUEUE_WORKER=true \
-e DB_CONNECTION=sqlite \
-e DB_DATABASE=/data/database.sqlite \
"$PROD_IMAGE_NAME:latest"
sleep 3
LOGS=$(docker logs "$CONTAINER_NAME" 2>&1)
echo "$LOGS" | head -10
if echo "$LOGS" | grep -q "Setting up user application"; then
pass "Permission setup ran"
else
fail "Permission setup did not run"
fi
verify_frankenphp
wait_for_healthy
cleanup
echo ""
# Test 2: Custom PUID/PGID
echo "=========================================="
echo "Test 2: Custom PUID=1500 PGID=1500"
echo "=========================================="
TEST_DATA_DIR=$(create_data_dir 1500 1500)
info "Created data dir: $TEST_DATA_DIR (owned by 1500:1500)"
info "Starting container with PUID=1500 PGID=1500..."
docker run -d \
--name "$CONTAINER_NAME" \
-p "$PORT:2226" \
-v "$TEST_DATA_DIR:/data" \
-e PUID=1500 \
-e PGID=1500 \
-e ENABLE_QUEUE_WORKER=true \
-e DB_CONNECTION=sqlite \
-e DB_DATABASE=/data/database.sqlite \
"$PROD_IMAGE_NAME:latest"
sleep 3
LOGS=$(docker logs "$CONTAINER_NAME" 2>&1)
echo "$LOGS" | head -10
if echo "$LOGS" | grep -q "Setting up user application with PUID=1500"; then
pass "Custom PUID/PGID detected"
else
fail "Custom PUID/PGID not detected"
fi
verify_frankenphp
wait_for_healthy
cleanup
echo ""
# Test 3: --user flag (no permission fix, runs as specified user)
echo "=========================================="
echo "Test 3: --user 1001:1001 flag"
echo "=========================================="
TEST_DATA_DIR=$(create_data_dir 1001 1001)
info "Created data dir: $TEST_DATA_DIR (owned by 1001:1001)"
info "Starting container with --user 1001:1001..."
docker run -d \
--name "$CONTAINER_NAME" \
-p "$PORT:2226" \
--user 1001:1001 \
-v "$TEST_DATA_DIR:/data" \
-e ENABLE_QUEUE_WORKER=true \
-e DB_CONNECTION=sqlite \
-e DB_DATABASE=/data/database.sqlite \
"$PROD_IMAGE_NAME:latest"
sleep 3
LOGS=$(docker logs "$CONTAINER_NAME" 2>&1)
echo "$LOGS" | head -10
if echo "$LOGS" | grep -q "Not running as root, skipping permission fix"; then
pass "Permission fix skipped (expected when using --user)"
else
fail "Should skip permission fix when using --user"
fi
if echo "$LOGS" | grep -q "supervisord started with pid"; then
pass "Supervisor started successfully as non-root user"
else
fail "Supervisor did not start"
fi
verify_frankenphp
wait_for_healthy
cleanup
echo ""
echo "=========================================="
echo -e "${GREEN}All tests passed!${NC}"
echo "=========================================="