Skip to content

Commit d18b21a

Browse files
committed
test: add end-to-end tests
1 parent 96b4891 commit d18b21a

File tree

5 files changed

+275
-2
lines changed

5 files changed

+275
-2
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ test-cov: ## run tests with coverage
8787
$(GO) tool cover -html=coverage.out -o coverage.html
8888
@echo "✓ Coverage report generated: coverage.html"
8989

90+
.PHONY: e2e
91+
e2e: build ## run end-to-end tests
92+
@echo "Running end-to-end tests..."
93+
@bash tests/e2e_test.sh
94+
@echo "✓ End-to-end tests completed"
95+
9096
.PHONY: bench
9197
bench: ## run benchmarks
9298
@echo "Running benchmarks..."

internal/config/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var (
99
BuildTime string
1010
BuildCommit string
1111
BuildBranch string
12-
Version = "0.4.1"
12+
Version = "0.4.2"
1313
)
1414

1515
func FullVersion() string {

pkg/client/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (c *Client) Download(ctx context.Context) error {
108108
}
109109

110110
// Basic download, no concurrency, no resume support
111+
c.logger.Debug("", zap.String("msg", "Starting basic download"))
111112
return c.BasicDownload(ctx)
112113
}
113114

pkg/client/resume.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (c *Client) downloadWithResume(ctx context.Context, fileSize int64) error {
5050

5151
chunks := c.calculateChunks(newExistingSize, fileSize)
5252

53-
c.logger.Info("",
53+
c.logger.Debug("",
5454
zap.String("msg", "Starting resume download"),
5555
zap.Int("chunks", len(chunks)),
5656
zap.Int(("concurrent"), c.config.MaxConcurrency),

tests/e2e_test.sh

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
#!/bin/bash
2+
3+
# EZFT End-to-End Test Script
4+
# Tests basic download, non-concurrent chunked download, and concurrent chunked download
5+
6+
set -e
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
BLUE='\033[0;34m'
13+
NC='\033[0m' # No Color
14+
15+
# Configuration
16+
SERVER_PORT=18080
17+
SERVER_DIR="./temp/server_files"
18+
CLIENT_OUTPUT_DIR="./temp/downloads"
19+
LOG_DIR="./logs"
20+
BINARY_PATH="./build/ezft"
21+
TEST_FILE_NAME="test_file.txt"
22+
TEST_FILE_SIZE="10MB"
23+
24+
# Function to print colored output
25+
print_info() {
26+
echo -e "${BLUE}[INFO]${NC} $1"
27+
}
28+
29+
print_success() {
30+
echo -e "${GREEN}[SUCCESS]${NC} $1"
31+
}
32+
33+
print_error() {
34+
echo -e "${RED}[ERROR]${NC} $1"
35+
}
36+
37+
print_warning() {
38+
echo -e "${YELLOW}[WARNING]${NC} $1"
39+
}
40+
41+
# Function to cleanup
42+
cleanup() {
43+
print_info "Cleaning up..."
44+
45+
# Kill server process if running
46+
if [[ -n "$SERVER_PID" ]]; then
47+
kill $SERVER_PID 2>/dev/null || true
48+
wait $SERVER_PID 2>/dev/null || true
49+
print_info "Server process stopped"
50+
fi
51+
52+
# Clean up test files
53+
rm -rf "$CLIENT_OUTPUT_DIR"
54+
print_info "Cleanup completed"
55+
}
56+
57+
# Set trap for cleanup on exit
58+
trap cleanup EXIT INT TERM
59+
60+
# Function to wait for server to be ready
61+
wait_for_server() {
62+
local max_attempts=30
63+
local attempt=1
64+
65+
print_info "Waiting for server to be ready..."
66+
67+
while [[ $attempt -le $max_attempts ]]; do
68+
if curl -s -f "http://localhost:$SERVER_PORT/" >/dev/null 2>&1; then
69+
print_success "Server is ready!"
70+
return 0
71+
fi
72+
73+
print_info "Attempt $attempt/$max_attempts - Server not ready yet, waiting..."
74+
sleep 1
75+
((attempt++))
76+
done
77+
78+
print_error "Server failed to start within $max_attempts seconds"
79+
return 1
80+
}
81+
82+
# Function to create test file
83+
create_test_file() {
84+
print_info "Creating test file: $TEST_FILE_NAME ($TEST_FILE_SIZE)"
85+
86+
# Ensure server directory exists
87+
mkdir -p "$SERVER_DIR"
88+
89+
# Create a test file with specified size
90+
case $TEST_FILE_SIZE in
91+
"10MB")
92+
dd if=/dev/zero of="$SERVER_DIR/$TEST_FILE_NAME" bs=1024 count=10240 2>/dev/null
93+
;;
94+
"1MB")
95+
dd if=/dev/zero of="$SERVER_DIR/$TEST_FILE_NAME" bs=1024 count=1024 2>/dev/null
96+
;;
97+
*)
98+
# Default to 10MB
99+
dd if=/dev/zero of="$SERVER_DIR/$TEST_FILE_NAME" bs=1024 count=10240 2>/dev/null
100+
;;
101+
esac
102+
103+
print_success "Test file created: $(ls -lh "$SERVER_DIR/$TEST_FILE_NAME" | awk '{print $5}')"
104+
}
105+
106+
# Function to build binary if not exists
107+
ensure_binary() {
108+
if [[ ! -f "$BINARY_PATH" ]]; then
109+
print_info "Binary not found, building..."
110+
make build
111+
if [[ ! -f "$BINARY_PATH" ]]; then
112+
print_error "Failed to build binary"
113+
exit 1
114+
fi
115+
print_success "Binary built successfully"
116+
else
117+
print_info "Using existing binary: $BINARY_PATH"
118+
fi
119+
}
120+
121+
# Function to start server
122+
start_server() {
123+
print_info "Starting EZFT server on port $SERVER_PORT..."
124+
print_info "Server directory: $SERVER_DIR"
125+
126+
# Ensure directories exist
127+
mkdir -p "$SERVER_DIR" "$LOG_DIR"
128+
129+
# Start server in background
130+
"$BINARY_PATH" server --dir "$SERVER_DIR" --port "$SERVER_PORT" --log-home "$LOG_DIR" --log-level "info" &
131+
SERVER_PID=$!
132+
133+
print_info "Server started with PID: $SERVER_PID"
134+
135+
# Wait for server to be ready
136+
if ! wait_for_server; then
137+
print_error "Failed to start server"
138+
exit 1
139+
fi
140+
}
141+
142+
# Function to test download
143+
test_download() {
144+
local test_name="$1"
145+
local concurrency="$2"
146+
local auto_chunk="$3"
147+
local output_file="$4"
148+
local enable_resume="$5"
149+
150+
print_info "Testing: $test_name"
151+
print_info " Concurrency: $concurrency"
152+
print_info " Auto-chunk: $auto_chunk"
153+
print_info " Output: $output_file"
154+
155+
# Ensure output directory exists
156+
mkdir -p "$(dirname "$output_file")"
157+
158+
# Remove existing file if exists
159+
rm -f "$output_file"
160+
161+
# Build download command
162+
local url="http://localhost:$SERVER_PORT/$TEST_FILE_NAME"
163+
local cmd="$BINARY_PATH client --url $url --output $output_file --concurrency $concurrency --auto-chunk $auto_chunk --resume=$enable_resume --progress false --log-home $LOG_DIR --log-level info"
164+
165+
print_info "Executing: $cmd"
166+
167+
# Execute download with timeout
168+
if timeout 60s $cmd; then
169+
# Verify file exists and has correct size
170+
if [[ -f "$output_file" ]]; then
171+
local original_size=$(stat -f%z "$SERVER_DIR/$TEST_FILE_NAME" 2>/dev/null || stat -c%s "$SERVER_DIR/$TEST_FILE_NAME" 2>/dev/null)
172+
local downloaded_size=$(stat -f%z "$output_file" 2>/dev/null || stat -c%s "$output_file" 2>/dev/null)
173+
174+
if [[ "$original_size" == "$downloaded_size" ]]; then
175+
print_success "$test_name completed successfully!"
176+
print_info " File size: $(ls -lh "$output_file" | awk '{print $5}')"
177+
return 0
178+
else
179+
print_error "$test_name failed - size mismatch (original: $original_size, downloaded: $downloaded_size)"
180+
return 1
181+
fi
182+
else
183+
print_error "$test_name failed - output file not found"
184+
return 1
185+
fi
186+
else
187+
print_error "$test_name failed - download command failed or timed out"
188+
return 1
189+
fi
190+
}
191+
192+
# Main execution
193+
main() {
194+
print_info "Starting EZFT End-to-End Tests"
195+
print_info "================================"
196+
197+
# Ensure binary exists
198+
ensure_binary
199+
200+
# Create test file
201+
create_test_file
202+
203+
# Start server
204+
start_server
205+
206+
# Test results
207+
local test_results=()
208+
209+
print_info ""
210+
print_info "Running download tests..."
211+
print_info "========================"
212+
213+
# Test 1: Basic download (no concurrency, no auto-chunk)
214+
if test_download "Basic Download" 1 false "$CLIENT_OUTPUT_DIR/basic_download.txt" false; then
215+
test_results+=("✓ Basic Download")
216+
else
217+
test_results+=("✗ Basic Download")
218+
fi
219+
220+
sleep 2
221+
222+
# Test 2: Non-concurrent chunked download (no concurrency, with auto-chunk)
223+
if test_download "Non-Concurrent Chunked Download" 1 true "$CLIENT_OUTPUT_DIR/chunked_download.txt" true; then
224+
test_results+=("✓ Non-Concurrent Chunked Download")
225+
else
226+
test_results+=("✗ Non-Concurrent Chunked Download")
227+
fi
228+
229+
sleep 2
230+
231+
# Test 3: Concurrent chunked download (with concurrency and auto-chunk)
232+
if test_download "Concurrent Chunked Download" 4 true "$CLIENT_OUTPUT_DIR/concurrent_download.txt" true; then
233+
test_results+=("✓ Concurrent Chunked Download")
234+
else
235+
test_results+=("✗ Concurrent Chunked Download")
236+
fi
237+
238+
# Print results summary
239+
print_info ""
240+
print_info "Test Results Summary"
241+
print_info "===================="
242+
243+
local passed=0
244+
local total=${#test_results[@]}
245+
246+
for result in "${test_results[@]}"; do
247+
if [[ $result ==* ]]; then
248+
print_success "$result"
249+
((passed++))
250+
else
251+
print_error "$result"
252+
fi
253+
done
254+
255+
print_info ""
256+
if [[ $passed -eq $total ]]; then
257+
print_success "All tests passed! ($passed/$total)"
258+
exit 0
259+
else
260+
print_error "Some tests failed! ($passed/$total passed)"
261+
exit 1
262+
fi
263+
}
264+
265+
# Run main function
266+
main "$@"

0 commit comments

Comments
 (0)