-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
s3_storage_client_test.go
249 lines (218 loc) · 5.84 KB
/
s3_storage_client_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
239
240
241
242
243
244
245
246
247
248
249
package aws
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"gopkg.in/yaml.v2"
"github.com/grafana/dskit/backoff"
"github.com/grafana/dskit/flagext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"github.com/grafana/loki/pkg/storage/chunk/client/hedging"
)
type RoundTripperFunc func(*http.Request) (*http.Response, error)
func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestRequestPrefix(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.URL.Path)
}))
defer ts.Close()
cfg := S3Config{
Endpoint: ts.URL,
BucketNames: "buck-o",
KeyPrefix: "some/prefix/",
S3ForcePathStyle: true,
Insecure: true,
AccessKeyID: "key",
SecretAccessKey: flagext.SecretWithValue("secret"),
}
tests := []struct {
name string
key string
expected string
}{
{
name: "Single",
key: "key",
expected: "/buck-o/some/prefix/key",
},
{
name: "Multi",
key: "some/random/key",
expected: "/buck-o/some/prefix/some/random/key",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewS3ObjectClient(cfg, hedging.Config{})
require.NoError(t, err)
readCloser, _, err := client.GetObject(context.Background(), tt.key)
require.NoError(t, err)
buffer := make([]byte, 100)
_, err = readCloser.Read(buffer)
if err != io.EOF {
require.NoError(t, err)
}
assert.Equal(t, tt.expected, strings.Trim(string(buffer), "\n\x00"))
})
}
}
func TestRequestMiddleware(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.Header.Get("echo-me"))
}))
defer ts.Close()
cfg := S3Config{
Endpoint: ts.URL,
BucketNames: "buck-o",
S3ForcePathStyle: true,
Insecure: true,
AccessKeyID: "key",
SecretAccessKey: flagext.SecretWithValue("secret"),
}
tests := []struct {
name string
fn InjectRequestMiddleware
expected string
}{
{
name: "Test Nil",
fn: nil,
expected: "",
},
{
name: "Test Header Injection",
fn: func(next http.RoundTripper) http.RoundTripper {
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
req.Header["echo-me"] = []string{"blerg"}
return next.RoundTrip(req)
})
},
expected: "blerg",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg.Inject = tt.fn
client, err := NewS3ObjectClient(cfg, hedging.Config{})
require.NoError(t, err)
readCloser, _, err := client.GetObject(context.Background(), "key")
require.NoError(t, err)
buffer := make([]byte, 100)
_, err = readCloser.Read(buffer)
if err != io.EOF {
require.NoError(t, err)
}
assert.Equal(t, tt.expected, strings.Trim(string(buffer), "\n\x00"))
})
}
}
func Test_Hedging(t *testing.T) {
for _, tc := range []struct {
name string
expectedCalls int32
hedgeAt time.Duration
upTo int
do func(c *S3ObjectClient)
}{
{
"delete/put/list are not hedged",
3,
20 * time.Nanosecond,
10,
func(c *S3ObjectClient) {
_ = c.DeleteObject(context.Background(), "foo")
_, _, _ = c.List(context.Background(), "foo", "/")
_ = c.PutObject(context.Background(), "foo", bytes.NewReader([]byte("bar")))
},
},
{
"gets are hedged",
3,
20 * time.Nanosecond,
3,
func(c *S3ObjectClient) {
_, _, _ = c.GetObject(context.Background(), "foo")
},
},
{
"gets are not hedged when not configured",
1,
0,
0,
func(c *S3ObjectClient) {
_, _, _ = c.GetObject(context.Background(), "foo")
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
count := atomic.NewInt32(0)
c, err := NewS3ObjectClient(S3Config{
AccessKeyID: "foo",
SecretAccessKey: flagext.SecretWithValue("bar"),
BackoffConfig: backoff.Config{MaxRetries: 1},
BucketNames: "foo",
Inject: func(next http.RoundTripper) http.RoundTripper {
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
count.Inc()
time.Sleep(200 * time.Millisecond)
return nil, errors.New("foo")
})
},
}, hedging.Config{
At: tc.hedgeAt,
UpTo: tc.upTo,
MaxPerSecond: 1000,
})
require.NoError(t, err)
tc.do(c)
require.Equal(t, tc.expectedCalls, count.Load())
})
}
}
func Test_ConfigRedactsCredentials(t *testing.T) {
underTest := S3Config{
AccessKeyID: "access key id",
SecretAccessKey: flagext.SecretWithValue("secret access key"),
}
output, err := yaml.Marshal(underTest)
require.NoError(t, err)
require.True(t, bytes.Contains(output, []byte("access key id")))
require.False(t, bytes.Contains(output, []byte("secret access id")))
}
func Test_ConfigParsesCredentialsInline(t *testing.T) {
var underTest = S3Config{}
yamlCfg := `
access_key_id: access key id
secret_access_key: secret access key
`
err := yaml.Unmarshal([]byte(yamlCfg), &underTest)
require.NoError(t, err)
require.Equal(t, underTest.AccessKeyID, "access key id")
require.Equal(t, underTest.SecretAccessKey.String(), "secret access key")
require.Equal(t, underTest.SessionToken.String(), "")
}
func Test_ConfigParsesCredentialsInlineWithSessionToken(t *testing.T) {
var underTest = S3Config{}
yamlCfg := `
access_key_id: access key id
secret_access_key: secret access key
session_token: session token
`
err := yaml.Unmarshal([]byte(yamlCfg), &underTest)
require.NoError(t, err)
require.Equal(t, underTest.AccessKeyID, "access key id")
require.Equal(t, underTest.SecretAccessKey.String(), "secret access key")
require.Equal(t, underTest.SessionToken.String(), "session token")
}