Skip to content

Commit 5bd0db0

Browse files
committed
Add new test cases for GetPreferredAllocation() in allocation path
1 parent 83f18d9 commit 5bd0db0

File tree

1 file changed

+169
-7
lines changed

1 file changed

+169
-7
lines changed

pkg/kubelet/cm/devicemanager/topology_hints_test.go

Lines changed: 169 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package devicemanager
1818

1919
import (
20+
"fmt"
2021
"reflect"
2122
"sort"
2223
"testing"
@@ -431,13 +432,15 @@ func TestGetTopologyHints(t *testing.T) {
431432

432433
func TestTopologyAlignedAllocation(t *testing.T) {
433434
tcases := []struct {
434-
description string
435-
resource string
436-
request int
437-
devices []pluginapi.Device
438-
allocatedDevices []string
439-
hint topologymanager.TopologyHint
440-
expectedAlignment map[int]int
435+
description string
436+
resource string
437+
request int
438+
devices []pluginapi.Device
439+
allocatedDevices []string
440+
hint topologymanager.TopologyHint
441+
getPreferredAllocationFunc func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error)
442+
expectedPreferredAllocation []string
443+
expectedAlignment map[int]int
441444
}{
442445
{
443446
description: "Single Request, no alignment",
@@ -583,6 +586,152 @@ func TestTopologyAlignedAllocation(t *testing.T) {
583586
},
584587
expectedAlignment: map[int]int{1: 2, 3: 2},
585588
},
589+
{
590+
description: "Request for 5, socket 0, preferred aligned accepted",
591+
resource: "resource",
592+
request: 5,
593+
devices: func() []pluginapi.Device {
594+
devices := []pluginapi.Device{}
595+
for i := 0; i < 100; i++ {
596+
id := fmt.Sprintf("Dev%d", i)
597+
devices = append(devices, makeNUMADevice(id, 0))
598+
}
599+
for i := 100; i < 200; i++ {
600+
id := fmt.Sprintf("Dev%d", i)
601+
devices = append(devices, makeNUMADevice(id, 1))
602+
}
603+
return devices
604+
}(),
605+
hint: topologymanager.TopologyHint{
606+
NUMANodeAffinity: makeSocketMask(0),
607+
Preferred: true,
608+
},
609+
getPreferredAllocationFunc: func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error) {
610+
return &pluginapi.PreferredAllocationResponse{
611+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
612+
{DeviceIDs: []string{"Dev0", "Dev19", "Dev83", "Dev42", "Dev77"}},
613+
},
614+
}, nil
615+
},
616+
expectedPreferredAllocation: []string{"Dev0", "Dev19", "Dev83", "Dev42", "Dev77"},
617+
expectedAlignment: map[int]int{0: 5},
618+
},
619+
{
620+
description: "Request for 5, socket 0, preferred aligned accepted, unaligned ignored",
621+
resource: "resource",
622+
request: 5,
623+
devices: func() []pluginapi.Device {
624+
devices := []pluginapi.Device{}
625+
for i := 0; i < 100; i++ {
626+
id := fmt.Sprintf("Dev%d", i)
627+
devices = append(devices, makeNUMADevice(id, 0))
628+
}
629+
for i := 100; i < 200; i++ {
630+
id := fmt.Sprintf("Dev%d", i)
631+
devices = append(devices, makeNUMADevice(id, 1))
632+
}
633+
return devices
634+
}(),
635+
hint: topologymanager.TopologyHint{
636+
NUMANodeAffinity: makeSocketMask(0),
637+
Preferred: true,
638+
},
639+
getPreferredAllocationFunc: func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error) {
640+
return &pluginapi.PreferredAllocationResponse{
641+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
642+
{DeviceIDs: []string{"Dev0", "Dev19", "Dev83", "Dev150", "Dev186"}},
643+
},
644+
}, nil
645+
},
646+
expectedPreferredAllocation: []string{"Dev0", "Dev19", "Dev83"},
647+
expectedAlignment: map[int]int{0: 5},
648+
},
649+
{
650+
description: "Request for 5, socket 1, preferred aligned accepted, bogus ignored",
651+
resource: "resource",
652+
request: 5,
653+
devices: func() []pluginapi.Device {
654+
devices := []pluginapi.Device{}
655+
for i := 0; i < 100; i++ {
656+
id := fmt.Sprintf("Dev%d", i)
657+
devices = append(devices, makeNUMADevice(id, 1))
658+
}
659+
return devices
660+
}(),
661+
hint: topologymanager.TopologyHint{
662+
NUMANodeAffinity: makeSocketMask(1),
663+
Preferred: true,
664+
},
665+
getPreferredAllocationFunc: func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error) {
666+
return &pluginapi.PreferredAllocationResponse{
667+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
668+
{DeviceIDs: []string{"Dev0", "Dev19", "Dev83", "bogus0", "bogus1"}},
669+
},
670+
}, nil
671+
},
672+
expectedPreferredAllocation: []string{"Dev0", "Dev19", "Dev83"},
673+
expectedAlignment: map[int]int{1: 5},
674+
},
675+
{
676+
description: "Request for 5, multisocket, preferred accepted",
677+
resource: "resource",
678+
request: 5,
679+
devices: func() []pluginapi.Device {
680+
devices := []pluginapi.Device{}
681+
for i := 0; i < 3; i++ {
682+
id := fmt.Sprintf("Dev%d", i)
683+
devices = append(devices, makeNUMADevice(id, 0))
684+
}
685+
for i := 3; i < 100; i++ {
686+
id := fmt.Sprintf("Dev%d", i)
687+
devices = append(devices, makeNUMADevice(id, 1))
688+
}
689+
return devices
690+
}(),
691+
hint: topologymanager.TopologyHint{
692+
NUMANodeAffinity: makeSocketMask(0),
693+
Preferred: true,
694+
},
695+
getPreferredAllocationFunc: func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error) {
696+
return &pluginapi.PreferredAllocationResponse{
697+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
698+
{DeviceIDs: []string{"Dev0", "Dev1", "Dev2", "Dev42", "Dev83"}},
699+
},
700+
}, nil
701+
},
702+
expectedPreferredAllocation: []string{"Dev0", "Dev1", "Dev2", "Dev42", "Dev83"},
703+
expectedAlignment: map[int]int{0: 3, 1: 2},
704+
},
705+
{
706+
description: "Request for 5, multisocket, preferred unaligned accepted, bogus ignored",
707+
resource: "resource",
708+
request: 5,
709+
devices: func() []pluginapi.Device {
710+
devices := []pluginapi.Device{}
711+
for i := 0; i < 3; i++ {
712+
id := fmt.Sprintf("Dev%d", i)
713+
devices = append(devices, makeNUMADevice(id, 0))
714+
}
715+
for i := 3; i < 100; i++ {
716+
id := fmt.Sprintf("Dev%d", i)
717+
devices = append(devices, makeNUMADevice(id, 1))
718+
}
719+
return devices
720+
}(),
721+
hint: topologymanager.TopologyHint{
722+
NUMANodeAffinity: makeSocketMask(0),
723+
Preferred: true,
724+
},
725+
getPreferredAllocationFunc: func(available, mustInclude []string, size int) (*pluginapi.PreferredAllocationResponse, error) {
726+
return &pluginapi.PreferredAllocationResponse{
727+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
728+
{DeviceIDs: []string{"Dev0", "Dev1", "Dev2", "Dev42", "bogus0"}},
729+
},
730+
}, nil
731+
},
732+
expectedPreferredAllocation: []string{"Dev0", "Dev1", "Dev2", "Dev42"},
733+
expectedAlignment: map[int]int{0: 3, 1: 2},
734+
},
586735
}
587736
for _, tc := range tcases {
588737
m := ManagerImpl{
@@ -605,6 +754,15 @@ func TestTopologyAlignedAllocation(t *testing.T) {
605754
m.healthyDevices[tc.resource].Insert(d.ID)
606755
}
607756

757+
if tc.getPreferredAllocationFunc != nil {
758+
m.endpoints[tc.resource] = endpointInfo{
759+
e: &MockEndpoint{
760+
getPreferredAllocationFunc: tc.getPreferredAllocationFunc,
761+
},
762+
opts: &pluginapi.DevicePluginOptions{GetPreferredAllocationAvailable: true},
763+
}
764+
}
765+
608766
allocated, err := m.devicesToAllocate("podUID", "containerName", tc.resource, tc.request, sets.NewString())
609767
if err != nil {
610768
t.Errorf("Unexpected error: %v", err)
@@ -615,6 +773,10 @@ func TestTopologyAlignedAllocation(t *testing.T) {
615773
t.Errorf("%v. expected allocation size: %v but got: %v", tc.description, tc.request, len(allocated))
616774
}
617775

776+
if !allocated.HasAll(tc.expectedPreferredAllocation...) {
777+
t.Errorf("%v. expected preferred allocation: %v but not present in: %v", tc.description, tc.expectedPreferredAllocation, allocated.UnsortedList())
778+
}
779+
618780
alignment := make(map[int]int)
619781
if m.deviceHasTopologyAlignment(tc.resource) {
620782
for d := range allocated {

0 commit comments

Comments
 (0)