-
Notifications
You must be signed in to change notification settings - Fork 44
/
s3_test.go
258 lines (225 loc) · 6.76 KB
/
s3_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
package desync
import (
"bufio"
"context"
"errors"
"io"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"testing"
minio "github.com/minio/minio-go/v6"
"github.com/minio/minio-go/v6/pkg/credentials"
"golang.org/x/sync/errgroup"
)
type MockCredProvider struct {
}
func (p *MockCredProvider) Retrieve() (credentials.Value, error) {
return credentials.Value{
AccessKeyID: "mainone",
SecretAccessKey: "thisiskeytrustmedude",
SessionToken: "youdontneedtoken",
SignerType: credentials.SignatureDefault,
}, nil
}
func (p *MockCredProvider) IsExpired() bool {
return false
}
func response(request *http.Request, headers http.Header, statusCode int, body string) *http.Response {
return &http.Response{
StatusCode: statusCode,
ProtoMajor: 1,
ProtoMinor: 0,
Request: request,
Body: io.NopCloser(strings.NewReader(body)),
Header: headers,
}
}
func sendObject(t *testing.T, conn *net.TCPConn, request *http.Request, filePath string, sendRst bool) {
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
resp := response(request, http.Header{}, 404, "")
resp.Write(conn)
} else {
resp := response(request, http.Header{}, 500, err.Error())
resp.Write(conn)
}
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
resp := response(request, http.Header{}, 500, err.Error())
resp.Write(conn)
return
}
headers := http.Header{}
headers.Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
headers.Add("Content-Type", "application/octet-stream")
headers.Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
if !sendRst {
resp := http.Response{
StatusCode: 200,
ProtoMajor: 1,
ProtoMinor: 0,
Request: request,
Body: file,
Header: headers,
}
resp.Write(conn)
} else {
if _, err := io.WriteString(conn, "HTTP/1.0 200 OK\r\n"); err != nil {
t.Fatal(err)
}
if err := headers.Write(conn); err != nil {
t.Fatal(err)
}
if _, err := io.WriteString(conn, "\r\n"); err != nil {
t.Fatal(err)
}
if _, err := io.CopyN(conn, file, stat.Size()/2); err != nil {
t.Fatal(err)
}
// it seems that setting SO_LINGER to 0 and calling close() on the socket forces server to
// send RST TCP packet, which we will use to emulate network error
if err := conn.SetLinger(0); err != nil {
t.Fatal(err)
}
if err := conn.Close(); err != nil {
t.Fatal(err)
}
}
}
func handleGetObjectRequest(t *testing.T, conn *net.TCPConn, bucket, store string, errorTimes *int, errorTimesLimit int) error {
defer conn.Close()
objectGetMatcher := regexp.MustCompile(`^/` + bucket + `/(.+)$`)
reader := bufio.NewReader(conn)
request, err := http.ReadRequest(reader)
if err != nil {
return err
}
matches := objectGetMatcher.FindStringSubmatch(request.URL.Path)
if matches != nil {
sendObject(t, conn, request, store+"/"+matches[1], *errorTimes < errorTimesLimit)
(*errorTimes)++
} else {
resp := response(request, http.Header{}, 400, "")
resp.Write(conn)
}
return nil
}
// Run S3 server that can respond objects from `store`
// if `errorTimesLimit` > 0 server will send RST packet `errorTimesLimit` times after sending half of file
func getTcpS3Server(t *testing.T, ctx context.Context, bucket, store string, errorTimesLimit int) (net.Listener, *errgroup.Group) {
group := errgroup.Group{}
var errorTimes int
// using localhost + resolver let us work on hosts that support only ipv6 or only ipv4
ip, err := net.DefaultResolver.LookupIP(ctx, "ip", "localhost")
if err != nil {
t.Fatal(err)
}
if len(ip) < 1 {
t.Fatalf("cannot resolve localhost")
}
listener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: ip[0], Port: 0})
if err != nil {
t.Fatal(err)
}
group.Go(func() error {
<-ctx.Done()
return listener.Close()
})
group.Go(func() error {
for {
conn, err := listener.AcceptTCP()
if err != nil {
if errors.Is(ctx.Err(), context.Canceled) {
return nil
}
return err
}
err = handleGetObjectRequest(t, conn, bucket, store, &errorTimes, errorTimesLimit)
if err != nil {
return err
}
}
})
return listener, &group
}
func TestS3StoreGetChunk(t *testing.T) {
chunkId, err := ChunkIDFromString("dda036db05bc2b99b6b9303d28496000c34b246457ae4bbf00fe625b5cabd7cd")
if err != nil {
t.Fatal(err)
}
location := "vertucon-central"
bucket := "doomsdaydevices"
provider := MockCredProvider{}
t.Run("no_error", func(t *testing.T) {
// Just try to get chunk from well-behaved S3 server, no errors expected
ctx, cancel := context.WithCancel(context.Background())
ln, group := getTcpS3Server(t, ctx, bucket, "cmd/desync/testdata", 0)
endpoint := url.URL{Scheme: "s3+http", Host: ln.Addr().String(), Path: "/" + bucket + "/blob1.store/"}
store, err := NewS3Store(&endpoint, credentials.New(&provider), location, StoreOptions{}, minio.BucketLookupAuto)
if err != nil {
t.Fatal(err)
}
chunk, err := store.GetChunk(chunkId)
if err != nil {
t.Fatal(err)
}
if chunk.ID() != chunkId {
t.Errorf("got chunk with id equal to %q, expected %q", chunk.ID(), chunkId)
}
cancel()
if err := group.Wait(); err != nil {
t.Fatal(err)
}
})
t.Run("fail", func(t *testing.T) {
// Emulate network error - after sending half of the file S3 server sends RST to the client
// We don't have retries here so we expect that GetChunk() will return error
ctx, cancel := context.WithCancel(context.Background())
ln, group := getTcpS3Server(t, ctx, bucket, "cmd/desync/testdata", 1)
endpoint := url.URL{Scheme: "s3+http", Host: ln.Addr().String(), Path: "/" + bucket + "/blob1.store/"}
store, err := NewS3Store(&endpoint, credentials.New(&provider), location, StoreOptions{}, minio.BucketLookupAuto)
if err != nil {
t.Fatal(err)
}
_, err = store.GetChunk(chunkId)
opError := &net.OpError{}
if err == nil || !errors.As(err, &opError) {
t.Fatal(err)
}
cancel()
if err := group.Wait(); err != nil {
t.Fatal(err)
}
})
t.Run("recover", func(t *testing.T) {
// Emulate network error - after sending half of the file S3 server sends RST to the client
// We have retries here so we expect that GetChunk() will not return error
ctx, cancel := context.WithCancel(context.Background())
ln, group := getTcpS3Server(t, ctx, bucket, "cmd/desync/testdata", 1)
endpoint := url.URL{Scheme: "s3+http", Host: ln.Addr().String(), Path: "/" + bucket + "/blob1.store/"}
store, err := NewS3Store(&endpoint, credentials.New(&provider), location, StoreOptions{ErrorRetry: 1}, minio.BucketLookupAuto)
if err != nil {
t.Fatal(err)
}
chunk, err := store.GetChunk(chunkId)
if err != nil {
t.Fatal(err)
}
if chunk.ID() != chunkId {
t.Errorf("got chunk with id equal to %q, expected %q", chunk.ID(), chunkId)
}
cancel()
if err := group.Wait(); err != nil {
t.Fatal(err)
}
})
}