From 6568cd08d28a089972847284994f92515436b4bb Mon Sep 17 00:00:00 2001 From: Scott Lai Date: Thu, 5 Oct 2023 13:18:46 -0700 Subject: [PATCH] test: add e2e tests for Target Group Policy CRD --- test/pkg/test/framework.go | 18 +++ test/pkg/test/target_group_policy.go | 59 ++++++++ .../integration/target_group_policy_test.go | 130 ++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 test/pkg/test/target_group_policy.go create mode 100644 test/suites/integration/target_group_policy_test.go diff --git a/test/pkg/test/framework.go b/test/pkg/test/framework.go index 6b3a5bd1..4de0f281 100644 --- a/test/pkg/test/framework.go +++ b/test/pkg/test/framework.go @@ -311,6 +311,24 @@ func (env *Framework) GetTargetGroupWithProtocol(ctx context.Context, service *v return found } +func (env *Framework) GetFullTargetGroupFromSummary( + ctx context.Context, + tgSummary *vpclattice.TargetGroupSummary) *vpclattice.GetTargetGroupOutput { + var tg *vpclattice.GetTargetGroupOutput + + Eventually(func(g Gomega) { + output, err := env.LatticeClient.GetTargetGroupWithContext(ctx, &vpclattice.GetTargetGroupInput{ + TargetGroupIdentifier: tgSummary.Arn, + }) + + g.Expect(err).To(BeNil()) + + tg = output + }).WithOffset(1).Should(Succeed()) + + return tg +} + // TODO: Create a new function that only verifying deployment len(podList.Items)==*deployment.Spec.Replicas, and don't do lattice.ListTargets() api call func (env *Framework) GetTargets(ctx context.Context, targetGroup *vpclattice.TargetGroupSummary, deployment *appsv1.Deployment) []*vpclattice.TargetSummary { var found []*vpclattice.TargetSummary diff --git a/test/pkg/test/target_group_policy.go b/test/pkg/test/target_group_policy.go new file mode 100644 index 00000000..bbd5df62 --- /dev/null +++ b/test/pkg/test/target_group_policy.go @@ -0,0 +1,59 @@ +package test + +import ( + anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/gateway-api/apis/v1alpha2" + "sigs.k8s.io/gateway-api/apis/v1beta1" +) + +type TargetGroupPolicyConfig struct { + PolicyName string + Protocol *string + ProtocolVersion *string + HealthCheck *anv1alpha1.HealthCheckConfig +} + +func (env *Framework) CreateTargetGroupPolicy( + service *v1.Service, + config *TargetGroupPolicyConfig, +) *anv1alpha1.TargetGroupPolicy { + return &anv1alpha1.TargetGroupPolicy{ + TypeMeta: metav1.TypeMeta{ + Kind: "TargetGroupPolicy", + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: service.Namespace, + Name: config.PolicyName, + }, + Spec: anv1alpha1.TargetGroupPolicySpec{ + TargetRef: &v1alpha2.PolicyTargetReference{ + Kind: v1beta1.Kind(service.Kind), + Name: v1beta1.ObjectName(service.Name), + }, + Protocol: config.Protocol, + ProtocolVersion: config.ProtocolVersion, + HealthCheck: config.HealthCheck, + }, + } +} + +func (env *Framework) UpdateTargetGroupPolicy( + policy *anv1alpha1.TargetGroupPolicy, + config *TargetGroupPolicyConfig, +) *anv1alpha1.TargetGroupPolicy { + if config.Protocol != nil { + policy.Spec.Protocol = config.Protocol + } + + if config.ProtocolVersion != nil { + policy.Spec.ProtocolVersion = config.ProtocolVersion + } + + if config.HealthCheck != nil { + policy.Spec.HealthCheck = config.HealthCheck + } + + return policy +} diff --git a/test/suites/integration/target_group_policy_test.go b/test/suites/integration/target_group_policy_test.go new file mode 100644 index 00000000..2d9d8d16 --- /dev/null +++ b/test/suites/integration/target_group_policy_test.go @@ -0,0 +1,130 @@ +package integration + +import ( + "time" + + "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1" + "github.com/aws/aws-application-networking-k8s/test/pkg/test" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/vpclattice" + . "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" + "sigs.k8s.io/gateway-api/apis/v1beta1" +) + +var _ = Describe("Target Group Policy Tests", func() { + var ( + deployment *appsv1.Deployment + service *v1.Service + httpRoute *v1beta1.HTTPRoute + policy *v1alpha1.TargetGroupPolicy + ) + + BeforeEach(func() { + deployment, service = testFramework.NewNginxApp(test.ElasticSearchOptions{ + Name: "target-group-policy-test", + Namespace: k8snamespace, + }) + }) + + AfterEach(func() { + testFramework.ExpectDeleted( + ctx, + deployment, + service, + policy, + httpRoute, + ) + }) + + It("Update Protocol creates new Target Group", func() { + policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{ + PolicyName: "test-policy", + Protocol: aws.String(vpclattice.TargetGroupProtocolHttp), + }) + + httpRoute = testFramework.NewHttpRoute(testGateway, service, service.Kind) + + testFramework.ExpectCreated(ctx, deployment, service, policy, httpRoute) + + tg := testFramework.GetTargetGroup(ctx, service) + + gomega.Expect(*tg.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttp)) + + testFramework.UpdateTargetGroupPolicy(policy, &test.TargetGroupPolicyConfig{ + Protocol: aws.String(vpclattice.TargetGroupProtocolHttps), + }) + + testFramework.ExpectUpdated(ctx, policy) + + httpsTG := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http1") + + gomega.Expect(*httpsTG.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttps)) + }) + + It("Delete Target Group Policy reset health check config for HTTP and HTTP1 Target Group", func() { + policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{ + PolicyName: "test-policy", + Protocol: aws.String(vpclattice.TargetGroupProtocolHttp), + ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp1), + HealthCheck: &v1alpha1.HealthCheckConfig{ + IntervalSeconds: aws.Int64(7), + StatusMatch: aws.String("200,204"), + }, + }) + + httpRoute = testFramework.NewHttpRoute(testGateway, service, service.Kind) + + testFramework.ExpectCreated(ctx, deployment, service, policy, httpRoute) + + tgSummary := testFramework.GetTargetGroup(ctx, service) + + tg := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary) + + gomega.Expect(*tg.Config.HealthCheck.ProtocolVersion).To(gomega.Equal(vpclattice.TargetGroupProtocolVersionHttp1)) + gomega.Expect(*tg.Config.HealthCheck.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttp)) + gomega.Expect(*tg.Config.HealthCheck.HealthCheckIntervalSeconds).To(gomega.BeEquivalentTo(7)) + gomega.Expect(*tg.Config.HealthCheck.Matcher.HttpCode).To(gomega.Equal("200,204")) + + testFramework.ExpectDeletedThenNotFound(ctx, policy) + + time.Sleep(2 * time.Second) + + updatedTG := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary) + + gomega.Expect(*updatedTG.Config.HealthCheck.Enabled).To(gomega.BeTrue()) + gomega.Expect(*updatedTG.Config.HealthCheck.Path).To(gomega.Equal("/")) + gomega.Expect(*updatedTG.Config.HealthCheck.HealthCheckIntervalSeconds).To(gomega.BeEquivalentTo(30)) + gomega.Expect(*updatedTG.Config.HealthCheck.HealthCheckTimeoutSeconds).To(gomega.BeEquivalentTo(5)) + gomega.Expect(*updatedTG.Config.HealthCheck.HealthyThresholdCount).To(gomega.BeEquivalentTo(5)) + gomega.Expect(*updatedTG.Config.HealthCheck.UnhealthyThresholdCount).To(gomega.BeEquivalentTo(2)) + gomega.Expect(*updatedTG.Config.HealthCheck.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttp)) + gomega.Expect(*updatedTG.Config.HealthCheck.ProtocolVersion).To(gomega.Equal(vpclattice.TargetGroupProtocolVersionHttp1)) + gomega.Expect(*updatedTG.Config.HealthCheck.Matcher.HttpCode).To(gomega.Equal("200")) + gomega.Expect(updatedTG.Config.HealthCheck.Port).To(gomega.BeNil()) + }) + + It("Delete Target Group Policy create HTTP and HTTP1 Target Group", func() { + policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{ + PolicyName: "test-policy", + Protocol: aws.String(vpclattice.TargetGroupProtocolHttps), + ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp2), + }) + + httpRoute = testFramework.NewHttpRoute(testGateway, service, service.Kind) + + testFramework.ExpectCreated(ctx, deployment, service, policy, httpRoute) + + tg := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http2") + + gomega.Expect(*tg.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttps)) + + testFramework.ExpectDeleted(ctx, policy) + + httpTG := testFramework.GetTargetGroup(ctx, service) + + gomega.Expect(*httpTG.Protocol).To(gomega.Equal(vpclattice.TargetGroupProtocolHttp)) + }) +})