9956: Load volume config from YAML file
[arvados.git] / services / keepstore / s3_volume_test.go
index 6ba390426f51dbe4f8ef1d6ac2ce2a27b4d3f3b2..76dcbc9f9ea2f8fb680a25a31b84735f991b1b51 100644 (file)
@@ -4,23 +4,17 @@ import (
        "bytes"
        "crypto/md5"
        "fmt"
+       "io/ioutil"
        "log"
        "os"
        "time"
 
-       "github.com/AdRoll/goamz/aws"
+       "git.curoverse.com/arvados.git/sdk/go/arvados"
        "github.com/AdRoll/goamz/s3"
        "github.com/AdRoll/goamz/s3/s3test"
        check "gopkg.in/check.v1"
 )
 
-type TestableS3Volume struct {
-       *S3Volume
-       server      *s3test.Server
-       c           *check.C
-       serverClock *fakeClock
-}
-
 const (
        TestBucketName = "testbucket"
 )
@@ -42,30 +36,6 @@ func init() {
        s3UnsafeDelete = true
 }
 
-func NewTestableS3Volume(c *check.C, raceWindow time.Duration, readonly bool, replication int) *TestableS3Volume {
-       clock := &fakeClock{}
-       srv, err := s3test.NewServer(&s3test.Config{Clock: clock})
-       c.Assert(err, check.IsNil)
-       auth := aws.Auth{}
-       region := aws.Region{
-               Name:                 "test-region-1",
-               S3Endpoint:           srv.URL(),
-               S3LocationConstraint: true,
-       }
-       bucket := &s3.Bucket{
-               S3:   s3.New(auth, region),
-               Name: TestBucketName,
-       }
-       err = bucket.PutBucket(s3.ACL("private"))
-       c.Assert(err, check.IsNil)
-
-       return &TestableS3Volume{
-               S3Volume:    NewS3Volume(auth, region, TestBucketName, raceWindow, readonly, replication),
-               server:      srv,
-               serverClock: clock,
-       }
-}
-
 var _ = check.Suite(&StubbedS3Suite{})
 
 type StubbedS3Suite struct {
@@ -76,19 +46,19 @@ func (s *StubbedS3Suite) TestGeneric(c *check.C) {
        DoGenericVolumeTests(c, func(t TB) TestableVolume {
                // Use a negative raceWindow so s3test's 1-second
                // timestamp precision doesn't confuse fixRace.
-               return NewTestableS3Volume(c, -2*time.Second, false, 2)
+               return s.newTestableVolume(c, -2*time.Second, false, 2)
        })
 }
 
 func (s *StubbedS3Suite) TestGenericReadOnly(c *check.C) {
        DoGenericVolumeTests(c, func(t TB) TestableVolume {
-               return NewTestableS3Volume(c, -2*time.Second, true, 2)
+               return s.newTestableVolume(c, -2*time.Second, true, 2)
        })
 }
 
 func (s *StubbedS3Suite) TestIndex(c *check.C) {
-       v := NewTestableS3Volume(c, 0, false, 2)
-       v.indexPageSize = 3
+       v := s.newTestableVolume(c, 0, false, 2)
+       v.IndexPageSize = 3
        for i := 0; i < 256; i++ {
                v.PutRaw(fmt.Sprintf("%02x%030x", i, i), []byte{102, 111, 111})
        }
@@ -112,14 +82,14 @@ func (s *StubbedS3Suite) TestIndex(c *check.C) {
 }
 
 func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
-       defer func(tl, bs time.Duration) {
-               trashLifetime = tl
-               blobSignatureTTL = bs
-       }(trashLifetime, blobSignatureTTL)
-       trashLifetime = time.Hour
-       blobSignatureTTL = time.Hour
+       defer func(tl, bs arvados.Duration) {
+               theConfig.TrashLifetime = tl
+               theConfig.BlobSignatureTTL = bs
+       }(theConfig.TrashLifetime, theConfig.BlobSignatureTTL)
+       theConfig.TrashLifetime.Set("1h")
+       theConfig.BlobSignatureTTL.Set("1h")
 
-       v := NewTestableS3Volume(c, 5*time.Minute, false, 2)
+       v := s.newTestableVolume(c, 5*time.Minute, false, 2)
        var none time.Time
 
        putS3Obj := func(t time.Time, key string, data []byte) {
@@ -127,7 +97,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
                        return
                }
                v.serverClock.now = &t
-               v.Bucket.Put(key, data, "application/octet-stream", s3ACL, s3.Options{})
+               v.bucket.Put(key, data, "application/octet-stream", s3ACL, s3.Options{})
        }
 
        t0 := time.Now()
@@ -214,12 +184,12 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
                        false, false, false, true, false, false,
                },
                {
-                       "Erroneously trashed during a race, detected before trashLifetime",
+                       "Erroneously trashed during a race, detected before TrashLifetime",
                        none, t0.Add(-30 * time.Minute), t0.Add(-29 * time.Minute),
                        true, false, true, true, true, false,
                },
                {
-                       "Erroneously trashed during a race, rescue during EmptyTrash despite reaching trashLifetime",
+                       "Erroneously trashed during a race, rescue during EmptyTrash despite reaching TrashLifetime",
                        none, t0.Add(-90 * time.Minute), t0.Add(-89 * time.Minute),
                        true, false, true, true, true, false,
                },
