-
Notifications
You must be signed in to change notification settings - Fork 0
/
walrus_test.go
429 lines (362 loc) · 11.8 KB
/
walrus_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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package walrus_go
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
const (
testContent = "Hello, Walrus!"
)
// Helper function to create a test client
func newTestClient(t *testing.T) *Client {
return NewClient()
}
// Helper function to store test content and return blobID
func storeTestContent(t *testing.T, client *Client) string {
resp, err := client.Store([]byte(testContent), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store test content: %v", err)
}
resp.NormalizeBlobResponse()
return resp.Blob.BlobID
}
// TestStore tests storing data
func TestStore(t *testing.T) {
client := newTestClient(t)
resp, err := client.Store([]byte(testContent), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store data: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("Store operation failed: received empty blob ID in response")
}
if resp.Blob.EndEpoch <= 0 {
t.Error("Store operation failed: received invalid end epoch (must be positive)")
}
}
// TestStoreFromReader tests storing data from a reader
func TestStoreFromReader(t *testing.T) {
client := newTestClient(t)
reader := strings.NewReader(testContent)
resp, err := client.StoreFromReader(reader, int64(len(testContent)), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store data from reader: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("StoreFromReader operation failed: received empty blob ID in response")
}
}
// TestStoreFromReaderWihoutContentLength tests storing data from a reader
func TestStoreFromReaderWihoutContentLength(t *testing.T) {
client := newTestClient(t)
reader := strings.NewReader(testContent)
resp, err := client.StoreFromReader(reader, -1, &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store data from reader: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("StoreFromReader operation failed: received empty blob ID in response")
}
}
// TestStoreFromURL tests storing data from a URL
func TestStoreFromURL(t *testing.T) {
client := newTestClient(t)
testURL := "https://raw.githubusercontent.com/suiet/walrus-go/main/README.md"
resp, err := client.StoreFromURL(testURL, &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store data from URL %s: %v", testURL, err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Errorf("StoreFromURL operation failed: received empty blob ID when storing from URL %s", testURL)
}
}
// TestStoreFile tests storing a file
func TestStoreFile(t *testing.T) {
client := newTestClient(t)
tmpfile, err := os.CreateTemp("", "walrus-test-*.txt")
if err != nil {
t.Fatalf("Failed to create temporary test file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(testContent)); err != nil {
t.Fatalf("Failed to write test content to temporary file: %v", err)
}
tmpfile.Close()
resp, err := client.StoreFile(tmpfile.Name(), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store file %s: %v", tmpfile.Name(), err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Errorf("StoreFile operation failed: received empty blob ID when storing file %s", tmpfile.Name())
}
}
// TestHead tests retrieving blob metadata
func TestHead(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
metadata, err := client.Head(blobID)
if err != nil {
t.Fatalf("Failed to retrieve metadata for blob %s: %v", blobID, err)
}
if metadata.ContentLength != int64(len(testContent)) {
t.Errorf("Head operation returned incorrect content length: expected %d bytes, got %d bytes",
len(testContent), metadata.ContentLength)
}
if metadata.ContentType == "" {
t.Errorf("Head operation failed: received empty content type for blob %s", blobID)
}
}
// TestRead tests reading blob content
func TestRead(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
data, err := client.Read(blobID)
if err != nil {
t.Fatalf("Failed to read blob %s: %v", blobID, err)
}
if string(data) != testContent {
t.Errorf("Read operation returned incorrect content: expected %q, got %q",
testContent, string(data))
}
}
// TestReadToFile tests reading blob to a file
func TestReadToFile(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
tmpfile, err := os.CreateTemp("", "walrus-read-test-*.txt")
if err != nil {
t.Fatalf("Failed to create temporary output file: %v", err)
}
defer os.Remove(tmpfile.Name())
tmpfile.Close()
if err := client.ReadToFile(blobID, tmpfile.Name()); err != nil {
t.Fatalf("Failed to read blob %s to file %s: %v", blobID, tmpfile.Name(), err)
}
content, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatalf("Failed to read content from output file %s: %v", tmpfile.Name(), err)
}
if string(content) != testContent {
t.Errorf("ReadToFile operation returned incorrect content: expected %q, got %q",
testContent, string(content))
}
}
// TestReadToReader tests reading blob to an io.Reader
func TestReadToReader(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
reader, err := client.ReadToReader(blobID)
if err != nil {
t.Fatalf("Failed to get reader for blob %s: %v", blobID, err)
}
defer reader.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, reader); err != nil {
t.Fatalf("Failed to read content from reader for blob %s: %v", blobID, err)
}
if buf.String() != testContent {
t.Errorf("ReadToReader operation returned incorrect content: expected %q, got %q",
testContent, buf.String())
}
}
// TestGetAPISpec tests retrieving API specifications
func TestGetAPISpec(t *testing.T) {
client := newTestClient(t)
// Test aggregator API spec
aggSpec, err := client.GetAPISpec(true)
if err != nil {
t.Fatalf("Failed to retrieve aggregator API specification: %v", err)
}
if len(aggSpec) == 0 {
t.Error("GetAPISpec operation failed: received empty aggregator API specification")
}
// Test publisher API spec
pubSpec, err := client.GetAPISpec(false)
if err != nil {
t.Fatalf("Failed to retrieve publisher API specification: %v", err)
}
if len(pubSpec) == 0 {
t.Error("GetAPISpec operation failed: received empty publisher API specification")
}
}
// TestNormalizeBlobResponse tests response normalization
func TestNormalizeBlobResponse(t *testing.T) {
// Test with NewlyCreated response
newResp := &StoreResponse{
NewlyCreated: &struct {
BlobObject BlobObject `json:"blobObject"`
EncodedSize int `json:"encodedSize"`
Cost int `json:"cost"`
}{
BlobObject: BlobObject{
BlobID: "test-blob-id",
Storage: StorageInfo{
EndEpoch: 100,
},
},
},
}
newResp.NormalizeBlobResponse()
if newResp.Blob.BlobID != "test-blob-id" || newResp.Blob.EndEpoch != 100 {
t.Error("Response normalization failed for NewlyCreated response: incorrect blob ID or end epoch")
}
// Test with AlreadyCertified response
certResp := &StoreResponse{
AlreadyCertified: &struct {
BlobID string `json:"blobId"`
Event EventInfo `json:"event"`
EndEpoch int `json:"endEpoch"`
}{
BlobID: "test-blob-id",
EndEpoch: 200,
},
}
certResp.NormalizeBlobResponse()
if certResp.Blob.BlobID != "test-blob-id" || certResp.Blob.EndEpoch != 200 {
t.Error("Response normalization failed for AlreadyCertified response: incorrect blob ID or end epoch")
}
}
// Example usage of the client
func ExampleClient_Store() {
client := NewClient()
resp, err := client.Store([]byte("Hello, World!"), &StoreOptions{Epochs: 1})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
resp.NormalizeBlobResponse()
fmt.Printf("Stored blob with ID: %s\n", resp.Blob.BlobID)
}
// Add new test for retry configuration
func TestWithRetryConfig(t *testing.T) {
maxRetries := 3
retryDelay := 100 * time.Millisecond
client := NewClient(
WithRetryConfig(maxRetries, retryDelay),
)
if client.retryConfig.MaxRetries != maxRetries {
t.Errorf("Expected MaxRetries to be %d, got %d", maxRetries, client.retryConfig.MaxRetries)
}
if client.retryConfig.RetryDelay != retryDelay {
t.Errorf("Expected RetryDelay to be %v, got %v", retryDelay, client.retryConfig.RetryDelay)
}
}
// Add test for retry functionality
func TestRetryLogic(t *testing.T) {
// Create a test server that fails first N-1 times and succeeds on Nth attempt
attemptCount := 0
maxAttempts := 3
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attemptCount++
if attemptCount < maxAttempts {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("success"))
}))
defer server.Close()
// Create client with retry config
client := NewClient(
WithRetryConfig(maxAttempts, 10*time.Millisecond),
WithAggregatorURLs([]string{server.URL}),
)
// Test read operation with retry
data, err := client.Read("test-blob")
if err != nil {
t.Fatalf("Expected successful read after retries, got error: %v", err)
}
if string(data) != "success" {
t.Errorf("Expected response 'success', got '%s'", string(data))
}
if attemptCount != maxAttempts {
t.Errorf("Expected %d attempts, got %d", maxAttempts, attemptCount)
}
}
// Add test for multiple endpoints
func TestMultipleEndpoints(t *testing.T) {
// Create two test servers
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server1.Close()
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("success from server 2"))
}))
defer server2.Close()
// Create client with multiple endpoints
client := NewClient(
WithRetryConfig(2, 10*time.Millisecond),
WithAggregatorURLs([]string{server1.URL, server2.URL}),
)
// Test read operation with endpoint failover
data, err := client.Read("test-blob")
if err != nil {
t.Fatalf("Expected successful read from second server, got error: %v", err)
}
if string(data) != "success from server 2" {
t.Errorf("Expected response 'success from server 2', got '%s'", string(data))
}
}
// Update TestNewClient to include retry config check
func TestNewClient(t *testing.T) {
client := NewClient()
// Check default retry configuration
if client.retryConfig.MaxRetries != 5 {
t.Errorf("Expected default MaxRetries to be 5, got %d", client.retryConfig.MaxRetries)
}
if client.retryConfig.RetryDelay != 500*time.Millisecond {
t.Errorf("Expected default RetryDelay to be 500ms, got %v", client.retryConfig.RetryDelay)
}
// ... (keep existing URL checks)
}
// Add test for request body preservation during retries
func TestRequestBodyPreservation(t *testing.T) {
attemptCount := 0
maxAttempts := 2
expectedBody := "test content"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read and verify request body
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Failed to read request body: %v", err)
}
if string(body) != expectedBody {
t.Errorf("Expected body '%s', got '%s'", expectedBody, string(body))
}
attemptCount++
if attemptCount < maxAttempts {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(StoreResponse{
Blob: BlobInfo{BlobID: "test-id", EndEpoch: 100},
})
}))
defer server.Close()
client := NewClient(
WithRetryConfig(maxAttempts, 10*time.Millisecond),
WithPublisherURLs([]string{server.URL}),
)
_, err := client.Store([]byte(expectedBody), nil)
if err != nil {
t.Fatalf("Expected successful store after retries, got error: %v", err)
}
if attemptCount != maxAttempts {
t.Errorf("Expected %d attempts, got %d", maxAttempts, attemptCount)
}
}