Skip to content

Commit

Permalink
tests: Move tests under termination test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jigisha620 committed Jul 18, 2024
1 parent a93bf5e commit 75b3297
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integration_test
package termination_test

import (
"fmt"
Expand Down
62 changes: 0 additions & 62 deletions test/suites/termination/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ 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"

Expand Down Expand Up @@ -57,57 +49,3 @@ var _ = BeforeEach(func() {

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)
})
})
79 changes: 79 additions & 0 deletions test/suites/termination/termination_grace_period_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 (
"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"

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

var _ = Describe("TerminationGracePeriod", func() {
BeforeEach(func() {
nodePool.Spec.Template.Spec.TerminationGracePeriod = &metav1.Duration{Duration: time.Second * 30}
// Set the expireAfter value to get the node deleted
nodePool.Spec.Template.Spec.ExpireAfter = karpv1.NillableDuration{Duration: lo.ToPtr(time.Minute)}
})
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)

// 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())
// Both nodeClaim and node should be gone once terminationGracePeriod is reached
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)

// 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())
// Both nodeClaim and node should be gone once terminationGracePeriod is reached
env.EventuallyExpectNotFound(nodeClaim, node)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package integration_test
package termination_test

import (
"time"
Expand Down

0 comments on commit 75b3297

Please sign in to comment.