@@ -18,23 +18,24 @@ package scheduler
18
18
19
19
import (
20
20
"fmt"
21
- "k8s.io/apimachinery/pkg/runtime"
22
21
"testing"
23
22
"time"
24
23
25
24
"k8s.io/api/core/v1"
25
+ "k8s.io/apimachinery/pkg/runtime"
26
26
"k8s.io/apimachinery/pkg/util/wait"
27
27
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
28
28
)
29
29
30
30
// TesterPlugin is common ancestor for a test plugin that allows injection of
31
31
// failures and some other test functionalities.
32
32
type TesterPlugin struct {
33
- numReserveCalled int
34
- numPrebindCalled int
35
- failReserve bool
36
- failPrebind bool
37
- rejectPrebind bool
33
+ numReserveCalled int
34
+ numPrebindCalled int
35
+ numUnreserveCalled int
36
+ failReserve bool
37
+ failPrebind bool
38
+ rejectPrebind bool
38
39
}
39
40
40
41
type ReservePlugin struct {
@@ -45,19 +46,27 @@ type PrebindPlugin struct {
45
46
TesterPlugin
46
47
}
47
48
49
+ type UnreservePlugin struct {
50
+ TesterPlugin
51
+ }
52
+
48
53
const (
49
- reservePluginName = "reserve-plugin"
50
- prebindPluginName = "prebind-plugin"
54
+ reservePluginName = "reserve-plugin"
55
+ prebindPluginName = "prebind-plugin"
56
+ unreservePluginName = "unreserve-plugin"
51
57
)
52
58
53
59
var _ = framework .ReservePlugin (& ReservePlugin {})
54
60
var _ = framework .PrebindPlugin (& PrebindPlugin {})
61
+ var _ = framework .UnreservePlugin (& UnreservePlugin {})
55
62
56
63
// Name returns name of the plugin.
57
64
func (rp * ReservePlugin ) Name () string {
58
65
return reservePluginName
59
66
}
60
67
68
+ var resPlugin = & ReservePlugin {}
69
+
61
70
// Reserve is a test function that returns an error or nil, depending on the
62
71
// value of "failReserve".
63
72
func (rp * ReservePlugin ) Reserve (pc * framework.PluginContext , pod * v1.Pod , nodeName string ) * framework.Status {
@@ -68,14 +77,13 @@ func (rp *ReservePlugin) Reserve(pc *framework.PluginContext, pod *v1.Pod, nodeN
68
77
return nil
69
78
}
70
79
71
- var resPlugin = & ReservePlugin {}
72
- var pbdPlugin = & PrebindPlugin {}
73
-
74
80
// NewReservePlugin is the factory for reserve plugin.
75
81
func NewReservePlugin (_ * runtime.Unknown , _ framework.FrameworkHandle ) (framework.Plugin , error ) {
76
82
return resPlugin , nil
77
83
}
78
84
85
+ var pbdPlugin = & PrebindPlugin {}
86
+
79
87
// Name returns name of the plugin.
80
88
func (pp * PrebindPlugin ) Name () string {
81
89
return prebindPluginName
@@ -98,6 +106,29 @@ func NewPrebindPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framewor
98
106
return pbdPlugin , nil
99
107
}
100
108
109
+ var unresPlugin = & UnreservePlugin {}
110
+
111
+ // Name returns name of the plugin.
112
+ func (up * UnreservePlugin ) Name () string {
113
+ return unreservePluginName
114
+ }
115
+
116
+ // Unreserve is a test function that returns an error or nil, depending on the
117
+ // value of "failUnreserve".
118
+ func (up * UnreservePlugin ) Unreserve (pc * framework.PluginContext , pod * v1.Pod , nodeName string ) {
119
+ up .numUnreserveCalled ++
120
+ }
121
+
122
+ // reset used to reset numUnreserveCalled.
123
+ func (up * UnreservePlugin ) reset () {
124
+ up .numUnreserveCalled = 0
125
+ }
126
+
127
+ // NewUnreservePlugin is the factory for unreserve plugin.
128
+ func NewUnreservePlugin (_ * runtime.Unknown , _ framework.FrameworkHandle ) (framework.Plugin , error ) {
129
+ return unresPlugin , nil
130
+ }
131
+
101
132
// TestReservePlugin tests invocation of reserve plugins.
102
133
func TestReservePlugin (t * testing.T ) {
103
134
// Create a plugin registry for testing. Register only a reserve plugin.
@@ -216,3 +247,86 @@ func TestPrebindPlugin(t *testing.T) {
216
247
cleanupPods (cs , t , []* v1.Pod {pod })
217
248
}
218
249
}
250
+
251
+ // TestUnreservePlugin tests invocation of un-reserve plugin
252
+ func TestUnreservePlugin (t * testing.T ) {
253
+ // TODO: register more plugin which would trigger un-reserve plugin
254
+ registry := framework.Registry {
255
+ unreservePluginName : NewUnreservePlugin ,
256
+ prebindPluginName : NewPrebindPlugin ,
257
+ }
258
+
259
+ // Create the master and the scheduler with the test plugin set.
260
+ context := initTestSchedulerWithOptions (t ,
261
+ initTestMaster (t , "unreserve-plugin" , nil ),
262
+ false , nil , registry , false , time .Second )
263
+ defer cleanupTest (t , context )
264
+
265
+ cs := context .clientSet
266
+ // Add a few nodes.
267
+ _ , err := createNodes (cs , "test-node" , nil , 2 )
268
+ if err != nil {
269
+ t .Fatalf ("Cannot create nodes: %v" , err )
270
+ }
271
+
272
+ tests := []struct {
273
+ prebindFail bool
274
+ prebindReject bool
275
+ }{
276
+ {
277
+ prebindFail : false ,
278
+ prebindReject : false ,
279
+ },
280
+ {
281
+ prebindFail : true ,
282
+ prebindReject : false ,
283
+ },
284
+ {
285
+ prebindFail : false ,
286
+ prebindReject : true ,
287
+ },
288
+ {
289
+ prebindFail : true ,
290
+ prebindReject : true ,
291
+ },
292
+ }
293
+
294
+ for i , test := range tests {
295
+ pbdPlugin .failPrebind = test .prebindFail
296
+ pbdPlugin .rejectPrebind = test .prebindReject
297
+
298
+ // Create a best effort pod.
299
+ pod , err := createPausePod (cs ,
300
+ initPausePod (cs , & pausePodConfig {Name : "test-pod" , Namespace : context .ns .Name }))
301
+ if err != nil {
302
+ t .Errorf ("Error while creating a test pod: %v" , err )
303
+ }
304
+
305
+ if test .prebindFail {
306
+ if err = wait .Poll (10 * time .Millisecond , 30 * time .Second , podSchedulingError (cs , pod .Namespace , pod .Name )); err != nil {
307
+ t .Errorf ("test #%v: Expected a scheduling error, but didn't get it. error: %v" , i , err )
308
+ }
309
+ if unresPlugin .numUnreserveCalled != 1 {
310
+ t .Errorf ("test #%v: Expected the unreserve plugin to be called only once." , i )
311
+ }
312
+ } else {
313
+ if test .prebindReject {
314
+ if err = waitForPodUnschedulable (cs , pod ); err != nil {
315
+ t .Errorf ("test #%v: Didn't expected the pod to be scheduled. error: %v" , i , err )
316
+ }
317
+ if unresPlugin .numUnreserveCalled != 1 {
318
+ t .Errorf ("test #%v: Expected the unreserve plugin to be called only once." , i )
319
+ }
320
+ } else {
321
+ if err = waitForPodToSchedule (cs , pod ); err != nil {
322
+ t .Errorf ("test #%v: Expected the pod to be scheduled. error: %v" , i , err )
323
+ }
324
+ if unresPlugin .numUnreserveCalled > 0 {
325
+ t .Errorf ("test #%v: Didn't expected the unreserve plugin to be called." , i )
326
+ }
327
+ }
328
+ }
329
+ unresPlugin .reset ()
330
+ cleanupPods (cs , t , []* v1.Pod {pod })
331
+ }
332
+ }
0 commit comments