10 "github.com/AdRoll/goamz/aws"
11 "github.com/AdRoll/goamz/s3"
12 "github.com/AdRoll/goamz/s3/s3test"
13 check "gopkg.in/check.v1"
16 type TestableS3Volume struct {
20 serverClock *fakeClock
24 TestBucketName = "testbucket"
27 type fakeClock struct {
31 func (c *fakeClock) Now() time.Time {
39 // Deleting isn't safe from races, but if it's turned on
40 // anyway we do expect it to pass the generic volume tests.
44 func NewTestableS3Volume(c *check.C, readonly bool, replication int) *TestableS3Volume {
46 srv, err := s3test.NewServer(&s3test.Config{Clock: clock})
47 c.Assert(err, check.IsNil)
50 Name: "test-region-1",
51 S3Endpoint: srv.URL(),
52 S3LocationConstraint: true,
55 S3: s3.New(auth, region),
58 err = bucket.PutBucket(s3.ACL("private"))
59 c.Assert(err, check.IsNil)
61 return &TestableS3Volume{
62 S3Volume: NewS3Volume(auth, region, TestBucketName, readonly, replication),
68 var _ = check.Suite(&StubbedS3Suite{})
70 type StubbedS3Suite struct {
71 volumes []*TestableS3Volume
74 func (s *StubbedS3Suite) TestGeneric(c *check.C) {
75 DoGenericVolumeTests(c, func(t TB) TestableVolume {
76 return NewTestableS3Volume(c, false, 2)
80 func (s *StubbedS3Suite) TestGenericReadOnly(c *check.C) {
81 DoGenericVolumeTests(c, func(t TB) TestableVolume {
82 return NewTestableS3Volume(c, true, 2)
86 func (s *StubbedS3Suite) TestIndex(c *check.C) {
87 v := NewTestableS3Volume(c, false, 2)
89 for i := 0; i < 256; i++ {
90 v.PutRaw(fmt.Sprintf("%02x%030x", i, i), []byte{102, 111, 111})
92 for _, spec := range []struct {
101 buf := new(bytes.Buffer)
102 err := v.IndexTo(spec.prefix, buf)
103 c.Check(err, check.IsNil)
105 idx := bytes.SplitAfter(buf.Bytes(), []byte{10})
106 c.Check(len(idx), check.Equals, spec.expectMatch+1)
107 c.Check(len(idx[len(idx)-1]), check.Equals, 0)
111 // PutRaw skips the ContentMD5 test
112 func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
113 err := v.Bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
115 log.Printf("PutRaw: %+v", err)
119 // TouchWithDate turns back the clock while doing a Touch(). We assume
120 // there are no other operations happening on the same s3test server
122 func (v *TestableS3Volume) TouchWithDate(locator string, lastPut time.Time) {
123 v.serverClock.now = &lastPut
124 err := v.Touch(locator)
125 if err != nil && !strings.Contains(err.Error(), "PutCopy returned old LastModified") {
126 log.Printf("Touch: %+v", err)
128 v.serverClock.now = nil
131 func (v *TestableS3Volume) Teardown() {