-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
225 lines (198 loc) · 6.17 KB
/
main_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"testing"
"time"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func ptrTo(s string) *string {
return &s
}
func ptrToI(s int64) *int64 {
return &s
}
func ptrToB(s bool) *bool {
return &s
}
func TestMain(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main")
}
// Define a mock struct to be used in your unit tests of myFunc.
type mockS3Client struct {
CopySrc *string
Bucket *string
Key *string
s3iface.S3API
}
func (m *mockS3Client) CopyObject(input *s3.CopyObjectInput) (*s3.CopyObjectOutput, error) {
m.Key = input.Key
m.Bucket = input.Bucket
m.CopySrc = input.CopySource
return nil, nil
}
func (m *mockS3Client) DeleteObject(input *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error) {
m.Key = input.Key
m.Bucket = input.Bucket
return nil, nil
}
func (m *mockS3Client) ListObjectVersionsPages(input *s3.ListObjectVersionsInput, process func(p *s3.ListObjectVersionsOutput, last bool) (shouldContinue bool)) error {
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
output := s3.ListObjectVersionsOutput{
Versions: []*s3.ObjectVersion{
&s3.ObjectVersion{
ETag: ptrTo("\"6994d44ab6c3b4c005357798f6b0d750\""),
IsLatest: ptrToB(false),
Key: ptrTo("file2"),
LastModified: &startTimeMinusHour,
Owner: &s3.Owner{
DisplayName: ptrTo("user"),
ID: ptrTo("ownerid"),
},
Size: ptrToI(6),
StorageClass: ptrTo("STANDARD"),
VersionId: ptrTo("versionid1"),
},
&s3.ObjectVersion{
ETag: ptrTo("\"6994d44ab6c3b4c005357798f6b0d750\""),
IsLatest: ptrToB(false),
Key: ptrTo("file1"),
LastModified: &startTime,
Owner: &s3.Owner{
DisplayName: ptrTo("user"),
ID: ptrTo("ownerid"),
},
Size: ptrToI(6),
StorageClass: ptrTo("STANDARD"),
VersionId: ptrTo("versionid1"),
},
},
}
process(&output, true)
return nil
}
var _ = Describe("Main", func() {
It("deletes correctly", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
startTimeMinus2Hour := startTimeMinusHour.Add(time.Hour * -1)
final["file1"] = s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinus2Hour,
VersionId: ptrTo("1"),
}
marker := s3.DeleteMarkerEntry{
Key: ptrTo("file1"),
VersionId: ptrTo("2"),
LastModified: &startTimeMinusHour,
}
processDeleteMarker(&final, startTime, &marker)
Expect(*(final["file1"].VersionId)).To(Equal("DELETE"))
})
It("doesn't delete when it shouldn't", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
startTimeMinus2Hour := startTimeMinusHour.Add(time.Hour * -1)
file := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinusHour,
VersionId: ptrTo("1"),
}
final["file1"] = file
marker := s3.DeleteMarkerEntry{
Key: ptrTo("file1"),
VersionId: ptrTo("2"),
LastModified: &startTimeMinus2Hour,
}
processDeleteMarker(&final, startTime, &marker)
Expect(final["file1"]).To(Equal(file))
})
It("Processes a version when it should", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
file := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinusHour,
VersionId: ptrTo("1"),
}
processVersion(&final, startTime, &file)
Expect(final["file1"]).To(Equal(file))
})
It("Creates a delete marker for future versions", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
file := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTime,
VersionId: ptrTo("1"),
}
processVersion(&final, startTimeMinusHour, &file)
Expect(*(final["file1"].VersionId)).To(Equal("DELETE"))
})
It("Chooses the newest applicable version", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
startTimeMinus2Hour := startTimeMinusHour.Add(time.Hour * -1)
file := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinus2Hour,
VersionId: ptrTo("1"),
}
filev2 := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinusHour,
VersionId: ptrTo("2"),
}
processVersion(&final, startTime, &file)
processVersion(&final, startTime, &filev2)
Expect(final["file1"]).To(Equal(filev2))
})
It("Chooses the newest applicable version processed backward", func() {
final := make(map[string]s3.ObjectVersion)
startTime := time.Now()
startTimeMinusHour := startTime.Add(time.Hour * -1)
startTimeMinus2Hour := startTimeMinusHour.Add(time.Hour * -1)
file := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinus2Hour,
VersionId: ptrTo("1"),
}
filev2 := s3.ObjectVersion{
Key: ptrTo("file1"),
LastModified: &startTimeMinusHour,
VersionId: ptrTo("2"),
}
processVersion(&final, startTime, &filev2)
processVersion(&final, startTime, &file)
Expect(final["file1"]).To(Equal(filev2))
})
It("Sets a version", func() {
mockSvc := &mockS3Client{}
setVersion(mockSvc, "bucket", "key", "versionid")
Expect(*(mockSvc.Key)).To(Equal("key"))
Expect(*(mockSvc.Bucket)).To(Equal("bucket"))
Expect(*(mockSvc.CopySrc)).To(Equal("/bucket/key?versionId=versionid"))
})
It("Deletes when a version is marked", func() {
mockSvc := &mockS3Client{}
setVersion(mockSvc, "bucket", "key", "DELETE")
Expect(*(mockSvc.Key)).To(Equal("key"))
Expect(*(mockSvc.Bucket)).To(Equal("bucket"))
})
It("Processes a dictionary of versions", func() {
startTime := time.Now()
mockSvc := &mockS3Client{}
dict, err := buildVersionDictionary(mockSvc, "bucket", startTime)
Expect(err).To(BeNil())
Expect(*(dict["file2"].VersionId)).To(Equal("versionid1"))
Expect(*(dict["file1"].VersionId)).To(Equal("DELETE"))
})
})