volcano.sh/volcano@v1.9.0/pkg/scheduler/api/namespace_info_test.go (about) 1 /* 2 Copyright 2018 The Volcano Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package api 17 18 import ( 19 "testing" 20 21 v1 "k8s.io/api/core/v1" 22 "k8s.io/apimachinery/pkg/api/resource" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 ) 25 26 func newQuota(name string, req int) *v1.ResourceQuota { 27 q := &v1.ResourceQuota{ 28 TypeMeta: metav1.TypeMeta{}, 29 ObjectMeta: metav1.ObjectMeta{ 30 Name: name, 31 }, 32 Spec: v1.ResourceQuotaSpec{ 33 Hard: make(v1.ResourceList), 34 }, 35 Status: v1.ResourceQuotaStatus{ 36 Hard: v1.ResourceList{ 37 v1.ResourceCPU: *resource.NewQuantity(int64(req), resource.DecimalSI), 38 }, 39 }, 40 } 41 42 return q 43 } 44 45 func TestNamespaceCollection(t *testing.T) { 46 c := NewNamespaceCollection("testCollection") 47 c.Update(newQuota("abc", 1)) 48 49 info := c.Snapshot() 50 req := info.QuotaStatus["abc"].Hard[v1.ResourceCPU] 51 if req.Value() != 1 { 52 t.Errorf("cpu request of quota %s should be %d, but got %d", "abc", 1, req.Value()) 53 } 54 55 c.Update(newQuota("abc", 2)) 56 c.Update(newQuota("def", 4)) 57 c.Update(newQuota("def", 8)) 58 c.Update(newQuota("ghi", 16)) 59 60 info = c.Snapshot() 61 req = info.QuotaStatus["abc"].Hard[v1.ResourceCPU] 62 if req.Value() != 2 { 63 t.Errorf("cpu request of quota %s should be %d, but got %d", "abc", 2, req.Value()) 64 } 65 66 c.Delete(newQuota("abc", 0)) 67 68 info = c.Snapshot() 69 if _, ok := info.QuotaStatus["abc"]; ok { 70 t.Errorf("QuotaStatus abc of namespace should not exist") 71 } 72 73 c.Delete(newQuota("abc", 0)) 74 c.Delete(newQuota("def", 0)) 75 c.Delete(newQuota("ghi", 0)) 76 }