Skip to content

Commit

Permalink
test: Add tests for termination grace period
Browse files Browse the repository at this point in the history
  • Loading branch information
jigisha620 committed Jul 18, 2024
1 parent e87e0e1 commit a93bf5e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/e2e-matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ jobs:
region: ${{ inputs.region }}
- name: IPv6
region: ${{ inputs.region }}
- name: Termination
region: ${{ inputs.region }}
- name: LocalZone
# LAX is the only local zone available in the CI account, therefore only use us-west-2
region: us-west-2
Expand Down
113 changes: 113 additions & 0 deletions test/suites/termination/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
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 termination_test

import (
"testing"
"time"

coretest "sigs.k8s.io/karpenter/pkg/test"

"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"

v1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1"
"github.com/aws/karpenter-provider-aws/test/pkg/environment/aws"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var env *aws.Environment
var nodeClass *v1.EC2NodeClass
var nodePool *karpv1.NodePool

func TestExpiration(t *testing.T) {
RegisterFailHandler(Fail)
BeforeSuite(func() {
env = aws.NewEnvironment(t)
})
AfterSuite(func() {
env.Stop()
})
RunSpecs(t, "Termination")
}

var _ = BeforeEach(func() {
env.BeforeEach()
nodeClass = env.DefaultEC2NodeClass()
nodePool = env.DefaultNodePool(nodeClass)
})

var _ = AfterEach(func() { env.Cleanup() })
var _ = AfterEach(func() { env.AfterEach() })

var _ = Describe("Termination", func() {
BeforeEach(func() {
nodePool.Spec.Template.Spec.TerminationGracePeriod = &metav1.Duration{Duration: time.Second * 30}
})
It("should delete pod that tolerates do-not-disrupt after termination grace period seconds", func() {
pod := coretest.UnschedulablePod(coretest.PodOptions{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
karpv1.DoNotDisruptAnnotationKey: "true",
}}})
env.ExpectCreated(nodeClass, nodePool, pod)

nodeClaim := env.EventuallyExpectCreatedNodeClaimCount("==", 1)[0]
node := env.EventuallyExpectCreatedNodeCount("==", 1)[0]
env.EventuallyExpectHealthy(pod)

// Set the expireAfter value to get the node deleted
nodePool.Spec.Template.Spec.ExpireAfter = karpv1.NillableDuration{Duration: lo.ToPtr(time.Second * 15)}
env.ExpectUpdated(nodePool)

// Eventually the node will be tainted
Eventually(func(g Gomega) {
g.Expect(env.Client.Get(env.Context, client.ObjectKeyFromObject(node), node)).Should(Succeed())
_, ok := lo.Find(node.Spec.Taints, func(t corev1.Taint) bool {
return karpv1.IsDisruptingTaint(t)
})
g.Expect(ok).To(BeTrue())
}).Should(Succeed())
// After the deletion timestamp is set the node should be gone
env.EventuallyExpectNotFound(nodeClaim, node)
})
It("should delete pod that has a pre-stop hook after termination grace period seconds", func() {
pod := coretest.UnschedulablePod(coretest.PodOptions{PreStopSleep: lo.ToPtr(int64(300))})
env.ExpectCreated(nodeClass, nodePool, pod)

nodeClaim := env.EventuallyExpectCreatedNodeClaimCount("==", 1)[0]
node := env.EventuallyExpectCreatedNodeCount("==", 1)[0]
env.EventuallyExpectHealthy(pod)

// Set the expireAfter value to get the node deleted
nodePool.Spec.Template.Spec.ExpireAfter = karpv1.NillableDuration{Duration: lo.ToPtr(time.Second * 15)}
env.ExpectUpdated(nodePool)

// Eventually the node will be tainted
Eventually(func(g Gomega) {
g.Expect(env.Client.Get(env.Context, client.ObjectKeyFromObject(node), node)).Should(Succeed())
_, ok := lo.Find(node.Spec.Taints, func(t corev1.Taint) bool {
return karpv1.IsDisruptingTaint(t)
})
g.Expect(ok).To(BeTrue())
}).Should(Succeed())
// After the deletion timestamp is set the node should be gone
env.EventuallyExpectNotFound(nodeClaim, node)
})
})

0 comments on commit a93bf5e

Please sign in to comment.