1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-31 04:04:51 +00:00

fix for UT

This commit is contained in:
AllenXu93 2024-12-20 18:58:14 +08:00
parent a694d916df
commit 17d374d78e
3 changed files with 157 additions and 3 deletions

View file

@ -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
@ -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)
}
@ -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))

View file

@ -165,6 +165,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli := fakeclient.NewSimpleClientset(pod)
@ -280,6 +283,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
@ -368,6 +374,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -458,6 +467,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -536,6 +548,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -628,6 +643,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -825,6 +843,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -1029,6 +1050,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -1113,6 +1137,9 @@ func TestPodScanner(t *testing.T) {
},
},
},
Status: corev1.PodStatus{
QOSClass: corev1.PodQOSGuaranteed,
},
}
fakeCli = fakeclient.NewSimpleClientset(pod)
resScan.(*PodResourcesScanner).k8sClient = fakeCli
@ -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)
})
})
}

View file

@ -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)