-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathverify-new.sh
More file actions
executable file
·288 lines (245 loc) · 7.55 KB
/
verify-new.sh
File metadata and controls
executable file
·288 lines (245 loc) · 7.55 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
# Improved verification script for basic-docker engine
# Tests core runtime features including cgroup detection, container lifecycle, and new CLI commands
set -e # Exit on any error
# Color output helpers
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
success() {
echo -e "${GREEN}✓${NC} $1"
}
error() {
echo -e "${RED}✗${NC} $1"
}
info() {
echo -e "${YELLOW}→${NC} $1"
}
section() {
echo ""
echo "======================================"
echo "$1"
echo "======================================"
}
# Track test results
TESTS_PASSED=0
TESTS_FAILED=0
check_result() {
if [ $? -eq 0 ]; then
success "$1"
((TESTS_PASSED++))
else
error "$1"
((TESTS_FAILED++))
return 1
fi
}
# Build the binary
section "Building Project"
if go build -o basic-docker .; then
success "Build successful"
else
error "Build failed"
exit 1
fi
# Test 1: System Information and Cgroup Detection
section "Test 1: System Information & Cgroup Detection"
info "Running: ./basic-docker info"
OUTPUT=$(./basic-docker info 2>&1)
echo "$OUTPUT"
# Check for cgroup version detection
if echo "$OUTPUT" | grep -q "Cgroup version:"; then
success "Cgroup version detected"
else
error "Cgroup version not detected"
fi
# Check for controller information
if echo "$OUTPUT" | grep -q "Memory controller:"; then
success "Memory controller status reported"
else
error "Memory controller status not reported"
fi
if echo "$OUTPUT" | grep -q "CPU controller:"; then
success "CPU controller status reported"
else
error "CPU controller status not reported"
fi
# Test 2: Create a test image
section "Test 2: Creating Test Image"
info "Setting up test image with basic binaries"
TEST_IMAGE_DIR="/tmp/basic-docker/images/test-image/rootfs"
sudo mkdir -p "$TEST_IMAGE_DIR/bin"
# Try to copy echo binary
if [ -f /bin/echo ]; then
sudo cp /bin/echo "$TEST_IMAGE_DIR/bin/"
elif [ -f /usr/bin/echo ]; then
sudo cp /usr/bin/echo "$TEST_IMAGE_DIR/bin/"
else
error "Could not find echo binary"
exit 1
fi
# Try to copy shell binary
if [ -f /bin/sh ]; then
sudo cp /bin/sh "$TEST_IMAGE_DIR/bin/"
elif [ -f /usr/bin/sh ]; then
sudo cp /usr/bin/sh "$TEST_IMAGE_DIR/bin/"
elif [ -f /bin/bash ]; then
sudo cp /bin/bash "$TEST_IMAGE_DIR/bin/sh"
else
error "Could not find shell binary"
exit 1
fi
# Verify binaries are actually present
if [ ! -f "$TEST_IMAGE_DIR/bin/echo" ]; then
error "Failed to copy echo binary to test image"
exit 1
fi
if [ ! -f "$TEST_IMAGE_DIR/bin/sh" ]; then
error "Failed to copy shell binary to test image"
exit 1
fi
success "Test image created with required binaries"
# Test 3: Container Lifecycle - Run and State Tracking
section "Test 3: Container Lifecycle - Run Command"
info "Running: sudo ./basic-docker run test-image /bin/echo 'Hello World'"
if sudo ./basic-docker run test-image /bin/echo "Hello World" 2>&1 | grep -q "Hello World"; then
success "Container executed successfully"
else
error "Container execution failed"
fi
# Test 4: List Containers with State
section "Test 4: List Containers (ps)"
info "Running: sudo ./basic-docker ps"
PS_OUTPUT=$(sudo ./basic-docker ps 2>&1)
echo "$PS_OUTPUT"
if echo "$PS_OUTPUT" | grep -q "STATE"; then
success "Container list shows state column"
else
error "State column missing from ps output"
fi
if echo "$PS_OUTPUT" | grep -q "exited\|running"; then
success "Container state displayed"
else
error "Container state not displayed"
fi
# Get the container ID from ps output
CONTAINER_ID=$(echo "$PS_OUTPUT" | tail -n 1 | awk '{print $1}')
info "Test container ID: $CONTAINER_ID"
# Test 5: Inspect Container
section "Test 5: Inspect Container"
info "Running: sudo ./basic-docker inspect $CONTAINER_ID"
INSPECT_OUTPUT=$(sudo ./basic-docker inspect "$CONTAINER_ID" 2>&1)
echo "$INSPECT_OUTPUT"
if echo "$INSPECT_OUTPUT" | grep -q "\"state\""; then
success "Inspect shows container state"
else
error "Inspect missing state field"
fi
if echo "$INSPECT_OUTPUT" | grep -q "\"command\""; then
success "Inspect shows command"
else
error "Inspect missing command field"
fi
if echo "$INSPECT_OUTPUT" | grep -q "\"created_at\""; then
success "Inspect shows timestamps"
else
error "Inspect missing timestamp fields"
fi
if echo "$INSPECT_OUTPUT" | grep -q "\"exit_code\""; then
success "Inspect shows exit code"
else
error "Inspect missing exit code"
fi
# Test 6: Container Logs
section "Test 6: Container Logs"
info "Running: sudo ./basic-docker logs $CONTAINER_ID"
LOGS_OUTPUT=$(sudo ./basic-docker logs "$CONTAINER_ID" 2>&1)
echo "$LOGS_OUTPUT"
if echo "$LOGS_OUTPUT" | grep -q "Hello World"; then
success "Logs retrieved successfully"
else
error "Logs not retrieved or empty"
fi
# Test 7: Run a Failing Container
section "Test 7: Failed Container State"
info "Running container that should fail"
sudo ./basic-docker run test-image /bin/false 2>&1 || true
# Check that failed state is tracked
PS_FAILED=$(sudo ./basic-docker ps 2>&1)
if echo "$PS_FAILED" | grep -q "failed\|exited"; then
success "Failed container state tracked"
else
error "Failed container state not tracked"
fi
# Test 8: Remove Container
section "Test 8: Remove Container (rm)"
info "Running: sudo ./basic-docker rm $CONTAINER_ID"
if sudo ./basic-docker rm "$CONTAINER_ID" 2>&1 | grep -q "removed successfully"; then
success "Container removed successfully"
else
error "Container removal failed"
fi
# Verify container is gone
PS_AFTER_RM=$(sudo ./basic-docker ps 2>&1)
if echo "$PS_AFTER_RM" | grep -q "$CONTAINER_ID"; then
error "Container still appears after rm"
else
success "Container no longer listed after rm"
fi
# Test 9: Cannot Remove Running Container (safety check)
section "Test 9: Safety - Cannot Remove Running Container"
# This test would require a long-running container, skip for now
info "Skipped (requires long-running container implementation)"
# Test 10: Help Command
section "Test 10: Help Command"
info "Running: ./basic-docker --help"
HELP_OUTPUT=$(./basic-docker 2>&1 || true)
if echo "$HELP_OUTPUT" | grep -q "Usage:"; then
success "Help text displayed"
else
error "Help text not displayed"
fi
if echo "$HELP_OUTPUT" | grep -q "rm\|logs\|inspect"; then
success "New commands documented in help"
else
error "New commands missing from help"
fi
# Test 11: Network Commands (existing functionality)
section "Test 11: Network Commands"
info "Testing network-create"
if ./basic-docker network-create test-network 2>&1 | grep -q "created\|Network"; then
success "Network creation works"
else
error "Network creation failed"
fi
info "Testing network-list"
if ./basic-docker network-list 2>&1 | grep -q "test-network\|net-"; then
success "Network listing works"
else
error "Network listing failed"
fi
# Test 12: Cgroup Cleanup
section "Test 12: Cgroup Cleanup"
info "Verifying cgroup directories are cleaned up"
# Run and remove a container
sudo ./basic-docker run test-image /bin/echo "cleanup test" >/dev/null 2>&1 || true
CLEANUP_CONTAINER=$(sudo ./basic-docker ps 2>&1 | tail -n 1 | awk '{print $1}')
if [ -n "$CLEANUP_CONTAINER" ] && [ "$CLEANUP_CONTAINER" != "CONTAINER" ]; then
sudo ./basic-docker rm "$CLEANUP_CONTAINER" >/dev/null 2>&1 || true
success "Container cleanup completed"
else
info "No container to cleanup"
fi
# Summary
section "Test Summary"
echo "Tests Passed: $TESTS_PASSED"
echo "Tests Failed: $TESTS_FAILED"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
success "All tests passed!"
exit 0
else
error "Some tests failed"
exit 1
fi