diff --git a/pkg/controllers/queue/queue_controller_test.go b/pkg/controllers/queue/queue_controller_test.go new file mode 100644 index 0000000000..073f9b4bbd --- /dev/null +++ b/pkg/controllers/queue/queue_controller_test.go @@ -0,0 +1,279 @@ +/* +Copyright 2019 The Volcano Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package queue + +import ( + "testing" + + kbv1alpha1 "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1" + kubebatchclient "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned/fake" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubeclient "k8s.io/client-go/kubernetes/fake" + "k8s.io/client-go/tools/cache" +) + +func newFakeController() *Controller { + KubeBatchClientSet := kubebatchclient.NewSimpleClientset() + KubeClientSet := kubeclient.NewSimpleClientset() + + controller := NewQueueController(KubeClientSet, KubeBatchClientSet) + return controller +} + +func TestAddQueue(t *testing.T) { + testCases := []struct { + Name string + queue *kbv1alpha1.Queue + ExpectValue int + }{ + { + Name: "AddQueue", + queue: &kbv1alpha1.Queue{ + ObjectMeta: metav1.ObjectMeta{ + Name: "c1", + }, + Spec: kbv1alpha1.QueueSpec{ + Weight: 1, + }, + }, + ExpectValue: 1, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + + c.addQueue(testcase.queue) + + if testcase.ExpectValue != c.queue.Len() { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len()) + } + } +} + +func TestDeleteQueue(t *testing.T) { + testCases := []struct { + Name string + queue *kbv1alpha1.Queue + ExpectValue bool + }{ + { + Name: "DeleteQueue", + queue: &kbv1alpha1.Queue{ + ObjectMeta: metav1.ObjectMeta{ + Name: "c1", + }, + Spec: kbv1alpha1.QueueSpec{ + Weight: 1, + }, + }, + ExpectValue: false, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + c.podGroups[testcase.queue.Name] = make(map[string]struct{}) + + c.deleteQueue(testcase.queue) + + if _, ok := c.podGroups[testcase.queue.Name]; ok != testcase.ExpectValue { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok) + } + } + +} + +func TestAddPodGroup(t *testing.T) { + namespace := "c1" + + testCases := []struct { + Name string + podGroup *kbv1alpha1.PodGroup + ExpectValue int + }{ + { + Name: "addpodgroup", + podGroup: &kbv1alpha1.PodGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pg1", + Namespace: namespace, + }, + Spec: kbv1alpha1.PodGroupSpec{ + Queue: "c1", + }, + }, + ExpectValue: 1, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + + c.addPodGroup(testcase.podGroup) + + if testcase.ExpectValue != c.queue.Len() { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len()) + } + if testcase.ExpectValue != len(c.podGroups[testcase.podGroup.Spec.Queue]) { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, len(c.podGroups[testcase.podGroup.Spec.Queue])) + } + } + +} + +func TestDeletePodGroup(t *testing.T) { + namespace := "c1" + + testCases := []struct { + Name string + podGroup *kbv1alpha1.PodGroup + ExpectValue bool + }{ + { + Name: "deletepodgroup", + podGroup: &kbv1alpha1.PodGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pg1", + Namespace: namespace, + }, + Spec: kbv1alpha1.PodGroupSpec{ + Queue: "c1", + }, + }, + ExpectValue: false, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + + key, _ := cache.MetaNamespaceKeyFunc(testcase.podGroup) + c.podGroups[testcase.podGroup.Spec.Queue] = make(map[string]struct{}) + c.podGroups[testcase.podGroup.Spec.Queue][key] = struct{}{} + + c.deletePodGroup(testcase.podGroup) + if _, ok := c.podGroups[testcase.podGroup.Spec.Queue][key]; ok != testcase.ExpectValue { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok) + } + } +} + +func TestUpdatePodGroup(t *testing.T) { + namespace := "c1" + + testCases := []struct { + Name string + podGroupold *kbv1alpha1.PodGroup + podGroupnew *kbv1alpha1.PodGroup + ExpectValue int + }{ + { + Name: "updatepodgroup", + podGroupold: &kbv1alpha1.PodGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pg1", + Namespace: namespace, + }, + Spec: kbv1alpha1.PodGroupSpec{ + Queue: "c1", + }, + Status: kbv1alpha1.PodGroupStatus{ + Phase: kbv1alpha1.PodGroupPending, + }, + }, + podGroupnew: &kbv1alpha1.PodGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pg1", + Namespace: namespace, + }, + Spec: kbv1alpha1.PodGroupSpec{ + Queue: "c1", + }, + Status: kbv1alpha1.PodGroupStatus{ + Phase: kbv1alpha1.PodGroupRunning, + }, + }, + ExpectValue: 1, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + + c.updatePodGroup(testcase.podGroupold, testcase.podGroupnew) + + if testcase.ExpectValue != c.queue.Len() { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len()) + } + } +} + +func TestSyncQueue(t *testing.T) { + namespace := "c1" + + testCases := []struct { + Name string + podGroup *kbv1alpha1.PodGroup + queue *kbv1alpha1.Queue + ExpectValue int32 + }{ + { + Name: "syncQueue", + podGroup: &kbv1alpha1.PodGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pg1", + Namespace: namespace, + }, + Spec: kbv1alpha1.PodGroupSpec{ + Queue: "c1", + }, + Status: kbv1alpha1.PodGroupStatus{ + Phase: kbv1alpha1.PodGroupPending, + }, + }, + queue: &kbv1alpha1.Queue{ + ObjectMeta: metav1.ObjectMeta{ + Name: "c1", + }, + Spec: kbv1alpha1.QueueSpec{ + Weight: 1, + }, + }, + ExpectValue: 1, + }, + } + + for i, testcase := range testCases { + c := newFakeController() + + key, _ := cache.MetaNamespaceKeyFunc(testcase.podGroup) + c.podGroups[testcase.podGroup.Spec.Queue] = make(map[string]struct{}) + c.podGroups[testcase.podGroup.Spec.Queue][key] = struct{}{} + + c.pgInformer.Informer().GetIndexer().Add(testcase.podGroup) + c.queueInformer.Informer().GetIndexer().Add(testcase.queue) + c.kbClient.SchedulingV1alpha1().Queues().Create(testcase.queue) + + err := c.syncQueue(testcase.queue.Name) + item, _ := c.kbClient.SchedulingV1alpha1().Queues().Get(testcase.queue.Name, metav1.GetOptions{}) + if err != nil && testcase.ExpectValue != item.Status.Pending { + t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len()) + } + } + +}