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

refactor test suite #722

Merged
merged 1 commit into from
Nov 15, 2022
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GUM = $(BIN_DIR)/gum
GH = $(BIN_DIR)/gh
GOLICENSES = $(BIN_DIR)/go-licenses
HELM_BASE_OPTS ?= --set aws.region=${AWS_REGION},serviceAccount.name=${SERVICE_ACCOUNT_NAME},serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn=${SERVICE_ACCOUNT_ROLE_ARN}
GINKGO_BASE_OPTS ?= --coverpkg $(shell head -n 1 $(PROJECT_DIR)/go.mod | cut -s -d ' ' -f 2)/pkg/...
GINKGO_BASE_OPTS ?= -r --coverpkg $(shell head -n 1 $(PROJECT_DIR)/go.mod | cut -s -d ' ' -f 2)/pkg/...
KODATA = \
cmd/controller/kodata/HEAD \
cmd/controller/kodata/refs \
Expand Down
134 changes: 134 additions & 0 deletions test/reconciler/asg_lifecycle_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciler

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/aws/aws-node-termination-handler/test/reconciler/mock"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
)

var _ = Describe("Reconciliation", func() {
When("the SQS queue contains an ASG Lifecycle Notification v1", func() {
var (
infra *mock.Infrastructure
result reconcile.Result
err error
)

BeforeEach(func() {
infra = mock.NewInfrastructure()
})

JustBeforeEach(func() {
result, err = infra.Reconcile()
})

When("the lifecycle transition is termination", func() {
BeforeEach(func() {
infra.ResizeCluster(3)

infra.SQSQueues[mock.QueueURL] = append(infra.SQSQueues[mock.QueueURL], &sqs.Message{
ReceiptHandle: aws.String("msg-1"),
Body: aws.String(fmt.Sprintf(`{
"source": "aws.autoscaling",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"version": "1",
"detail": {
"EC2InstanceId": "%s",
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING"
}
}`, infra.InstanceIDs[1])),
})

infra.CreatePendingASGLifecycleAction(infra.InstanceIDs[1])
})

It("returns success and requeues the request with the reconciler's configured interval", func() {
Expect(result, err).To(HaveField("RequeueAfter", Equal(infra.Reconciler.RequeueInterval)))
})

It("cordons and drains only the targeted node", func() {
Expect(infra.CordonedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
Expect(infra.DrainedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
})

It("completes the ASG lifecycle action", func() {
Expect(infra.ASGLifecycleActions).To(
SatisfyAll(
HaveKeyWithValue(infra.InstanceIDs[1], Equal(mock.StateComplete)),
HaveLen(1),
),
)
})

It("deletes the message from the SQS queue", func() {
Expect(infra.SQSQueues[mock.QueueURL]).To(BeEmpty())
})
})

When("the lifecycle transition is not termination", func() {
BeforeEach(func() {
infra.ResizeCluster(3)

infra.SQSQueues[mock.QueueURL] = append(infra.SQSQueues[mock.QueueURL], &sqs.Message{
ReceiptHandle: aws.String("msg-1"),
Body: aws.String(fmt.Sprintf(`{
"source": "aws.autoscaling",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"version": "1",
"detail": {
"EC2InstanceId": "%s",
"LifecycleTransition": "test:INVALID"
}
}`, infra.InstanceIDs[1])),
})

infra.CreatePendingASGLifecycleAction(infra.InstanceIDs[1])
})

It("returns success and requeues the request with the reconciler's configured interval", func() {
Expect(result, err).To(HaveField("RequeueAfter", Equal(infra.Reconciler.RequeueInterval)))
})

It("does not cordon or drain any nodes", func() {
Expect(infra.CordonedNodes).To(BeEmpty())
Expect(infra.DrainedNodes).To(BeEmpty())
})

It("does not complete the ASG lifecycle action", func() {
Expect(infra.ASGLifecycleActions).To(
SatisfyAll(
HaveKeyWithValue(infra.InstanceIDs[1], Equal(mock.StatePending)),
HaveLen(1),
),
)
})

It("does not delete the message from the SQS queue", func() {
Expect(infra.SQSQueues[mock.QueueURL]).To(HaveLen(1))
})
})
})
})
134 changes: 134 additions & 0 deletions test/reconciler/asg_lifecycle_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciler

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/aws/aws-node-termination-handler/test/reconciler/mock"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
)

var _ = Describe("Reconciliation", func() {
When("the SQS queue contains an ASG Lifecycle Notification v2", func() {
var (
infra *mock.Infrastructure
result reconcile.Result
err error
)

BeforeEach(func() {
infra = mock.NewInfrastructure()
})

JustBeforeEach(func() {
result, err = infra.Reconcile()
})

When("the lifecycle transition is termination", func() {
BeforeEach(func() {
infra.ResizeCluster(3)

infra.SQSQueues[mock.QueueURL] = append(infra.SQSQueues[mock.QueueURL], &sqs.Message{
ReceiptHandle: aws.String("msg-1"),
Body: aws.String(fmt.Sprintf(`{
"source": "aws.autoscaling",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"version": "2",
"detail": {
"EC2InstanceId": "%s",
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING"
}
}`, infra.InstanceIDs[1])),
})

infra.CreatePendingASGLifecycleAction(infra.InstanceIDs[1])
})

It("returns success and requeues the request with the reconciler's configured interval", func() {
Expect(result, err).To(HaveField("RequeueAfter", Equal(infra.Reconciler.RequeueInterval)))
})

It("cordons and drains only the targeted node", func() {
Expect(infra.CordonedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
Expect(infra.DrainedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
})

It("completes the ASG lifecycle action", func() {
Expect(infra.ASGLifecycleActions).To(
SatisfyAll(
HaveKeyWithValue(infra.InstanceIDs[1], Equal(mock.StateComplete)),
HaveLen(1),
),
)
})

It("deletes the message from the SQS queue", func() {
Expect(infra.SQSQueues[mock.QueueURL]).To(BeEmpty())
})
})

When("the lifecycle transition is not termination", func() {
BeforeEach(func() {
infra.ResizeCluster(3)

infra.SQSQueues[mock.QueueURL] = append(infra.SQSQueues[mock.QueueURL], &sqs.Message{
ReceiptHandle: aws.String("msg-1"),
Body: aws.String(fmt.Sprintf(`{
"source": "aws.autoscaling",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"version": "2",
"detail": {
"EC2InstanceId": "%s",
"LifecycleTransition": "test:INVALID"
}
}`, infra.InstanceIDs[1])),
})

infra.CreatePendingASGLifecycleAction(infra.InstanceIDs[1])
})

It("returns success and requeues the request with the reconciler's configured interval", func() {
Expect(result, err).To(HaveField("RequeueAfter", Equal(infra.Reconciler.RequeueInterval)))
})

It("does not cordon or drain any nodes", func() {
Expect(infra.CordonedNodes).To(BeEmpty())
Expect(infra.DrainedNodes).To(BeEmpty())
})

It("does not complete the ASG lifecycle action", func() {
Expect(infra.ASGLifecycleActions).To(
SatisfyAll(
HaveKeyWithValue(infra.InstanceIDs[1], Equal(mock.StatePending)),
HaveLen(1),
),
)
})

It("does not delete the message from the SQS queue", func() {
Expect(infra.SQSQueues[mock.QueueURL]).To(HaveLen(1))
})
})
})
})
85 changes: 85 additions & 0 deletions test/reconciler/asg_v1_err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciler

import (
"errors"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/aws/aws-node-termination-handler/test/reconciler/mock"

"github.com/aws/aws-sdk-go/aws"
awsrequest "github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/sqs"
)

var _ = Describe("Reconciliation", func() {
When("completing an ASG Lifecycle Action (v1) fails", func() {
const errMsg = "test error"
var (
infra *mock.Infrastructure
result reconcile.Result
err error
)

BeforeEach(func() {
infra = mock.NewInfrastructure()
infra.ResizeCluster(3)

infra.SQSQueues[mock.QueueURL] = append(infra.SQSQueues[mock.QueueURL], &sqs.Message{
ReceiptHandle: aws.String("msg-1"),
Body: aws.String(fmt.Sprintf(`{
"source": "aws.autoscaling",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"version": "1",
"detail": {
"EC2InstanceId": "%s",
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING"
}
}`, infra.InstanceIDs[1])),
})

infra.CompleteASGLifecycleActionFunc = func(_ aws.Context, _ *autoscaling.CompleteLifecycleActionInput, _ ...awsrequest.Option) (*autoscaling.CompleteLifecycleActionOutput, error) {
return nil, errors.New(errMsg)
}

result, err = infra.Reconcile()
})

It("does not requeue the request", func() {
Expect(result).To(BeZero())
})

It("returns an error", func() {
Expect(err).To(MatchError(ContainSubstring(errMsg)))
})

It("cordons and drains only the targeted node", func() {
Expect(infra.CordonedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
Expect(infra.DrainedNodes).To(SatisfyAll(HaveKey(infra.NodeNames[1]), HaveLen(1)))
})

It("deletes the message from the SQS queue", func() {
Expect(infra.SQSQueues[mock.QueueURL]).To(BeEmpty())
})
})
})
Loading