|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/thanos-io/objstore" |
| 13 | +) |
| 14 | + |
| 15 | +func TestBucketWithRetries_UploadSeekable(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + m := mockBucket{ |
| 19 | + FailCount: 3, |
| 20 | + } |
| 21 | + b := BucketWithRetries{ |
| 22 | + bucket: &m, |
| 23 | + operationRetries: 5, |
| 24 | + retryMinBackoff: 10 * time.Millisecond, |
| 25 | + retryMaxBackoff: time.Second, |
| 26 | + } |
| 27 | + |
| 28 | + input := []byte("test input") |
| 29 | + err := b.Upload(context.Background(), "dummy", bytes.NewReader(input)) |
| 30 | + require.NoError(t, err) |
| 31 | + require.Equal(t, input, m.uploadedContent) |
| 32 | +} |
| 33 | + |
| 34 | +func TestBucketWithRetries_UploadNonSeekable(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + maxFailCount := 3 |
| 38 | + m := mockBucket{ |
| 39 | + FailCount: maxFailCount, |
| 40 | + } |
| 41 | + b := BucketWithRetries{ |
| 42 | + bucket: &m, |
| 43 | + operationRetries: 5, |
| 44 | + retryMinBackoff: 10 * time.Millisecond, |
| 45 | + retryMaxBackoff: time.Second, |
| 46 | + } |
| 47 | + |
| 48 | + input := &fakeReader{} |
| 49 | + err := b.Upload(context.Background(), "dummy", input) |
| 50 | + require.Errorf(t, err, "empty byte slice") |
| 51 | + require.Equal(t, maxFailCount, m.FailCount) |
| 52 | +} |
| 53 | + |
| 54 | +type fakeReader struct { |
| 55 | +} |
| 56 | + |
| 57 | +func (f *fakeReader) Read(p []byte) (n int, err error) { |
| 58 | + return 0, fmt.Errorf("empty byte slice") |
| 59 | +} |
| 60 | + |
| 61 | +type mockBucket struct { |
| 62 | + FailCount int |
| 63 | + uploadedContent []byte |
| 64 | +} |
| 65 | + |
| 66 | +// Upload mocks objstore.Bucket.Upload() |
| 67 | +func (m *mockBucket) Upload(ctx context.Context, name string, r io.Reader) error { |
| 68 | + var buf bytes.Buffer |
| 69 | + if _, err := buf.ReadFrom(r); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + m.uploadedContent = buf.Bytes() |
| 73 | + if m.FailCount > 0 { |
| 74 | + m.FailCount-- |
| 75 | + return fmt.Errorf("failed upload: %d", m.FailCount) |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// Delete mocks objstore.Bucket.Delete() |
| 81 | +func (m *mockBucket) Delete(ctx context.Context, name string) error { |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// Name mocks objstore.Bucket.Name() |
| 86 | +func (m *mockBucket) Name() string { |
| 87 | + return "mock" |
| 88 | +} |
| 89 | + |
| 90 | +// Iter mocks objstore.Bucket.Iter() |
| 91 | +func (m *mockBucket) Iter(ctx context.Context, dir string, f func(string) error, options ...objstore.IterOption) error { |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// Get mocks objstore.Bucket.Get() |
| 96 | +func (m *mockBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) { |
| 97 | + return nil, nil |
| 98 | +} |
| 99 | + |
| 100 | +// GetRange mocks objstore.Bucket.GetRange() |
| 101 | +func (m *mockBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) { |
| 102 | + return nil, nil |
| 103 | +} |
| 104 | + |
| 105 | +// Exists mocks objstore.Bucket.Exists() |
| 106 | +func (m *mockBucket) Exists(ctx context.Context, name string) (bool, error) { |
| 107 | + return false, nil |
| 108 | +} |
| 109 | + |
| 110 | +// IsObjNotFoundErr mocks objstore.Bucket.IsObjNotFoundErr() |
| 111 | +func (m *mockBucket) IsObjNotFoundErr(err error) bool { |
| 112 | + return false |
| 113 | +} |
| 114 | + |
| 115 | +// ObjectSize mocks objstore.Bucket.Attributes() |
| 116 | +func (m *mockBucket) Attributes(ctx context.Context, name string) (objstore.ObjectAttributes, error) { |
| 117 | + return objstore.ObjectAttributes{Size: 0, LastModified: time.Now()}, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Close mocks objstore.Bucket.Close() |
| 121 | +func (m *mockBucket) Close() error { |
| 122 | + return nil |
| 123 | +} |
0 commit comments