github.com/abayer/test-infra@v0.0.5/prow/pjutil/pjutil_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 "reflect" 21 "testing" 22 "time" 23 24 "k8s.io/apimachinery/pkg/api/equality" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/util/diff" 27 28 "k8s.io/test-infra/prow/kube" 29 ) 30 31 func TestPartitionActive(t *testing.T) { 32 tests := []struct { 33 pjs []kube.ProwJob 34 35 pending map[string]struct{} 36 triggered map[string]struct{} 37 }{ 38 { 39 pjs: []kube.ProwJob{ 40 { 41 ObjectMeta: metav1.ObjectMeta{ 42 Name: "foo", 43 }, 44 Status: kube.ProwJobStatus{ 45 State: kube.TriggeredState, 46 }, 47 }, 48 { 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: "bar", 51 }, 52 Status: kube.ProwJobStatus{ 53 State: kube.PendingState, 54 }, 55 }, 56 { 57 ObjectMeta: metav1.ObjectMeta{ 58 Name: "baz", 59 }, 60 Status: kube.ProwJobStatus{ 61 State: kube.SuccessState, 62 }, 63 }, 64 { 65 ObjectMeta: metav1.ObjectMeta{ 66 Name: "error", 67 }, 68 Status: kube.ProwJobStatus{ 69 State: kube.ErrorState, 70 }, 71 }, 72 { 73 ObjectMeta: metav1.ObjectMeta{ 74 Name: "bak", 75 }, 76 Status: kube.ProwJobStatus{ 77 State: kube.PendingState, 78 }, 79 }, 80 }, 81 pending: map[string]struct{}{ 82 "bar": {}, "bak": {}, 83 }, 84 triggered: map[string]struct{}{ 85 "foo": {}, 86 }, 87 }, 88 } 89 90 for i, test := range tests { 91 t.Logf("test run #%d", i) 92 pendingCh, triggeredCh := PartitionActive(test.pjs) 93 for job := range pendingCh { 94 if _, ok := test.pending[job.ObjectMeta.Name]; !ok { 95 t.Errorf("didn't find pending job %#v", job) 96 } 97 } 98 for job := range triggeredCh { 99 if _, ok := test.triggered[job.ObjectMeta.Name]; !ok { 100 t.Errorf("didn't find triggered job %#v", job) 101 } 102 } 103 } 104 } 105 106 func TestGetLatestProwJobs(t *testing.T) { 107 tests := []struct { 108 name string 109 110 pjs []kube.ProwJob 111 jobType string 112 113 expected map[string]struct{} 114 }{ 115 { 116 pjs: []kube.ProwJob{ 117 { 118 ObjectMeta: metav1.ObjectMeta{ 119 Name: "831c7df0-baa4-11e7-a1a4-0a58ac10134a", 120 }, 121 Spec: kube.ProwJobSpec{ 122 Type: kube.PresubmitJob, 123 Agent: kube.JenkinsAgent, 124 Job: "test_pull_request_origin_extended_networking_minimal", 125 Refs: &kube.Refs{ 126 Org: "openshift", 127 Repo: "origin", 128 BaseRef: "master", 129 BaseSHA: "e92d5c525795eafb82cf16e3ab151b567b47e333", 130 Pulls: []kube.Pull{ 131 { 132 Number: 17061, 133 Author: "enj", 134 SHA: "f94a3a51f59a693642e39084f03efa83af9442d3", 135 }, 136 }, 137 }, 138 Report: true, 139 Context: "ci/openshift-jenkins/extended_networking_minimal", 140 RerunCommand: "/test extended_networking_minimal", 141 }, 142 Status: kube.ProwJobStatus{ 143 StartTime: metav1.Date(2017, time.October, 26, 23, 22, 19, 0, time.UTC), 144 State: kube.FailureState, 145 Description: "Jenkins job failed.", 146 URL: "https://openshift-gce-devel.appspot.com/build/origin-ci-test/pr-logs/pull/17061/test_pull_request_origin_extended_networking_minimal/9756/", 147 PodName: "test_pull_request_origin_extended_networking_minimal-9756", 148 BuildID: "9756", 149 }, 150 }, 151 { 152 ObjectMeta: metav1.ObjectMeta{ 153 Name: "0079d4d3-ba25-11e7-ae3f-0a58ac10123b", 154 }, 155 Spec: kube.ProwJobSpec{ 156 Type: kube.PresubmitJob, 157 Agent: kube.JenkinsAgent, 158 Job: "test_pull_request_origin_extended_networking_minimal", 159 Refs: &kube.Refs{ 160 Org: "openshift", 161 Repo: "origin", 162 BaseRef: "master", 163 BaseSHA: "e92d5c525795eafb82cf16e3ab151b567b47e333", 164 Pulls: []kube.Pull{ 165 { 166 Number: 17061, 167 Author: "enj", 168 SHA: "f94a3a51f59a693642e39084f03efa83af9442d3", 169 }, 170 }, 171 }, 172 Report: true, 173 Context: "ci/openshift-jenkins/extended_networking_minimal", 174 RerunCommand: "/test extended_networking_minimal", 175 }, 176 Status: kube.ProwJobStatus{ 177 StartTime: metav1.Date(2017, time.October, 26, 22, 22, 19, 0, time.UTC), 178 State: kube.FailureState, 179 Description: "Jenkins job failed.", 180 URL: "https://openshift-gce-devel.appspot.com/build/origin-ci-test/pr-logs/pull/17061/test_pull_request_origin_extended_networking_minimal/9755/", 181 PodName: "test_pull_request_origin_extended_networking_minimal-9755", 182 BuildID: "9755", 183 }, 184 }, 185 }, 186 jobType: "presubmit", 187 expected: map[string]struct{}{"831c7df0-baa4-11e7-a1a4-0a58ac10134a": {}}, 188 }, 189 } 190 191 for _, test := range tests { 192 got := GetLatestProwJobs(test.pjs, kube.ProwJobType(test.jobType)) 193 if len(got) != len(test.expected) { 194 t.Errorf("expected jobs:\n%+v\ngot jobs:\n%+v", test.expected, got) 195 continue 196 } 197 for name := range test.expected { 198 if _, ok := got[name]; ok { 199 t.Errorf("expected job: %s", name) 200 } 201 } 202 } 203 } 204 205 func TestNewProwJob(t *testing.T) { 206 var testCases = []struct { 207 name string 208 spec kube.ProwJobSpec 209 labels map[string]string 210 expectedLabels map[string]string 211 }{ 212 { 213 name: "periodic job, no extra labels", 214 spec: kube.ProwJobSpec{ 215 Job: "job", 216 Type: kube.PeriodicJob, 217 }, 218 labels: map[string]string{}, 219 expectedLabels: map[string]string{ 220 "prow.k8s.io/job": "job", 221 "prow.k8s.io/type": "periodic", 222 }, 223 }, 224 { 225 name: "periodic job, extra labels", 226 spec: kube.ProwJobSpec{ 227 Job: "job", 228 Type: kube.PeriodicJob, 229 }, 230 labels: map[string]string{ 231 "extra": "stuff", 232 }, 233 expectedLabels: map[string]string{ 234 "prow.k8s.io/job": "job", 235 "prow.k8s.io/type": "periodic", 236 "extra": "stuff", 237 }, 238 }, 239 { 240 name: "presubmit job", 241 spec: kube.ProwJobSpec{ 242 Job: "job", 243 Type: kube.PresubmitJob, 244 Refs: &kube.Refs{ 245 Org: "org", 246 Repo: "repo", 247 Pulls: []kube.Pull{ 248 {Number: 1}, 249 }, 250 }, 251 }, 252 labels: map[string]string{}, 253 expectedLabels: map[string]string{ 254 "prow.k8s.io/job": "job", 255 "prow.k8s.io/type": "presubmit", 256 "prow.k8s.io/refs.org": "org", 257 "prow.k8s.io/refs.repo": "repo", 258 "prow.k8s.io/refs.pull": "1", 259 }, 260 }, 261 { 262 name: "non-github presubmit job", 263 spec: kube.ProwJobSpec{ 264 Job: "job", 265 Type: kube.PresubmitJob, 266 Refs: &kube.Refs{ 267 Org: "https://some-gerrit-instance.foo.com", 268 Repo: "some/invalid/repo", 269 Pulls: []kube.Pull{ 270 {Number: 1}, 271 }, 272 }, 273 }, 274 labels: map[string]string{}, 275 expectedLabels: map[string]string{ 276 "prow.k8s.io/job": "job", 277 "prow.k8s.io/type": "presubmit", 278 "prow.k8s.io/refs.org": "some-gerrit-instance.foo.com", 279 "prow.k8s.io/refs.repo": "repo", 280 "prow.k8s.io/refs.pull": "1", 281 }, 282 }, 283 } 284 285 for _, testCase := range testCases { 286 pj := NewProwJob(testCase.spec, testCase.labels) 287 if actual, expected := pj.Spec, testCase.spec; !equality.Semantic.DeepEqual(actual, expected) { 288 t.Errorf("%s: incorrect ProwJobSpec created: %s", testCase.name, diff.ObjectReflectDiff(actual, expected)) 289 } 290 if actual, expected := pj.Labels, testCase.expectedLabels; !reflect.DeepEqual(actual, expected) { 291 t.Errorf("%s: incorrect ProwJob labels created: %s", testCase.name, diff.ObjectReflectDiff(actual, expected)) 292 } 293 } 294 }