Skip to content

Commit ad00db0

Browse files
authored
test: add e2e tests for Target Group Policy CRD (#434)
1 parent a2d5b97 commit ad00db0

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

test/pkg/test/framework.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ func (env *Framework) GetTargetGroupWithProtocol(ctx context.Context, service *v
322322
return found
323323
}
324324

325+
func (env *Framework) GetFullTargetGroupFromSummary(
326+
ctx context.Context,
327+
tgSummary *vpclattice.TargetGroupSummary) *vpclattice.GetTargetGroupOutput {
328+
329+
tg, err := env.LatticeClient.GetTargetGroupWithContext(ctx, &vpclattice.GetTargetGroupInput{
330+
TargetGroupIdentifier: tgSummary.Arn,
331+
})
332+
333+
if err != nil {
334+
panic(err)
335+
}
336+
337+
return tg
338+
}
339+
325340
// TODO: Create a new function that only verifying deployment len(podList.Items)==*deployment.Spec.Replicas, and don't do lattice.ListTargets() api call
326341
func (env *Framework) GetTargets(ctx context.Context, targetGroup *vpclattice.TargetGroupSummary, deployment *appsv1.Deployment) []*vpclattice.TargetSummary {
327342
var found []*vpclattice.TargetSummary
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package test
2+
3+
import (
4+
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
5+
corev1 "k8s.io/api/core/v1"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
8+
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
9+
)
10+
11+
type TargetGroupPolicyConfig struct {
12+
PolicyName string
13+
Protocol *string
14+
ProtocolVersion *string
15+
HealthCheck *anv1alpha1.HealthCheckConfig
16+
}
17+
18+
func (env *Framework) CreateTargetGroupPolicy(
19+
service *corev1.Service,
20+
config *TargetGroupPolicyConfig,
21+
) *anv1alpha1.TargetGroupPolicy {
22+
return &anv1alpha1.TargetGroupPolicy{
23+
TypeMeta: metav1.TypeMeta{
24+
Kind: "TargetGroupPolicy",
25+
},
26+
ObjectMeta: metav1.ObjectMeta{
27+
Namespace: service.Namespace,
28+
Name: config.PolicyName,
29+
},
30+
Spec: anv1alpha1.TargetGroupPolicySpec{
31+
TargetRef: &gwv1alpha2.PolicyTargetReference{
32+
Kind: gwv1beta1.Kind("Service"),
33+
Name: gwv1beta1.ObjectName(service.Name),
34+
},
35+
Protocol: config.Protocol,
36+
ProtocolVersion: config.ProtocolVersion,
37+
HealthCheck: config.HealthCheck,
38+
},
39+
}
40+
}
41+
42+
func (env *Framework) UpdateTargetGroupPolicy(
43+
policy *anv1alpha1.TargetGroupPolicy,
44+
config *TargetGroupPolicyConfig,
45+
) *anv1alpha1.TargetGroupPolicy {
46+
if config.Protocol != nil {
47+
policy.Spec.Protocol = config.Protocol
48+
}
49+
50+
if config.ProtocolVersion != nil {
51+
policy.Spec.ProtocolVersion = config.ProtocolVersion
52+
}
53+
54+
if config.HealthCheck != nil {
55+
policy.Spec.HealthCheck = config.HealthCheck
56+
}
57+
58+
return policy
59+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package integration
2+
3+
import (
4+
"time"
5+
6+
"github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
7+
"github.com/aws/aws-application-networking-k8s/test/pkg/test"
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/service/vpclattice"
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
appsv1 "k8s.io/api/apps/v1"
13+
corev1 "k8s.io/api/core/v1"
14+
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
15+
)
16+
17+
var _ = Describe("Target Group Policy Tests", Ordered, func() {
18+
var (
19+
deployment *appsv1.Deployment
20+
service *corev1.Service
21+
httpRoute *gwv1beta1.HTTPRoute
22+
policy *v1alpha1.TargetGroupPolicy
23+
)
24+
25+
BeforeAll(func() {
26+
deployment, service = testFramework.NewNginxApp(test.ElasticSearchOptions{
27+
Name: "target-group-policy-test",
28+
Namespace: k8snamespace,
29+
})
30+
31+
httpRoute = testFramework.NewHttpRoute(testGateway, service, service.Kind)
32+
33+
testFramework.ExpectCreated(ctx, deployment, service, httpRoute)
34+
})
35+
36+
It("Update Protocol creates new Target Group", func() {
37+
policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{
38+
PolicyName: "test-policy",
39+
Protocol: aws.String(vpclattice.TargetGroupProtocolHttp),
40+
})
41+
42+
testFramework.ExpectCreated(ctx, policy)
43+
44+
tg := testFramework.GetTargetGroup(ctx, service)
45+
46+
Expect(*tg.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))
47+
48+
testFramework.UpdateTargetGroupPolicy(policy, &test.TargetGroupPolicyConfig{
49+
Protocol: aws.String(vpclattice.TargetGroupProtocolHttps),
50+
})
51+
52+
testFramework.ExpectUpdated(ctx, policy)
53+
54+
httpsTG := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http1")
55+
56+
Expect(*httpsTG.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttps))
57+
})
58+
59+
It("Delete Target Group Policy reset health check config for HTTP and HTTP1 Target Group", func() {
60+
policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{
61+
PolicyName: "test-policy",
62+
Protocol: aws.String(vpclattice.TargetGroupProtocolHttp),
63+
ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp1),
64+
HealthCheck: &v1alpha1.HealthCheckConfig{
65+
IntervalSeconds: aws.Int64(7),
66+
StatusMatch: aws.String("200,204"),
67+
},
68+
})
69+
70+
testFramework.ExpectCreated(ctx, policy)
71+
72+
// time.Sleep(10 * time.Second)
73+
74+
Eventually(func(g Gomega) {
75+
tgSummary := testFramework.GetTargetGroup(ctx, service)
76+
77+
tg := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary)
78+
79+
g.Expect(*tg.Config.HealthCheck.ProtocolVersion).To(Equal(vpclattice.TargetGroupProtocolVersionHttp1))
80+
g.Expect(*tg.Config.HealthCheck.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))
81+
g.Expect(*tg.Config.HealthCheck.HealthCheckIntervalSeconds).To(BeEquivalentTo(7))
82+
g.Expect(*tg.Config.HealthCheck.Matcher.HttpCode).To(Equal("200,204"))
83+
}).Within(10 * time.Second).WithPolling(1 * time.Second).Should(Succeed())
84+
85+
testFramework.ExpectDeletedThenNotFound(ctx, policy)
86+
87+
Eventually(func(g Gomega) {
88+
tgSummary := testFramework.GetTargetGroup(ctx, service)
89+
90+
tg := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary)
91+
92+
g.Expect(*tg.Config.HealthCheck).To(Equal(vpclattice.HealthCheckConfig{
93+
Enabled: aws.Bool(true),
94+
Path: aws.String("/"),
95+
HealthCheckIntervalSeconds: aws.Int64(30),
96+
HealthCheckTimeoutSeconds: aws.Int64(5),
97+
HealthyThresholdCount: aws.Int64(5),
98+
UnhealthyThresholdCount: aws.Int64(2),
99+
Protocol: aws.String(vpclattice.TargetGroupProtocolHttp),
100+
ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp1),
101+
Port: nil,
102+
Matcher: &vpclattice.Matcher{
103+
HttpCode: aws.String("200"),
104+
},
105+
}))
106+
}).Within(10 * time.Second).WithPolling(1 * time.Second).Should(Succeed())
107+
})
108+
109+
It("Delete Target Group Policy create HTTP and HTTP1 Target Group", func() {
110+
policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{
111+
PolicyName: "test-policy",
112+
Protocol: aws.String(vpclattice.TargetGroupProtocolHttps),
113+
ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp2),
114+
})
115+
116+
testFramework.ExpectCreated(ctx, policy)
117+
118+
tg := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http2")
119+
120+
Expect(*tg.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttps))
121+
122+
testFramework.ExpectDeleted(ctx, policy)
123+
124+
httpTG := testFramework.GetTargetGroup(ctx, service)
125+
126+
Expect(*httpTG.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))
127+
})
128+
129+
AfterEach(func() {
130+
testFramework.ExpectDeleted(
131+
ctx,
132+
policy,
133+
)
134+
})
135+
136+
AfterAll(func() {
137+
testFramework.ExpectDeleted(
138+
ctx,
139+
deployment,
140+
service,
141+
httpRoute,
142+
)
143+
})
144+
})

0 commit comments

Comments
 (0)