volcano.sh/volcano@v1.9.0/pkg/controllers/jobflow/jobflow_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 jobflow 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" 24 25 batch "volcano.sh/apis/pkg/apis/batch/v1alpha1" 26 jobflowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1" 27 "volcano.sh/apis/pkg/client/clientset/versioned/scheme" 28 "volcano.sh/volcano/pkg/controllers/apis" 29 ) 30 31 func TestAddJobFlowFunc(t *testing.T) { 32 namespace := "test" 33 34 testCases := []struct { 35 Name string 36 jobFlow *jobflowv1alpha1.JobFlow 37 ExpectValue int 38 }{ 39 { 40 Name: "AddJobFlow Success", 41 jobFlow: &jobflowv1alpha1.JobFlow{ 42 ObjectMeta: metav1.ObjectMeta{ 43 Name: "jobflow1", 44 Namespace: namespace, 45 }, 46 }, 47 ExpectValue: 1, 48 }, 49 } 50 for i, testcase := range testCases { 51 t.Run(testcase.Name, func(t *testing.T) { 52 fakeController := newFakeController() 53 fakeController.addJobFlow(testcase.jobFlow) 54 queueLen := fakeController.queue.Len() 55 if testcase.ExpectValue != queueLen { 56 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen) 57 } 58 }) 59 } 60 } 61 62 func TestUpdateJobFlowFunc(t *testing.T) { 63 namespace := "test" 64 65 testCases := []struct { 66 Name string 67 newJobFlow *jobflowv1alpha1.JobFlow 68 oldJobFlow *jobflowv1alpha1.JobFlow 69 ExpectValue int 70 }{ 71 { 72 Name: "UpdateJobFlow Success", 73 newJobFlow: &jobflowv1alpha1.JobFlow{ 74 ObjectMeta: metav1.ObjectMeta{ 75 Name: "jobflow1", 76 Namespace: namespace, 77 }, 78 Spec: jobflowv1alpha1.JobFlowSpec{ 79 Flows: nil, 80 JobRetainPolicy: jobflowv1alpha1.Delete, 81 }, 82 Status: jobflowv1alpha1.JobFlowStatus{ 83 State: jobflowv1alpha1.State{ 84 Phase: jobflowv1alpha1.Succeed, 85 }, 86 }, 87 }, 88 oldJobFlow: &jobflowv1alpha1.JobFlow{ 89 ObjectMeta: metav1.ObjectMeta{ 90 Name: "jobflow1", 91 Namespace: namespace, 92 ResourceVersion: "1223", 93 }, 94 Spec: jobflowv1alpha1.JobFlowSpec{ 95 Flows: nil, 96 JobRetainPolicy: jobflowv1alpha1.Delete, 97 }, 98 Status: jobflowv1alpha1.JobFlowStatus{ 99 State: jobflowv1alpha1.State{ 100 Phase: jobflowv1alpha1.Succeed, 101 }, 102 }, 103 }, 104 ExpectValue: 1, 105 }, 106 } 107 for i, testcase := range testCases { 108 t.Run(testcase.Name, func(t *testing.T) { 109 fakeController := newFakeController() 110 fakeController.updateJobFlow(testcase.oldJobFlow, testcase.newJobFlow) 111 queueLen := fakeController.queue.Len() 112 if testcase.ExpectValue != queueLen { 113 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen) 114 } 115 }) 116 } 117 } 118 119 func TestUpdateJobFunc(t *testing.T) { 120 namespace := "test" 121 122 testCases := []struct { 123 Name string 124 newJob *batch.Job 125 oldJob *batch.Job 126 ExpectValue int 127 }{ 128 { 129 Name: "UpdateJob Success", 130 newJob: &batch.Job{ 131 ObjectMeta: metav1.ObjectMeta{ 132 Name: "job1", 133 Namespace: namespace, 134 }, 135 }, 136 oldJob: &batch.Job{ 137 ObjectMeta: metav1.ObjectMeta{ 138 Name: "job1", 139 Namespace: namespace, 140 ResourceVersion: "1223", 141 OwnerReferences: []metav1.OwnerReference{}, 142 }, 143 }, 144 ExpectValue: 1, 145 }, 146 } 147 jobFlow := &jobflowv1alpha1.JobFlow{ 148 TypeMeta: metav1.TypeMeta{}, 149 ObjectMeta: metav1.ObjectMeta{ 150 Name: "jobflow1", 151 Namespace: namespace, 152 }, 153 Spec: jobflowv1alpha1.JobFlowSpec{}, 154 Status: jobflowv1alpha1.JobFlowStatus{}, 155 } 156 for i, testcase := range testCases { 157 t.Run(testcase.Name, func(t *testing.T) { 158 fakeController := newFakeController() 159 160 if err := controllerutil.SetControllerReference(jobFlow, testcase.oldJob, scheme.Scheme); err != nil { 161 t.Errorf("SetControllerReference error : %s", err.Error()) 162 } 163 if err := controllerutil.SetControllerReference(jobFlow, testcase.newJob, scheme.Scheme); err != nil { 164 t.Errorf("SetControllerReference error : %s", err.Error()) 165 } 166 fakeController.updateJob(testcase.oldJob, testcase.newJob) 167 queueLen := fakeController.queue.Len() 168 if testcase.ExpectValue != queueLen { 169 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen) 170 } 171 }) 172 } 173 } 174 175 func TestEnqueueJobFlow(t *testing.T) { 176 177 namespace := "test" 178 179 req1 := apis.FlowRequest{ 180 Namespace: namespace, 181 JobFlowName: "name1", 182 183 Action: jobflowv1alpha1.SyncJobFlowAction, 184 Event: jobflowv1alpha1.OutOfSyncEvent, 185 } 186 req2 := apis.FlowRequest{ 187 Namespace: namespace, 188 JobFlowName: "name2", 189 190 Action: jobflowv1alpha1.SyncJobFlowAction, 191 Event: jobflowv1alpha1.OutOfSyncEvent, 192 } 193 194 testCases := []struct { 195 Name string 196 newReq apis.FlowRequest 197 oldReq apis.FlowRequest 198 ExpectValue int 199 }{ 200 { 201 Name: "de-duplicate", 202 newReq: req1, 203 oldReq: req1, 204 ExpectValue: 1, 205 }, 206 { 207 Name: "no-deduplicate", 208 newReq: req1, 209 oldReq: req2, 210 ExpectValue: 2, 211 }, 212 } 213 214 for i, testcase := range testCases { 215 t.Run(testcase.Name, func(t *testing.T) { 216 fakeController := newFakeController() 217 218 fakeController.enqueueJobFlow(testcase.oldReq) 219 fakeController.enqueueJobFlow(testcase.newReq) 220 queueLen := fakeController.queue.Len() 221 if testcase.ExpectValue != queueLen { 222 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, queueLen) 223 } 224 }) 225 } 226 227 }