@@ -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
@@ -93,11 +101,39 @@ func (pp *PrebindPlugin) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeN
93
101
return nil
94
102
}
95
103
104
+ // reset used to reset numPrebindCalled.
105
+ func (pp * PrebindPlugin ) reset () {
106
+ pp .numPrebindCalled = 0
107
+ }
108
+
96
109
// NewPrebindPlugin is the factory for prebind plugin.
97
110
func NewPrebindPlugin (_ * runtime.Unknown , _ framework.FrameworkHandle ) (framework.Plugin , error ) {
98
111
return pbdPlugin , nil
99
112
}
100
113
114
+ var unresPlugin = & UnreservePlugin {}
115
+
116
+ // Name returns name of the plugin.
117
+ func (up * UnreservePlugin ) Name () string {
118
+ return unreservePluginName
119
+ }
120
+
121
+ // Unreserve is a test function that returns an error or nil, depending on the
122
+ // value of "failUnreserve".
123
+ func (up * UnreservePlugin ) Unreserve (pc * framework.PluginContext , pod * v1.Pod , nodeName string ) {
124
+ up .numUnreserveCalled ++
125
+ }
126
+
127
+ // reset used to reset numUnreserveCalled.
128
+ func (up * UnreservePlugin ) reset () {
129
+ up .numUnreserveCalled = 0
130
+ }
131
+
132
+ // NewUnreservePlugin is the factory for unreserve plugin.
133
+ func NewUnreservePlugin (_ * runtime.Unknown , _ framework.FrameworkHandle ) (framework.Plugin , error ) {
134
+ return unresPlugin , nil
135
+ }
136
+
101
137
// TestReservePlugin tests invocation of reserve plugins.
102
138
func TestReservePlugin (t * testing.T ) {
103
139
// Create a plugin registry for testing. Register only a reserve plugin.
@@ -216,3 +252,87 @@ func TestPrebindPlugin(t *testing.T) {
216
252
cleanupPods (cs , t , []* v1.Pod {pod })
217
253
}
218
254
}
255
+
256
+ // TestUnreservePlugin tests invocation of un-reserve plugin
257
+ func TestUnreservePlugin (t * testing.T ) {
258
+ // TODO: register more plugin which would trigger un-reserve plugin
259
+ registry := framework.Registry {
260
+ unreservePluginName : NewUnreservePlugin ,
261
+ prebindPluginName : NewPrebindPlugin ,
262
+ }
263
+
264
+ // Create the master and the scheduler with the test plugin set.
265
+ context := initTestSchedulerWithOptions (t ,
266
+ initTestMaster (t , "unreserve-plugin" , nil ),
267
+ false , nil , registry , false , time .Second )
268
+ defer cleanupTest (t , context )
269
+
270
+ cs := context .clientSet
271
+ // Add a few nodes.
272
+ _ , err := createNodes (cs , "test-node" , nil , 2 )
273
+ if err != nil {
274
+ t .Fatalf ("Cannot create nodes: %v" , err )
275
+ }
276
+
277
+ tests := []struct {
278
+ prebindFail bool
279
+ prebindReject bool
280
+ }{
281
+ {
282
+ prebindFail : false ,
283
+ prebindReject : false ,
284
+ },
285
+ {
286
+ prebindFail : true ,
287
+ prebindReject : false ,
288
+ },
289
+ {
290
+ prebindFail : false ,
291
+ prebindReject : true ,
292
+ },
293
+ {
294
+ prebindFail : true ,
295
+ prebindReject : true ,
296
+ },
297
+ }
298
+
299
+ for i , test := range tests {
300
+ pbdPlugin .failPrebind = test .prebindFail
301
+ pbdPlugin .rejectPrebind = test .prebindReject
302
+
303
+ // Create a best effort pod.
304
+ pod , err := createPausePod (cs ,
305
+ initPausePod (cs , & pausePodConfig {Name : "test-pod" , Namespace : context .ns .Name }))
306
+ if err != nil {
307
+ t .Errorf ("Error while creating a test pod: %v" , err )
308
+ }
309
+
310
+ if test .prebindFail {
311
+ if err = wait .Poll (10 * time .Millisecond , 30 * time .Second , podSchedulingError (cs , pod .Namespace , pod .Name )); err != nil {
312
+ t .Errorf ("test #%v: Expected a scheduling error, but didn't get it. error: %v" , i , err )
313
+ }
314
+ if unresPlugin .numUnreserveCalled == 0 || unresPlugin .numUnreserveCalled != pbdPlugin .numPrebindCalled {
315
+ t .Errorf ("test #%v: Expected the unreserve plugin to be called %d times, was called %d times." , i , pbdPlugin .numPrebindCalled , unresPlugin .numUnreserveCalled )
316
+ }
317
+ } else {
318
+ if test .prebindReject {
319
+ if err = waitForPodUnschedulable (cs , pod ); err != nil {
320
+ t .Errorf ("test #%v: Didn't expected the pod to be scheduled. error: %v" , i , err )
321
+ }
322
+ if unresPlugin .numUnreserveCalled == 0 || unresPlugin .numUnreserveCalled != pbdPlugin .numPrebindCalled {
323
+ t .Errorf ("test #%v: Expected the unreserve plugin to be called %d times, was called %d times." , i , pbdPlugin .numPrebindCalled , unresPlugin .numUnreserveCalled )
324
+ }
325
+ } else {
326
+ if err = waitForPodToSchedule (cs , pod ); err != nil {
327
+ t .Errorf ("test #%v: Expected the pod to be scheduled. error: %v" , i , err )
328
+ }
329
+ if unresPlugin .numUnreserveCalled > 0 {
330
+ t .Errorf ("test #%v: Didn't expected the unreserve plugin to be called, was called %d times." , i , unresPlugin .numUnreserveCalled )
331
+ }
332
+ }
333
+ }
334
+ unresPlugin .reset ()
335
+ pbdPlugin .reset ()
336
+ cleanupPods (cs , t , []* v1.Pod {pod })
337
+ }
338
+ }
0 commit comments