-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for termination grace period
- Loading branch information
1 parent
e87e0e1
commit a93bf5e
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |