Skip to content

Commit

Permalink
[unit testing] additional simplifications to node/pod unit tests (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Gabrielson authored Apr 9, 2021
1 parent 30ba533 commit 94daf1b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
32 changes: 10 additions & 22 deletions pkg/cloudprovider/aws/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
. "github.com/awslabs/karpenter/pkg/test/expectations"
"github.com/patrickmn/go-cache"
"knative.dev/pkg/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"

"github.com/Pallinder/go-randomdata"
Expand All @@ -38,7 +37,6 @@ import (
webhooksprovisioning "github.com/awslabs/karpenter/pkg/webhooks/provisioning/v1alpha1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -149,10 +147,8 @@ var _ = Describe("Allocation", func() {
ExpectCreated(env.Client, provisioner)
ExpectEventuallyReconciled(env.Client, provisioner)
// Assertions
scheduled := &v1.Pod{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, scheduled)).To(Succeed())
node := &v1.Node{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: scheduled.Spec.NodeName}, node)).To(Succeed())
scheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
ExpectNodeExists(env.Client, scheduled.Spec.NodeName)
Expect(fakeEC2API.CalledWithCreateFleetInput).To(HaveLen(1))
Expect(fakeEC2API.CalledWithCreateFleetInput[0].LaunchTemplateConfigs[0].Overrides).To(
ContainElements(
Expand Down Expand Up @@ -183,10 +179,8 @@ var _ = Describe("Allocation", func() {
ExpectCreated(env.Client, provisioner)
ExpectEventuallyReconciled(env.Client, provisioner)
// Assertions
scheduled := &v1.Pod{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, scheduled)).To(Succeed())
node := &v1.Node{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: scheduled.Spec.NodeName}, node)).To(Succeed())
scheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
ExpectNodeExists(env.Client, scheduled.Spec.NodeName)
Expect(fakeEC2API.CalledWithCreateFleetInput).To(HaveLen(1))
Expect(fakeEC2API.CalledWithCreateFleetInput[0].LaunchTemplateConfigs[0].Overrides).To(
ContainElements(
Expand Down Expand Up @@ -214,10 +208,8 @@ var _ = Describe("Allocation", func() {
ExpectCreated(env.Client, provisioner)
ExpectEventuallyReconciled(env.Client, provisioner)
// Assertions
scheduled := &v1.Pod{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, scheduled)).To(Succeed())
node := &v1.Node{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: scheduled.Spec.NodeName}, node)).To(Succeed())
scheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
ExpectNodeExists(env.Client, scheduled.Spec.NodeName)
Expect(fakeEC2API.CalledWithCreateFleetInput).To(HaveLen(1))
Expect(fakeEC2API.CalledWithCreateFleetInput[0].LaunchTemplateConfigs[0].Overrides).To(
ContainElements(
Expand All @@ -236,15 +228,11 @@ var _ = Describe("Allocation", func() {
ExpectCreated(env.Client, provisioner)
ExpectEventuallyReconciled(env.Client, provisioner)
// Assertions
scheduled1 := &v1.Pod{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: pod1.GetName(), Namespace: pod1.GetNamespace()}, scheduled1)).To(Succeed())
scheduled2 := &v1.Pod{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: pod2.GetName(), Namespace: pod2.GetNamespace()}, scheduled2)).To(Succeed())
scheduled1 := ExpectPodExists(env.Client, pod1.GetName(), pod1.GetNamespace())
scheduled2 := ExpectPodExists(env.Client, pod2.GetName(), pod2.GetNamespace())
ExpectNodeExists(env.Client, scheduled1.Spec.NodeName)
ExpectNodeExists(env.Client, scheduled2.Spec.NodeName)
Expect(scheduled1.Spec.NodeName).NotTo(Equal(scheduled2.Spec.NodeName))
node1 := &v1.Node{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: scheduled1.Spec.NodeName}, node1)).To(Succeed())
node2 := &v1.Node{}
Expect(env.Client.Get(context.Background(), client.ObjectKey{Name: scheduled2.Spec.NodeName}, node2)).To(Succeed())
Expect(fakeEC2API.CalledWithCreateFleetInput).To(HaveLen(2))
})
})
Expand Down
32 changes: 9 additions & 23 deletions pkg/controllers/provisioning/v1alpha1/allocation/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ var _ = Describe("Allocation", func() {
Expect(env.Client.List(ctx, nodes)).To(Succeed())
Expect(len(nodes.Items)).To(Equal(1))
for _, object := range pods {
pod := v1.Pod{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: object.GetName(), Namespace: object.GetNamespace()}, &pod)).To(Succeed())
pod := ExpectPodExists(env.Client, object.GetName(), object.GetNamespace())
Expect(pod.Spec.NodeName).To(Equal(nodes.Items[0].Name))
}
})
Expand Down Expand Up @@ -156,17 +155,14 @@ var _ = Describe("Allocation", func() {
Expect(env.Client.List(ctx, nodes)).To(Succeed())
Expect(len(nodes.Items)).To(Equal(6)) // 5 schedulable -> 5 node, 2 coschedulable -> 1 node
for _, pod := range append(schedulable, coschedulable...) {
scheduled := &v1.Pod{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, scheduled)).To(Succeed())
node := &v1.Node{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: scheduled.Spec.NodeName}, node)).To(Succeed())
scheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
node := ExpectNodeExists(env.Client, scheduled.Spec.NodeName)
for key, value := range scheduled.Spec.NodeSelector {
Expect(node.Labels[key]).To(Equal(value))
}
}
for _, pod := range unschedulable {
unscheduled := &v1.Pod{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, unscheduled)).To(Succeed())
unscheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
Expect(unscheduled.Spec.NodeName).To(Equal(""))
}
})
Expand All @@ -176,33 +172,26 @@ var _ = Describe("Allocation", func() {
// Tolerates with OpExists
test.PendingPodWith(test.PodOptions{
Tolerations: []v1.Toleration{{Key: "test-key", Operator: v1.TolerationOpExists}},
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
// Tolerates with OpEqual
test.PendingPodWith(test.PodOptions{
Tolerations: []v1.Toleration{{Key: "test-key", Value: "test-value", Operator: v1.TolerationOpEqual}},
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
}
unschedulable := []client.Object{
// Missing toleration
test.PendingPodWith(test.PodOptions{
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
test.PendingPod(),
// key mismatch with OpExists
test.PendingPodWith(test.PodOptions{
Tolerations: []v1.Toleration{{Key: "invalid", Operator: v1.TolerationOpExists}},
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
// value mismatch with OpEqual
test.PendingPodWith(test.PodOptions{
Tolerations: []v1.Toleration{{Key: "test-key", Value: "invalid", Operator: v1.TolerationOpEqual}},
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
// key mismatch with OpEqual
test.PendingPodWith(test.PodOptions{
Tolerations: []v1.Toleration{{Key: "invalid", Value: "test-value", Operator: v1.TolerationOpEqual}},
Conditions: []v1.PodCondition{{Type: v1.PodScheduled, Reason: v1.PodReasonUnschedulable, Status: v1.ConditionFalse}},
}),
}
ExpectCreatedWithStatus(env.Client, schedulable...)
Expand All @@ -214,15 +203,12 @@ var _ = Describe("Allocation", func() {
Expect(env.Client.List(ctx, nodes)).To(Succeed())
Expect(len(nodes.Items)).To(Equal(1))
for _, pod := range schedulable {
scheduled := &v1.Pod{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, scheduled)).To(Succeed())
node := &v1.Node{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: scheduled.Spec.NodeName}, node)).To(Succeed())
scheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
ExpectNodeExists(env.Client, scheduled.Spec.NodeName)
}
for _, pod := range unschedulable {
unscheduled := &v1.Pod{}
Expect(env.Client.Get(ctx, client.ObjectKey{Name: pod.GetName(), Namespace: pod.GetNamespace()}, unscheduled)).To(Succeed())
Expect(unscheduled.Spec.NodeName).To(Equal(""))
unscheduled := ExpectPodExists(env.Client, pod.GetName(), pod.GetNamespace())
Expect(unscheduled.Spec.NodeName).To(BeEmpty())
}
})
})
Expand Down
12 changes: 12 additions & 0 deletions pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ const (
RequestInterval = 1 * time.Second
)

func ExpectPodExists(c client.Client, name string, namespace string) *v1.Pod {
pod := &v1.Pod{}
Expect(c.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, pod)).To(Succeed())
return pod
}

func ExpectNodeExists(c client.Client, name string) *v1.Node {
node := &v1.Node{}
Expect(c.Get(context.Background(), client.ObjectKey{Name: name}, node)).To(Succeed())
return node
}

func ExpectCreated(c client.Client, objects ...client.Object) {
for _, object := range objects {
nn := types.NamespacedName{Name: object.GetName(), Namespace: object.GetNamespace()}
Expand Down

0 comments on commit 94daf1b

Please sign in to comment.