1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "github.com/AdRoll/goamz/s3"
21 "github.com/AdRoll/goamz/s3/s3test"
22 log "github.com/Sirupsen/logrus"
23 check "gopkg.in/check.v1"
27 TestBucketName = "testbucket"
30 type fakeClock struct {
34 func (c *fakeClock) Now() time.Time {
42 // Deleting isn't safe from races, but if it's turned on
43 // anyway we do expect it to pass the generic volume tests.
47 var _ = check.Suite(&StubbedS3Suite{})
49 type StubbedS3Suite struct {
50 volumes []*TestableS3Volume
53 func (s *StubbedS3Suite) TestGeneric(c *check.C) {
54 DoGenericVolumeTests(c, func(t TB) TestableVolume {
55 // Use a negative raceWindow so s3test's 1-second
56 // timestamp precision doesn't confuse fixRace.
57 return s.newTestableVolume(c, -2*time.Second, false, 2)
61 func (s *StubbedS3Suite) TestGenericReadOnly(c *check.C) {
62 DoGenericVolumeTests(c, func(t TB) TestableVolume {
63 return s.newTestableVolume(c, -2*time.Second, true, 2)
67 func (s *StubbedS3Suite) TestIndex(c *check.C) {
68 v := s.newTestableVolume(c, 0, false, 2)
70 for i := 0; i < 256; i++ {
71 v.PutRaw(fmt.Sprintf("%02x%030x", i, i), []byte{102, 111, 111})
73 for _, spec := range []struct {
82 buf := new(bytes.Buffer)
83 err := v.IndexTo(spec.prefix, buf)
84 c.Check(err, check.IsNil)
86 idx := bytes.SplitAfter(buf.Bytes(), []byte{10})
87 c.Check(len(idx), check.Equals, spec.expectMatch+1)
88 c.Check(len(idx[len(idx)-1]), check.Equals, 0)
92 func (s *StubbedS3Suite) TestStats(c *check.C) {
93 v := s.newTestableVolume(c, 5*time.Minute, false, 2)
94 stats := func() string {
95 buf, err := json.Marshal(v.InternalStats())
96 c.Check(err, check.IsNil)
100 c.Check(stats(), check.Matches, `.*"Ops":0,.*`)
102 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
103 _, err := v.Get(context.Background(), loc, make([]byte, 3))
104 c.Check(err, check.NotNil)
105 c.Check(stats(), check.Matches, `.*"Ops":[^0],.*`)
106 c.Check(stats(), check.Matches, `.*"\*s3.Error 404 [^"]*":[^0].*`)
107 c.Check(stats(), check.Matches, `.*"InBytes":0,.*`)
109 err = v.Put(context.Background(), loc, []byte("foo"))
110 c.Check(err, check.IsNil)
111 c.Check(stats(), check.Matches, `.*"OutBytes":3,.*`)
112 c.Check(stats(), check.Matches, `.*"PutOps":2,.*`)
114 _, err = v.Get(context.Background(), loc, make([]byte, 3))
115 c.Check(err, check.IsNil)
116 _, err = v.Get(context.Background(), loc, make([]byte, 3))
117 c.Check(err, check.IsNil)
118 c.Check(stats(), check.Matches, `.*"InBytes":6,.*`)
121 type blockingHandler struct {
122 requested chan *http.Request
123 unblock chan struct{}
126 func (h *blockingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
127 if h.requested != nil {
130 if h.unblock != nil {
133 http.Error(w, "nothing here", http.StatusNotFound)
136 func (s *StubbedS3Suite) TestGetContextCancel(c *check.C) {
137 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
138 buf := make([]byte, 3)
140 s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
141 _, err := v.Get(ctx, loc, buf)
146 func (s *StubbedS3Suite) TestCompareContextCancel(c *check.C) {
147 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
150 s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
151 return v.Compare(ctx, loc, buf)
155 func (s *StubbedS3Suite) TestPutContextCancel(c *check.C) {
156 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
159 s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
160 return v.Put(ctx, loc, buf)
164 func (s *StubbedS3Suite) testContextCancel(c *check.C, testFunc func(context.Context, *TestableS3Volume) error) {
165 handler := &blockingHandler{}
166 srv := httptest.NewServer(handler)
169 v := s.newTestableVolume(c, 5*time.Minute, false, 2)
171 vol.Endpoint = srv.URL
172 v = &TestableS3Volume{S3Volume: &vol}
175 ctx, cancel := context.WithCancel(context.Background())
177 handler.requested = make(chan *http.Request)
178 handler.unblock = make(chan struct{})
179 defer close(handler.unblock)
181 doneFunc := make(chan struct{})
183 err := testFunc(ctx, v)
184 c.Check(err, check.Equals, context.Canceled)
188 timeout := time.After(10 * time.Second)
190 // Wait for the stub server to receive a request, meaning
191 // Get() is waiting for an s3 operation.
194 c.Fatal("timed out waiting for test func to call our handler")
196 c.Fatal("test func finished without even calling our handler!")
197 case <-handler.requested:
209 func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
210 defer func(tl, bs arvados.Duration) {
211 theConfig.TrashLifetime = tl
212 theConfig.BlobSignatureTTL = bs
213 }(theConfig.TrashLifetime, theConfig.BlobSignatureTTL)
214 theConfig.TrashLifetime.Set("1h")
215 theConfig.BlobSignatureTTL.Set("1h")
217 v := s.newTestableVolume(c, 5*time.Minute, false, 2)
220 putS3Obj := func(t time.Time, key string, data []byte) {
224 v.serverClock.now = &t
225 v.bucket.Put(key, data, "application/octet-stream", s3ACL, s3.Options{})
230 for _, scenario := range []struct {
237 canGetAfterTrash bool
239 haveTrashAfterEmpty bool
243 "No related objects",
245 false, false, false, false, false, false,
248 // Stored by older version, or there was a
249 // race between EmptyTrash and Put: Trash is a
250 // no-op even though the data object is very
253 t0.Add(-48 * time.Hour), none, none,
254 true, true, true, false, false, false,
257 "Not trash, but old enough to be eligible for trash",
258 t0.Add(-24 * time.Hour), t0.Add(-2 * time.Hour), none,
259 true, true, false, false, false, false,
262 "Not trash, and not old enough to be eligible for trash",
263 t0.Add(-24 * time.Hour), t0.Add(-30 * time.Minute), none,
264 true, true, true, false, false, false,
267 "Trashed + untrashed copies exist, due to recent race between Trash and Put",
268 t0.Add(-24 * time.Hour), t0.Add(-3 * time.Minute), t0.Add(-2 * time.Minute),
269 true, true, true, true, true, false,
272 "Trashed + untrashed copies exist, trash nearly eligible for deletion: prone to Trash race",
273 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-59 * time.Minute),
274 true, false, true, true, true, false,
277 "Trashed + untrashed copies exist, trash is eligible for deletion: prone to Trash race",
278 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-61 * time.Minute),
279 true, false, true, true, false, false,
282 "Trashed + untrashed copies exist, due to old race between Put and unfinished Trash: emptying trash is unsafe",
283 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-12 * time.Hour),
284 true, false, true, true, true, true,
287 "Trashed + untrashed copies exist, used to be unsafe to empty, but since made safe by fixRace+Touch",
288 t0.Add(-time.Second), t0.Add(-time.Second), t0.Add(-12 * time.Hour),
289 true, true, true, true, false, false,
292 "Trashed + untrashed copies exist because Trash operation was interrupted (no race)",
293 t0.Add(-24 * time.Hour), t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour),
294 true, false, true, true, false, false,
297 "Trash, not yet eligible for deletion",
298 none, t0.Add(-12 * time.Hour), t0.Add(-time.Minute),
299 false, false, false, true, true, false,
302 "Trash, not yet eligible for deletion, prone to races",
303 none, t0.Add(-12 * time.Hour), t0.Add(-59 * time.Minute),
304 false, false, false, true, true, false,
307 "Trash, eligible for deletion",
308 none, t0.Add(-12 * time.Hour), t0.Add(-2 * time.Hour),
309 false, false, false, true, false, false,
312 "Erroneously trashed during a race, detected before TrashLifetime",
313 none, t0.Add(-30 * time.Minute), t0.Add(-29 * time.Minute),
314 true, false, true, true, true, false,
317 "Erroneously trashed during a race, rescue during EmptyTrash despite reaching TrashLifetime",
318 none, t0.Add(-90 * time.Minute), t0.Add(-89 * time.Minute),
319 true, false, true, true, true, false,
322 "Trashed copy exists with no recent/* marker (cause unknown); repair by untrashing",
323 none, none, t0.Add(-time.Minute),
324 false, false, false, true, true, true,
327 c.Log("Scenario: ", scenario.label)
329 // We have a few tests to run for each scenario, and
330 // the tests are expected to change state. By calling
331 // this setup func between tests, we (re)create the
332 // scenario as specified, using a new unique block
333 // locator to prevent interference from previous
336 setupScenario := func() (string, []byte) {
338 blk := []byte(fmt.Sprintf("%d", nextKey))
339 loc := fmt.Sprintf("%x", md5.Sum(blk))
341 putS3Obj(scenario.dataT, loc, blk)
342 putS3Obj(scenario.recentT, "recent/"+loc, nil)
343 putS3Obj(scenario.trashT, "trash/"+loc, blk)
344 v.serverClock.now = &t0
349 loc, blk := setupScenario()
350 buf := make([]byte, len(blk))
351 _, err := v.Get(context.Background(), loc, buf)
352 c.Check(err == nil, check.Equals, scenario.canGet)
354 c.Check(os.IsNotExist(err), check.Equals, true)
357 // Call Trash, then check canTrash and canGetAfterTrash
358 loc, blk = setupScenario()
360 c.Check(err == nil, check.Equals, scenario.canTrash)
361 _, err = v.Get(context.Background(), loc, buf)
362 c.Check(err == nil, check.Equals, scenario.canGetAfterTrash)
364 c.Check(os.IsNotExist(err), check.Equals, true)
367 // Call Untrash, then check canUntrash
368 loc, blk = setupScenario()
370 c.Check(err == nil, check.Equals, scenario.canUntrash)
371 if scenario.dataT != none || scenario.trashT != none {
372 // In all scenarios where the data exists, we
373 // should be able to Get after Untrash --
374 // regardless of timestamps, errors, race
376 _, err = v.Get(context.Background(), loc, buf)
377 c.Check(err, check.IsNil)
380 // Call EmptyTrash, then check haveTrashAfterEmpty and
382 loc, blk = setupScenario()
384 _, err = v.bucket.Head("trash/"+loc, nil)
385 c.Check(err == nil, check.Equals, scenario.haveTrashAfterEmpty)
386 if scenario.freshAfterEmpty {
387 t, err := v.Mtime(loc)
388 c.Check(err, check.IsNil)
389 // new mtime must be current (with an
390 // allowance for 1s timestamp precision)
391 c.Check(t.After(t0.Add(-time.Second)), check.Equals, true)
394 // Check for current Mtime after Put (applies to all
396 loc, blk = setupScenario()
397 err = v.Put(context.Background(), loc, blk)
398 c.Check(err, check.IsNil)
399 t, err := v.Mtime(loc)
400 c.Check(err, check.IsNil)
401 c.Check(t.After(t0.Add(-time.Second)), check.Equals, true)
405 type TestableS3Volume struct {
407 server *s3test.Server
409 serverClock *fakeClock
412 func (s *StubbedS3Suite) newTestableVolume(c *check.C, raceWindow time.Duration, readonly bool, replication int) *TestableS3Volume {
413 clock := &fakeClock{}
414 srv, err := s3test.NewServer(&s3test.Config{Clock: clock})
415 c.Assert(err, check.IsNil)
417 v := &TestableS3Volume{
419 Bucket: TestBucketName,
421 Region: "test-region-1",
422 LocationConstraint: true,
423 RaceWindow: arvados.Duration(raceWindow),
424 S3Replication: replication,
425 UnsafeDelete: s3UnsafeDelete,
434 err = v.bucket.PutBucket(s3.ACL("private"))
435 c.Assert(err, check.IsNil)
439 func (v *TestableS3Volume) Start() error {
440 tmp, err := ioutil.TempFile("", "keepstore")
441 v.c.Assert(err, check.IsNil)
442 defer os.Remove(tmp.Name())
443 _, err = tmp.Write([]byte("xxx\n"))
444 v.c.Assert(err, check.IsNil)
445 v.c.Assert(tmp.Close(), check.IsNil)
447 v.S3Volume.AccessKeyFile = tmp.Name()
448 v.S3Volume.SecretKeyFile = tmp.Name()
450 v.c.Assert(v.S3Volume.Start(), check.IsNil)
454 // PutRaw skips the ContentMD5 test
455 func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
456 err := v.bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
458 log.Printf("PutRaw: %s: %+v", loc, err)
460 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
462 log.Printf("PutRaw: recent/%s: %+v", loc, err)
466 // TouchWithDate turns back the clock while doing a Touch(). We assume
467 // there are no other operations happening on the same s3test server
469 func (v *TestableS3Volume) TouchWithDate(locator string, lastPut time.Time) {
470 v.serverClock.now = &lastPut
471 err := v.bucket.Put("recent/"+locator, nil, "application/octet-stream", s3ACL, s3.Options{})
475 v.serverClock.now = nil
478 func (v *TestableS3Volume) Teardown() {