Skip to content

Commit

Permalink
fix for UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenXu93 committed Dec 23, 2024
1 parent a694d91 commit 17d374d
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/resourcemonitor/podresourcesscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func hasExclusiveCPUs(pod *corev1.Pod) bool {
}

//No CPUs requested in all the containers in the pod
return totalCPU != 0
return false
}

// hasIntegralCPUs returns true if a container in pod is requesting integral CPUs else returns false
Expand Down Expand Up @@ -152,7 +152,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
for _, podResource := range respPodResources {
klog.InfoS("scanning pod", "podName", podResource.GetName())
hasDevice := hasDevice(podResource)
isWatchable, _, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
if err != nil {
return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %w", podResource.GetNamespace(), podResource.GetName(), err)
}
Expand All @@ -171,7 +171,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
}

cpuIDs := container.GetCpuIds()
if len(cpuIDs) > 0 {
if len(cpuIDs) > 0 && isIntegralGuaranteed {
var resCPUs []string
for _, cpuID := range container.GetCpuIds() {
resCPUs = append(resCPUs, strconv.FormatInt(cpuID, 10))
Expand Down
151 changes: 151 additions & 0 deletions pkg/resourcemonitor/podresourcesscanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}

fakeCli := fakeclient.NewSimpleClientset(pod)
Expand Down Expand Up @@ -280,6 +283,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}

fakeCli = fakeclient.NewSimpleClientset(pod)
Expand Down Expand Up @@ -368,6 +374,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -458,6 +467,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -536,6 +548,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -628,6 +643,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -825,6 +843,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -1029,6 +1050,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -1113,6 +1137,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
Expand Down Expand Up @@ -1145,5 +1172,129 @@ func TestPodScanner(t *testing.T) {
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
})

Convey("When I successfully get valid response for guaranteed pods with not cpu pin containers", func() {
resp := &v1.ListPodResourcesResponse{
PodResources: []*v1.PodResources{
{
Name: "test-pod-0",
Namespace: "pod-res-test",
Containers: []*v1.ContainerResources{
{
Name: "test-cnt-0",
CpuIds: []int64{0, 1},
Devices: []*v1.ContainerDevices{
{
ResourceName: "fake.io/resource",
DeviceIds: []string{"devA"},
},
},
},
{
Name: "test-cnt-1",
Devices: []*v1.ContainerDevices{
{
ResourceName: "fake.io/resource",
DeviceIds: []string{"devA"},
},
},
},
},
},
},
}
mockPodResClient.On("List", mock.AnythingOfType("*context.timerCtx"), mock.AnythingOfType("*v1.ListPodResourcesRequest")).Return(resp, nil)
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod-0",
Namespace: "pod-res-test",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test-cnt-0",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{

corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("2"),
},
Limits: corev1.ResourceList{
corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("2"),
},
},
},
{
Name: "test-cnt-1",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{

corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("1500m"),
},
Limits: corev1.ResourceList{
corev1.ResourceName("fake.io/resource"): *resource.NewQuantity(1, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceCPU: resource.MustParse("1500m"),
},
},
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
res, err := resScan.Scan()

Convey("Error is nil", func() {
So(err, ShouldBeNil)
})
Convey("Return PodResources should have values", func() {
So(len(res.PodResources), ShouldBeGreaterThan, 0)
})

expected := []PodResources{
{
Name: "test-pod-0",
Namespace: "pod-res-test",
Containers: []ContainerResources{
{
Name: "test-cnt-0",
Resources: []ResourceInfo{
{
Name: corev1.ResourceCPU,
Data: []string{"0", "1"},
},
{
Name: "fake.io/resource",
Data: []string{"devA"},
},
},
},
{
Name: "test-cnt-1",
Resources: []ResourceInfo{
{
Name: "fake.io/resource",
Data: []string{"devA"},
},
},
},
},
},
}
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
})

})
}
3 changes: 3 additions & 0 deletions test/e2e/utils/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func GuaranteedSleeper(opts ...func(pod *corev1.Pod)) *corev1.Pod {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
for _, o := range opts {
o(p)
Expand Down

0 comments on commit 17d374d

Please sign in to comment.