volcano.sh/volcano@v1.9.0/pkg/controllers/jobtemplate/jobtemplate_controller_handler_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  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    25  	jobflowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1"
    26  	"volcano.sh/volcano/pkg/controllers/apis"
    27  )
    28  
    29  func TestAddJobTemplateFunc(t *testing.T) {
    30  	namespace := "test"
    31  
    32  	testCases := []struct {
    33  		Name        string
    34  		jobTemplate *jobflowv1alpha1.JobTemplate
    35  		ExpectValue int
    36  	}{
    37  		{
    38  			Name: "AddJobTemplate Success",
    39  			jobTemplate: &jobflowv1alpha1.JobTemplate{
    40  				ObjectMeta: metav1.ObjectMeta{
    41  					Name:      "jobtemplate1",
    42  					Namespace: namespace,
    43  				},
    44  			},
    45  			ExpectValue: 1,
    46  		},
    47  	}
    48  	for i, testcase := range testCases {
    49  		t.Run(testcase.Name, func(t *testing.T) {
    50  			fakeController := newFakeController()
    51  			fakeController.addJobTemplate(testcase.jobTemplate)
    52  			queueLen := fakeController.queue.Len()
    53  			if testcase.ExpectValue != queueLen {
    54  				t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestAddJob(t *testing.T) {
    61  	namespace := "test"
    62  
    63  	testCases := []struct {
    64  		Name        string
    65  		job         *batch.Job
    66  		ExpectValue int
    67  	}{
    68  		{
    69  			Name: "AddJob Success",
    70  			job: &batch.Job{
    71  				ObjectMeta: metav1.ObjectMeta{
    72  					Name:        "job1",
    73  					Namespace:   namespace,
    74  					Labels:      map[string]string{CreatedByJobTemplate: "volcano.sh/createdByJobTemplate"},
    75  					Annotations: map[string]string{CreatedByJobTemplate: "test.jobtemplate1"},
    76  				},
    77  			},
    78  			ExpectValue: 1,
    79  		},
    80  	}
    81  	for i, testcase := range testCases {
    82  		t.Run(testcase.Name, func(t *testing.T) {
    83  			fakeController := newFakeController()
    84  			fakeController.addJob(testcase.job)
    85  			queueLen := fakeController.queue.Len()
    86  			if testcase.ExpectValue != queueLen {
    87  				t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestEnqueueJobTemplate(t *testing.T) {
    94  
    95  	namespace := "test"
    96  
    97  	req1 := apis.FlowRequest{
    98  		Namespace:       namespace,
    99  		JobTemplateName: "name1",
   100  
   101  		Action: jobflowv1alpha1.SyncJobTemplateAction,
   102  	}
   103  	req2 := apis.FlowRequest{
   104  		Namespace:       namespace,
   105  		JobTemplateName: "name2",
   106  
   107  		Action: jobflowv1alpha1.SyncJobTemplateAction,
   108  	}
   109  
   110  	testCases := []struct {
   111  		Name        string
   112  		newReq      apis.FlowRequest
   113  		oldReq      apis.FlowRequest
   114  		ExpectValue int
   115  	}{
   116  		{
   117  			Name:        "de-duplicate",
   118  			newReq:      req1,
   119  			oldReq:      req1,
   120  			ExpectValue: 1,
   121  		},
   122  		{
   123  			Name:        "no-deduplicate",
   124  			newReq:      req1,
   125  			oldReq:      req2,
   126  			ExpectValue: 2,
   127  		},
   128  	}
   129  
   130  	for i, testcase := range testCases {
   131  		t.Run(testcase.Name, func(t *testing.T) {
   132  			fakeController := newFakeController()
   133  
   134  			fakeController.enqueueJobTemplate(testcase.oldReq)
   135  			fakeController.enqueueJobTemplate(testcase.newReq)
   136  			queueLen := fakeController.queue.Len()
   137  			if testcase.ExpectValue != queueLen {
   138  				t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen)
   139  			}
   140  		})
   141  	}
   142  
   143  }