-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-car-split_test.go
257 lines (210 loc) · 6.45 KB
/
cmd-car-split_test.go
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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSortCarFiles(t *testing.T) {
fixturesDir := "fixtures"
carFiles := []string{
filepath.Join(fixturesDir, "epoch-0-3.car"),
filepath.Join(fixturesDir, "epoch-0-2.car"),
filepath.Join(fixturesDir, "epoch-0-1.car"),
}
result, err := SortCarFiles(carFiles)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(result) != 3 {
t.Fatalf("unexpected result length: %d", len(result))
}
expectedResults := []struct {
name string
firstSlot int64
size int64
}{
{filepath.Join(fixturesDir, "epoch-0-1.car"), 0, 96932},
{filepath.Join(fixturesDir, "epoch-0-2.car"), 10, 100027},
{filepath.Join(fixturesDir, "epoch-0-3.car"), 20, 99487},
}
for i, expected := range expectedResults {
if result[i].name != expected.name {
t.Fatalf("unexpected name: %s", result[i].name)
}
if result[i].firstSlot != expected.firstSlot {
t.Fatalf("unexpected firstSlot: %d", result[i].firstSlot)
}
if result[i].size != expected.size {
t.Fatalf("unexpected size: %d", result[i].size)
}
}
}
func TestSortCarURLs(t *testing.T) {
// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the filename from the URL path
filename := filepath.Base(r.URL.Path)
fixturesDir := "fixtures"
filePath := filepath.Join(fixturesDir, filename)
// Open the local CAR file
file, err := os.Open(filePath)
if err != nil {
t.Fatalf("failed to open fixture file %s: %v", filePath, err)
}
defer file.Close()
// Get file info for Content-Length header
fileInfo, err := file.Stat()
if err != nil {
t.Fatalf("failed to get file info: %v", err)
}
// Handle HEAD requests
if r.Method == "HEAD" {
w.Header().Set("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
return
}
// Handle range requests
if rangeHeader := r.Header.Get("Range"); rangeHeader != "" {
// Parse the range header
start, end := int64(0), fileInfo.Size()
fmt.Sscanf(rangeHeader, "bytes=%d-", &start)
if start < 0 {
start = 0
}
if end > fileInfo.Size() {
end = fileInfo.Size()
}
// Seek to the start position
_, err = file.Seek(start, 0)
if err != nil {
t.Fatalf("failed to seek in file: %v", err)
}
// Set response headers
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end-1, fileInfo.Size()))
w.WriteHeader(http.StatusPartialContent)
// Copy the requested range to the response
_, err = io.Copy(w, file)
if err != nil {
t.Fatalf("failed to copy file content: %v", err)
}
return
}
// Handle regular GET requests
http.ServeFile(w, r, filePath)
}))
defer server.Close()
// Create URLs for our test files
carURLs := []string{
server.URL + "/epoch-0-3.car",
server.URL + "/epoch-0-2.car",
server.URL + "/epoch-0-1.car",
}
// Call SortCarURLs
result, err := SortCarURLs(carURLs)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify the results
if len(result) != 3 {
t.Fatalf("unexpected result length: got %d, want 3", len(result))
}
expectedResults := []struct {
name string
firstSlot int64
size int64
}{
{server.URL + "/epoch-0-1.car", 0, 96932},
{server.URL + "/epoch-0-2.car", 10, 100027},
{server.URL + "/epoch-0-3.car", 20, 99487},
}
for i, expected := range expectedResults {
if result[i].name != expected.name {
t.Errorf("unexpected name at index %d: got %s, want %s", i, result[i].name, expected.name)
}
if result[i].firstSlot != expected.firstSlot {
t.Errorf("unexpected firstSlot at index %d: got %d, want %d", i, result[i].firstSlot, expected.firstSlot)
}
if result[i].size != expected.size {
t.Errorf("unexpected size at index %d: got %d, want %d", i, result[i].size, expected.size)
}
}
}
func TestSortCarFiles_EmptyInput(t *testing.T) {
result, err := SortCarFiles([]string{})
if err != nil {
t.Fatalf("unexpected error for empty input: %s", err)
}
if len(result) != 0 {
t.Fatalf("expected empty result for empty input, got %d items", len(result))
}
}
func TestSortCarFiles_NonExistentFile(t *testing.T) {
nonExistentFile := filepath.Join("fixtures", "non-existent.car")
_, err := SortCarFiles([]string{nonExistentFile})
if err == nil {
t.Fatal("expected error for non-existent file, got nil")
}
if !strings.Contains(err.Error(), "no such file or directory") {
t.Fatalf("unexpected error message: %s", err)
}
}
func TestSortCarFiles_InvalidCAR(t *testing.T) {
invalidCarFile := filepath.Join("fixtures", "invalid.car")
// Create an invalid CAR file for testing
err := os.WriteFile(invalidCarFile, []byte("invalid car content"), 0644)
if err != nil {
t.Fatalf("failed to create invalid CAR file: %s", err)
}
defer os.Remove(invalidCarFile)
_, err = SortCarFiles([]string{invalidCarFile})
if err == nil {
t.Fatal("expected error for invalid CAR file, got nil")
}
if !strings.Contains(err.Error(), "failed to create CarReader") {
t.Fatalf("unexpected error message: %s", err)
}
}
func TestSortCarURLs_EmptyInput(t *testing.T) {
result, err := SortCarURLs([]string{})
if err != nil {
t.Fatalf("unexpected error for empty input: %s", err)
}
if len(result) != 0 {
t.Fatalf("expected empty result for empty input, got %d items", len(result))
}
}
func TestSortCarURLs_InvalidURL(t *testing.T) {
invalidURL := "http://invalid.url/non-existent.car"
_, err := SortCarURLs([]string{invalidURL})
if err == nil {
t.Fatal("expected error for invalid URL, got nil")
}
if !strings.Contains(err.Error(), "failed to get first slot from URL") {
t.Fatalf("unexpected error message: %s", err)
}
}
func TestSortCarURLs_MixedValidAndInvalidURLs(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve a valid CAR file for testing
fixturesDir := "fixtures"
filePath := filepath.Join(fixturesDir, "epoch-0-1.car")
http.ServeFile(w, r, filePath)
}))
defer server.Close()
validURL := server.URL + "/valid.car"
invalidURL := "http://invalid.url/non-existent.car"
result, err := SortCarURLs([]string{validURL, invalidURL})
if err == nil {
t.Fatal("expected error for mixed valid and invalid URLs, got nil")
}
if !strings.Contains(err.Error(), "failed to get first slot from URL") {
t.Fatalf("unexpected error message: %s", err)
}
if len(result) != 0 {
t.Fatalf("expected empty result for error case, got %d items", len(result))
}
}