Skip to content

Commit ec1e94a

Browse files
authored
Merge pull request #3886 from Ankitasw/e2e-flake
[E2E] Fix AWS services creation flake in external infrastructure test
2 parents 36938d5 + 555fb03 commit ec1e94a

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

test/e2e/shared/aws.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -165,36 +165,27 @@ func (i *AWSInfrastructure) AllocateAddress() AWSInfrastructure {
165165
return *i
166166
}
167167

168-
t := 0
169-
addr, _ := GetAddress(i.Context, *aa.AllocationId)
170-
for addr == nil && t < 180 {
171-
time.Sleep(1 * time.Second)
168+
var addr *ec2.Address
169+
Eventually(func(gomega Gomega) {
172170
addr, _ = GetAddress(i.Context, *aa.AllocationId)
173-
t++
174-
}
171+
}, 2*time.Minute, 5*time.Second).Should(Succeed())
175172
i.ElasticIP = addr
176173
return *i
177174
}
178175

179176
func (i *AWSInfrastructure) CreateNatGateway(ct string) AWSInfrastructure {
180-
t := 0
181-
s, serr := GetSubnetByName(i.Context, i.Spec.ClusterName+"-subnet-"+ct)
182-
if serr != nil {
183-
return *i
184-
}
185-
for s == nil && t < 180 {
186-
time.Sleep(1 * time.Second)
177+
var s *ec2.Subnet
178+
Eventually(func(gomega Gomega) {
187179
s, _ = GetSubnetByName(i.Context, i.Spec.ClusterName+"-subnet-"+ct)
188-
t++
189-
}
180+
}, 2*time.Minute, 5*time.Second).Should(Succeed())
190181
if s == nil {
191182
return *i
192183
}
193184
ngwC, ngwce := CreateNatGateway(i.Context, i.Spec.ClusterName+"-nat", ct, *i.ElasticIP.AllocationId, *s.SubnetId)
194185
if ngwce != nil {
195186
return *i
196187
}
197-
if WaitForNatGatewayState(i.Context, *ngwC.NatGatewayId, 180, "available") {
188+
if WaitForNatGatewayState(i.Context, *ngwC.NatGatewayId, "available") {
198189
ngw, _ := GetNatGateway(i.Context, *ngwC.NatGatewayId)
199190
i.NatGateway = ngw
200191
i.State.NatGatewayState = ngw.State
@@ -238,6 +229,10 @@ func (i *AWSInfrastructure) GetRouteTable(rtID string) AWSInfrastructure {
238229
// routes to their respective gateway.
239230
func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
240231
i.CreateVPC()
232+
Eventually(func(gomega Gomega) bool {
233+
return *i.RefreshVPCState().State.VpcState == "available"
234+
}, 2*time.Minute, 5*time.Second).Should(BeTrue())
235+
241236
Byf("Created VPC - %s", *i.VPC.VpcId)
242237
if i.VPC != nil {
243238
i.CreatePublicSubnet()
@@ -248,12 +243,6 @@ func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
248243
if i.State.PrivateSubnetID != nil {
249244
Byf("Created Private Subnet - %s", *i.State.PrivateSubnetID)
250245
}
251-
for t := 0; t < 30; t++ {
252-
if *i.RefreshVPCState().State.VpcState == "available" {
253-
break
254-
}
255-
time.Sleep(1 * time.Second)
256-
}
257246
i.CreateInternetGateway()
258247
if i.InternetGateway != nil {
259248
Byf("Created Internet Gateway - %s", *i.InternetGateway.InternetGatewayId)
@@ -264,7 +253,7 @@ func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
264253
Byf("Created Elastic IP - %s", *i.ElasticIP.AllocationId)
265254
i.CreateNatGateway("public")
266255
if i.NatGateway != nil && i.NatGateway.NatGatewayId != nil {
267-
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, 180, "available")
256+
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, "available")
268257
Byf("Created NAT Gateway - %s", *i.NatGateway.NatGatewayId)
269258
}
270259
}
@@ -304,7 +293,7 @@ func (i *AWSInfrastructure) DeleteInfrastructure() {
304293
Byf("Deleting orphaned instance: %s - %v", *instance.InstanceId, TerminateInstance(i.Context, *instance.InstanceId))
305294
}
306295
}
307-
WaitForInstanceState(i.Context, i.Spec.ClusterName, 300, "terminated")
296+
WaitForInstanceState(i.Context, i.Spec.ClusterName, "terminated")
308297

309298
loadbalancers, _ := ListLoadBalancers(i.Context, i.Spec.ClusterName)
310299
for _, lb := range loadbalancers {
@@ -320,7 +309,7 @@ func (i *AWSInfrastructure) DeleteInfrastructure() {
320309

321310
if i.NatGateway != nil {
322311
Byf("Deleting NAT Gateway - %s - %v", *i.NatGateway.NatGatewayId, DeleteNatGateway(i.Context, *i.NatGateway.NatGatewayId))
323-
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, 180, "deleted")
312+
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, "deleted")
324313
}
325314

326315
if i.ElasticIP != nil {
@@ -1009,9 +998,8 @@ func ListClusterEC2Instances(e2eCtx *E2EContext, clusterName string) ([]*ec2.Ins
1009998
return instances, nil
1010999
}
10111000

1012-
func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, timeout int, state string) bool {
1013-
t := 0
1014-
for t < timeout {
1001+
func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, state string) bool {
1002+
Eventually(func(gomega Gomega) bool {
10151003
st := map[string]int{
10161004
"pending": 0,
10171005
"running": 0,
@@ -1026,9 +1014,9 @@ func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, timeout int, s
10261014
if st[state] == len(instances) || len(instances) == 0 {
10271015
return true
10281016
}
1029-
time.Sleep(1 * time.Second)
1030-
t++
1031-
}
1017+
return false
1018+
}, 5*time.Minute, 5*time.Second).Should(BeTrue())
1019+
10321020
return false
10331021
}
10341022

@@ -1416,17 +1404,12 @@ func DeleteNatGateway(e2eCtx *E2EContext, gatewayID string) bool {
14161404
return true
14171405
}
14181406

1419-
func WaitForNatGatewayState(e2eCtx *E2EContext, gatewayID string, timeout int, state string) bool {
1420-
t := 0
1421-
for t < timeout {
1407+
func WaitForNatGatewayState(e2eCtx *E2EContext, gatewayID string, state string) bool {
1408+
Eventually(func(gomega Gomega) bool {
14221409
gw, _ := GetNatGateway(e2eCtx, gatewayID)
14231410
gwState := *gw.State
1424-
if gwState == state {
1425-
return true
1426-
}
1427-
time.Sleep(1 * time.Second)
1428-
t++
1429-
}
1411+
return gwState == state
1412+
}, 3*time.Minute, 5*time.Second).Should(BeTrue())
14301413
return false
14311414
}
14321415

0 commit comments

Comments
 (0)