github.com/abayer/test-infra@v0.0.5/prow/plugins/skip/skip_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 skip 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/test-infra/prow/config" 26 "k8s.io/test-infra/prow/github" 27 "k8s.io/test-infra/prow/github/fakegithub" 28 ) 29 30 func TestSkipStatus(t *testing.T) { 31 tests := []struct { 32 name string 33 34 presubmits []config.Presubmit 35 sha string 36 event *github.GenericCommentEvent 37 prChanges map[int][]github.PullRequestChange 38 existing []github.Status 39 40 expected []github.Status 41 }{ 42 { 43 name: "Skip some tests", 44 45 presubmits: []config.Presubmit{ 46 { 47 AlwaysRun: true, 48 Context: "unit-tests", 49 }, 50 { 51 AlwaysRun: false, 52 Context: "extended-tests", 53 }, 54 { 55 AlwaysRun: false, 56 Context: "integration-tests", 57 }, 58 }, 59 sha: "shalala", 60 event: &github.GenericCommentEvent{ 61 IsPR: true, 62 IssueState: "open", 63 Action: github.GenericCommentActionCreated, 64 Body: "/skip", 65 Number: 1, 66 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 67 }, 68 existing: []github.Status{ 69 { 70 State: github.StatusSuccess, 71 Context: "unit-tests", 72 }, 73 { 74 State: github.StatusFailure, 75 Context: "extended-tests", 76 }, 77 { 78 State: github.StatusPending, 79 Context: "integration-tests", 80 }, 81 }, 82 83 expected: []github.Status{ 84 { 85 State: github.StatusSuccess, 86 Context: "unit-tests", 87 }, 88 { 89 State: github.StatusSuccess, 90 Description: "Skipped", 91 Context: "extended-tests", 92 }, 93 { 94 State: github.StatusSuccess, 95 Description: "Skipped", 96 Context: "integration-tests", 97 }, 98 }, 99 }, 100 { 101 name: "Do not skip tests with PR changes that need to run", 102 103 presubmits: []config.Presubmit{ 104 { 105 AlwaysRun: true, 106 Context: "unit-tests", 107 }, 108 { 109 AlwaysRun: false, 110 Context: "extended-tests", 111 }, 112 { 113 RunIfChanged: "^(test/integration)", 114 Context: "integration-tests", 115 }, 116 }, 117 sha: "shalala", 118 event: &github.GenericCommentEvent{ 119 IsPR: true, 120 IssueState: "open", 121 Action: github.GenericCommentActionCreated, 122 Body: "/skip", 123 Number: 1, 124 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 125 }, 126 existing: []github.Status{ 127 { 128 State: github.StatusSuccess, 129 Context: "unit-tests", 130 }, 131 { 132 State: github.StatusFailure, 133 Context: "extended-tests", 134 }, 135 { 136 State: github.StatusPending, 137 Context: "integration-tests", 138 }, 139 }, 140 prChanges: map[int][]github.PullRequestChange{ 141 1: { 142 { 143 Filename: "test/integration/main.go", 144 }, 145 { 146 Filename: "README.md", 147 }, 148 }, 149 }, 150 151 expected: []github.Status{ 152 { 153 State: github.StatusSuccess, 154 Context: "unit-tests", 155 }, 156 { 157 State: github.StatusSuccess, 158 Description: "Skipped", 159 Context: "extended-tests", 160 }, 161 { 162 State: github.StatusPending, 163 Context: "integration-tests", 164 }, 165 }, 166 }, 167 { 168 name: "Skip tests with PR changes that do not need to run", 169 170 presubmits: []config.Presubmit{ 171 { 172 RunIfChanged: "^(test/integration)", 173 Context: "integration-tests", 174 }, 175 }, 176 sha: "shalala", 177 event: &github.GenericCommentEvent{ 178 IsPR: true, 179 IssueState: "open", 180 Action: github.GenericCommentActionCreated, 181 Body: "/skip", 182 Number: 1, 183 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 184 }, 185 existing: []github.Status{ 186 { 187 State: github.StatusPending, 188 Context: "integration-tests", 189 }, 190 }, 191 prChanges: map[int][]github.PullRequestChange{ 192 1: { 193 { 194 Filename: "build/core.sh", 195 }, 196 { 197 Filename: "README.md", 198 }, 199 }, 200 }, 201 202 expected: []github.Status{ 203 { 204 State: github.StatusSuccess, 205 Description: "Skipped", 206 Context: "integration-tests", 207 }, 208 }, 209 }, 210 { 211 name: "Skip broken but skippable tests", 212 213 presubmits: []config.Presubmit{ 214 { 215 SkipReport: true, 216 RunIfChanged: "^(test/integration)", 217 Context: "integration-tests", 218 }, 219 }, 220 sha: "shalala", 221 event: &github.GenericCommentEvent{ 222 IsPR: true, 223 IssueState: "open", 224 Action: github.GenericCommentActionCreated, 225 Body: "/skip", 226 Number: 1, 227 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 228 }, 229 existing: []github.Status{ 230 { 231 State: github.StatusPending, 232 Context: "integration-tests", 233 }, 234 }, 235 prChanges: map[int][]github.PullRequestChange{ 236 1: { 237 { 238 Filename: "test/integration/main.go", 239 }, 240 { 241 Filename: "README.md", 242 }, 243 }, 244 }, 245 246 expected: []github.Status{ 247 { 248 State: github.StatusSuccess, 249 Description: "Skipped", 250 Context: "integration-tests", 251 }, 252 }, 253 }, 254 } 255 256 for _, test := range tests { 257 t.Logf("running scenario %q", test.name) 258 if err := config.SetPresubmitRegexes(test.presubmits); err != nil { 259 t.Fatal(err) 260 } 261 262 fghc := &fakegithub.FakeClient{ 263 IssueComments: make(map[int][]github.IssueComment), 264 PullRequests: map[int]*github.PullRequest{ 265 test.event.Number: { 266 Head: github.PullRequestBranch{ 267 SHA: test.sha, 268 }, 269 }, 270 }, 271 PullRequestChanges: test.prChanges, 272 CreatedStatuses: map[string][]github.Status{ 273 test.sha: test.existing, 274 }, 275 } 276 l := logrus.WithField("plugin", pluginName) 277 278 if err := handle(fghc, l, test.event, test.presubmits); err != nil { 279 t.Errorf("unexpected error: %v.", err) 280 continue 281 } 282 283 // Check that the correct statuses have been updated. 284 created := fghc.CreatedStatuses[test.sha] 285 if len(test.expected) != len(created) { 286 t.Errorf("status mismatch: expected:\n%+v\ngot:\n%+v", test.expected, created) 287 continue 288 } 289 out: 290 for _, got := range created { 291 var found bool 292 for _, exp := range test.expected { 293 if exp.Context == got.Context { 294 found = true 295 if !reflect.DeepEqual(exp, got) { 296 t.Errorf("expected status: %v, got: %v", exp, got) 297 break out 298 } 299 } 300 } 301 if !found { 302 t.Errorf("expected context %q in the results: %v", got.Context, created) 303 break 304 } 305 } 306 } 307 }