volcano.sh/volcano@v1.9.0/pkg/controllers/jobtemplate/jobtemplate_controller_action_test.go (about)

     1  /*
     2  Copyright 2022 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 jobtemplate
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/client-go/informers"
    26  	kubeclient "k8s.io/client-go/kubernetes/fake"
    27  
    28  	"volcano.sh/apis/pkg/apis/batch/v1alpha1"
    29  	jobflowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1"
    30  
    31  	volcanoclient "volcano.sh/apis/pkg/client/clientset/versioned/fake"
    32  	"volcano.sh/volcano/pkg/controllers/framework"
    33  )
    34  
    35  func newFakeController() *jobtemplatecontroller {
    36  	volcanoClientSet := volcanoclient.NewSimpleClientset()
    37  	kubeClientSet := kubeclient.NewSimpleClientset()
    38  
    39  	sharedInformers := informers.NewSharedInformerFactory(kubeClientSet, 0)
    40  
    41  	controller := &jobtemplatecontroller{}
    42  	opt := &framework.ControllerOption{
    43  		VolcanoClient:         volcanoClientSet,
    44  		KubeClient:            kubeClientSet,
    45  		SharedInformerFactory: sharedInformers,
    46  		WorkerNum:             3,
    47  	}
    48  
    49  	controller.Initialize(opt)
    50  
    51  	return controller
    52  }
    53  
    54  func TestSyncJobTemplateFunc(t *testing.T) {
    55  	namespace := "test"
    56  
    57  	type args struct {
    58  		jobTemplate *jobflowv1alpha1.JobTemplate
    59  		jobList     []*v1alpha1.Job
    60  	}
    61  	type wantRes struct {
    62  		jobTemplateStatus *jobflowv1alpha1.JobTemplateStatus
    63  		err               error
    64  	}
    65  	tests := []struct {
    66  		name string
    67  		args args
    68  		want wantRes
    69  	}{
    70  		{
    71  			name: "SyncJobTemplate success case",
    72  			args: args{
    73  				jobTemplate: &jobflowv1alpha1.JobTemplate{
    74  					ObjectMeta: metav1.ObjectMeta{
    75  						Name:      "jobtemplate",
    76  						Namespace: namespace,
    77  					},
    78  					Spec: v1alpha1.JobSpec{},
    79  				},
    80  				jobList: []*v1alpha1.Job{
    81  					{
    82  						ObjectMeta: metav1.ObjectMeta{
    83  							Name:        "job1",
    84  							Namespace:   namespace,
    85  							Labels:      map[string]string{CreatedByJobTemplate: GetTemplateString(namespace, "jobtemplate")},
    86  							Annotations: map[string]string{CreatedByJobTemplate: GetTemplateString(namespace, "jobtemplate")},
    87  						},
    88  						Spec:   v1alpha1.JobSpec{},
    89  						Status: v1alpha1.JobStatus{},
    90  					},
    91  				},
    92  			},
    93  			want: wantRes{
    94  				jobTemplateStatus: &jobflowv1alpha1.JobTemplateStatus{
    95  					JobDependsOnList: []string{"job1"},
    96  				},
    97  				err: nil,
    98  			},
    99  		},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			fakeController := newFakeController()
   104  
   105  			for i := range tt.args.jobList {
   106  				if err := fakeController.jobInformer.Informer().GetIndexer().Add(tt.args.jobList[i]); err != nil {
   107  					t.Errorf("add vcjob to informerFake,error : %s", err.Error())
   108  				}
   109  			}
   110  
   111  			if _, err := fakeController.vcClient.FlowV1alpha1().JobTemplates(namespace).Create(context.Background(), tt.args.jobTemplate, metav1.CreateOptions{}); err != nil {
   112  				t.Errorf("create jobTemplate failed,error : %s", err.Error())
   113  			}
   114  
   115  			if got := fakeController.syncJobTemplate(tt.args.jobTemplate); got != tt.want.err {
   116  				t.Error("Expected deleteAllJobsCreateByJobFlow() return nil, but not nil")
   117  			}
   118  			if !reflect.DeepEqual(&tt.args.jobTemplate.Status, tt.want.jobTemplateStatus) {
   119  				t.Error("not the expected result")
   120  			}
   121  		})
   122  	}
   123  }