@@ -286,7 +256,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
                // freshAfterEmpty
                loc, blk = setupScenario()
                v.EmptyTrash()
-               _, err = v.Bucket.Head("trash/"+loc, nil)
+               _, err = v.bucket.Head("trash/"+loc, nil)
                c.Check(err == nil, check.Equals, scenario.haveTrashAfterEmpty)
                if scenario.freshAfterEmpty {
                        t, err := v.Mtime(loc)
@@ -307,9 +277,51 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
        }
 }
 
+type TestableS3Volume struct {
+       *S3Volume
+       server      *s3test.Server
+       c           *check.C
+       serverClock *fakeClock
+}
+
+func (s *StubbedS3Suite) newTestableVolume(c *check.C, raceWindow time.Duration, readonly bool, replication int) *TestableS3Volume {
+       clock := &fakeClock{}
+       srv, err := s3test.NewServer(&s3test.Config{Clock: clock})
+       c.Assert(err, check.IsNil)
+
+       tmp, err := ioutil.TempFile("", "keepstore")
+       c.Assert(err, check.IsNil)
+       defer os.Remove(tmp.Name())
+       _, err = tmp.Write([]byte("xxx\n"))
+       c.Assert(err, check.IsNil)
+       c.Assert(tmp.Close(), check.IsNil)
+
+       v := &TestableS3Volume{
+               S3Volume: &S3Volume{
+                       Bucket:             TestBucketName,
+                       AccessKeyFile:      tmp.Name(),
+                       SecretKeyFile:      tmp.Name(),
+                       Endpoint:           srv.URL(),
+                       Region:             "test-region-1",
+                       LocationConstraint: true,
+                       RaceWindow:         arvados.Duration(raceWindow),
+                       S3Replication:      replication,
+                       UnsafeDelete:       s3UnsafeDelete,
+                       ReadOnly:           readonly,
+                       IndexPageSize:      1000,
+               },
+               server:      srv,
+               serverClock: clock,
+       }
+       c.Assert(v.Start(), check.IsNil)
+       err = v.bucket.PutBucket(s3.ACL("private"))
+       c.Assert(err, check.IsNil)
+       return v
+}
+
 // PutRaw skips the ContentMD5 test
 func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
-       err := v.Bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
+       err := v.bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
        if err != nil {
                log.Printf("PutRaw: %+v", err)
        }
@@ -320,7 +332,7 @@ func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
 // while we do this.
 func (v *TestableS3Volume) TouchWithDate(locator string, lastPut time.Time) {
        v.serverClock.now = &lastPut
-       err := v.Bucket.Put("recent/"+locator, nil, "application/octet-stream", s3ACL, s3.Options{})
+       err := v.bucket.Put("recent/"+locator, nil, "application/octet-stream", s3ACL, s3.Options{})
        if err != nil {
                panic(err)
        }