Skip to content

Commit

Permalink
add test for koordlet metric
Browse files Browse the repository at this point in the history
Signed-off-by: j4ckstraw <j4ckstraw@foxmail.com>
  • Loading branch information
j4ckstraw committed Jan 30, 2024
1 parent 73abcdc commit cc0b0e5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/koordlet/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,16 @@ func TestResourceSummaryCollectors(t *testing.T) {
corev1.ResourceMemory: resource.MustParse("200Gi"),
apiext.BatchCPU: resource.MustParse("50000"),
apiext.BatchMemory: resource.MustParse("80Gi"),
apiext.MidCPU: resource.MustParse("50000"),
apiext.MidMemory: resource.MustParse("80Gi"),
},
Capacity: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100"),
corev1.ResourceMemory: resource.MustParse("200Gi"),
apiext.BatchCPU: resource.MustParse("50000"),
apiext.BatchMemory: resource.MustParse("80Gi"),
apiext.MidCPU: resource.MustParse("50000"),
apiext.MidMemory: resource.MustParse("80Gi"),
},
},
}
Expand Down Expand Up @@ -236,19 +240,57 @@ func TestResourceSummaryCollectors(t *testing.T) {
},
},
}
testingMidPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test_mid_pod",
Namespace: "test_mid_pod_namespace",
UID: "mid01",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test_mid_container",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
apiext.MidCPU: resource.MustParse("1000"),
apiext.MidMemory: resource.MustParse("2Gi"),
},
Limits: corev1.ResourceList{
apiext.MidCPU: resource.MustParse("1000"),
apiext.MidMemory: resource.MustParse("2Gi"),
},
},
},
},
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "test_mid_container",
ContainerID: "containerd://midxxx",
},
},
},
}

t.Run("test not panic", func(t *testing.T) {
Register(testingNode)
defer Register(nil)

RecordNodeResourceAllocatable(string(apiext.BatchCPU), UnitInteger, float64(util.QuantityPtr(testingNode.Status.Allocatable[apiext.BatchCPU]).Value()))
RecordNodeResourceAllocatable(string(apiext.BatchMemory), UnitByte, float64(util.QuantityPtr(testingNode.Status.Allocatable[apiext.BatchMemory]).Value()))
RecordNodeResourceAllocatable(string(apiext.MidCPU), UnitInteger, float64(util.QuantityPtr(testingNode.Status.Allocatable[apiext.MidCPU]).Value()))
RecordNodeResourceAllocatable(string(apiext.MidMemory), UnitByte, float64(util.QuantityPtr(testingNode.Status.Allocatable[apiext.MidMemory]).Value()))
RecordContainerResourceRequests(string(corev1.ResourceCPU), UnitCore, &testingPod.Status.ContainerStatuses[0], testingPod, float64(testingPod.Spec.Containers[0].Resources.Requests.Cpu().Value()))
RecordContainerResourceRequests(string(corev1.ResourceMemory), UnitByte, &testingPod.Status.ContainerStatuses[0], testingPod, float64(testingPod.Spec.Containers[0].Resources.Requests.Memory().Value()))
RecordContainerResourceRequests(string(apiext.BatchCPU), UnitInteger, &testingBatchPod.Status.ContainerStatuses[0], testingBatchPod, float64(util.QuantityPtr(testingBatchPod.Spec.Containers[0].Resources.Requests[apiext.BatchCPU]).Value()))
RecordContainerResourceRequests(string(apiext.BatchMemory), UnitByte, &testingBatchPod.Status.ContainerStatuses[0], testingBatchPod, float64(util.QuantityPtr(testingBatchPod.Spec.Containers[0].Resources.Requests[apiext.BatchMemory]).Value()))
RecordContainerResourceRequests(string(apiext.MidCPU), UnitInteger, &testingMidPod.Status.ContainerStatuses[0], testingMidPod, float64(util.QuantityPtr(testingMidPod.Spec.Containers[0].Resources.Requests[apiext.MidCPU]).Value()))
RecordContainerResourceRequests(string(apiext.MidMemory), UnitByte, &testingMidPod.Status.ContainerStatuses[0], testingMidPod, float64(util.QuantityPtr(testingMidPod.Spec.Containers[0].Resources.Requests[apiext.MidMemory]).Value()))
RecordContainerResourceLimits(string(apiext.BatchCPU), UnitInteger, &testingBatchPod.Status.ContainerStatuses[0], testingBatchPod, float64(util.QuantityPtr(testingBatchPod.Spec.Containers[0].Resources.Limits[apiext.BatchCPU]).Value()))
RecordContainerResourceLimits(string(apiext.BatchMemory), UnitByte, &testingBatchPod.Status.ContainerStatuses[0], testingBatchPod, float64(util.QuantityPtr(testingBatchPod.Spec.Containers[0].Resources.Limits[apiext.BatchMemory]).Value()))
RecordContainerResourceLimits(string(apiext.MidCPU), UnitInteger, &testingMidPod.Status.ContainerStatuses[0], testingMidPod, float64(util.QuantityPtr(testingMidPod.Spec.Containers[0].Resources.Limits[apiext.MidCPU]).Value()))
RecordContainerResourceLimits(string(apiext.MidMemory), UnitByte, &testingMidPod.Status.ContainerStatuses[0], testingMidPod, float64(util.QuantityPtr(testingMidPod.Spec.Containers[0].Resources.Limits[apiext.MidMemory]).Value()))

ResetContainerResourceRequests()
ResetContainerResourceLimits()
Expand Down

0 comments on commit cc0b0e5

Please sign in to comment.