@@ -3,19 +3,17 @@ package test
33import (
44 "context"
55 "reflect"
6- "sync"
76 "time"
87
98 "github.com/aws/aws-application-networking-k8s/pkg/aws/services"
109 "github.com/aws/aws-application-networking-k8s/pkg/latticestore"
1110 "github.com/aws/aws-sdk-go/aws/session"
1211 "github.com/aws/aws-sdk-go/service/vpclattice"
1312 . "github.com/onsi/ginkgo/v2"
14- "github.com/onsi/gomega"
1513 . "github.com/onsi/gomega"
1614 "github.com/onsi/gomega/format"
17- "github.com/onsi/gomega/types"
1815 "github.com/samber/lo"
16+ "github.com/samber/lo/parallel"
1917 appsv1 "k8s.io/api/apps/v1"
2018 v1 "k8s.io/api/core/v1"
2119 "k8s.io/apimachinery/pkg/api/errors"
@@ -31,14 +29,13 @@ func init() {
3129 format .MaxLength = 0
3230}
3331
32+ type TestObject struct {
33+ Type client.Object
34+ ListType client.ObjectList
35+ }
36+
3437var (
35- CleanupTimeout = 300 * time .Second
36- CreationTimeout = 120 * time .Second
37- TestObjects = []struct {
38- Type client.Object
39- ListType client.ObjectList
40- }{
41- // Must currently be deleted in order to avoid https://github.com/aws/aws-application-networking-k8s/issues/115
38+ TestObjects = []TestObject {
4239 {& v1.Service {}, & v1.ServiceList {}},
4340 {& appsv1.Deployment {}, & appsv1.DeploymentList {}},
4441 {& v1beta1.HTTPRoute {}, & v1beta1.HTTPRouteList {}},
@@ -59,7 +56,8 @@ func NewFramework(ctx context.Context) *Framework {
5956 Client : lo .Must (client .New (controllerruntime .GetConfigOrDie (), client.Options {Scheme : scheme })),
6057 LatticeClient : services .NewDefaultLattice (session .Must (session .NewSession ()), "" ), // region is currently hardcoded
6158 }
62- gomega .Default .SetDefaultEventuallyPollingInterval (time .Second * 1 )
59+ SetDefaultEventuallyTimeout (180 * time .Second )
60+ SetDefaultEventuallyPollingInterval (1 * time .Second )
6361 BeforeEach (func () { framework .ExpectToBeClean (ctx ) })
6462 AfterSuite (func () { framework .ExpectToClean (ctx ) })
6563 return framework
@@ -68,35 +66,31 @@ func NewFramework(ctx context.Context) *Framework {
6866func (env * Framework ) ExpectToBeClean (ctx context.Context ) {
6967 Logger (ctx ).Info ("Expecting the test environment to be clean" )
7068 // Kubernetes API Objects
71- for _ , testObject := range TestObjects {
72- env .EventuallyExpectNoneFound (ctx , testObject .ListType ).WithOffset (1 ).Should (Succeed ())
73- }
69+ parallel .ForEach (TestObjects , func (testObject TestObject , _ int ) {
70+ defer GinkgoRecover ()
71+ env .EventuallyExpectNoneFound (ctx , testObject .ListType )
72+ })
73+
7474 // AWS API Objects
7575 Eventually (func (g Gomega ) {
7676 g .Expect (env .LatticeClient .ListServicesWithContext (ctx , & vpclattice.ListServicesInput {})).To (HaveField ("Items" , BeEmpty ()))
7777 g .Expect (env .LatticeClient .ListServiceNetworksWithContext (ctx , & vpclattice.ListServiceNetworksInput {})).To (HaveField ("Items" , BeEmpty ()))
7878 g .Expect (env .LatticeClient .ListTargetGroupsWithContext (ctx , & vpclattice.ListTargetGroupsInput {})).To (HaveField ("Items" , BeEmpty ()))
79- })
79+ }). Should ( Succeed ())
8080}
8181
8282func (env * Framework ) ExpectToClean (ctx context.Context ) {
8383 Logger (ctx ).Info ("Cleaning the test environment" )
84- wg := sync.WaitGroup {}
85- namespaces := & v1.NamespaceList {}
8684 // Kubernetes API Objects
85+ namespaces := & v1.NamespaceList {}
8786 Expect (env .List (ctx , namespaces )).WithOffset (1 ).To (Succeed ())
8887 for _ , namespace := range namespaces .Items {
89- for _ , object := range TestObjects {
90- wg .Add (1 )
91- go func (object client.Object , objectList client.ObjectList , namespace string ) {
92- defer wg .Done ()
93- defer GinkgoRecover ()
94- env .ExpectDeleteAllToSucceed (ctx , object , namespace )
95- env .EventuallyExpectNoneFound (ctx , objectList ).Should (Succeed ())
96- }(object .Type .DeepCopyObject ().(client.Object ), object .ListType .DeepCopyObject ().(client.ObjectList ), namespace .Name )
97- }
88+ parallel .ForEach (TestObjects , func (testObject TestObject , _ int ) {
89+ defer GinkgoRecover ()
90+ env .ExpectDeleteAllToSucceed (ctx , testObject .Type , namespace .Name )
91+ env .EventuallyExpectNoneFound (ctx , testObject .ListType )
92+ })
9893 }
99- wg .Wait ()
10094
10195 // AWS API Objects
10296 // Delete Services
@@ -167,19 +161,19 @@ func (env *Framework) ExpectDeleteAllToSucceed(ctx context.Context, object clien
167161 Expect (env .DeleteAllOf (ctx , object , client .InNamespace (namespace ), client .HasLabels ([]string {DiscoveryLabel }))).WithOffset (1 ).To (Succeed ())
168162}
169163
170- func (env * Framework ) EventuallyExpectNotFound (ctx context.Context , objects ... client.Object ) types. AsyncAssertion {
171- return Eventually (func (g Gomega ) {
164+ func (env * Framework ) EventuallyExpectNotFound (ctx context.Context , objects ... client.Object ) {
165+ Eventually (func (g Gomega ) {
172166 for _ , object := range objects {
173167 g .Expect (errors .IsNotFound (env .Get (ctx , client .ObjectKeyFromObject (object ), object ))).To (BeTrue ())
174168 }
175- }, CleanupTimeout )
169+ }). Should ( Succeed () )
176170}
177171
178- func (env * Framework ) EventuallyExpectNoneFound (ctx context.Context , objectList client.ObjectList ) types. AsyncAssertion {
179- return Eventually (func (g Gomega ) {
172+ func (env * Framework ) EventuallyExpectNoneFound (ctx context.Context , objectList client.ObjectList ) {
173+ Eventually (func (g Gomega ) {
180174 g .Expect (env .List (ctx , objectList , client .HasLabels ([]string {DiscoveryLabel }))).To (Succeed ())
181175 g .Expect (meta .ExtractList (objectList )).To (HaveLen (0 ), "Expected to not find any %q with label %q" , reflect .TypeOf (objectList ), DiscoveryLabel )
182- }, CleanupTimeout )
176+ }). WithOffset ( 1 ). Should ( Succeed () )
183177}
184178
185179func (env * Framework ) GetServiceNetwork (ctx context.Context , gateway * v1beta1.Gateway ) * vpclattice.ServiceNetworkSummary {
@@ -194,7 +188,7 @@ func (env *Framework) GetServiceNetwork(ctx context.Context, gateway *v1beta1.Ga
194188 }
195189 }
196190 g .Expect (found ).ToNot (BeNil ())
197- }, CreationTimeout ).WithOffset (1 ).Should (Succeed ())
191+ }).WithOffset (1 ).Should (Succeed ())
198192 return found
199193}
200194
@@ -211,7 +205,7 @@ func (env *Framework) GetService(ctx context.Context, httpRoute *v1beta1.HTTPRou
211205 }
212206 g .Expect (found ).ToNot (BeNil ())
213207 g .Expect (found .Status ).To (Equal (lo .ToPtr (vpclattice .ServiceStatusActive )))
214- }, CreationTimeout ).WithOffset (1 ).Should (Succeed ())
208+ }).WithOffset (1 ).Should (Succeed ())
215209
216210 return found
217211}
@@ -229,7 +223,7 @@ func (env *Framework) GetTargetGroup(ctx context.Context, service *v1.Service) *
229223 }
230224 g .Expect (found ).ToNot (BeNil ())
231225 g .Expect (found .Status ).To (Equal (lo .ToPtr (vpclattice .TargetGroupStatusActive )))
232- }, CreationTimeout ).WithOffset (1 ).Should (Succeed ())
226+ }).WithOffset (1 ).Should (Succeed ())
233227 return found
234228}
235229
@@ -245,10 +239,12 @@ func (env *Framework) GetTargets(ctx context.Context, targetGroup *vpclattice.Ta
245239 g .Expect (listTargetsOutput .Items ).To (HaveLen (int (* deployment .Spec .Replicas )))
246240
247241 podIps := lo .Map (podList .Items , func (pod v1.Pod , _ int ) string { return pod .Status .PodIP })
248- targetIps := lo .Filter (listTargetsOutput .Items , func (target * vpclattice.TargetSummary , _ int ) bool { return lo .Contains (podIps , * target .Id ) })
242+ targetIps := lo .Filter (listTargetsOutput .Items , func (target * vpclattice.TargetSummary , _ int ) bool {
243+ return * target .Status == vpclattice .TargetStatusInitial && lo .Contains (podIps , * target .Id )
244+ })
249245 g .Expect (targetIps ).To (HaveLen (int (* deployment .Spec .Replicas )))
250246
251247 found = listTargetsOutput .Items
252- }, CreationTimeout ).WithOffset (1 ).Should (Succeed ())
248+ }).WithOffset (1 ).Should (Succeed ())
253249 return found
254250}
0 commit comments