Skip to content

Commit f6081d1

Browse files
authored
test: Minor improvements to test framework (#134)
1 parent e5d1786 commit f6081d1

File tree

6 files changed

+67
-60
lines changed

6 files changed

+67
-60
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ build-deploy: ## Create a deployment file that can be applied with `kubectl appl
9090
## Run e2e tests against cluster pointed to by ~/.kube/config
9191
.PHONY: e2etest
9292
e2etest:
93-
cd test && go test -v ./... -count=1 --ginkgo.v
93+
cd test && go test \
94+
-p 1 \
95+
-count 1 \
96+
-timeout 60m \
97+
-v \
98+
./suites/... \
99+
--ginkgo.focus="${FOCUS}" \
100+
--ginkgo.timeout=60m \
101+
--ginkgo.v

test/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/aws/aws-sdk-go v1.42.18
1414
github.com/imdario/mergo v0.3.13
1515
github.com/onsi/ginkgo/v2 v2.9.1
16-
github.com/onsi/gomega v1.27.3
16+
github.com/onsi/gomega v1.27.4
1717
github.com/samber/lo v1.37.0
1818
go.uber.org/zap v1.24.0
1919
k8s.io/api v0.26.2

test/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ
564564
github.com/onsi/gomega v1.23.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
565565
github.com/onsi/gomega v1.24.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
566566
github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM=
567-
github.com/onsi/gomega v1.27.3 h1:5VwIwnBY3vbBDOJrNtA4rVdiTZCsq9B5F12pvy1Drmk=
568-
github.com/onsi/gomega v1.27.3/go.mod h1:5vG284IBtfDAmDyrK+eGyZmUgUlmi+Wngqo557cZ6Gw=
567+
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
568+
github.com/onsi/gomega v1.27.4/go.mod h1:riYq/GJKh8hhoM01HN6Vmuy93AarCXCBGpvFDK3q3fQ=
569569
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
570570
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
571571
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=

test/pkg/test/framework.go

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ package test
33
import (
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+
3437
var (
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 {
6866
func (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

8282
func (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

185179
func (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
}

test/suites/integration/httproute_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
77
"github.com/aws/aws-application-networking-k8s/test/pkg/test"
8+
"github.com/aws/aws-sdk-go/service/vpclattice"
89
. "github.com/onsi/ginkgo/v2"
910
. "github.com/onsi/gomega"
1011
"github.com/samber/lo"
@@ -66,7 +67,7 @@ var _ = Describe("HTTPRoute", func() {
6667
})
6768

6869
// Create Kubernetes API Objects
69-
env.ExpectCreated(ctx,
70+
framework.ExpectCreated(ctx,
7071
test.Gateway,
7172
httpRoute,
7273
serviceV1,
@@ -78,41 +79,43 @@ var _ = Describe("HTTPRoute", func() {
7879
// Verify AWS API Objects
7980

8081
// (reverse order to allow dependency propagation)
81-
targetGroupV1 := env.GetTargetGroup(ctx, serviceV1)
82-
targetsV1 := env.GetTargets(ctx, targetGroupV1, deploymentV1)
82+
targetGroupV1 := framework.GetTargetGroup(ctx, serviceV1)
83+
targetsV1 := framework.GetTargets(ctx, targetGroupV1, deploymentV1)
8384
Expect(*targetGroupV1.VpcIdentifier).To(Equal(os.Getenv("CLUSTER_VPC_ID")))
8485
Expect(*targetGroupV1.Protocol).To(Equal("HTTP")) // TODO(liwenwu) should this be TCP?
8586
Expect(*targetGroupV1.Port).To(BeEquivalentTo(serviceV1.Spec.Ports[0].TargetPort.IntVal)) // TODO(liwenwu) should this be .Spec.Port[0].Port?
8687
for _, target := range targetsV1 {
8788
Expect(*target.Port).To(BeEquivalentTo(serviceV1.Spec.Ports[0].TargetPort.IntVal))
88-
Expect(*target.Status).To(BeEquivalentTo("UNUSED")) // TODO(liwenwu) should this be ACTIVE?
89+
Expect(*target.Status).To(Equal(vpclattice.TargetStatusInitial)) // TODO(liwenwu) should this be HEALTHY?
8990
}
9091

91-
targetGroupV2 := env.GetTargetGroup(ctx, serviceV2)
92-
targetsV2 := env.GetTargets(ctx, targetGroupV2, deploymentV2)
92+
targetGroupV2 := framework.GetTargetGroup(ctx, serviceV2)
93+
targetsV2 := framework.GetTargets(ctx, targetGroupV2, deploymentV2)
9394
Expect(*targetGroupV2.VpcIdentifier).To(Equal(os.Getenv("CLUSTER_VPC_ID")))
9495
Expect(*targetGroupV2.Protocol).To(Equal("HTTP")) // TODO(liwenwu) should this be TCP?
9596
Expect(*targetGroupV2.Port).To(BeEquivalentTo(serviceV2.Spec.Ports[0].TargetPort.IntVal)) // TODO(liwenwu) should this be .Spec.Port[0].Port?
9697
for _, target := range targetsV2 {
9798
Expect(*target.Port).To(BeEquivalentTo(serviceV2.Spec.Ports[0].TargetPort.IntVal))
98-
Expect(*target.Status).To(BeEquivalentTo("UNUSED")) // TODO(liwenwu) should this be ACTIVE?
99+
Expect(*target.Status).To(Equal(vpclattice.TargetStatusInitial)) // TODO(liwenwu) should this be HEALTHY?
99100
}
100101

101-
service := env.GetService(ctx, httpRoute)
102+
service := framework.GetService(ctx, httpRoute)
102103
Expect(*service.DnsEntry).To(ContainSubstring(latticestore.AWSServiceName(httpRoute.Name, httpRoute.Namespace))) // TODO(liwenwu) is there something else we should verify about service?
103104

104-
serviceNetwork := env.GetServiceNetwork(ctx, test.Gateway)
105+
serviceNetwork := framework.GetServiceNetwork(ctx, test.Gateway)
105106
Expect(*serviceNetwork.NumberOfAssociatedServices).To(BeEquivalentTo(1))
106107
Expect(*serviceNetwork.NumberOfAssociatedVPCs).To(BeEquivalentTo(0)) // TODO(liwenwu) should this be 1?
107108

108109
// Cleanup Kubernetes API Objects
109-
Expect(env.Delete(ctx, test.Gateway)).To(Succeed())
110-
Expect(env.Delete(ctx, httpRoute)).To(Succeed())
111-
Expect(env.Delete(ctx, serviceV1)).To(Succeed())
112-
Expect(env.Delete(ctx, deploymentV1)).To(Succeed())
113-
Expect(env.Delete(ctx, serviceV2)).To(Succeed())
114-
Expect(env.Delete(ctx, deploymentV2)).To(Succeed())
115-
env.ExpectToBeClean(ctx)
110+
framework.ExpectDeleted(ctx,
111+
test.Gateway,
112+
httpRoute,
113+
serviceV1,
114+
deploymentV1,
115+
serviceV2,
116+
deploymentV2,
117+
)
118+
framework.ExpectToBeClean(ctx)
116119
})
117120
})
118121
})

test/suites/integration/suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
. "github.com/onsi/gomega"
1010
)
1111

12-
var env *test.Framework
12+
var framework *test.Framework
1313
var ctx context.Context
1414

1515
func TestIntegration(t *testing.T) {
1616
ctx = test.NewContext(t)
17-
env = test.NewFramework(ctx)
17+
framework = test.NewFramework(ctx)
1818
RegisterFailHandler(Fail)
1919
RunSpecs(t, "Integration")
2020
}

0 commit comments

Comments
 (0)