volcano.sh/volcano@v1.9.0/pkg/controllers/queue/queue_controller_test.go (about)

     1  /*
     2  Copyright 2019 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  
    17  package queue
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	kubeclient "k8s.io/client-go/kubernetes/fake"
    26  	"k8s.io/client-go/tools/cache"
    27  
    28  	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    29  	vcclient "volcano.sh/apis/pkg/client/clientset/versioned/fake"
    30  	"volcano.sh/volcano/pkg/controllers/framework"
    31  )
    32  
    33  func newFakeController() *queuecontroller {
    34  	KubeBatchClientSet := vcclient.NewSimpleClientset()
    35  	KubeClientSet := kubeclient.NewSimpleClientset()
    36  
    37  	controller := &queuecontroller{}
    38  	opt := framework.ControllerOption{
    39  		VolcanoClient: KubeBatchClientSet,
    40  		KubeClient:    KubeClientSet,
    41  	}
    42  
    43  	controller.Initialize(&opt)
    44  
    45  	return controller
    46  }
    47  
    48  func TestAddQueue(t *testing.T) {
    49  	testCases := []struct {
    50  		Name        string
    51  		queue       *schedulingv1beta1.Queue
    52  		ExpectValue int
    53  	}{
    54  		{
    55  			Name: "AddQueue",
    56  			queue: &schedulingv1beta1.Queue{
    57  				ObjectMeta: metav1.ObjectMeta{
    58  					Name: "c1",
    59  				},
    60  				Spec: schedulingv1beta1.QueueSpec{
    61  					Weight: 1,
    62  				},
    63  			},
    64  			ExpectValue: 1,
    65  		},
    66  	}
    67  
    68  	for i, testcase := range testCases {
    69  		c := newFakeController()
    70  
    71  		c.addQueue(testcase.queue)
    72  
    73  		if testcase.ExpectValue != c.queue.Len() {
    74  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len())
    75  		}
    76  	}
    77  }
    78  
    79  func TestDeleteQueue(t *testing.T) {
    80  	testCases := []struct {
    81  		Name        string
    82  		queue       *schedulingv1beta1.Queue
    83  		ExpectValue bool
    84  	}{
    85  		{
    86  			Name: "DeleteQueue",
    87  			queue: &schedulingv1beta1.Queue{
    88  				ObjectMeta: metav1.ObjectMeta{
    89  					Name: "c1",
    90  				},
    91  				Spec: schedulingv1beta1.QueueSpec{
    92  					Weight: 1,
    93  				},
    94  			},
    95  			ExpectValue: false,
    96  		},
    97  	}
    98  
    99  	for i, testcase := range testCases {
   100  		c := newFakeController()
   101  		c.podGroups[testcase.queue.Name] = make(map[string]struct{})
   102  
   103  		c.deleteQueue(testcase.queue)
   104  
   105  		if _, ok := c.podGroups[testcase.queue.Name]; ok != testcase.ExpectValue {
   106  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
   107  		}
   108  	}
   109  
   110  }
   111  
   112  func TestAddPodGroup(t *testing.T) {
   113  	namespace := "c1"
   114  
   115  	testCases := []struct {
   116  		Name        string
   117  		podGroup    *schedulingv1beta1.PodGroup
   118  		ExpectValue int
   119  	}{
   120  		{
   121  			Name: "addpodgroup",
   122  			podGroup: &schedulingv1beta1.PodGroup{
   123  				ObjectMeta: metav1.ObjectMeta{
   124  					Name:      "pg1",
   125  					Namespace: namespace,
   126  				},
   127  				Spec: schedulingv1beta1.PodGroupSpec{
   128  					Queue: "c1",
   129  				},
   130  			},
   131  			ExpectValue: 1,
   132  		},
   133  	}
   134  
   135  	for i, testcase := range testCases {
   136  		c := newFakeController()
   137  
   138  		c.addPodGroup(testcase.podGroup)
   139  
   140  		if testcase.ExpectValue != c.queue.Len() {
   141  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len())
   142  		}
   143  		if testcase.ExpectValue != len(c.podGroups[testcase.podGroup.Spec.Queue]) {
   144  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, len(c.podGroups[testcase.podGroup.Spec.Queue]))
   145  		}
   146  	}
   147  
   148  }
   149  
   150  func TestDeletePodGroup(t *testing.T) {
   151  	namespace := "c1"
   152  
   153  	testCases := []struct {
   154  		Name        string
   155  		podGroup    *schedulingv1beta1.PodGroup
   156  		ExpectValue bool
   157  	}{
   158  		{
   159  			Name: "deletepodgroup",
   160  			podGroup: &schedulingv1beta1.PodGroup{
   161  				ObjectMeta: metav1.ObjectMeta{
   162  					Name:      "pg1",
   163  					Namespace: namespace,
   164  				},
   165  				Spec: schedulingv1beta1.PodGroupSpec{
   166  					Queue: "c1",
   167  				},
   168  			},
   169  			ExpectValue: false,
   170  		},
   171  	}
   172  
   173  	for i, testcase := range testCases {
   174  		c := newFakeController()
   175  
   176  		key, _ := cache.MetaNamespaceKeyFunc(testcase.podGroup)
   177  		c.podGroups[testcase.podGroup.Spec.Queue] = make(map[string]struct{})
   178  		c.podGroups[testcase.podGroup.Spec.Queue][key] = struct{}{}
   179  
   180  		c.deletePodGroup(testcase.podGroup)
   181  		if _, ok := c.podGroups[testcase.podGroup.Spec.Queue][key]; ok != testcase.ExpectValue {
   182  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
   183  		}
   184  	}
   185  }
   186  
   187  func TestUpdatePodGroup(t *testing.T) {
   188  	namespace := "c1"
   189  
   190  	testCases := []struct {
   191  		Name        string
   192  		podGroupold *schedulingv1beta1.PodGroup
   193  		podGroupnew *schedulingv1beta1.PodGroup
   194  		ExpectValue int
   195  	}{
   196  		{
   197  			Name: "updatepodgroup",
   198  			podGroupold: &schedulingv1beta1.PodGroup{
   199  				ObjectMeta: metav1.ObjectMeta{
   200  					Name:      "pg1",
   201  					Namespace: namespace,
   202  				},
   203  				Spec: schedulingv1beta1.PodGroupSpec{
   204  					Queue: "c1",
   205  				},
   206  				Status: schedulingv1beta1.PodGroupStatus{
   207  					Phase: schedulingv1beta1.PodGroupPending,
   208  				},
   209  			},
   210  			podGroupnew: &schedulingv1beta1.PodGroup{
   211  				ObjectMeta: metav1.ObjectMeta{
   212  					Name:      "pg1",
   213  					Namespace: namespace,
   214  				},
   215  				Spec: schedulingv1beta1.PodGroupSpec{
   216  					Queue: "c1",
   217  				},
   218  				Status: schedulingv1beta1.PodGroupStatus{
   219  					Phase: schedulingv1beta1.PodGroupRunning,
   220  				},
   221  			},
   222  			ExpectValue: 1,
   223  		},
   224  	}
   225  
   226  	for i, testcase := range testCases {
   227  		c := newFakeController()
   228  
   229  		c.updatePodGroup(testcase.podGroupold, testcase.podGroupnew)
   230  
   231  		if testcase.ExpectValue != c.queue.Len() {
   232  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len())
   233  		}
   234  	}
   235  }
   236  
   237  func TestSyncQueue(t *testing.T) {
   238  	namespace := "c1"
   239  
   240  	testCases := []struct {
   241  		Name        string
   242  		podGroup    *schedulingv1beta1.PodGroup
   243  		queue       *schedulingv1beta1.Queue
   244  		ExpectValue int32
   245  	}{
   246  		{
   247  			Name: "syncQueue",
   248  			podGroup: &schedulingv1beta1.PodGroup{
   249  				ObjectMeta: metav1.ObjectMeta{
   250  					Name:      "pg1",
   251  					Namespace: namespace,
   252  				},
   253  				Spec: schedulingv1beta1.PodGroupSpec{
   254  					Queue: "c1",
   255  				},
   256  				Status: schedulingv1beta1.PodGroupStatus{
   257  					Phase: schedulingv1beta1.PodGroupPending,
   258  				},
   259  			},
   260  			queue: &schedulingv1beta1.Queue{
   261  				ObjectMeta: metav1.ObjectMeta{
   262  					Name: "c1",
   263  				},
   264  				Spec: schedulingv1beta1.QueueSpec{
   265  					Weight: 1,
   266  				},
   267  			},
   268  			ExpectValue: 1,
   269  		},
   270  	}
   271  
   272  	for i, testcase := range testCases {
   273  		c := newFakeController()
   274  
   275  		key, _ := cache.MetaNamespaceKeyFunc(testcase.podGroup)
   276  		c.podGroups[testcase.podGroup.Spec.Queue] = make(map[string]struct{})
   277  		c.podGroups[testcase.podGroup.Spec.Queue][key] = struct{}{}
   278  
   279  		c.pgInformer.Informer().GetIndexer().Add(testcase.podGroup)
   280  		c.queueInformer.Informer().GetIndexer().Add(testcase.queue)
   281  		c.vcClient.SchedulingV1beta1().Queues().Create(context.TODO(), testcase.queue, metav1.CreateOptions{})
   282  
   283  		err := c.syncQueue(testcase.queue, nil)
   284  		item, _ := c.vcClient.SchedulingV1beta1().Queues().Get(context.TODO(), testcase.queue.Name, metav1.GetOptions{})
   285  		if err != nil && testcase.ExpectValue != item.Status.Pending {
   286  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len())
   287  		}
   288  	}
   289  
   290  }
   291  
   292  func TestProcessNextWorkItem(t *testing.T) {
   293  	testCases := []struct {
   294  		Name        string
   295  		ExpectValue int32
   296  	}{
   297  		{
   298  			Name:        "processNextWorkItem",
   299  			ExpectValue: 0,
   300  		},
   301  	}
   302  
   303  	for i, testcase := range testCases {
   304  		c := newFakeController()
   305  		c.queue.Add("test")
   306  		bVal := c.processNextWorkItem()
   307  		fmt.Println("The value of boolean is ", bVal)
   308  		if c.queue.Len() != 0 {
   309  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, c.queue.Len())
   310  		}
   311  	}
   312  }