sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/pjutil/trigger_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes 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 pjutil 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/watch" 26 coretesting "k8s.io/client-go/testing" 27 prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 28 "sigs.k8s.io/prow/pkg/client/clientset/versioned/fake" 29 ) 30 31 func Test_resultForJob(t *testing.T) { 32 type args struct { 33 pj prowapi.ProwJob 34 watchResults []prowapi.ProwJob 35 } 36 testcases := []struct { 37 name string 38 args args 39 expected prowapi.ProwJobStatus 40 expectToContinue bool 41 expectedErr error 42 }{ 43 { 44 name: "Prowjob completed successfully", 45 args: args{ 46 pj: prowapi.ProwJob{ 47 ObjectMeta: metav1.ObjectMeta{ 48 Name: "winwin", 49 Namespace: "prowjobs", 50 }, 51 Spec: prowapi.ProwJobSpec{ 52 Job: "test-job", 53 }, 54 Status: prowapi.ProwJobStatus{ 55 State: prowapi.TriggeredState, 56 }, 57 }, 58 watchResults: []prowapi.ProwJob{ 59 { 60 ObjectMeta: metav1.ObjectMeta{ 61 Name: "winwin", 62 Namespace: "prowjobs", 63 }, 64 Spec: prowapi.ProwJobSpec{ 65 Job: "test-job", 66 }, 67 Status: prowapi.ProwJobStatus{ 68 State: prowapi.SuccessState, 69 }, 70 }, 71 }, 72 }, 73 expected: prowapi.ProwJobStatus{ 74 State: prowapi.SuccessState, 75 }, 76 expectToContinue: false, 77 }, 78 { 79 name: "Longer running prowjob", 80 args: args{ 81 pj: prowapi.ProwJob{ 82 ObjectMeta: metav1.ObjectMeta{ 83 Name: "winwin", 84 Namespace: "prowjobs", 85 }, 86 Spec: prowapi.ProwJobSpec{ 87 Job: "test-job", 88 }, 89 Status: prowapi.ProwJobStatus{ 90 State: prowapi.TriggeredState, 91 }, 92 }, 93 watchResults: []prowapi.ProwJob{ 94 { 95 ObjectMeta: metav1.ObjectMeta{ 96 Name: "winwin", 97 Namespace: "prowjobs", 98 }, 99 Spec: prowapi.ProwJobSpec{ 100 Job: "test-job", 101 }, 102 Status: prowapi.ProwJobStatus{ 103 State: prowapi.PendingState, 104 }, 105 }, 106 { 107 ObjectMeta: metav1.ObjectMeta{ 108 Name: "winwin", 109 Namespace: "prowjobs", 110 }, 111 Spec: prowapi.ProwJobSpec{ 112 Job: "test-job", 113 }, 114 Status: prowapi.ProwJobStatus{ 115 State: prowapi.SuccessState, 116 }, 117 }, 118 }, 119 }, 120 expected: prowapi.ProwJobStatus{ 121 State: prowapi.SuccessState, 122 }, 123 expectToContinue: false, 124 }, 125 { 126 name: "Prowjob failed", 127 args: args{ 128 pj: prowapi.ProwJob{ 129 ObjectMeta: metav1.ObjectMeta{ 130 Name: "winwin", 131 Namespace: "prowjobs", 132 }, 133 Spec: prowapi.ProwJobSpec{ 134 Job: "test-job", 135 }, 136 Status: prowapi.ProwJobStatus{ 137 State: prowapi.TriggeredState, 138 }, 139 }, 140 watchResults: []prowapi.ProwJob{ 141 { 142 ObjectMeta: metav1.ObjectMeta{ 143 Name: "winwin", 144 Namespace: "prowjobs", 145 }, 146 Spec: prowapi.ProwJobSpec{ 147 Job: "test-job", 148 }, 149 Status: prowapi.ProwJobStatus{ 150 State: prowapi.FailureState, 151 }, 152 }, 153 }, 154 }, 155 expected: prowapi.ProwJobStatus{ 156 State: prowapi.FailureState, 157 }, 158 expectToContinue: false, 159 }, 160 } 161 for _, tc := range testcases { 162 t.Run(tc.name, func(t *testing.T) { 163 cs := fake.NewSimpleClientset(&tc.args.pj) 164 cs.Fake.PrependWatchReactor("prowjobs", func(action coretesting.Action) (bool, watch.Interface, error) { 165 ret := watch.NewFakeWithChanSize(len(tc.args.watchResults), true) 166 for _, res := range tc.args.watchResults { 167 ret.Modify(&res) 168 } 169 return true, ret, nil 170 }) 171 pjr, shouldContinue, err := resultForJob(cs.ProwV1().ProwJobs("prowjobs"), fmt.Sprintf("metadata.name=%s", tc.args.pj.Name)) 172 if !reflect.DeepEqual(pjr.State, tc.expected.State) { 173 t.Errorf("resultForJob() ProwJobStatus got = %v, want %v", pjr, tc.expected) 174 } 175 if tc.expectToContinue != shouldContinue { 176 t.Errorf("resultForJob() ShouldContinue got = %v, want %v", shouldContinue, tc.expectToContinue) 177 } 178 if !reflect.DeepEqual(tc.expectedErr, err) { 179 t.Errorf("resultForJob() error got = %v, want %v", err, tc.expectedErr) 180 } 181 }) 182 } 183 }