forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_test.go
134 lines (120 loc) · 3.5 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
package checkup
import (
"bytes"
"io/ioutil"
"strconv"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/service/s3"
)
func TestS3Store(t *testing.T) {
keyID, accessKey, region, bucket := "fakeKeyID", "fakeKey", "fakeRegion", "fakeBucket"
fakes3 := new(s3Mock)
results := []Result{{Title: "Testing"}}
resultsBytes := []byte(`[{"title":"Testing"}]`)
newS3 = func(p client.ConfigProvider, cfgs ...*aws.Config) s3svc {
if len(cfgs) != 1 {
t.Fatalf("Expected 1 aws.Config, got %d", len(cfgs))
}
creds, err := cfgs[0].Credentials.Get()
if err != nil {
t.Fatalf("Got an error when calling Get() on Credentials: %v", err)
}
if got, want := creds.AccessKeyID, keyID; got != want {
t.Errorf("Expected AccessKeyID to be '%s', got '%s'", want, got)
}
if got, want := creds.SecretAccessKey, accessKey; got != want {
t.Errorf("Expected SecretAccessKey to be '%s', got '%s'", want, got)
}
if got, want := *cfgs[0].Region, region; got != want {
t.Errorf("Expected Region to be '%s', got '%s'", want, got)
}
return fakes3
}
specimen := S3{
AccessKeyID: keyID,
SecretAccessKey: accessKey,
Region: region,
Bucket: bucket,
}
err := specimen.Store(results)
if err != nil {
t.Fatalf("Expected no error from Store(), got: %v", err)
}
// Make sure bucket name is right
if got, want := *fakes3.input.Bucket, bucket; got != want {
t.Errorf("Expected Bucket to be '%s', got '%s'", want, got)
}
// Make sure filename has timestamp of check
key := *fakes3.input.Key
hyphenPos := strings.Index(key, "-")
if hyphenPos < 0 {
t.Fatalf("Expected Key to have timestamp then hyphen, got: %s", key)
}
tsString := key[:hyphenPos]
tsNs, err := strconv.ParseInt(tsString, 10, 64)
if err != nil {
t.Fatalf("Expected Key's timestamp to be integer; got error: %v", err)
}
ts := time.Unix(0, tsNs)
if time.Since(ts) > 1*time.Second {
t.Errorf("Timestamp of filename is %s but expected something very recent", ts)
}
// Make sure body bytes are correct
bodyBytes, err := ioutil.ReadAll(fakes3.input.Body)
if err != nil {
t.Fatalf("Expected no error reading body, got: %v", err)
}
if bytes.Compare(bodyBytes, resultsBytes) != 0 {
t.Errorf("Contents of file are wrong\nExpected %s\n Got %s", resultsBytes, bodyBytes)
}
}
func TestS3Maintain(t *testing.T) {
fakes3 := new(s3Mock)
newS3 = func(p client.ConfigProvider, cfgs ...*aws.Config) s3svc {
return fakes3
}
var specimen S3
err := specimen.Maintain()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if fakes3.deleted {
t.Fatal("No deletions should happen unless CheckExpiry is set")
}
specimen.CheckExpiry = 24 * 30 * time.Hour
err = specimen.Maintain()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !fakes3.deleted {
t.Fatal("Expected deletions, but there weren't any")
}
}
// s3Mock mocks s3.S3.
type s3Mock struct {
input *s3.PutObjectInput
deleted bool
}
func (s *s3Mock) PutObject(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
s.input = input
return nil, nil
}
func (s *s3Mock) ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
return &s3.ListObjectsOutput{
Contents: []*s3.Object{
{
Key: aws.String("foobar"),
LastModified: new(time.Time),
},
},
IsTruncated: aws.Bool(input.Marker == nil),
}, nil
}
func (s *s3Mock) DeleteObjects(input *s3.DeleteObjectsInput) (*s3.DeleteObjectsOutput, error) {
s.deleted = true
return nil, nil
}