github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/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 RegexpChangeMatcher: config.RegexpChangeMatcher{ 114 RunIfChanged: "^(test/integration)", 115 }, 116 Context: "integration-tests", 117 }, 118 }, 119 sha: "shalala", 120 event: &github.GenericCommentEvent{ 121 IsPR: true, 122 IssueState: "open", 123 Action: github.GenericCommentActionCreated, 124 Body: "/skip", 125 Number: 1, 126 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 127 }, 128 existing: []github.Status{ 129 { 130 State: github.StatusSuccess, 131 Context: "unit-tests", 132 }, 133 { 134 State: github.StatusFailure, 135 Context: "extended-tests", 136 }, 137 { 138 State: github.StatusPending, 139 Context: "integration-tests", 140 }, 141 }, 142 prChanges: map[int][]github.PullRequestChange{ 143 1: { 144 { 145 Filename: "test/integration/main.go", 146 }, 147 { 148 Filename: "README.md", 149 }, 150 }, 151 }, 152 153 expected: []github.Status{ 154 { 155 State: github.StatusSuccess, 156 Context: "unit-tests", 157 }, 158 { 159 State: github.StatusSuccess, 160 Description: "Skipped", 161 Context: "extended-tests", 162 }, 163 { 164 State: github.StatusPending, 165 Context: "integration-tests", 166 }, 167 }, 168 }, 169 { 170 name: "Skip tests with PR changes that do not need to run", 171 172 presubmits: []config.Presubmit{ 173 { 174 RegexpChangeMatcher: config.RegexpChangeMatcher{ 175 RunIfChanged: "^(test/integration)", 176 }, 177 Context: "integration-tests", 178 }, 179 }, 180 sha: "shalala", 181 event: &github.GenericCommentEvent{ 182 IsPR: true, 183 IssueState: "open", 184 Action: github.GenericCommentActionCreated, 185 Body: "/skip", 186 Number: 1, 187 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 188 }, 189 existing: []github.Status{ 190 { 191 State: github.StatusPending, 192 Context: "integration-tests", 193 }, 194 }, 195 prChanges: map[int][]github.PullRequestChange{ 196 1: { 197 { 198 Filename: "build/core.sh", 199 }, 200 { 201 Filename: "README.md", 202 }, 203 }, 204 }, 205 206 expected: []github.Status{ 207 { 208 State: github.StatusSuccess, 209 Description: "Skipped", 210 Context: "integration-tests", 211 }, 212 }, 213 }, 214 { 215 name: "Skip broken but skippable tests", 216 217 presubmits: []config.Presubmit{ 218 { 219 SkipReport: true, 220 RegexpChangeMatcher: config.RegexpChangeMatcher{ 221 RunIfChanged: "^(test/integration)", 222 }, 223 Context: "integration-tests", 224 }, 225 }, 226 sha: "shalala", 227 event: &github.GenericCommentEvent{ 228 IsPR: true, 229 IssueState: "open", 230 Action: github.GenericCommentActionCreated, 231 Body: "/skip", 232 Number: 1, 233 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 234 }, 235 existing: []github.Status{ 236 { 237 State: github.StatusPending, 238 Context: "integration-tests", 239 }, 240 }, 241 prChanges: map[int][]github.PullRequestChange{ 242 1: { 243 { 244 Filename: "test/integration/main.go", 245 }, 246 { 247 Filename: "README.md", 248 }, 249 }, 250 }, 251 252 expected: []github.Status{ 253 { 254 State: github.StatusSuccess, 255 Description: "Skipped", 256 Context: "integration-tests", 257 }, 258 }, 259 }, 260 } 261 262 for _, test := range tests { 263 t.Logf("running scenario %q", test.name) 264 if err := config.SetPresubmitRegexes(test.presubmits); err != nil { 265 t.Fatal(err) 266 } 267 268 fghc := &fakegithub.FakeClient{ 269 IssueComments: make(map[int][]github.IssueComment), 270 PullRequests: map[int]*github.PullRequest{ 271 test.event.Number: { 272 Head: github.PullRequestBranch{ 273 SHA: test.sha, 274 }, 275 }, 276 }, 277 PullRequestChanges: test.prChanges, 278 CreatedStatuses: map[string][]github.Status{ 279 test.sha: test.existing, 280 }, 281 } 282 l := logrus.WithField("plugin", pluginName) 283 284 if err := handle(fghc, l, test.event, test.presubmits); err != nil { 285 t.Errorf("unexpected error: %v.", err) 286 continue 287 } 288 289 // Check that the correct statuses have been updated. 290 created := fghc.CreatedStatuses[test.sha] 291 if len(test.expected) != len(created) { 292 t.Errorf("status mismatch: expected:\n%+v\ngot:\n%+v", test.expected, created) 293 continue 294 } 295 out: 296 for _, got := range created { 297 var found bool 298 for _, exp := range test.expected { 299 if exp.Context == got.Context { 300 found = true 301 if !reflect.DeepEqual(exp, got) { 302 t.Errorf("expected status: %v, got: %v", exp, got) 303 break out 304 } 305 } 306 } 307 if !found { 308 t.Errorf("expected context %q in the results: %v", got.Context, created) 309 break 310 } 311 } 312 } 313 }