@@ -2,18 +2,19 @@ package shield
2
2
3
3
import (
4
4
"context"
5
- shieldtypes "github.com/aws/aws-sdk-go-v2/service/shield/types"
6
5
"testing"
7
6
"time"
8
7
9
- shieldsdk "github.com/aws/aws-sdk-go-v2/service/shield"
8
+ shieldtypes "github.com/aws/aws-sdk-go-v2/service/shield/types"
9
+ "github.com/aws/aws-sdk-go-v2/aws"
10
10
"github.com/go-logr/logr"
11
11
"github.com/golang/mock/gomock"
12
12
"github.com/pkg/errors"
13
13
"github.com/stretchr/testify/assert"
14
14
"k8s.io/apimachinery/pkg/util/cache"
15
15
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
16
16
"sigs.k8s.io/controller-runtime/pkg/log"
17
+ shieldsdk "github.com/aws/aws-sdk-go-v2/service/shield"
17
18
)
18
19
19
20
func Test_defaultProtectionManager_IsSubscribed (t * testing.T ) {
@@ -169,3 +170,129 @@ func Test_defaultProtectionManager_IsSubscribed(t *testing.T) {
169
170
})
170
171
}
171
172
}
173
+
174
+ func Test_defaultProtectionManager_DeleteProtection (t * testing.T ) {
175
+ type deleteProtectionCall struct {
176
+ req * shieldsdk.DeleteProtectionInput
177
+ resp * shieldsdk.DeleteProtectionOutput
178
+ err error
179
+ }
180
+ type describeProtectionCall struct {
181
+ req * shieldsdk.DescribeProtectionInput
182
+ resp * shieldsdk.DescribeProtectionOutput
183
+ err error
184
+ }
185
+ type fields struct {
186
+ deleteProtectionCalls []deleteProtectionCall
187
+ describeProtectionCalls []describeProtectionCall
188
+ protectionInfoByResourceARNCacheTTL time.Duration
189
+ }
190
+ type testCase struct {
191
+ resourceARN string
192
+ protectionID string
193
+ wantErr error
194
+ }
195
+ tests := []struct {
196
+ name string
197
+ fields fields
198
+ testCases []testCase
199
+ }{
200
+ {
201
+ name : "delete protection successfully" ,
202
+ fields : fields {
203
+ deleteProtectionCalls : []deleteProtectionCall {
204
+ {
205
+ req : & shieldsdk.DeleteProtectionInput {ProtectionId : aws .String ("protection-id" )},
206
+ resp : & shieldsdk.DeleteProtectionOutput {},
207
+ },
208
+ },
209
+ describeProtectionCalls : []describeProtectionCall {
210
+ {
211
+ req : & shieldsdk.DescribeProtectionInput {ProtectionId : aws .String ("protection-id" )},
212
+ err : & shieldtypes.ResourceNotFoundException {},
213
+ },
214
+ },
215
+ protectionInfoByResourceARNCacheTTL : 10 * time .Minute ,
216
+ },
217
+ testCases : []testCase {
218
+ {
219
+ resourceARN : "resource-arn" ,
220
+ protectionID : "protection-id" ,
221
+ },
222
+ },
223
+ },
224
+ {
225
+ name : "delete protection fails" ,
226
+ fields : fields {
227
+ deleteProtectionCalls : []deleteProtectionCall {
228
+ {
229
+ req : & shieldsdk.DeleteProtectionInput {ProtectionId : aws .String ("protection-id" )},
230
+ err : errors .New ("some aws api error" ),
231
+ },
232
+ },
233
+ protectionInfoByResourceARNCacheTTL : 10 * time .Minute ,
234
+ },
235
+ testCases : []testCase {
236
+ {
237
+ resourceARN : "resource-arn" ,
238
+ protectionID : "protection-id" ,
239
+ wantErr : errors .New ("some aws api error" ),
240
+ },
241
+ },
242
+ },
243
+ {
244
+ name : "protection still exists after deletion" ,
245
+ fields : fields {
246
+ deleteProtectionCalls : []deleteProtectionCall {
247
+ {
248
+ req : & shieldsdk.DeleteProtectionInput {ProtectionId : aws .String ("protection-id" )},
249
+ resp : & shieldsdk.DeleteProtectionOutput {},
250
+ },
251
+ },
252
+ describeProtectionCalls : []describeProtectionCall {
253
+ {
254
+ req : & shieldsdk.DescribeProtectionInput {ProtectionId : aws .String ("protection-id" )},
255
+ resp : & shieldsdk.DescribeProtectionOutput {Protection : & shieldtypes.Protection {}},
256
+ },
257
+ },
258
+ protectionInfoByResourceARNCacheTTL : 10 * time .Minute ,
259
+ },
260
+ testCases : []testCase {
261
+ {
262
+ resourceARN : "resource-arn" ,
263
+ protectionID : "protection-id" ,
264
+ wantErr : errors .New ("protection resource still exists" ),
265
+ },
266
+ },
267
+ },
268
+ }
269
+ for _ , tt := range tests {
270
+ t .Run (tt .name , func (t * testing.T ) {
271
+ ctrl := gomock .NewController (t )
272
+ defer ctrl .Finish ()
273
+
274
+ shieldClient := services .NewMockShield (ctrl )
275
+ for _ , call := range tt .fields .deleteProtectionCalls {
276
+ shieldClient .EXPECT ().DeleteProtectionWithContext (gomock .Any (), call .req ).Return (call .resp , call .err )
277
+ }
278
+ for _ , call := range tt .fields .describeProtectionCalls {
279
+ shieldClient .EXPECT ().DescribeProtectionWithContext (gomock .Any (), call .req ).Return (call .resp , call .err )
280
+ }
281
+
282
+ m := & defaultProtectionManager {
283
+ shieldClient : shieldClient ,
284
+ logger : logr .New (& log.NullLogSink {}),
285
+ protectionInfoByResourceARNCache : cache .NewExpiring (),
286
+ protectionInfoByResourceARNCacheTTL : tt .fields .protectionInfoByResourceARNCacheTTL ,
287
+ }
288
+ for _ , testCase := range tt .testCases {
289
+ err := m .DeleteProtection (context .Background (), testCase .resourceARN , testCase .protectionID )
290
+ if testCase .wantErr != nil {
291
+ assert .EqualError (t , err , testCase .wantErr .Error ())
292
+ } else {
293
+ assert .NoError (t , err )
294
+ }
295
+ }
296
+ })
297
+ }
298
+ }
0 commit comments