9 "github.com/AdRoll/goamz/aws"
10 "github.com/AdRoll/goamz/s3"
11 "github.com/AdRoll/goamz/s3/s3test"
12 check "gopkg.in/check.v1"
15 type TestableS3Volume struct {
19 serverClock *fakeClock
23 TestBucketName = "testbucket"
26 type fakeClock struct {
30 func (c *fakeClock) Now() time.Time {
38 // Deleting isn't safe from races, but if it's turned on
39 // anyway we do expect it to pass the generic volume tests.
43 func NewTestableS3Volume(c *check.C, raceWindow time.Duration, readonly bool, replication int) *TestableS3Volume {
45 srv, err := s3test.NewServer(&s3test.Config{Clock: clock})
46 c.Assert(err, check.IsNil)
49 Name: "test-region-1",
50 S3Endpoint: srv.URL(),
51 S3LocationConstraint: true,
54 S3: s3.New(auth, region),
57 err = bucket.PutBucket(s3.ACL("private"))
58 c.Assert(err, check.IsNil)
60 return &TestableS3Volume{
61 S3Volume: NewS3Volume(auth, region, TestBucketName, raceWindow, readonly, replication),
67 var _ = check.Suite(&StubbedS3Suite{})
69 type StubbedS3Suite struct {
70 volumes []*TestableS3Volume
73 func (s *StubbedS3Suite) TestGeneric(c *check.C) {
74 DoGenericVolumeTests(c, func(t TB) TestableVolume {
75 // Use a negative raceWindow so s3test's 1-second
76 // timestamp precision doesn't confuse fixRace.
77 return NewTestableS3Volume(c, -time.Second, false, 2)
81 func (s *StubbedS3Suite) TestGenericReadOnly(c *check.C) {
82 DoGenericVolumeTests(c, func(t TB) TestableVolume {
83 return NewTestableS3Volume(c, -time.Second, true, 2)
87 func (s *StubbedS3Suite) TestIndex(c *check.C) {
88 v := NewTestableS3Volume(c, 0, false, 2)
90 for i := 0; i < 256; i++ {
91 v.PutRaw(fmt.Sprintf("%02x%030x", i, i), []byte{102, 111, 111})
93 for _, spec := range []struct {
102 buf := new(bytes.Buffer)
103 err := v.IndexTo(spec.prefix, buf)
104 c.Check(err, check.IsNil)
106 idx := bytes.SplitAfter(buf.Bytes(), []byte{10})
107 c.Check(len(idx), check.Equals, spec.expectMatch+1)
108 c.Check(len(idx[len(idx)-1]), check.Equals, 0)
112 // PutRaw skips the ContentMD5 test
113 func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
114 err := v.Bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
116 log.Printf("PutRaw: %+v", err)
120 // TouchWithDate turns back the clock while doing a Touch(). We assume
121 // there are no other operations happening on the same s3test server
123 func (v *TestableS3Volume) TouchWithDate(locator string, lastPut time.Time) {
124 v.serverClock.now = &lastPut
125 err := v.Bucket.Put("recent/"+locator, nil, "application/octet-stream", s3ACL, s3.Options{})
129 v.serverClock.now = nil
132 func (v *TestableS3Volume) Teardown() {