@@ -42,9 +42,10 @@ func TestEviction(t *testing.T) {
42
42
testcases := []struct {
43
43
name string
44
44
pdbs []runtime.Object
45
- pod * api.Pod
46
45
eviction * policy.Eviction
47
46
47
+ badNameInURL bool
48
+
48
49
expectError bool
49
50
expectDeleted bool
50
51
}{
@@ -55,12 +56,6 @@ func TestEviction(t *testing.T) {
55
56
Spec : policyv1beta1.PodDisruptionBudgetSpec {Selector : & metav1.LabelSelector {MatchLabels : map [string ]string {"a" : "true" }}},
56
57
Status : policyv1beta1.PodDisruptionBudgetStatus {PodDisruptionsAllowed : 0 },
57
58
}},
58
- pod : func () * api.Pod {
59
- pod := validNewPod ()
60
- pod .Labels = map [string ]string {"a" : "true" }
61
- pod .Spec .NodeName = "foo"
62
- return pod
63
- }(),
64
59
eviction : & policy.Eviction {ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" }, DeleteOptions : metav1 .NewDeleteOptions (0 )},
65
60
expectError : true ,
66
61
},
@@ -71,12 +66,6 @@ func TestEviction(t *testing.T) {
71
66
Spec : policyv1beta1.PodDisruptionBudgetSpec {Selector : & metav1.LabelSelector {MatchLabels : map [string ]string {"a" : "true" }}},
72
67
Status : policyv1beta1.PodDisruptionBudgetStatus {PodDisruptionsAllowed : 1 },
73
68
}},
74
- pod : func () * api.Pod {
75
- pod := validNewPod ()
76
- pod .Labels = map [string ]string {"a" : "true" }
77
- pod .Spec .NodeName = "foo"
78
- return pod
79
- }(),
80
69
eviction : & policy.Eviction {ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" }, DeleteOptions : metav1 .NewDeleteOptions (0 )},
81
70
expectDeleted : true ,
82
71
},
@@ -87,15 +76,20 @@ func TestEviction(t *testing.T) {
87
76
Spec : policyv1beta1.PodDisruptionBudgetSpec {Selector : & metav1.LabelSelector {MatchLabels : map [string ]string {"b" : "true" }}},
88
77
Status : policyv1beta1.PodDisruptionBudgetStatus {PodDisruptionsAllowed : 0 },
89
78
}},
90
- pod : func () * api.Pod {
91
- pod := validNewPod ()
92
- pod .Labels = map [string ]string {"a" : "true" }
93
- pod .Spec .NodeName = "foo"
94
- return pod
95
- }(),
96
79
eviction : & policy.Eviction {ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" }, DeleteOptions : metav1 .NewDeleteOptions (0 )},
97
80
expectDeleted : true ,
98
81
},
82
+ {
83
+ name : "matching pdbs with disruptions allowed but bad name in Url" ,
84
+ pdbs : []runtime.Object {& policyv1beta1.PodDisruptionBudget {
85
+ ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" },
86
+ Spec : policyv1beta1.PodDisruptionBudgetSpec {Selector : & metav1.LabelSelector {MatchLabels : map [string ]string {"a" : "true" }}},
87
+ Status : policyv1beta1.PodDisruptionBudgetStatus {PodDisruptionsAllowed : 1 },
88
+ }},
89
+ badNameInURL : true ,
90
+ eviction : & policy.Eviction {ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" }, DeleteOptions : metav1 .NewDeleteOptions (0 )},
91
+ expectError : true ,
92
+ },
99
93
}
100
94
101
95
for _ , tc := range testcases {
@@ -104,43 +98,58 @@ func TestEviction(t *testing.T) {
104
98
storage , _ , _ , server := newStorage (t )
105
99
defer server .Terminate (t )
106
100
defer storage .Store .DestroyFunc ()
107
- if tc .pod != nil {
108
- if _ , err := storage .Create (testContext , tc .pod , nil , & metav1.CreateOptions {}); err != nil {
109
- t .Error (err )
110
- }
101
+
102
+ pod := validNewPod ()
103
+ pod .Labels = map [string ]string {"a" : "true" }
104
+ pod .Spec .NodeName = "foo"
105
+
106
+ if _ , err := storage .Create (testContext , pod , nil , & metav1.CreateOptions {}); err != nil {
107
+ t .Error (err )
111
108
}
112
109
113
110
client := fake .NewSimpleClientset (tc .pdbs ... )
114
111
evictionRest := newEvictionStorage (storage .Store , client .PolicyV1beta1 ())
115
- _ , err := evictionRest .Create (testContext , tc .eviction , nil , & metav1.CreateOptions {})
112
+
113
+ name := pod .Name
114
+ if tc .badNameInURL {
115
+ name += "bad-name"
116
+ }
117
+ _ , err := evictionRest .Create (testContext , name , tc .eviction , nil , & metav1.CreateOptions {})
116
118
if (err != nil ) != tc .expectError {
117
119
t .Errorf ("expected error=%v, got %v" , tc .expectError , err )
118
120
return
119
121
}
122
+ if tc .badNameInURL {
123
+ if err == nil {
124
+ t .Error ("expected error here, but got nil" )
125
+ return
126
+ }
127
+ if err .Error () != "name in URL does not match name in Eviction object" {
128
+ t .Errorf ("got unexpected error: %v" , err )
129
+ }
130
+ }
120
131
if tc .expectError {
121
132
return
122
133
}
123
134
124
- if tc .pod != nil {
125
- existingPod , err := storage .Get (testContext , tc .pod .Name , & metav1.GetOptions {})
126
- if tc .expectDeleted {
127
- if ! apierrors .IsNotFound (err ) {
128
- t .Errorf ("expected to be deleted, lookup returned %#v" , existingPod )
129
- }
130
- return
131
- } else if apierrors .IsNotFound (err ) {
132
- t .Errorf ("expected graceful deletion, got %v" , err )
133
- return
135
+ existingPod , err := storage .Get (testContext , pod .Name , & metav1.GetOptions {})
136
+ if tc .expectDeleted {
137
+ if ! apierrors .IsNotFound (err ) {
138
+ t .Errorf ("expected to be deleted, lookup returned %#v" , existingPod )
134
139
}
140
+ return
141
+ } else if apierrors .IsNotFound (err ) {
142
+ t .Errorf ("expected graceful deletion, got %v" , err )
143
+ return
144
+ }
135
145
136
- if err != nil {
137
- t .Errorf ("%#v" , err )
138
- return
139
- }
146
+ if err != nil {
147
+ t .Errorf ("%#v" , err )
148
+ return
149
+ }
140
150
141
- if existingPod .(* api.Pod ).DeletionTimestamp == nil {
142
- t .Errorf ("expected gracefully deleted pod with deletionTimestamp set, got %#v" , existingPod )
143
- }
151
+ if existingPod .(* api.Pod ).DeletionTimestamp == nil {
152
+ t .Errorf ("expected gracefully deleted pod with deletionTimestamp set, got %#v" , existingPod )
144
153
}
145
154
})
146
155
}
@@ -186,52 +195,27 @@ func TestEvictionDryRun(t *testing.T) {
186
195
name string
187
196
evictionOptions * metav1.DeleteOptions
188
197
requestOptions * metav1.CreateOptions
189
- pod * api.Pod
190
198
pdbs []runtime.Object
191
199
}{
192
200
{
193
201
name : "just request-options" ,
194
202
requestOptions : & metav1.CreateOptions {DryRun : []string {"All" }},
195
203
evictionOptions : & metav1.DeleteOptions {},
196
- pod : func () * api.Pod {
197
- pod := validNewPod ()
198
- pod .Labels = map [string ]string {"a" : "true" }
199
- pod .Spec .NodeName = "foo"
200
- return pod
201
- }(),
202
204
},
203
205
{
204
206
name : "just eviction-options" ,
205
207
requestOptions : & metav1.CreateOptions {},
206
208
evictionOptions : & metav1.DeleteOptions {DryRun : []string {"All" }},
207
- pod : func () * api.Pod {
208
- pod := validNewPod ()
209
- pod .Labels = map [string ]string {"a" : "true" }
210
- pod .Spec .NodeName = "foo"
211
- return pod
212
- }(),
213
209
},
214
210
{
215
211
name : "both options" ,
216
212
evictionOptions : & metav1.DeleteOptions {DryRun : []string {"All" }},
217
213
requestOptions : & metav1.CreateOptions {DryRun : []string {"All" }},
218
- pod : func () * api.Pod {
219
- pod := validNewPod ()
220
- pod .Labels = map [string ]string {"a" : "true" }
221
- pod .Spec .NodeName = "foo"
222
- return pod
223
- }(),
224
214
},
225
215
{
226
216
name : "with pdbs" ,
227
217
evictionOptions : & metav1.DeleteOptions {DryRun : []string {"All" }},
228
218
requestOptions : & metav1.CreateOptions {DryRun : []string {"All" }},
229
- pod : func () * api.Pod {
230
- pod := validNewPod ()
231
- pod .Labels = map [string ]string {"a" : "true" }
232
- pod .Spec .NodeName = "foo"
233
- return pod
234
- }(),
235
219
pdbs : []runtime.Object {& policyv1beta1.PodDisruptionBudget {
236
220
ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" },
237
221
Spec : policyv1beta1.PodDisruptionBudgetSpec {Selector : & metav1.LabelSelector {MatchLabels : map [string ]string {"a" : "true" }}},
@@ -257,7 +241,7 @@ func TestEvictionDryRun(t *testing.T) {
257
241
client := fake .NewSimpleClientset (tc .pdbs ... )
258
242
evictionRest := newEvictionStorage (storage .Store , client .PolicyV1beta1 ())
259
243
eviction := & policy.Eviction {ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" }, DeleteOptions : tc .evictionOptions }
260
- _ , err := evictionRest .Create (testContext , eviction , nil , tc .requestOptions )
244
+ _ , err := evictionRest .Create (testContext , pod . Name , eviction , nil , tc .requestOptions )
261
245
if err != nil {
262
246
t .Fatalf ("Failed to run eviction: %v" , err )
263
247
}
0 commit comments