Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add e2e tests for Target Group Policy CRD #434

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions test/pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ func (env *Framework) GetTargetGroupWithProtocol(ctx context.Context, service *v
return found
}

func (env *Framework) GetFullTargetGroupFromSummary(
ctx context.Context,
tgSummary *vpclattice.TargetGroupSummary) *vpclattice.GetTargetGroupOutput {

tg, err := env.LatticeClient.GetTargetGroupWithContext(ctx, &vpclattice.GetTargetGroupInput{
TargetGroupIdentifier: tgSummary.Arn,
})

if err != nil {
panic(err)
}

return tg
}
mikhail-aws marked this conversation as resolved.
Show resolved Hide resolved

// 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
Expand Down
59 changes: 59 additions & 0 deletions test/pkg/test/target_group_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package test

import (
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

type TargetGroupPolicyConfig struct {
PolicyName string
Protocol *string
ProtocolVersion *string
HealthCheck *anv1alpha1.HealthCheckConfig
}

func (env *Framework) CreateTargetGroupPolicy(
service *corev1.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: &gwv1alpha2.PolicyTargetReference{
Kind: gwv1beta1.Kind("Service"),
Name: gwv1beta1.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
}
144 changes: 144 additions & 0 deletions test/suites/integration/target_group_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
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"
corev1 "k8s.io/api/core/v1"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

var _ = Describe("Target Group Policy Tests", Ordered, func() {
var (
deployment *appsv1.Deployment
service *corev1.Service
httpRoute *gwv1beta1.HTTPRoute
policy *v1alpha1.TargetGroupPolicy
)

BeforeAll(func() {
deployment, service = testFramework.NewNginxApp(test.ElasticSearchOptions{
Name: "target-group-policy-test",
Namespace: k8snamespace,
})

httpRoute = testFramework.NewHttpRoute(testGateway, service, service.Kind)

testFramework.ExpectCreated(ctx, deployment, service, httpRoute)
})

It("Update Protocol creates new Target Group", func() {
policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{
PolicyName: "test-policy",
Protocol: aws.String(vpclattice.TargetGroupProtocolHttp),
})

testFramework.ExpectCreated(ctx, policy)

tg := testFramework.GetTargetGroup(ctx, service)

Expect(*tg.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))

testFramework.UpdateTargetGroupPolicy(policy, &test.TargetGroupPolicyConfig{
Protocol: aws.String(vpclattice.TargetGroupProtocolHttps),
})

testFramework.ExpectUpdated(ctx, policy)

httpsTG := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify old tg is deleted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not mix use cases. Let deletion check happens in another spec.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove empty lines

Expect(*httpsTG.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttps))
})

It("Delete Target Group Policy reset health check config for HTTP and HTTP1 Target Group", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be a better name : "Delete Target Group Policy will reset health check config to default value" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, since for other type of Target Group (e.g. HTTPS or HTTP2), when we delete the Target Group Policy, the controller will create new Target group since those fields are create-only

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"),
},
})

testFramework.ExpectCreated(ctx, policy)

// time.Sleep(10 * time.Second)

Eventually(func(g Gomega) {
tgSummary := testFramework.GetTargetGroup(ctx, service)

tg := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary)

g.Expect(*tg.Config.HealthCheck.ProtocolVersion).To(Equal(vpclattice.TargetGroupProtocolVersionHttp1))
g.Expect(*tg.Config.HealthCheck.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))
g.Expect(*tg.Config.HealthCheck.HealthCheckIntervalSeconds).To(BeEquivalentTo(7))
g.Expect(*tg.Config.HealthCheck.Matcher.HttpCode).To(Equal("200,204"))
}).Within(10 * time.Second).WithPolling(1 * time.Second).Should(Succeed())

testFramework.ExpectDeletedThenNotFound(ctx, policy)

Eventually(func(g Gomega) {
tgSummary := testFramework.GetTargetGroup(ctx, service)

tg := testFramework.GetFullTargetGroupFromSummary(ctx, tgSummary)

g.Expect(*tg.Config.HealthCheck).To(Equal(vpclattice.HealthCheckConfig{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

Enabled: aws.Bool(true),
Path: aws.String("/"),
HealthCheckIntervalSeconds: aws.Int64(30),
HealthCheckTimeoutSeconds: aws.Int64(5),
HealthyThresholdCount: aws.Int64(5),
UnhealthyThresholdCount: aws.Int64(2),
Protocol: aws.String(vpclattice.TargetGroupProtocolHttp),
ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp1),
Port: nil,
Matcher: &vpclattice.Matcher{
HttpCode: aws.String("200"),
},
}))
}).Within(10 * time.Second).WithPolling(1 * time.Second).Should(Succeed())
})

It("Delete Target Group Policy create HTTP and HTTP1 Target Group", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name ? "Delete Target Group Policy fallback to default TargetGroupProtocol and TargetGroupProtocolVersion"

policy = testFramework.CreateTargetGroupPolicy(service, &test.TargetGroupPolicyConfig{
PolicyName: "test-policy",
Protocol: aws.String(vpclattice.TargetGroupProtocolHttps),
ProtocolVersion: aws.String(vpclattice.TargetGroupProtocolVersionHttp2),
})

testFramework.ExpectCreated(ctx, policy)

tg := testFramework.GetTargetGroupWithProtocol(ctx, service, "https", "http2")

Expect(*tg.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttps))

testFramework.ExpectDeleted(ctx, policy)

httpTG := testFramework.GetTargetGroup(ctx, service)

Expect(*httpTG.Protocol).To(Equal(vpclattice.TargetGroupProtocolHttp))
})

AfterEach(func() {
testFramework.ExpectDeleted(
ctx,
policy,
)
})

AfterAll(func() {
testFramework.ExpectDeleted(
ctx,
deployment,
service,
httpRoute,
)
})
})