From a531596776d7603f89c1d3d8d15b6939e8d13fc6 Mon Sep 17 00:00:00 2001 From: Jonathan Innis Date: Thu, 1 Feb 2024 22:37:03 -0800 Subject: [PATCH] Remove redundant testing --- pkg/utils/resources/suite_test.go | 309 ++---------------------------- 1 file changed, 20 insertions(+), 289 deletions(-) diff --git a/pkg/utils/resources/suite_test.go b/pkg/utils/resources/suite_test.go index b2e6f3cd95..8b72d9f830 100644 --- a/pkg/utils/resources/suite_test.go +++ b/pkg/utils/resources/suite_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package resources +package resources_test import ( "testing" @@ -25,21 +25,15 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + . "sigs.k8s.io/karpenter/pkg/test/expectations" + "sigs.k8s.io/karpenter/pkg/utils/resources" + "sigs.k8s.io/karpenter/pkg/test" ) -// ExpectResources expects all the resources in expected to exist in real with the same values -func ExpectResources(expected, real v1.ResourceList) { - GinkgoHelper() - for k, v := range expected { - realV := real[k] - Expect(v.Value()).To(BeNumerically("~", realV.Value())) - } -} - func TestResources(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "Resources Suite") + RunSpecs(t, "Resources") } var _ = Describe("Resources Handling", func() { @@ -50,7 +44,6 @@ var _ = Describe("Resources Handling", func() { Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, }, - InitContainers: []v1.Container{ { RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), @@ -61,7 +54,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi"), @@ -81,7 +74,6 @@ var _ = Describe("Resources Handling", func() { Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, }, - InitContainers: []v1.Container{ { Resources: v1.ResourceRequirements{ @@ -98,7 +90,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("10"), v1.ResourceMemory: resource.MustParse("5Gi"), @@ -108,7 +100,7 @@ var _ = Describe("Resources Handling", func() { v1.ResourceMemory: resource.MustParse("5Gi"), }) }) - It("should calculate resource requests when there is a large initContainer after a sidecarContainer that exceeds container resource requests", func() { + It("should calculate resource requests when there is an initContainer after a sidecarContainer that exceeds container resource requests", func() { pod := test.Pod(test.PodOptions{ ResourceRequirements: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, @@ -130,7 +122,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("14"), v1.ResourceMemory: resource.MustParse("4Gi"), @@ -163,7 +155,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("6"), v1.ResourceMemory: resource.MustParse("4Gi"), @@ -173,73 +165,12 @@ var _ = Describe("Resources Handling", func() { v1.ResourceMemory: resource.MustParse("4Gi"), }) }) - It("should calculate resource requests when a single sidecarContainer is bigger than the container resource requests", func() { - pod := test.Pod(test.PodOptions{ - ResourceRequirements: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - - InitContainers: []v1.Container{ - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("4"), v1.ResourceMemory: resource.MustParse("2Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("4"), v1.ResourceMemory: resource.MustParse("2Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - }, - }) - podResources := Ceiling(pod) - ExpectResources(podResources.Requests, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("6"), - v1.ResourceMemory: resource.MustParse("3Gi"), - }) - ExpectResources(podResources.Limits, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("6"), - v1.ResourceMemory: resource.MustParse("3Gi"), - }) - }) - It("should calculate resource requests when a single sidecarContainer is not bigger than container resource requests", func() { - pod := test.Pod(test.PodOptions{ - ResourceRequirements: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - }, - - InitContainers: []v1.Container{ - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - }, - }) - podResources := Ceiling(pod) - ExpectResources(podResources.Requests, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("3"), - v1.ResourceMemory: resource.MustParse("3Gi"), - }) - ExpectResources(podResources.Limits, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("3"), - v1.ResourceMemory: resource.MustParse("3Gi"), - }) - }) - It("should calculate resource requests when an initContainer comes after multiple sidecarContainers that exceeds container resource requests", func() { + It("should calculate resource requests when there is an initContainer after multiple sidecarContainers that exceeds container resource requests", func() { pod := test.Pod(test.PodOptions{ ResourceRequirements: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, }, - InitContainers: []v1.Container{ { RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), @@ -277,7 +208,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("31"), v1.ResourceMemory: resource.MustParse("31Gi"), @@ -287,7 +218,7 @@ var _ = Describe("Resources Handling", func() { v1.ResourceMemory: resource.MustParse("31Gi"), }) }) - It("should calculate resource requests when an initContainer comes after multiple sidecarContainers that doesn't exceed container resource requests", func() { + It("should calculate resource requests when there is an initContainer after multiple sidecarContainers that doesn't exceed container resource requests", func() { pod := test.Pod(test.PodOptions{ ResourceRequirements: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, @@ -330,7 +261,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("14"), v1.ResourceMemory: resource.MustParse("14Gi"), @@ -340,67 +271,12 @@ var _ = Describe("Resources Handling", func() { v1.ResourceMemory: resource.MustParse("14Gi"), }) }) - It("should calculate resource requests when an initContainer comes after multiple sidecarContainers that exceeds container resource requests", func() { + It("should calculate resource requests with multiple sidecarContainers when the first initContainer exceeds the sum of all sidecarContainers and container resource requests", func() { pod := test.Pod(test.PodOptions{ ResourceRequirements: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, }, - - InitContainers: []v1.Container{ - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("15"), v1.ResourceMemory: resource.MustParse("15Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("15"), v1.ResourceMemory: resource.MustParse("15Gi")}, - }, - }, - }, - }) - podResources := Ceiling(pod) - ExpectResources(podResources.Requests, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("26"), - v1.ResourceMemory: resource.MustParse("26Gi"), - }) - ExpectResources(podResources.Limits, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("26"), - v1.ResourceMemory: resource.MustParse("26Gi"), - }) - }) - It("should calculate resource requests with multiple sidecarContainers when one initContainer exceeds the sum of all sidecarContainers and container resource requests", func() { - pod := test.Pod(test.PodOptions{ - ResourceRequirements: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - InitContainers: []v1.Container{ { Resources: v1.ResourceRequirements{ @@ -421,7 +297,6 @@ var _ = Describe("Resources Handling", func() { Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, }, }, - { Resources: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, @@ -456,7 +331,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("25"), v1.ResourceMemory: resource.MustParse("25Gi"), @@ -466,151 +341,7 @@ var _ = Describe("Resources Handling", func() { v1.ResourceMemory: resource.MustParse("25Gi"), }) }) - It("should calculate resource requests with multiple sidecarContainers and initContainers when one initContainer exceeds the sum of all sidecarContainers and container resource requests", func() { - pod := test.Pod(test.PodOptions{ - ResourceRequirements: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - - InitContainers: []v1.Container{ - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("25"), v1.ResourceMemory: resource.MustParse("25Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("25"), v1.ResourceMemory: resource.MustParse("25Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - }, - }) - podResources := Ceiling(pod) - ExpectResources(podResources.Requests, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("26"), - v1.ResourceMemory: resource.MustParse("26Gi"), - }) - ExpectResources(podResources.Limits, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("26"), - v1.ResourceMemory: resource.MustParse("26Gi"), - }) - }) - It("should calculate resource requests with multiple sidecarContainers and initContainers when there are multiple initContainers interspersed with sidecarContainers", func() { - pod := test.Pod(test.PodOptions{ - ResourceRequirements: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - - InitContainers: []v1.Container{ - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("2Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, - }, - }, - - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("5"), v1.ResourceMemory: resource.MustParse("5Gi")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1")}, - }, - }, - { - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - { - RestartPolicy: lo.ToPtr(v1.ContainerRestartPolicyAlways), - Resources: v1.ResourceRequirements{ - Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}, - }, - }, - }, - }) - podResources := Ceiling(pod) - ExpectResources(podResources.Requests, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("10"), - v1.ResourceMemory: resource.MustParse("10Gi"), - }) - ExpectResources(podResources.Limits, v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("10"), - v1.ResourceMemory: resource.MustParse("10Gi"), - }) - }) - It("should calculate resource requests with multiple sidecarContainers and initContainers when there are multiple initContainers interspersed with initContainers", func() { + It("should calculate resource requests with multiple interspersed sidecarContainers and initContainers", func() { pod := test.Pod(test.PodOptions{ ResourceRequirements: v1.ResourceRequirements{ Limits: v1.ResourceList{v1.ResourceCPU: resource.MustParse("3"), v1.ResourceMemory: resource.MustParse("3Gi")}, @@ -678,7 +409,7 @@ var _ = Describe("Resources Handling", func() { }, }, }) - podResources := Ceiling(pod) + podResources := resources.Ceiling(pod) ExpectResources(podResources.Requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("10"), v1.ResourceMemory: resource.MustParse("10Gi"), @@ -699,7 +430,7 @@ var _ = Describe("Resources Handling", func() { }, }, } - requests := MergeResourceLimitsIntoRequests(container) + requests := resources.MergeResourceLimitsIntoRequests(container) ExpectResources(requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi"), @@ -715,7 +446,7 @@ var _ = Describe("Resources Handling", func() { }, }, } - requests := MergeResourceLimitsIntoRequests(container) + requests := resources.MergeResourceLimitsIntoRequests(container) ExpectResources(requests, v1.ResourceList{ v1.ResourceCPU: resource.MustParse("2"), v1.ResourceMemory: resource.MustParse("1Gi"),