@@ -103,55 +103,57 @@ func TestDevicePluginReRegistration(t *testing.T) {
103
103
{ID : "Dev3" , Health : pluginapi .Healthy },
104
104
}
105
105
for _ , preStartContainerFlag := range []bool {false , true } {
106
- m , ch , p1 := setup (t , devs , nil , socketName , pluginSocketName )
107
- p1 .Register (socketName , testResourceName , "" )
108
-
109
- select {
110
- case <- ch :
111
- case <- time .After (5 * time .Second ):
112
- t .Fatalf ("timeout while waiting for manager update" )
113
- }
114
- capacity , allocatable , _ := m .GetCapacity ()
115
- resourceCapacity := capacity [v1 .ResourceName (testResourceName )]
116
- resourceAllocatable := allocatable [v1 .ResourceName (testResourceName )]
117
- require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
118
- require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices are not updated." )
119
-
120
- p2 := NewDevicePluginStub (devs , pluginSocketName + ".new" , testResourceName , preStartContainerFlag )
121
- err = p2 .Start ()
122
- require .NoError (t , err )
123
- p2 .Register (socketName , testResourceName , "" )
124
-
125
- select {
126
- case <- ch :
127
- case <- time .After (5 * time .Second ):
128
- t .Fatalf ("timeout while waiting for manager update" )
129
- }
130
- capacity , allocatable , _ = m .GetCapacity ()
131
- resourceCapacity = capacity [v1 .ResourceName (testResourceName )]
132
- resourceAllocatable = allocatable [v1 .ResourceName (testResourceName )]
133
- require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
134
- require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices shouldn't change." )
135
-
136
- // Test the scenario that a plugin re-registers with different devices.
137
- p3 := NewDevicePluginStub (devsForRegistration , pluginSocketName + ".third" , testResourceName , preStartContainerFlag )
138
- err = p3 .Start ()
139
- require .NoError (t , err )
140
- p3 .Register (socketName , testResourceName , "" )
141
-
142
- select {
143
- case <- ch :
144
- case <- time .After (5 * time .Second ):
145
- t .Fatalf ("timeout while waiting for manager update" )
106
+ for _ , getPreferredAllocationFlag := range []bool {false , true } {
107
+ m , ch , p1 := setup (t , devs , nil , socketName , pluginSocketName )
108
+ p1 .Register (socketName , testResourceName , "" )
109
+
110
+ select {
111
+ case <- ch :
112
+ case <- time .After (5 * time .Second ):
113
+ t .Fatalf ("timeout while waiting for manager update" )
114
+ }
115
+ capacity , allocatable , _ := m .GetCapacity ()
116
+ resourceCapacity := capacity [v1 .ResourceName (testResourceName )]
117
+ resourceAllocatable := allocatable [v1 .ResourceName (testResourceName )]
118
+ require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
119
+ require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices are not updated." )
120
+
121
+ p2 := NewDevicePluginStub (devs , pluginSocketName + ".new" , testResourceName , preStartContainerFlag , getPreferredAllocationFlag )
122
+ err = p2 .Start ()
123
+ require .NoError (t , err )
124
+ p2 .Register (socketName , testResourceName , "" )
125
+
126
+ select {
127
+ case <- ch :
128
+ case <- time .After (5 * time .Second ):
129
+ t .Fatalf ("timeout while waiting for manager update" )
130
+ }
131
+ capacity , allocatable , _ = m .GetCapacity ()
132
+ resourceCapacity = capacity [v1 .ResourceName (testResourceName )]
133
+ resourceAllocatable = allocatable [v1 .ResourceName (testResourceName )]
134
+ require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
135
+ require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices shouldn't change." )
136
+
137
+ // Test the scenario that a plugin re-registers with different devices.
138
+ p3 := NewDevicePluginStub (devsForRegistration , pluginSocketName + ".third" , testResourceName , preStartContainerFlag , getPreferredAllocationFlag )
139
+ err = p3 .Start ()
140
+ require .NoError (t , err )
141
+ p3 .Register (socketName , testResourceName , "" )
142
+
143
+ select {
144
+ case <- ch :
145
+ case <- time .After (5 * time .Second ):
146
+ t .Fatalf ("timeout while waiting for manager update" )
147
+ }
148
+ capacity , allocatable , _ = m .GetCapacity ()
149
+ resourceCapacity = capacity [v1 .ResourceName (testResourceName )]
150
+ resourceAllocatable = allocatable [v1 .ResourceName (testResourceName )]
151
+ require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
152
+ require .Equal (t , int64 (1 ), resourceAllocatable .Value (), "Devices of plugin previously registered should be removed." )
153
+ p2 .Stop ()
154
+ p3 .Stop ()
155
+ cleanup (t , m , p1 )
146
156
}
147
- capacity , allocatable , _ = m .GetCapacity ()
148
- resourceCapacity = capacity [v1 .ResourceName (testResourceName )]
149
- resourceAllocatable = allocatable [v1 .ResourceName (testResourceName )]
150
- require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
151
- require .Equal (t , int64 (1 ), resourceAllocatable .Value (), "Devices of plugin previously registered should be removed." )
152
- p2 .Stop ()
153
- p3 .Stop ()
154
- cleanup (t , m , p1 )
155
157
}
156
158
}
157
159
@@ -186,7 +188,7 @@ func TestDevicePluginReRegistrationProbeMode(t *testing.T) {
186
188
require .Equal (t , resourceCapacity .Value (), resourceAllocatable .Value (), "capacity should equal to allocatable" )
187
189
require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices are not updated." )
188
190
189
- p2 := NewDevicePluginStub (devs , pluginSocketName + ".new" , testResourceName , false )
191
+ p2 := NewDevicePluginStub (devs , pluginSocketName + ".new" , testResourceName , false , false )
190
192
err = p2 .Start ()
191
193
require .NoError (t , err )
192
194
// Wait for the second callback to be issued.
@@ -203,7 +205,7 @@ func TestDevicePluginReRegistrationProbeMode(t *testing.T) {
203
205
require .Equal (t , int64 (2 ), resourceAllocatable .Value (), "Devices are not updated." )
204
206
205
207
// Test the scenario that a plugin re-registers with different devices.
206
- p3 := NewDevicePluginStub (devsForRegistration , pluginSocketName + ".third" , testResourceName , false )
208
+ p3 := NewDevicePluginStub (devsForRegistration , pluginSocketName + ".third" , testResourceName , false , false )
207
209
err = p3 .Start ()
208
210
require .NoError (t , err )
209
211
// Wait for the third callback to be issued.
@@ -249,7 +251,7 @@ func setupDeviceManager(t *testing.T, devs []*pluginapi.Device, callback monitor
249
251
}
250
252
251
253
func setupDevicePlugin (t * testing.T , devs []* pluginapi.Device , pluginSocketName string ) * Stub {
252
- p := NewDevicePluginStub (devs , pluginSocketName , testResourceName , false )
254
+ p := NewDevicePluginStub (devs , pluginSocketName , testResourceName , false , false )
253
255
err := p .Start ()
254
256
require .NoError (t , err )
255
257
return p
@@ -549,8 +551,9 @@ func (a *activePodsStub) updateActivePods(newPods []*v1.Pod) {
549
551
}
550
552
551
553
type MockEndpoint struct {
552
- allocateFunc func (devs []string ) (* pluginapi.AllocateResponse , error )
553
- initChan chan []string
554
+ getPreferredAllocationFunc func (available , mustInclude []string , size int ) (* pluginapi.PreferredAllocationResponse , error )
555
+ allocateFunc func (devs []string ) (* pluginapi.AllocateResponse , error )
556
+ initChan chan []string
554
557
}
555
558
556
559
func (m * MockEndpoint ) stop () {}
@@ -563,6 +566,13 @@ func (m *MockEndpoint) preStartContainer(devs []string) (*pluginapi.PreStartCont
563
566
return & pluginapi.PreStartContainerResponse {}, nil
564
567
}
565
568
569
+ func (m * MockEndpoint ) getPreferredAllocation (available , mustInclude []string , size int ) (* pluginapi.PreferredAllocationResponse , error ) {
570
+ if m .getPreferredAllocationFunc != nil {
571
+ return m .getPreferredAllocationFunc (available , mustInclude , size )
572
+ }
573
+ return nil , nil
574
+ }
575
+
566
576
func (m * MockEndpoint ) allocate (devs []string ) (* pluginapi.AllocateResponse , error ) {
567
577
if m .allocateFunc != nil {
568
578
return m .allocateFunc (devs )
0 commit comments