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 24, 2024
1 parent 66d13a5 commit d0f2242
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 64 deletions.
14 changes: 14 additions & 0 deletions test/pkg/environment/common/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ func (env *Environment) EventuallyExpectHealthyWithTimeout(timeout time.Duration

}

func (env *Environment) ConsistentlyExpectHealthyPods(duration time.Duration, pods ...*corev1.Pod) {
GinkgoHelper()
By(fmt.Sprintf("expecting %d pods to be ready for %s", len(pods), duration))
Consistently(func(g Gomega) {
for _, pod := range pods {
g.Expect(env.Client.Get(env, client.ObjectKeyFromObject(pod), pod)).To(Succeed())
g.Expect(pod.Status.Conditions).To(ContainElement(And(
HaveField("Type", Equal(corev1.PodReady)),
HaveField("Status", Equal(corev1.ConditionTrue)),
)))
}
}, duration.String()).Should(Succeed())
}

func (env *Environment) EventuallyExpectKarpenterRestarted() {
GinkgoHelper()
By("rolling out the new karpenter deployment")
Expand Down
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)
})
})
83 changes: 83 additions & 0 deletions test/suites/termination/termination_grace_period_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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.Second * 90)}
})
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)
// Check that pod remains healthy until termination grace period
env.ConsistentlyExpectHealthyPods(time.Second*30, 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)
// Check that pod remains healthy until termination grace period
env.ConsistentlyExpectHealthyPods(time.Second*30, 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 d0f2242

Please sign in to comment.