@@ -175,6 +175,45 @@ func TestMaxProjectByRequester(t *testing.T) {
175
175
}
176
176
}
177
177
178
+ func TestProjectCountByRequester (t * testing.T ) {
179
+ pCache := fakeProjectCache (map [string ]projectCount {
180
+ "user1" : {1 , 5 }, // total 6, expect 4
181
+ "user2" : {5 , 1 }, // total 6, expect 5
182
+ "user3" : {1 , 0 }, // total 1, expect 1
183
+ })
184
+ reqLimit := & projectRequestLimit {
185
+ cache : pCache ,
186
+ }
187
+ tests := []struct {
188
+ user string
189
+ expect int
190
+ }{
191
+ {
192
+ user : "user1" ,
193
+ expect : 4 ,
194
+ },
195
+ {
196
+ user : "user2" ,
197
+ expect : 5 ,
198
+ },
199
+ {
200
+ user : "user3" ,
201
+ expect : 1 ,
202
+ },
203
+ }
204
+
205
+ for _ , test := range tests {
206
+ actual , err := reqLimit .projectCountByRequester (test .user )
207
+ if err != nil {
208
+ t .Errorf ("unexpected: %v" , err )
209
+ }
210
+ if actual != test .expect {
211
+ t .Errorf ("user %s got %d, expected %d" , test .user , actual , test .expect )
212
+ }
213
+ }
214
+
215
+ }
216
+
178
217
func TestAdmit (t * testing.T ) {
179
218
tests := []struct {
180
219
config * requestlimitapi.ProjectRequestLimitConfig
@@ -219,10 +258,11 @@ func TestAdmit(t *testing.T) {
219
258
}
220
259
221
260
for _ , tc := range tests {
222
- pCache := fakeProjectCache (map [string ]int {
223
- "user2" : 2 ,
224
- "user3" : 5 ,
225
- "user4" : 1 ,
261
+ pCache := fakeProjectCache (map [string ]projectCount {
262
+ "user1" : {0 , 1 },
263
+ "user2" : {2 , 2 },
264
+ "user3" : {5 , 3 },
265
+ "user4" : {1 , 0 },
226
266
})
227
267
client := & testclient.Fake {}
228
268
client .AddReactor ("get" , "users" , userFn (map [string ]labels.Set {
@@ -296,12 +336,15 @@ func configEquals(a, b *requestlimitapi.ProjectRequestLimitConfig) bool {
296
336
return true
297
337
}
298
338
299
- func fakeNs (name string ) * kapi.Namespace {
339
+ func fakeNs (name string , terminating bool ) * kapi.Namespace {
300
340
ns := & kapi.Namespace {}
301
341
ns .Name = kapi .SimpleNameGenerator .GenerateName ("testns" )
302
342
ns .Annotations = map [string ]string {
303
343
"openshift.io/requester" : name ,
304
344
}
345
+ if terminating {
346
+ ns .Status .Phase = kapi .NamespaceTerminating
347
+ }
305
348
return ns
306
349
}
307
350
@@ -312,12 +355,20 @@ func fakeUser(name string, labels map[string]string) *userapi.User {
312
355
return user
313
356
}
314
357
315
- func fakeProjectCache (requesters map [string ]int ) * projectcache.ProjectCache {
358
+ type projectCount struct {
359
+ active int
360
+ terminating int
361
+ }
362
+
363
+ func fakeProjectCache (requesters map [string ]projectCount ) * projectcache.ProjectCache {
316
364
kclient := & ktestclient.Fake {}
317
365
pCache := projectcache .NewFake (kclient .Namespaces (), projectcache .NewCacheStore (cache .MetaNamespaceKeyFunc ), "" )
318
366
for requester , count := range requesters {
319
- for i := 0 ; i < count ; i ++ {
320
- pCache .Store .Add (fakeNs (requester ))
367
+ for i := 0 ; i < count .active ; i ++ {
368
+ pCache .Store .Add (fakeNs (requester , false ))
369
+ }
370
+ for i := 0 ; i < count .terminating ; i ++ {
371
+ pCache .Store .Add (fakeNs (requester , true ))
321
372
}
322
373
}
323
374
return pCache
0 commit comments