/
testing.go
416 lines (350 loc) · 10.4 KB
/
testing.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package storage
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/hashicorp/boundary-plugin-aws/internal/credential"
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/storagebuckets"
pb "github.com/hashicorp/boundary/sdk/pbs/plugin"
"github.com/hashicorp/go-secure-stdlib/awsutil/v2"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/structpb"
)
const (
testOptionErr = "test option error"
testGetUserErr = "test error for GetUser"
testGetCallerIdentityErr = "test error for GetCallerIdentity"
testDeleteAccessKeyErr = "test error for DeleteAccessKey"
testGetObjectErr = "test error for GetObject"
testPutObjectErr = "test error for PutObject"
testHeadObjectErr = "test error for HeadObject"
testListObjectV2Err = "test error for ListObjectV2"
testDeleteObjectErr = "test error for DeleteObject"
testDeleteObjectsErr = "test error for DeleteObjects"
)
// throttleErr is a mocked error used for testing the aws s3 client
type throttleErr struct{}
func (e *throttleErr) ErrorCode() string {
return "ThrottlingException"
}
func (e *throttleErr) Error() string {
return "ThrottlingException"
}
type testMockS3State struct {
GetObjectCalled bool
GetObjectInputParams *s3.GetObjectInput
PutObjectCalled bool
PutObjectInputParams *s3.PutObjectInput
PutObjectBody []byte
HeadObjectCalled bool
HeadObjectInputParams *s3.HeadObjectInput
ListObjectsV2Called bool
ListObjectsV2InputParams *s3.ListObjectsV2Input
DeleteObjectCalled bool
DeleteObjectInputParams *s3.DeleteObjectInput
DeleteObjectsCalled bool
DeleteObjectsInputParams *s3.DeleteObjectsInput
}
func (s *testMockS3State) Reset() {
s.GetObjectCalled = false
s.GetObjectInputParams = nil
s.PutObjectCalled = false
s.PutObjectInputParams = nil
s.PutObjectBody = nil
s.HeadObjectCalled = false
s.HeadObjectInputParams = nil
s.ListObjectsV2Called = false
s.ListObjectsV2InputParams = nil
s.DeleteObjectCalled = false
s.DeleteObjectInputParams = nil
s.DeleteObjectsCalled = false
s.DeleteObjectsInputParams = nil
}
type testMockS3 struct {
S3API
State *testMockS3State
Region string
// mocked responses for getObject
GetObjectOutput *s3.GetObjectOutput
GetObjectErr error
// mocked responses for putObject
PutObjectOutput *s3.PutObjectOutput
PutObjectErr error
// mocked responses for headObject
HeadObjectOutput *s3.HeadObjectOutput
HeadObjectErr error
// mocked responses for ListObjectsV2 (needed for delete)
ListObjectsV2Output *s3.ListObjectsV2Output
ListObjectsV2Err error
// sometimes we need to create a wildcard output
ListObjectsV2OutputFunc func(*s3.ListObjectsV2Input) *s3.ListObjectsV2Output
// mocked responses for DeleteObject(s)
DeleteObjectOutput *s3.DeleteObjectOutput
DeleteObjectErr error
DeleteObjectsOutput *s3.DeleteObjectsOutput
DeleteObjectsErr error
}
type testMockS3Option func(m *testMockS3) error
func testMockS3WithPutObjectOutput(o *s3.PutObjectOutput) testMockS3Option {
return func(m *testMockS3) error {
m.PutObjectOutput = o
return nil
}
}
func testMockS3WithPutObjectError(e error) testMockS3Option {
return func(m *testMockS3) error {
m.PutObjectErr = e
return nil
}
}
func testMockS3WithGetObjectOutput(o *s3.GetObjectOutput) testMockS3Option {
return func(m *testMockS3) error {
m.GetObjectOutput = o
return nil
}
}
func testMockS3WithGetObjectError(e error) testMockS3Option {
return func(m *testMockS3) error {
m.GetObjectErr = e
return nil
}
}
func testMockS3WithHeadObjectOutput(o *s3.HeadObjectOutput) testMockS3Option {
return func(m *testMockS3) error {
m.HeadObjectOutput = o
return nil
}
}
func testMockS3WithHeadObjectError(e error) testMockS3Option {
return func(m *testMockS3) error {
m.HeadObjectErr = e
return nil
}
}
func testMockS3WithListObjectsV2Output(o *s3.ListObjectsV2Output) testMockS3Option {
return func(m *testMockS3) error {
m.ListObjectsV2Output = o
return nil
}
}
func testMockS3WithListObjectsV2Error(e error) testMockS3Option {
return func(m *testMockS3) error {
m.ListObjectsV2Err = e
return nil
}
}
func testMockS3WithListObjectsV2OutputFunc(o func(*s3.ListObjectsV2Input) *s3.ListObjectsV2Output) testMockS3Option {
return func(m *testMockS3) error {
m.ListObjectsV2OutputFunc = o
return nil
}
}
func testMockS3WithDeleteObjectOutput(o *s3.DeleteObjectOutput) testMockS3Option {
return func(m *testMockS3) error {
m.DeleteObjectOutput = o
return nil
}
}
func testMockS3WithDeleteObjectError(e error) testMockS3Option {
return func(m *testMockS3) error {
m.DeleteObjectErr = e
return nil
}
}
func testMockS3WithDeleteObjectsOutput(o *s3.DeleteObjectsOutput) testMockS3Option {
return func(m *testMockS3) error {
m.DeleteObjectsOutput = o
return nil
}
}
func testMockS3WithDeleteObjectsError(e error) testMockS3Option {
return func(m *testMockS3) error {
m.DeleteObjectsErr = e
return nil
}
}
func newTestMockS3(state *testMockS3State, opts ...testMockS3Option) s3APIFunc {
return func(cfgs ...aws.Config) (S3API, error) {
m := &testMockS3{
State: state,
}
for _, opt := range opts {
if err := opt(m); err != nil {
return nil, err
}
}
for _, cfg := range cfgs {
// Last region takes precedence
if cfg.Region != "" {
m.Region = cfg.Region
}
}
return m, nil
}
}
func (m *testMockS3) GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
if m.State != nil {
m.State.GetObjectCalled = true
m.State.GetObjectInputParams = params
}
if m.GetObjectErr != nil {
return nil, m.GetObjectErr
}
return m.GetObjectOutput, nil
}
func (m *testMockS3) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
if m.State != nil {
m.State.PutObjectCalled = true
m.State.PutObjectInputParams = params
}
if m.PutObjectErr != nil {
return nil, m.PutObjectErr
}
// Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write.
// Therefore, we need to read the entire body to ensure that the pipe is not blocked.
if params.Body != nil {
data, err := io.ReadAll(params.Body)
if err != nil {
return nil, err
}
if m.State == nil || m.State.PutObjectBody == nil {
return m.PutObjectOutput, nil
}
if !bytes.Equal(data, m.State.PutObjectBody) {
return nil, fmt.Errorf("expected body %q, got %q", m.State.PutObjectBody, data)
}
}
return m.PutObjectOutput, nil
}
func (m *testMockS3) ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error) {
if m.State != nil {
m.State.ListObjectsV2Called = true
m.State.ListObjectsV2InputParams = params
}
if m.ListObjectsV2Err != nil {
return nil, m.ListObjectsV2Err
}
if m.ListObjectsV2OutputFunc != nil {
return m.ListObjectsV2OutputFunc(params), nil
}
return m.ListObjectsV2Output, nil
}
func (m *testMockS3) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
if m.State != nil {
m.State.DeleteObjectCalled = true
m.State.DeleteObjectInputParams = params
}
if m.DeleteObjectErr != nil {
return nil, m.DeleteObjectErr
}
return m.DeleteObjectOutput, nil
}
func (m *testMockS3) DeleteObjects(ctx context.Context, params *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error) {
if m.State != nil {
m.State.DeleteObjectsCalled = true
m.State.DeleteObjectsInputParams = params
}
if m.DeleteObjectsErr != nil {
return nil, m.DeleteObjectsErr
}
return m.DeleteObjectsOutput, nil
}
func (m *testMockS3) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
if m.State != nil {
m.State.HeadObjectCalled = true
m.State.HeadObjectInputParams = params
}
if m.HeadObjectErr != nil {
return nil, m.HeadObjectErr
}
return m.HeadObjectOutput, nil
}
type getObjectStreamMock struct {
grpc.ServerStream
ctx context.Context
err error
data *pb.GetObjectResponse
}
func (s *getObjectStreamMock) Context() context.Context {
return s.ctx
}
// Send streams a response message to the client.
// This function should only be called on the server side.
func (g *getObjectStreamMock) Send(req *pb.GetObjectResponse) error {
if req == nil {
return fmt.Errorf(`parameter arg "resp GetObjectResponse" cannot be nil.`)
}
g.data = req
return nil
}
// Recv streams a response message from the server.
// This function should only be called on the client side.
func (g *getObjectStreamMock) Recv() (*pb.GetObjectResponse, error) {
if g.data == nil {
if g.err != nil {
return nil, g.err
}
return nil, io.EOF
}
resp := &pb.GetObjectResponse{
FileChunk: append([]byte{}, g.data.FileChunk...),
}
g.data = nil
return resp, nil
}
func newGetObjectStreamMock() *getObjectStreamMock {
return &getObjectStreamMock{
ctx: context.Background(),
}
}
func createTime(t *testing.T, timestamp string) time.Time {
require := require.New(t)
testTime, err := time.Parse(time.RFC3339, timestamp)
require.NoError(err)
return testTime
}
func deepCopyPutObjectRequest(v *pb.PutObjectRequest) *pb.PutObjectRequest {
deepCopy := &pb.PutObjectRequest{
Key: v.Key,
Path: v.Path,
}
if v.Bucket != nil {
deepCopy.Bucket = &storagebuckets.StorageBucket{
BucketName: v.Bucket.BucketName,
BucketPrefix: v.Bucket.BucketPrefix,
}
if v.Bucket.Attributes != nil {
attrs, _ := structpb.NewStruct(v.Bucket.Attributes.AsMap())
deepCopy.Bucket.Attributes = attrs
}
if v.Bucket.Secrets != nil {
secrets, _ := structpb.NewStruct(v.Bucket.Secrets.AsMap())
deepCopy.Bucket.Secrets = secrets
}
}
return deepCopy
}
func validSTSMock() []credential.AwsCredentialPersistedStateOption {
return []credential.AwsCredentialPersistedStateOption{
credential.WithStateTestOpts([]awsutil.Option{
awsutil.WithSTSAPIFunc(
awsutil.NewMockSTS(
awsutil.WithGetCallerIdentityOutput(&sts.GetCallerIdentityOutput{
Account: aws.String("0123456789"),
Arn: aws.String("arn:aws:iam::0123456789:user/test"),
UserId: aws.String("test"),
}),
),
),
}),
}
}