-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_test.go
More file actions
238 lines (225 loc) 路 6.21 KB
/
Copy pathget_test.go
File metadata and controls
238 lines (225 loc) 路 6.21 KB
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 paramstore
import (
"context"
"errors"
"fmt"
"log/slog"
"reflect"
"strings"
"testing"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/hashicorp/go-multierror"
)
// mockGetParameters is a mock used to mimic the behavior of pulling multiple
// parameters from AWS SSM Parameter Store.
func mockGetParameters(
t string,
) func(ctx context.Context, input *ssm.GetParametersInput, optFns ...func(*ssm.Options)) (*ssm.GetParametersOutput, error) {
return func(ctx context.Context, input *ssm.GetParametersInput, optFns ...func(*ssm.Options)) (*ssm.GetParametersOutput, error) {
out := &ssm.GetParametersOutput{}
switch t {
case "success":
out.Parameters = make([]types.Parameter, len(input.Names))
testdata := validTestdata.toMap()
for i, n := range input.Names {
param, found := testdata[n]
if !found {
return nil, fmt.Errorf("%q is not found in testdata", n)
}
out.Parameters[i] = types.Parameter{
Name: ¶m.Name,
Value: ¶m.Value,
Type: types.ParameterType(param.Type),
}
}
case "invalid":
out.InvalidParameters = append(out.InvalidParameters, input.Names...)
case "error":
return nil, fmt.Errorf(
"failed to get parameter: %v",
strings.Join(input.Names, ", "),
)
}
return out, nil
}
}
func Test_Get(t *testing.T) {
tests := map[string]struct {
client *Client
name string
want *Parameter
errs *multierror.Error
}{
"get parameter": {
client: &Client{
withDecryption: true,
logger: slog.Default(),
batchSize: 1,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("success"),
},
},
name: validTestdata.toParameter().Name,
want: validTestdata.toParameter(),
},
"catch invalid parameters": {
client: &Client{
logger: slog.Default(),
batchSize: 1,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("invalid"),
},
},
name: validTestdata.toParameter().Name,
errs: &multierror.Error{
Errors: []error{validTestdata.toError("%q is an invalid parameter")},
},
},
"catch fail to get parameter": {
client: &Client{
logger: slog.Default(),
batchSize: 1,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("error"),
},
},
name: validTestdata.toParameter().Name,
errs: &multierror.Error{
Errors: []error{validTestdata.toError("failed to get parameter: %v")},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := tt.client.Get(context.Background(), tt.name)
// determine what to do if the error returned is a multierror or not.
// NOTE: got is populated by the errors.As() function.
var errs *multierror.Error
if errors.As(err, &errs) {
// check if the number of errors match with the expected errors.
if len(errs.Errors) != len(tt.errs.Errors) {
t.Errorf(
"Get() returned unexpected number of errors;\nwant=%v\ngot=%v\n",
tt.errs.Errors,
errs.Errors,
)
return
}
// compare each error returned with the expected errors.
for i, expectedErr := range tt.errs.Errors {
if errs.Errors[i].Error() != expectedErr.Error() {
t.Errorf(
"Get() got unexpected error;\nwant=%v\ngot=%v\n",
expectedErr,
errs.Errors[i].Error(),
)
return
}
}
} else if tt.errs != nil {
// an error was returned but it wasn't a multierror.
t.Errorf("Get() expected a multierror but got a regular error instead;\nwant=%v\ngot=%v\n", tt.errs, err)
return
}
if !reflect.DeepEqual(tt.want, got) {
t.Errorf(
"Get() returned unexpected configuration;\nwant=%+v\ngot=%+v\n",
tt.want,
got,
)
return
}
})
}
}
func Test_GetMultiple(t *testing.T) {
tests := map[string]struct {
client *Client
names []string
want Parameters
errs *multierror.Error
}{
"get parameters": {
client: &Client{
withDecryption: true,
logger: slog.Default(),
batchSize: 2,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("success"),
},
},
names: validTestdata.toSliceString(),
want: validTestdata.toParameters(),
errs: nil,
},
"catch invalid parameters": {
client: &Client{
logger: slog.Default(),
batchSize: 1,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("invalid"),
},
},
names: validTestdata.toSliceString(),
errs: &multierror.Error{
Errors: validTestdata.toSliceError("%q is an invalid parameter"),
},
},
"catch fail to get parameters": {
client: &Client{
logger: slog.Default(),
batchSize: 1,
ssmsvc: &mockSSMClient{
GetParametersFunc: mockGetParameters("error"),
},
},
names: validTestdata.toSliceString(),
errs: &multierror.Error{
Errors: validTestdata.toSliceError("failed to get parameter: %v"),
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := tt.client.GetMultiple(context.Background(), tt.names...)
// determine what to do if the error returned is a multierror or not.
// NOTE: got is populated by the errors.As() function.
var errs *multierror.Error
if errors.As(err, &errs) {
// check if the number of errors match with the expected errors.
if len(errs.Errors) != len(tt.errs.Errors) {
t.Errorf(
"GetMultiple() returned unexpected number of errors;\nwant=%v\ngot=%v\n",
tt.errs.Errors,
errs.Errors,
)
return
}
// compare each error returned with the expected errors.
for i, expectedErr := range tt.errs.Errors {
if errs.Errors[i].Error() != expectedErr.Error() {
t.Errorf(
"GetMultiple() got unexpected error;\nwant=%v\ngot=%v\n",
expectedErr,
errs.Errors[i].Error(),
)
return
}
}
} else if tt.errs != nil {
// an error was returned but it wasn't a multierror.
t.Errorf("GetMultiple() expected a multierror but got a regular error instead;\nwant=%v\ngot=%v\n", tt.errs, err)
return
}
if !reflect.DeepEqual(tt.want, got) {
t.Errorf(
"GetMultiple() returned unexpected configuration;\nwant=%+v\ngot=%+v\n",
tt.want,
got,
)
return
}
})
}
}