@@ -8,17 +8,23 @@ import (
8
8
"testing"
9
9
10
10
"github.com/aws/aws-sdk-go-v2/service/ec2"
11
+ elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
12
+ elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
13
+
11
14
"github.com/stretchr/testify/assert"
12
15
"k8s.io/cloud-provider-aws/pkg/providers/v1/config"
13
16
)
14
17
15
18
// Given an override, a custom endpoint should be used when making API requests
16
- func TestComputeEndpointOverride (t * testing.T ) {
19
+ func TestClientsEndpointOverride (t * testing.T ) {
17
20
usedCustomEndpoint := false
21
+ // Dummy server that sets usedCustomEndpoint when called
18
22
testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
19
23
usedCustomEndpoint = true
20
24
}))
21
25
26
+ // Pass in the test server URL through a CloudConfig, which is used by each client's custom endpoint
27
+ // resolver to override the URL for a request (see EC2Resolver.ResolveEndpoint for an example)
22
28
cfgWithServiceOverride := config.CloudConfig {
23
29
ServiceOverride : map [string ]* struct {
24
30
Service string
@@ -29,7 +35,21 @@ func TestComputeEndpointOverride(t *testing.T) {
29
35
SigningName string
30
36
}{
31
37
"1" : {
32
- Service : "EC2" ,
38
+ Service : ec2 .ServiceID ,
39
+ Region : "us-west-2" ,
40
+ URL : testServer .URL ,
41
+ SigningRegion : "signingRegion" ,
42
+ SigningName : "signingName" ,
43
+ },
44
+ "2" : {
45
+ Service : elb .ServiceID ,
46
+ Region : "us-west-2" ,
47
+ URL : testServer .URL ,
48
+ SigningRegion : "signingRegion" ,
49
+ SigningName : "signingName" ,
50
+ },
51
+ "3" : {
52
+ Service : elbv2 .ServiceID ,
33
53
Region : "us-west-2" ,
34
54
URL : testServer .URL ,
35
55
SigningRegion : "signingRegion" ,
@@ -42,17 +62,35 @@ func TestComputeEndpointOverride(t *testing.T) {
42
62
regionDelayers : make (map [string ]* CrossRequestRetryDelay ),
43
63
}
44
64
65
+ // EC2 Client
45
66
ec2Client , err := mockProvider .Compute (context .TODO (), "us-west-2" , nil )
46
67
if err != nil {
47
68
t .Errorf ("error creating client, %v" , err )
48
69
}
49
70
_ , err = ec2Client .DescribeInstances (context .TODO (), & ec2.DescribeInstancesInput {})
50
71
assert .True (t , usedCustomEndpoint == true , "custom endpoint was not used for EC2 Client" )
72
+
73
+ usedCustomEndpoint = false // reset boolean flag for next request
74
+ elbClient , err := mockProvider .LoadBalancing (context .TODO (), "us-west-2" , nil )
75
+ if err != nil {
76
+ t .Errorf ("error creating client, %v" , err )
77
+ }
78
+ _ , err = elbClient .DescribeLoadBalancers (context .TODO (), & elb.DescribeLoadBalancersInput {})
79
+ assert .True (t , usedCustomEndpoint == true , "custom endpoint was not used for ELB Client" )
80
+
81
+ usedCustomEndpoint = false
82
+ elbv2Client , err := mockProvider .LoadBalancingV2 (context .TODO (), "us-west-2" , nil )
83
+ if err != nil {
84
+ t .Errorf ("error creating client, %v" , err )
85
+ }
86
+ _ , err = elbv2Client .DescribeLoadBalancers (context .TODO (), & elbv2.DescribeLoadBalancersInput {})
87
+ assert .True (t , usedCustomEndpoint == true , "custom endpoint was not used for ELBV2 Client" )
51
88
}
52
89
53
- // When a nonRetryableError is thrown, an API request should not be retried
54
- func TestComputeNoRetry (t * testing.T ) {
90
+ // Test whether SDK clients refrain from retrying an API request when given a nonRetryableError.
91
+ func TestClientsNoRetry (t * testing.T ) {
55
92
attemptCount := 0
93
+ // Dummy server that counts attempts and returns a nonRetryableError
56
94
testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
57
95
attemptCount ++
58
96
w .Header ().Set ("Content-Type" , "text/xml" )
@@ -74,6 +112,7 @@ func TestComputeNoRetry(t *testing.T) {
74
112
}))
75
113
defer testServer .Close ()
76
114
115
+ // Override service endpoints with dummy server URL
77
116
cfgWithServiceOverride := config.CloudConfig {
78
117
ServiceOverride : map [string ]* struct {
79
118
Service string
@@ -84,7 +123,21 @@ func TestComputeNoRetry(t *testing.T) {
84
123
SigningName string
85
124
}{
86
125
"1" : {
87
- Service : "EC2" ,
126
+ Service : ec2 .ServiceID ,
127
+ Region : "us-west-2" ,
128
+ URL : testServer .URL ,
129
+ SigningRegion : "signingRegion" ,
130
+ SigningName : "signingName" ,
131
+ },
132
+ "2" : {
133
+ Service : elb .ServiceID ,
134
+ Region : "us-west-2" ,
135
+ URL : testServer .URL ,
136
+ SigningRegion : "signingRegion" ,
137
+ SigningName : "signingName" ,
138
+ },
139
+ "3" : {
140
+ Service : elbv2 .ServiceID ,
88
141
Region : "us-west-2" ,
89
142
URL : testServer .URL ,
90
143
SigningRegion : "signingRegion" ,
@@ -97,23 +150,45 @@ func TestComputeNoRetry(t *testing.T) {
97
150
regionDelayers : make (map [string ]* CrossRequestRetryDelay ),
98
151
}
99
152
153
+ // EC2 Client
100
154
ec2Client , err := mockProvider .Compute (context .TODO (), "us-west-2" , nil )
101
155
if err != nil {
102
156
t .Errorf ("error creating client, %v" , err )
103
157
}
104
158
_ , err = ec2Client .DescribeInstances (context .TODO (), & ec2.DescribeInstancesInput {})
105
- assert .True (t , attemptCount == 1 , fmt .Sprintf ("expected an attempt count of 1, got %d" , attemptCount ))
159
+ // Ensure that only 1 attempt was made, signifying no retries
160
+ assert .True (t , attemptCount == 1 , fmt .Sprintf ("expected an attempt count of 1 for EC2 client, got %d" , attemptCount ))
161
+
162
+ // ELB Client
163
+ attemptCount = 0 // reset attempt count for next request
164
+ elbClient , err := mockProvider .LoadBalancing (context .TODO (), "us-west-2" , nil )
165
+ if err != nil {
166
+ t .Errorf ("error creating client, %v" , err )
167
+ }
168
+ _ , err = elbClient .DescribeLoadBalancers (context .TODO (), & elb.DescribeLoadBalancersInput {})
169
+ assert .True (t , attemptCount == 1 , fmt .Sprintf ("expected an attempt count of 1 for ELB client, got %d" , attemptCount ))
170
+
171
+ // ELBV2 Client
172
+ attemptCount = 0
173
+ elbv2Client , err := mockProvider .LoadBalancingV2 (context .TODO (), "us-west-2" , nil )
174
+ if err != nil {
175
+ t .Errorf ("error creating client, %v" , err )
176
+ }
177
+ _ , err = elbv2Client .DescribeLoadBalancers (context .TODO (), & elbv2.DescribeLoadBalancersInput {})
178
+ assert .True (t , attemptCount == 1 , fmt .Sprintf ("expected an attempt count of 1 for ELBV2 client, got %d" , attemptCount ))
106
179
}
107
180
108
- // When a retryable error is thrown, an API request should be retried
109
- func TestComputeWithRetry (t * testing.T ) {
181
+ // Test whether SDK clients retry an API request when given a retryable error code.
182
+ func TestClientsWithRetry (t * testing.T ) {
110
183
attemptCount := 0
184
+ // Dummy server that counts attempts and returns a retryable error
111
185
testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
112
186
attemptCount ++
113
187
// 500 status codes are retried by SDK (see https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws/retry)
114
188
http .Error (w , "RequestTimeout" , 500 )
115
189
}))
116
190
191
+ // Override service endpoints with dummy server URL
117
192
cfgWithServiceOverride := config.CloudConfig {
118
193
ServiceOverride : map [string ]* struct {
119
194
Service string
@@ -124,7 +199,21 @@ func TestComputeWithRetry(t *testing.T) {
124
199
SigningName string
125
200
}{
126
201
"1" : {
127
- Service : "EC2" ,
202
+ Service : ec2 .ServiceID ,
203
+ Region : "us-west-2" ,
204
+ URL : testServer .URL ,
205
+ SigningRegion : "signingRegion" ,
206
+ SigningName : "signingName" ,
207
+ },
208
+ "2" : {
209
+ Service : elb .ServiceID ,
210
+ Region : "us-west-2" ,
211
+ URL : testServer .URL ,
212
+ SigningRegion : "signingRegion" ,
213
+ SigningName : "signingName" ,
214
+ },
215
+ "3" : {
216
+ Service : elbv2 .ServiceID ,
128
217
Region : "us-west-2" ,
129
218
URL : testServer .URL ,
130
219
SigningRegion : "signingRegion" ,
@@ -137,10 +226,30 @@ func TestComputeWithRetry(t *testing.T) {
137
226
regionDelayers : make (map [string ]* CrossRequestRetryDelay ),
138
227
}
139
228
229
+ // EC2 Client
140
230
ec2Client , err := mockProvider .Compute (context .TODO (), "us-west-2" , nil )
141
231
if err != nil {
142
232
t .Errorf ("error creating client, %v" , err )
143
233
}
144
234
_ , err = ec2Client .DescribeInstances (context .TODO (), & ec2.DescribeInstancesInput {})
145
- assert .True (t , attemptCount > 1 , fmt .Sprintf ("expected an attempt count >1, got %d" , attemptCount ))
235
+ // Ensure that more than 1 attempt was made, signifying retries
236
+ assert .True (t , attemptCount > 1 , fmt .Sprintf ("expected an attempt count of >1 for EC2 client, got %d" , attemptCount ))
237
+
238
+ // ELB Client
239
+ attemptCount = 0 // Reset the attempt count before the next request
240
+ elbClient , err := mockProvider .LoadBalancing (context .TODO (), "us-west-2" , nil )
241
+ if err != nil {
242
+ t .Errorf ("error creating client, %v" , err )
243
+ }
244
+ _ , err = elbClient .DescribeLoadBalancers (context .TODO (), & elb.DescribeLoadBalancersInput {})
245
+ assert .True (t , attemptCount > 1 , fmt .Sprintf ("expected an attempt count of >1 for ELB client, got %d" , attemptCount ))
246
+
247
+ // ELBV2 Client
248
+ attemptCount = 0
249
+ elbv2Client , err := mockProvider .LoadBalancingV2 (context .TODO (), "us-west-2" , nil )
250
+ if err != nil {
251
+ t .Errorf ("error creating client, %v" , err )
252
+ }
253
+ _ , err = elbv2Client .DescribeLoadBalancers (context .TODO (), & elbv2.DescribeLoadBalancersInput {})
254
+ assert .True (t , attemptCount > 1 , fmt .Sprintf ("expected an attempt count of >1 for ELB client, got %d" , attemptCount ))
146
255
}
0 commit comments