sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/crier/reporters/resultstore/reporter_test.go (about) 1 /* 2 Copyright 2023 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 resultstore 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/sirupsen/logrus" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 prowv1 "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 27 "sigs.k8s.io/prow/pkg/config" 28 "sigs.k8s.io/prow/pkg/io/fakeopener" 29 "sigs.k8s.io/prow/pkg/resultstore" 30 ) 31 32 type fakeConfigGetter struct { 33 c config.Config 34 } 35 36 func (fcg fakeConfigGetter) Config() *config.Config { 37 return &fcg.c 38 } 39 40 func TestGetName(t *testing.T) { 41 gr := New(fakeConfigGetter{}.Config, &fakeopener.FakeOpener{}, &resultstore.Uploader{}, false) 42 want := "resultstorereporter" 43 if got := gr.GetName(); got != want { 44 t.Errorf("GetName() got %v, want %v", got, want) 45 } 46 } 47 48 func TestShouldReport(t *testing.T) { 49 tests := []struct { 50 name string 51 job *prowv1.ProwJob 52 shouldReport bool 53 }{ 54 { 55 name: "Successful job reported", 56 job: &prowv1.ProwJob{ 57 Spec: prowv1.ProwJobSpec{ 58 Type: prowv1.PostsubmitJob, 59 Agent: prowv1.KubernetesAgent, 60 Job: "prow-job", 61 Report: true, 62 Refs: &prowv1.Refs{ 63 Org: "org", 64 Repo: "repo", 65 }, 66 DecorationConfig: &prowv1.DecorationConfig{ 67 GCSConfiguration: &prowv1.GCSConfiguration{ 68 Bucket: "gs://bucket", 69 }, 70 }, 71 ProwJobDefault: &prowv1.ProwJobDefault{ 72 ResultStoreConfig: &prowv1.ResultStoreConfig{ 73 ProjectID: "cloud-project-id", 74 }, 75 }, 76 }, 77 Status: prowv1.ProwJobStatus{ 78 State: prowv1.SuccessState, 79 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 80 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 81 BuildID: "build-id", 82 }, 83 }, 84 shouldReport: true, 85 }, 86 { 87 name: "Failed job reported", 88 job: &prowv1.ProwJob{ 89 Spec: prowv1.ProwJobSpec{ 90 Type: prowv1.PostsubmitJob, 91 Agent: prowv1.KubernetesAgent, 92 Job: "prow-job", 93 Report: true, 94 Refs: &prowv1.Refs{ 95 Org: "org", 96 Repo: "repo", 97 }, 98 DecorationConfig: &prowv1.DecorationConfig{ 99 GCSConfiguration: &prowv1.GCSConfiguration{ 100 Bucket: "gs://bucket", 101 }, 102 }, 103 ProwJobDefault: &prowv1.ProwJobDefault{ 104 ResultStoreConfig: &prowv1.ResultStoreConfig{ 105 ProjectID: "cloud-project-id", 106 }, 107 }, 108 }, 109 Status: prowv1.ProwJobStatus{ 110 State: prowv1.FailureState, 111 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 112 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 113 BuildID: "build-id", 114 }, 115 }, 116 shouldReport: true, 117 }, 118 { 119 name: "Bare bucket name okay", 120 job: &prowv1.ProwJob{ 121 Spec: prowv1.ProwJobSpec{ 122 Type: prowv1.PostsubmitJob, 123 Agent: prowv1.KubernetesAgent, 124 Job: "prow-job", 125 Report: true, 126 Refs: &prowv1.Refs{ 127 Org: "org", 128 Repo: "repo", 129 }, 130 DecorationConfig: &prowv1.DecorationConfig{ 131 GCSConfiguration: &prowv1.GCSConfiguration{ 132 Bucket: "bucket", 133 }, 134 }, 135 ProwJobDefault: &prowv1.ProwJobDefault{ 136 ResultStoreConfig: &prowv1.ResultStoreConfig{ 137 ProjectID: "cloud-project-id", 138 }, 139 }, 140 }, 141 Status: prowv1.ProwJobStatus{ 142 State: prowv1.SuccessState, 143 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 144 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 145 BuildID: "build-id", 146 }, 147 }, 148 shouldReport: true, 149 }, 150 { 151 name: "Report: false job not reported", 152 job: &prowv1.ProwJob{ 153 Spec: prowv1.ProwJobSpec{ 154 Type: prowv1.PostsubmitJob, 155 Agent: prowv1.KubernetesAgent, 156 Job: "prow-job", 157 Report: false, 158 Refs: &prowv1.Refs{ 159 Org: "org", 160 Repo: "repo", 161 }, 162 DecorationConfig: &prowv1.DecorationConfig{ 163 GCSConfiguration: &prowv1.GCSConfiguration{ 164 Bucket: "gs://bucket", 165 }, 166 }, 167 ProwJobDefault: &prowv1.ProwJobDefault{ 168 ResultStoreConfig: &prowv1.ResultStoreConfig{ 169 ProjectID: "cloud-project-id", 170 }, 171 }, 172 }, 173 Status: prowv1.ProwJobStatus{ 174 State: prowv1.SuccessState, 175 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 176 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 177 BuildID: "build-id", 178 }, 179 }, 180 shouldReport: false, 181 }, 182 { 183 name: "Report: no resultstore project id job not reported", 184 job: &prowv1.ProwJob{ 185 Spec: prowv1.ProwJobSpec{ 186 Type: prowv1.PostsubmitJob, 187 Agent: prowv1.KubernetesAgent, 188 Job: "prow-job", 189 Report: true, 190 Refs: &prowv1.Refs{ 191 Org: "org", 192 Repo: "repo", 193 }, 194 DecorationConfig: &prowv1.DecorationConfig{ 195 GCSConfiguration: &prowv1.GCSConfiguration{ 196 Bucket: "gs://bucket", 197 }, 198 }, 199 }, 200 Status: prowv1.ProwJobStatus{ 201 State: prowv1.SuccessState, 202 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 203 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 204 BuildID: "build-id", 205 }, 206 }, 207 shouldReport: false, 208 }, 209 { 210 name: "Non-GCS job not reported", 211 job: &prowv1.ProwJob{ 212 Spec: prowv1.ProwJobSpec{ 213 Type: prowv1.PostsubmitJob, 214 Agent: prowv1.KubernetesAgent, 215 Job: "prow-job", 216 Report: true, 217 Refs: &prowv1.Refs{ 218 Org: "org", 219 Repo: "repo", 220 }, 221 DecorationConfig: &prowv1.DecorationConfig{ 222 GCSConfiguration: &prowv1.GCSConfiguration{ 223 Bucket: "non-gcs://bucket", 224 }, 225 }, 226 ProwJobDefault: &prowv1.ProwJobDefault{ 227 ResultStoreConfig: &prowv1.ResultStoreConfig{ 228 ProjectID: "cloud-project-id", 229 }, 230 }, 231 }, 232 Status: prowv1.ProwJobStatus{ 233 State: prowv1.SuccessState, 234 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 235 CompletionTime: &metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 25, time.UTC)}, 236 BuildID: "build-id", 237 }, 238 }, 239 shouldReport: false, 240 }, 241 { 242 name: "Pending job not reported", 243 job: &prowv1.ProwJob{ 244 Spec: prowv1.ProwJobSpec{ 245 Type: prowv1.PostsubmitJob, 246 Agent: prowv1.KubernetesAgent, 247 Job: "prow-job", 248 Report: true, 249 Refs: &prowv1.Refs{ 250 Org: "org", 251 Repo: "repo", 252 }, 253 DecorationConfig: &prowv1.DecorationConfig{ 254 GCSConfiguration: &prowv1.GCSConfiguration{ 255 Bucket: "gs://bucket", 256 }, 257 }, 258 ProwJobDefault: &prowv1.ProwJobDefault{ 259 ResultStoreConfig: &prowv1.ResultStoreConfig{ 260 ProjectID: "cloud-project-id", 261 }, 262 }, 263 }, 264 Status: prowv1.ProwJobStatus{ 265 State: prowv1.PendingState, 266 StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)}, 267 BuildID: "build-id", 268 }, 269 }, 270 shouldReport: false, 271 }, 272 } 273 for _, tc := range tests { 274 t.Run(tc.name, func(t *testing.T) { 275 gr := New(fakeConfigGetter{}.Config, &fakeopener.FakeOpener{}, &resultstore.Uploader{}, false) 276 result := gr.ShouldReport(context.Background(), logrus.NewEntry(logrus.StandardLogger()), tc.job) 277 if result != tc.shouldReport { 278 t.Errorf("ShouldReport() got %v, want %v", result, tc.shouldReport) 279 } 280 }) 281 } 282 }