1
1
package repository
2
2
3
3
import (
4
- "github.com/snyk/driftctl/enumeration/remote/cache"
5
4
"strings"
6
5
"testing"
7
6
8
7
"github.com/aws/aws-sdk-go/aws"
9
8
"github.com/aws/aws-sdk-go/service/elbv2"
10
9
"github.com/pkg/errors"
11
- awstest "github.com/snyk/driftctl/test/aws"
12
- "github.com/stretchr/testify/mock"
13
-
14
10
"github.com/r3labs/diff/v2"
11
+ "github.com/snyk/driftctl/enumeration/remote/cache"
12
+ awstest "github.com/snyk/driftctl/test/aws"
15
13
"github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/mock"
16
15
)
17
16
18
17
func Test_ELBV2Repository_ListAllLoadBalancers (t * testing.T ) {
@@ -133,17 +132,53 @@ func Test_ELBV2Repository_ListAllLoadBalancers(t *testing.T) {
133
132
func Test_ELBV2Repository_ListAllLoadBalancerListeners (t * testing.T ) {
134
133
dummyError := errors .New ("dummy error" )
135
134
135
+ type call struct {
136
+ loadBalancerArn string
137
+ mocks func (* awstest.MockFakeELBV2 , * cache.MockCache )
138
+ want []* elbv2.Listener
139
+ wantErr error
140
+ }
141
+
136
142
tests := []struct {
137
- name string
138
- mocks func (* awstest.MockFakeELBV2 , * cache.MockCache )
139
- want []* elbv2.Listener
140
- wantErr error
143
+ name string
144
+ calls []call
141
145
}{
142
146
{
143
147
name : "list load balancer listeners" ,
144
- mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
145
- results := & elbv2.DescribeListenersOutput {
146
- Listeners : []* elbv2.Listener {
148
+ calls : []call {
149
+ {
150
+ loadBalancerArn : "test-lb" ,
151
+ mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
152
+ results := & elbv2.DescribeListenersOutput {
153
+ Listeners : []* elbv2.Listener {
154
+ {
155
+ LoadBalancerArn : aws .String ("test-lb" ),
156
+ ListenerArn : aws .String ("test-lb-listener-1" ),
157
+ },
158
+ {
159
+ LoadBalancerArn : aws .String ("test-lb" ),
160
+ ListenerArn : aws .String ("test-lb-listener-2" ),
161
+ },
162
+ },
163
+ }
164
+
165
+ store .On ("Get" , "elbv2ListAllLoadBalancerListeners_test-lb" ).Return (nil ).Once ()
166
+
167
+ client .On ("DescribeListenersPages" ,
168
+ & elbv2.DescribeListenersInput {LoadBalancerArn : aws .String ("test-lb" )},
169
+ mock .MatchedBy (func (callback func (res * elbv2.DescribeListenersOutput , lastPage bool ) bool ) bool {
170
+ callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {
171
+ results .Listeners [0 ],
172
+ }}, false )
173
+ callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {
174
+ results .Listeners [1 ],
175
+ }}, true )
176
+ return true
177
+ })).Return (nil ).Once ()
178
+
179
+ store .On ("Put" , "elbv2ListAllLoadBalancerListeners_test-lb" , results .Listeners ).Return (false ).Once ()
180
+ },
181
+ want : []* elbv2.Listener {
147
182
{
148
183
LoadBalancerArn : aws .String ("test-lb" ),
149
184
ListenerArn : aws .String ("test-lb-listener-1" ),
@@ -153,90 +188,124 @@ func Test_ELBV2Repository_ListAllLoadBalancerListeners(t *testing.T) {
153
188
ListenerArn : aws .String ("test-lb-listener-2" ),
154
189
},
155
190
},
156
- }
157
-
158
- store .On ("Get" , "elbv2ListAllLoadBalancerListeners" ).Return (nil ).Once ()
159
-
160
- client .On ("DescribeListenersPages" ,
161
- & elbv2.DescribeListenersInput {LoadBalancerArn : aws .String ("test-lb" )},
162
- mock .MatchedBy (func (callback func (res * elbv2.DescribeListenersOutput , lastPage bool ) bool ) bool {
163
- callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {
164
- results .Listeners [0 ],
165
- }}, false )
166
- callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {
167
- results .Listeners [1 ],
168
- }}, true )
169
- return true
170
- })).Return (nil ).Once ()
171
-
172
- store .On ("Put" , "elbv2ListAllLoadBalancerListeners" , results .Listeners ).Return (false ).Once ()
173
- },
174
- want : []* elbv2.Listener {
175
- {
176
- LoadBalancerArn : aws .String ("test-lb" ),
177
- ListenerArn : aws .String ("test-lb-listener-1" ),
178
- },
179
- {
180
- LoadBalancerArn : aws .String ("test-lb" ),
181
- ListenerArn : aws .String ("test-lb-listener-2" ),
182
191
},
183
192
},
184
193
},
185
194
{
186
195
name : "list load balancer listeners from cache" ,
187
- mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
188
- output := & elbv2.DescribeListenersOutput {
189
- Listeners : []* elbv2.Listener {
196
+ calls : []call {
197
+ {
198
+ loadBalancerArn : "test-lb" ,
199
+ mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
200
+ output := & elbv2.DescribeListenersOutput {
201
+ Listeners : []* elbv2.Listener {
202
+ {
203
+ LoadBalancerArn : aws .String ("test-lb" ),
204
+ ListenerArn : aws .String ("test-lb-listener" ),
205
+ },
206
+ },
207
+ }
208
+
209
+ store .On ("Get" , "elbv2ListAllLoadBalancerListeners_test-lb" ).Return (output .Listeners ).Once ()
210
+ },
211
+ want : []* elbv2.Listener {
190
212
{
191
213
LoadBalancerArn : aws .String ("test-lb" ),
192
214
ListenerArn : aws .String ("test-lb-listener" ),
193
215
},
194
216
},
195
- }
196
-
197
- store .On ("Get" , "elbv2ListAllLoadBalancerListeners" ).Return (output .Listeners ).Once ()
217
+ },
198
218
},
199
- want : []* elbv2.Listener {
219
+ },
220
+ {
221
+ name : "list load balancer listeners from multiple load balancers" ,
222
+ calls : []call {
223
+ {
224
+ loadBalancerArn : "test-lb-1" ,
225
+ mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
226
+ output := & elbv2.DescribeListenersOutput {
227
+ Listeners : []* elbv2.Listener {
228
+ {
229
+ LoadBalancerArn : aws .String ("test-lb-1" ),
230
+ ListenerArn : aws .String ("test-lb-1-listener" ),
231
+ },
232
+ },
233
+ }
234
+
235
+ store .On ("Get" , "elbv2ListAllLoadBalancerListeners_test-lb-1" ).Return (output .Listeners ).Once ()
236
+ },
237
+ want : []* elbv2.Listener {
238
+ {
239
+ LoadBalancerArn : aws .String ("test-lb-1" ),
240
+ ListenerArn : aws .String ("test-lb-1-listener" ),
241
+ },
242
+ },
243
+ },
200
244
{
201
- LoadBalancerArn : aws .String ("test-lb" ),
202
- ListenerArn : aws .String ("test-lb-listener" ),
245
+ loadBalancerArn : "test-lb-2" ,
246
+ mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
247
+ output := & elbv2.DescribeListenersOutput {
248
+ Listeners : []* elbv2.Listener {
249
+ {
250
+ LoadBalancerArn : aws .String ("test-lb-2" ),
251
+ ListenerArn : aws .String ("test-lb-2-listener" ),
252
+ },
253
+ },
254
+ }
255
+
256
+ store .On ("Get" , "elbv2ListAllLoadBalancerListeners_test-lb-2" ).Return (output .Listeners ).Once ()
257
+ },
258
+ want : []* elbv2.Listener {
259
+ {
260
+ LoadBalancerArn : aws .String ("test-lb-2" ),
261
+ ListenerArn : aws .String ("test-lb-2-listener" ),
262
+ },
263
+ },
203
264
},
204
265
},
205
266
},
206
267
{
207
268
name : "error listing load balancer listeners" ,
208
- mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
209
- store .On ("Get" , "elbv2ListAllLoadBalancerListeners" ).Return (nil ).Once ()
269
+ calls : []call {
270
+ {
271
+ loadBalancerArn : "test-lb" ,
272
+ mocks : func (client * awstest.MockFakeELBV2 , store * cache.MockCache ) {
273
+ store .On ("Get" , "elbv2ListAllLoadBalancerListeners_test-lb" ).Return (nil ).Once ()
210
274
211
- client .On ("DescribeListenersPages" ,
212
- & elbv2.DescribeListenersInput {LoadBalancerArn : aws .String ("test-lb" )},
213
- mock .MatchedBy (func (callback func (res * elbv2.DescribeListenersOutput , lastPage bool ) bool ) bool {
214
- callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {}}, true )
215
- return true
216
- })).Return (dummyError ).Once ()
275
+ client .On ("DescribeListenersPages" ,
276
+ & elbv2.DescribeListenersInput {LoadBalancerArn : aws .String ("test-lb" )},
277
+ mock .MatchedBy (func (callback func (res * elbv2.DescribeListenersOutput , lastPage bool ) bool ) bool {
278
+ callback (& elbv2.DescribeListenersOutput {Listeners : []* elbv2.Listener {}}, true )
279
+ return true
280
+ })).Return (dummyError ).Once ()
281
+ },
282
+ wantErr : dummyError ,
283
+ },
217
284
},
218
- wantErr : dummyError ,
219
285
},
220
286
}
221
287
for _ , tt := range tests {
222
288
t .Run (tt .name , func (t * testing.T ) {
223
289
store := & cache.MockCache {}
224
290
client := & awstest.MockFakeELBV2 {}
225
- tt .mocks (client , store )
226
- r := & elbv2Repository {
227
- client : client ,
228
- cache : store ,
229
- }
230
- got , err := r .ListAllLoadBalancerListeners ("test-lb" )
231
- assert .Equal (t , tt .wantErr , err )
232
291
233
- changelog , err := diff .Diff (got , tt .want )
234
- assert .Nil (t , err )
235
- if len (changelog ) > 0 {
236
- for _ , change := range changelog {
237
- t .Errorf ("%s: %v -> %v" , strings .Join (change .Path , "." ), change .From , change .To )
292
+ for _ , call := range tt .calls {
293
+ call .mocks (client , store )
294
+ r := & elbv2Repository {
295
+ client : client ,
296
+ cache : store ,
297
+ }
298
+ got , err := r .ListAllLoadBalancerListeners (call .loadBalancerArn )
299
+ assert .Equal (t , call .wantErr , err )
300
+
301
+ changelog , err := diff .Diff (got , call .want )
302
+ assert .Nil (t , err )
303
+ if len (changelog ) > 0 {
304
+ for _ , change := range changelog {
305
+ t .Errorf ("%s: %v -> %v" , strings .Join (change .Path , "." ), change .From , change .To )
306
+ }
307
+ t .Fail ()
238
308
}
239
- t .Fail ()
240
309
}
241
310
})
242
311
}
0 commit comments