volcano.sh/volcano@v1.9.0/pkg/controllers/podgroup/pg_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 podgroup
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"k8s.io/client-go/informers"
    28  	kubeclient "k8s.io/client-go/kubernetes/fake"
    29  
    30  	scheduling "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    31  	vcclient "volcano.sh/apis/pkg/client/clientset/versioned/fake"
    32  	"volcano.sh/volcano/pkg/controllers/framework"
    33  )
    34  
    35  func newFakeController() *pgcontroller {
    36  	kubeClient := kubeclient.NewSimpleClientset()
    37  	vcClient := vcclient.NewSimpleClientset()
    38  	sharedInformers := informers.NewSharedInformerFactory(kubeClient, 0)
    39  
    40  	controller := &pgcontroller{}
    41  	opt := &framework.ControllerOption{
    42  		KubeClient:            kubeClient,
    43  		VolcanoClient:         vcClient,
    44  		SharedInformerFactory: sharedInformers,
    45  		SchedulerNames:        []string{"volcano"},
    46  	}
    47  
    48  	controller.Initialize(opt)
    49  
    50  	return controller
    51  }
    52  
    53  func TestAddPodGroup(t *testing.T) {
    54  	namespace := "test"
    55  	isController := true
    56  	blockOwnerDeletion := true
    57  
    58  	testCases := []struct {
    59  		name             string
    60  		pod              *v1.Pod
    61  		expectedPodGroup *scheduling.PodGroup
    62  	}{
    63  		{
    64  			name: "AddPodGroup: pod has ownerReferences and priorityClassName",
    65  			pod: &v1.Pod{
    66  				TypeMeta: metav1.TypeMeta{
    67  					APIVersion: "v1",
    68  					Kind:       "Pod",
    69  				},
    70  				ObjectMeta: metav1.ObjectMeta{
    71  					Name:      "pod1",
    72  					Namespace: namespace,
    73  					OwnerReferences: []metav1.OwnerReference{
    74  						{
    75  							APIVersion: "app/v1",
    76  							Kind:       "ReplicaSet",
    77  							Name:       "rs1",
    78  							UID:        "7a09885b-b753-4924-9fba-77c0836bac20",
    79  							Controller: &isController,
    80  						},
    81  					},
    82  				},
    83  				Spec: v1.PodSpec{
    84  					PriorityClassName: "test-pc",
    85  				},
    86  			},
    87  			expectedPodGroup: &scheduling.PodGroup{
    88  				TypeMeta: metav1.TypeMeta{
    89  					APIVersion: "scheduling.volcano.sh/v1beta1",
    90  					Kind:       "PodGroup",
    91  				},
    92  				ObjectMeta: metav1.ObjectMeta{
    93  					Name:      "podgroup-7a09885b-b753-4924-9fba-77c0836bac20",
    94  					Namespace: namespace,
    95  					OwnerReferences: []metav1.OwnerReference{
    96  						{
    97  							APIVersion: "app/v1",
    98  							Kind:       "ReplicaSet",
    99  							Name:       "rs1",
   100  							UID:        "7a09885b-b753-4924-9fba-77c0836bac20",
   101  							Controller: &isController,
   102  						},
   103  					},
   104  				},
   105  				Spec: scheduling.PodGroupSpec{
   106  					MinMember:         1,
   107  					PriorityClassName: "test-pc",
   108  				},
   109  			},
   110  		},
   111  		{
   112  			name: "AddPodGroup: pod has no ownerReferences or priorityClassName",
   113  			pod: &v1.Pod{
   114  				TypeMeta: metav1.TypeMeta{
   115  					APIVersion: "v1",
   116  					Kind:       "Pod",
   117  				},
   118  				ObjectMeta: metav1.ObjectMeta{
   119  					Name:      "pod1",
   120  					Namespace: namespace,
   121  					UID:       types.UID("7a09885b-b753-4924-9fba-77c0836bac20"),
   122  				},
   123  			},
   124  			expectedPodGroup: &scheduling.PodGroup{
   125  				TypeMeta: metav1.TypeMeta{
   126  					APIVersion: "scheduling.volcano.sh/v1beta1",
   127  					Kind:       "PodGroup",
   128  				},
   129  				ObjectMeta: metav1.ObjectMeta{
   130  					Name:      "podgroup-7a09885b-b753-4924-9fba-77c0836bac20",
   131  					Namespace: namespace,
   132  					OwnerReferences: []metav1.OwnerReference{
   133  						{
   134  							APIVersion:         "v1",
   135  							Kind:               "Pod",
   136  							Name:               "pod1",
   137  							UID:                "7a09885b-b753-4924-9fba-77c0836bac20",
   138  							Controller:         &isController,
   139  							BlockOwnerDeletion: &blockOwnerDeletion,
   140  						},
   141  					},
   142  				},
   143  				Spec: scheduling.PodGroupSpec{
   144  					MinMember: 1,
   145  				},
   146  			},
   147  		},
   148  	}
   149  
   150  	for _, testCase := range testCases {
   151  		c := newFakeController()
   152  
   153  		pod, err := c.kubeClient.CoreV1().Pods(testCase.pod.Namespace).Create(context.TODO(), testCase.pod, metav1.CreateOptions{})
   154  		if err != nil {
   155  			t.Errorf("Case %s failed when creating pod for %v", testCase.name, err)
   156  		}
   157  
   158  		c.addPod(pod)
   159  		c.createNormalPodPGIfNotExist(pod)
   160  
   161  		pg, err := c.vcClient.SchedulingV1beta1().PodGroups(pod.Namespace).Get(context.TODO(),
   162  			testCase.expectedPodGroup.Name,
   163  			metav1.GetOptions{},
   164  		)
   165  		if err != nil {
   166  			t.Errorf("Case %s failed when getting podGroup for %v", testCase.name, err)
   167  		}
   168  
   169  		if false == reflect.DeepEqual(pg.OwnerReferences, testCase.expectedPodGroup.OwnerReferences) {
   170  			t.Errorf("Case %s failed, expect %v, got %v", testCase.name, testCase.expectedPodGroup, pg)
   171  		}
   172  
   173  		newpod, err := c.kubeClient.CoreV1().Pods(testCase.pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
   174  		if err != nil {
   175  			t.Errorf("Case %s failed when creating pod for %v", testCase.name, err)
   176  		}
   177  
   178  		podAnnotation := newpod.Annotations[scheduling.KubeGroupNameAnnotationKey]
   179  		if testCase.expectedPodGroup.Name != podAnnotation {
   180  			t.Errorf("Case %s failed, expect %v, got %v", testCase.name,
   181  				testCase.expectedPodGroup.Name, podAnnotation)
   182  		}
   183  
   184  		if testCase.expectedPodGroup.Spec.PriorityClassName != pod.Spec.PriorityClassName {
   185  			t.Errorf("Case %s failed, expect %v, got %v", testCase.name,
   186  				testCase.expectedPodGroup.Spec.PriorityClassName, pod.Spec.PriorityClassName)
   187  		}
   188  	}
   189  }