Skip to content

Commit bb08fd1

Browse files
committed
Add a simple endpoint test for GetPreferredAllocation()
More extensive tests that exercise the allocation logic are to follow.
1 parent cbd405d commit bb08fd1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/kubelet/cm/devicemanager/endpoint_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,40 @@ func TestAllocate(t *testing.T) {
159159
require.Equal(t, resp, respOut)
160160
}
161161

162+
func TestGetPreferredAllocation(t *testing.T) {
163+
socket := path.Join("/tmp", esocketName)
164+
callbackCount := 0
165+
callbackChan := make(chan int)
166+
p, e := esetup(t, []*pluginapi.Device{}, socket, "mock", func(n string, d []pluginapi.Device) {
167+
callbackCount++
168+
callbackChan <- callbackCount
169+
})
170+
defer ecleanup(t, p, e)
171+
172+
resp := &pluginapi.PreferredAllocationResponse{
173+
ContainerResponses: []*pluginapi.ContainerPreferredAllocationResponse{
174+
{DeviceIDs: []string{"device0", "device1", "device2"}},
175+
},
176+
}
177+
178+
p.SetGetPreferredAllocFunc(func(r *pluginapi.PreferredAllocationRequest, devs map[string]pluginapi.Device) (*pluginapi.PreferredAllocationResponse, error) {
179+
return resp, nil
180+
})
181+
182+
go e.run()
183+
// Wait for the callback to be issued.
184+
select {
185+
case <-callbackChan:
186+
break
187+
case <-time.After(time.Second):
188+
t.FailNow()
189+
}
190+
191+
respOut, err := e.getPreferredAllocation([]string{}, []string{}, -1)
192+
require.NoError(t, err)
193+
require.Equal(t, resp, respOut)
194+
}
195+
162196
func esetup(t *testing.T, devs []*pluginapi.Device, socket, resourceName string, callback monitorCallback) (*Stub, *endpointImpl) {
163197
p := NewDevicePluginStub(devs, socket, resourceName, false, false)
164198

0 commit comments

Comments
 (0)