forked from benbjohnson/litestream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplica_client_test.go
More file actions
310 lines (261 loc) · 9.16 KB
/
replica_client_test.go
File metadata and controls
310 lines (261 loc) · 9.16 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package litestream_test
import (
"bytes"
"context"
"io"
"os"
"slices"
"strings"
"testing"
"time"
"github.com/superfly/ltx"
"github.com/benbjohnson/litestream"
"github.com/benbjohnson/litestream/internal/testingutil"
"github.com/benbjohnson/litestream/s3"
)
func TestReplicaClient_LTX(t *testing.T) {
RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
// Write files out of order to check for sorting.
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(4), ltx.TXID(8), strings.NewReader(`67`)); err != nil {
t.Fatal(err)
}
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(1), strings.NewReader(``)); err != nil {
t.Fatal(err)
}
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(9), ltx.TXID(9), strings.NewReader(`xyz`)); err != nil {
t.Fatal(err)
}
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(2), ltx.TXID(3), strings.NewReader(`12345`)); err != nil {
t.Fatal(err)
}
itr, err := c.LTXFiles(context.Background(), 0, 0)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
// Read all items and ensure they are sorted.
a, err := ltx.SliceFileIterator(itr)
if err != nil {
t.Fatal(err)
} else if got, want := len(a), 4; got != want {
t.Fatalf("len=%v, want %v", got, want)
}
if got, want := stripLTXFileInfo(a[0]), (<x.FileInfo{MinTXID: 1, MaxTXID: 1, Size: 0}); *got != *want {
t.Fatalf("Index=%v, want %v", got, want)
}
if got, want := stripLTXFileInfo(a[1]), (<x.FileInfo{MinTXID: 2, MaxTXID: 3, Size: 5}); *got != *want {
t.Fatalf("Index=%v, want %v", got, want)
}
if got, want := stripLTXFileInfo(a[2]), (<x.FileInfo{MinTXID: 4, MaxTXID: 8, Size: 2}); *got != *want {
t.Fatalf("Index=%v, want %v", got, want)
}
if got, want := stripLTXFileInfo(a[3]), (<x.FileInfo{MinTXID: 9, MaxTXID: 9, Size: 3}); *got != *want {
t.Fatalf("Index=%v, want %v", got, want)
}
if err := itr.Close(); err != nil {
t.Fatal(err)
}
})
RunWithReplicaClient(t, "NoWALs", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
itr, err := c.LTXFiles(context.Background(), 0, 0)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
if itr.Next() {
t.Fatal("expected no wal files")
}
})
}
func TestReplicaClient_WriteLTXFile(t *testing.T) {
RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), strings.NewReader(`foobar`)); err != nil {
t.Fatal(err)
}
r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0)
if err != nil {
t.Fatal(err)
}
defer func() { _ = r.Close() }()
buf, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if err := r.Close(); err != nil {
t.Fatal(err)
}
if got, want := string(buf), `foobar`; got != want {
t.Fatalf("data=%q, want %q", got, want)
}
})
}
func TestReplicaClient_OpenLTXFile(t *testing.T) {
RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), strings.NewReader(`foobar`)); err != nil {
t.Fatal(err)
}
r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0)
if err != nil {
t.Fatal(err)
}
defer r.Close()
if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err)
} else if got, want := string(buf), "foobar"; got != want {
t.Fatalf("ReadAll=%v, want %v", got, want)
}
})
RunWithReplicaClient(t, "ErrNotFound", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(1), 0, 0); !os.IsNotExist(err) {
t.Fatalf("expected not exist, got %#v", err)
}
})
}
func TestReplicaClient_DeleteWALSegments(t *testing.T) {
RunWithReplicaClient(t, "OK", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), strings.NewReader(`foo`)); err != nil {
t.Fatal(err)
}
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(3), ltx.TXID(4), strings.NewReader(`bar`)); err != nil {
t.Fatal(err)
}
if err := c.DeleteLTXFiles(context.Background(), []*ltx.FileInfo{
{Level: 0, MinTXID: 1, MaxTXID: 2},
{Level: 0, MinTXID: 3, MaxTXID: 4},
}); err != nil {
t.Fatal(err)
}
if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(2), 0, 0); !os.IsNotExist(err) {
t.Fatalf("expected not exist, got %#v", err)
}
if _, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(3), ltx.TXID(4), 0, 0); !os.IsNotExist(err) {
t.Fatalf("expected not exist, got %#v", err)
}
})
}
// RunWithReplicaClient executes fn with each replica specified by the -integration flag
func RunWithReplicaClient(t *testing.T, name string, fn func(*testing.T, litestream.ReplicaClient)) {
t.Run(name, func(t *testing.T) {
for _, typ := range testingutil.ReplicaClientTypes() {
t.Run(typ, func(t *testing.T) {
if !testingutil.Integration() {
t.Skip("skipping integration test, use -integration flag to run")
}
c := testingutil.NewReplicaClient(t, typ)
defer testingutil.MustDeleteAll(t, c)
fn(t, c)
})
}
})
}
func stripLTXFileInfo(info *ltx.FileInfo) *ltx.FileInfo {
other := *info
other.CreatedAt = time.Time{}
return &other
}
// TestReplicaClient_S3_UploaderConfig tests S3 uploader configuration for large files
func TestReplicaClient_S3_UploaderConfig(t *testing.T) {
// Only run for S3 integration tests
if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") {
t.Skip("Skipping S3-specific uploader config test")
}
RunWithReplicaClient(t, "LargeFileWithCustomConfig", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
// Type assert to S3 client to set custom config
s3Client, ok := c.(*s3.ReplicaClient)
if !ok {
t.Skip("Not an S3 client")
}
// Set custom upload configuration
s3Client.PartSize = 5 * 1024 * 1024 // 5MB parts
s3Client.Concurrency = 3 // 3 concurrent parts
// Determine file size based on whether we're testing against moto or real S3
// Moto has issue #8762 where composite checksums for multipart uploads
// don't have the -X suffix, causing checksum validation to fail.
// Reference: https://github.com/getmoto/moto/issues/8762
size := 10 * 1024 * 1024 // 10MB - triggers multipart upload
// If we're using moto (localhost endpoint), use smaller file to avoid multipart
if s3Client.Endpoint != "" && strings.Contains(s3Client.Endpoint, "127.0.0.1") {
size = 4 * 1024 * 1024 // 4MB - avoids multipart upload with moto
t.Log("Using 4MB file size to work around moto multipart checksum issue")
} else {
t.Log("Using 10MB file size to test multipart upload")
}
data := make([]byte, size)
for i := range data {
data[i] = byte(i % 256)
}
// Upload the file using bytes.Reader to avoid string conversion issues
if _, err := c.WriteLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(100), bytes.NewReader(data)); err != nil {
t.Fatalf("failed to write large file: %v", err)
}
// Read it back and verify size
r, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(1), ltx.TXID(100), 0, 0)
if err != nil {
t.Fatalf("failed to open large file: %v", err)
}
defer r.Close()
buf, err := io.ReadAll(r)
if err != nil {
t.Fatalf("failed to read large file: %v", err)
}
if len(buf) != size {
t.Errorf("size mismatch: got %d, want %d", len(buf), size)
}
// Verify the data matches what we uploaded
if !bytes.Equal(buf, data) {
t.Errorf("data mismatch: uploaded and downloaded data do not match")
}
})
}
// TestReplicaClient_S3_ErrorContext tests that S3 errors include helpful context
func TestReplicaClient_S3_ErrorContext(t *testing.T) {
// Only run for S3 integration tests
if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") {
t.Skip("Skipping S3-specific error context test")
}
RunWithReplicaClient(t, "ErrorContext", func(t *testing.T, c litestream.ReplicaClient) {
t.Helper()
t.Parallel()
// Test OpenLTXFile with non-existent file
_, err := c.OpenLTXFile(context.Background(), 0, ltx.TXID(999), ltx.TXID(999), 0, 0)
if err == nil {
t.Fatal("expected error for non-existent file")
}
// Should return os.ErrNotExist for S3 NoSuchKey
if !os.IsNotExist(err) {
t.Errorf("expected os.ErrNotExist, got %v", err)
}
})
}
// TestReplicaClient_S3_BucketValidation tests bucket validation in S3 client
func TestReplicaClient_S3_BucketValidation(t *testing.T) {
// Only run for S3 integration tests
if !slices.Contains(testingutil.ReplicaClientTypes(), "s3") {
t.Skip("Skipping S3-specific bucket validation test")
}
// Create a new S3 client with empty bucket
c := testingutil.NewS3ReplicaClient(t)
c.Bucket = ""
// Should fail with bucket validation error
err := c.Init(context.Background())
if err == nil {
t.Fatal("expected error for empty bucket name")
}
if !strings.Contains(err.Error(), "bucket name is required") {
t.Errorf("expected bucket validation error, got: %v", err)
}
}