forked from ribbybibby/s3_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_exporter_test.go
238 lines (217 loc) · 6.62 KB
/
s3_exporter_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
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
var (
mockSvc = &mockS3Client{}
testCases = s3ExporterTestCases{
// Test one object in a bucket
s3ExporterTestCase{
Name: "one object",
Bucket: "mock",
Prefix: "one",
ExpectedOutputLines: []string{
"s3_list_success{bucket=\"mock\",delimiter=\"\",prefix=\"one\"} 1",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"one\"} 1.5604596e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_objects{bucket=\"mock\",prefix=\"one\"} 1",
},
ListObjectsV2Response: &s3.ListObjectsV2Output{
Contents: []*s3.Object{
&s3.Object{
Key: String("one"),
LastModified: Time(time.Date(2019, time.June, 13, 21, 0, 0, 0, time.UTC)),
Size: Int64(1234),
},
},
IsTruncated: Bool(false),
KeyCount: Int64(1),
MaxKeys: Int64(1000),
Name: String("mock"),
Prefix: String("one"),
},
},
// Test no matching objects in the bucket
s3ExporterTestCase{
Name: "no objects",
Bucket: "mock",
Prefix: "none",
ExpectedOutputLines: []string{
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"none\"} -6.795364578e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_list_success{bucket=\"mock\",delimiter=\"\",prefix=\"none\"} 1",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_objects{bucket=\"mock\",prefix=\"none\"} 0",
},
ListObjectsV2Response: &s3.ListObjectsV2Output{
Contents: []*s3.Object{},
IsTruncated: Bool(false),
KeyCount: Int64(0),
MaxKeys: Int64(1000),
Name: String("mock"),
Prefix: String("none"),
},
},
// Test multiple objects
s3ExporterTestCase{
Name: "multiple objects",
Bucket: "mock",
Prefix: "multiple",
ExpectedOutputLines: []string{
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"multiple\"} 4567",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"multiple\"} 1.568592e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"multiple\"} 4567",
"s3_list_success{bucket=\"mock\",delimiter=\"\",prefix=\"multiple\"} 1",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"multiple\"} 11602",
"s3_objects{bucket=\"mock\",prefix=\"multiple\"} 4",
},
ListObjectsV2Response: &s3.ListObjectsV2Output{
Contents: []*s3.Object{
&s3.Object{
Key: String("multiple0"),
LastModified: Time(time.Date(2019, time.June, 13, 21, 0, 0, 0, time.UTC)),
Size: Int64(1234),
},
&s3.Object{
Key: String("multiple1"),
LastModified: Time(time.Date(2019, time.July, 14, 22, 0, 0, 0, time.UTC)),
Size: Int64(2345),
},
&s3.Object{
Key: String("multiple2"),
LastModified: Time(time.Date(2019, time.August, 15, 23, 0, 0, 0, time.UTC)),
Size: Int64(3456),
},
&s3.Object{
Key: String("multiple/0"),
LastModified: Time(time.Date(2019, time.September, 16, 00, 0, 0, 0, time.UTC)),
Size: Int64(4567),
},
},
IsTruncated: Bool(false),
KeyCount: Int64(4),
MaxKeys: Int64(1000),
Name: String("mock"),
Prefix: String("multiple"),
},
},
// Test delimiter
s3ExporterTestCase{
Name: "common prefixes",
Bucket: "mock",
Prefix: "mock-prefix",
Delimiter: "/",
ExpectedOutputLines: []string{
"s3_list_success{bucket=\"mock\",delimiter=\"/\",prefix=\"mock-prefix\"} 1",
"s3_common_prefixes{bucket=\"mock\",delimiter=\"/\",prefix=\"mock-prefix\"} 3",
},
ListObjectsV2Response: &s3.ListObjectsV2Output{
Name: aws.String("mock"),
Prefix: aws.String("mock-prefix"),
CommonPrefixes: []*s3.CommonPrefix{
{
Prefix: aws.String("one"),
},
{
Prefix: aws.String("two"),
},
{
Prefix: aws.String("three"),
},
},
},
},
}
)
type mockS3Client struct {
s3iface.S3API
}
type s3ExporterTestCase struct {
Name string
Bucket string
Prefix string
Delimiter string
ExpectedOutputLines []string
ListObjectsV2Response *s3.ListObjectsV2Output
}
// testBody tests the body returned by the exporter against the expected output
func (tc *s3ExporterTestCase) testBody(body string, t *testing.T) {
for _, l := range tc.ExpectedOutputLines {
ok := strings.Contains(body, l)
if !ok {
t.Errorf("expected " + l)
}
}
}
type s3ExporterTestCases []s3ExporterTestCase
// Returns the mocked response for a bucket+prefix combination
func (tcs *s3ExporterTestCases) response(bucket, prefix string) (*s3.ListObjectsV2Output, error) {
for _, c := range *tcs {
if c.Bucket == bucket && c.Prefix == prefix {
return c.ListObjectsV2Response, nil
}
}
return nil, errors.New("Can't find a response for the bucket and prefix combination")
}
// TestProbeHandler iterates over a list of test cases
func TestProbeHandler(t *testing.T) {
for _, c := range testCases {
rr, err := probe(c.Bucket, c.Prefix, c.Delimiter)
if err != nil {
t.Errorf(err.Error())
}
c.testBody(rr.Body.String(), t)
}
}
// ListObjectsV2 mocks out the corresponding function in the S3 client, returning the response that corresponds to the test case
func (m *mockS3Client) ListObjectsV2(input *s3.ListObjectsV2Input) (*s3.ListObjectsV2Output, error) {
r, err := testCases.response(*input.Bucket, *input.Prefix)
if err != nil {
return nil, err
}
return r, nil
}
// Repeatable probe function
func probe(bucket, prefix, delimiter string) (rr *httptest.ResponseRecorder, err error) {
uri := "/probe?bucket=" + bucket
if len(prefix) > 0 {
uri = uri + "&prefix=" + prefix
}
if len(delimiter) > 0 {
uri = uri + "&delimiter=" + delimiter
}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return
}
rr = httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, mockSvc)
})
handler.ServeHTTP(rr, req)
return
}
// Functions to help return pointers succinctly
func String(s string) *string {
return &s
}
func Time(t time.Time) *time.Time {
return &t
}
func Int64(i int64) *int64 {
return &i
}
func Bool(b bool) *bool {
return &b
}