github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/requiresig/requiresig_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 requiresig 18 19 import ( 20 "regexp" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 "k8s.io/test-infra/prow/github" 25 "k8s.io/test-infra/prow/github/fakegithub" 26 "k8s.io/test-infra/prow/labels" 27 ) 28 29 const ( 30 helpWanted = "help-wanted" 31 open = "open" 32 sigApps = "sig/apps" 33 committeeSteering = "committee/steering" 34 wgContainerIdentity = "wg/container-identity" 35 username = "Ali" 36 ) 37 38 type fakePruner struct{} 39 40 func (fp *fakePruner) PruneComments(shouldPrune func(github.IssueComment) bool) {} 41 42 func TestHandle(t *testing.T) { 43 tests := []struct { 44 name string 45 action github.IssueEventAction 46 isPR bool 47 body string 48 initialLabels []string 49 unrelatedLabel bool 50 expectComment bool 51 expectedAdd string 52 expectedRemove string 53 }{ 54 { 55 name: "ignore PRs", 56 action: github.IssueActionLabeled, 57 isPR: true, 58 initialLabels: []string{helpWanted}, 59 }, 60 { 61 name: "issue closed action", 62 action: github.IssueActionClosed, 63 initialLabels: []string{helpWanted}, 64 }, 65 { 66 name: "issue has sig/foo label, no needs-sig label", 67 action: github.IssueActionLabeled, 68 initialLabels: []string{helpWanted, sigApps}, 69 }, 70 { 71 name: "issue has no sig/foo label, no needs-sig label", 72 action: github.IssueActionUnlabeled, 73 initialLabels: []string{helpWanted}, 74 expectComment: true, 75 expectedAdd: labels.NeedsSig, 76 }, 77 { 78 name: "issue has needs-sig label, no sig/foo label", 79 action: github.IssueActionLabeled, 80 initialLabels: []string{helpWanted, labels.NeedsSig}, 81 }, 82 { 83 name: "issue has both needs-sig label and sig/foo label", 84 action: github.IssueActionLabeled, 85 initialLabels: []string{helpWanted, labels.NeedsSig, sigApps}, 86 expectedRemove: labels.NeedsSig, 87 }, 88 { 89 name: "issue has committee/foo label, no needs-sig label", 90 action: github.IssueActionLabeled, 91 initialLabels: []string{helpWanted, committeeSteering}, 92 }, 93 { 94 name: "issue has both needs-sig label and committee/foo label", 95 action: github.IssueActionLabeled, 96 initialLabels: []string{helpWanted, labels.NeedsSig, committeeSteering}, 97 expectedRemove: labels.NeedsSig, 98 }, 99 { 100 name: "issue has wg/foo label, no needs-sig label", 101 action: github.IssueActionLabeled, 102 initialLabels: []string{helpWanted, wgContainerIdentity}, 103 }, 104 { 105 name: "issue has both needs-sig label and wg/foo label", 106 action: github.IssueActionLabeled, 107 initialLabels: []string{helpWanted, labels.NeedsSig, wgContainerIdentity}, 108 expectedRemove: labels.NeedsSig, 109 }, 110 { 111 name: "issue has no sig/foo label, no needs-sig label, body mentions sig", 112 action: github.IssueActionOpened, 113 body: "I am mentioning a sig @kubernetes/sig-testing-misc more stuff.", 114 initialLabels: []string{helpWanted}, 115 }, 116 { 117 name: "issue has no sig/foo label, no needs-sig label, body uses /sig command", 118 action: github.IssueActionOpened, 119 body: "I am using a sig command.\n/sig testing", 120 initialLabels: []string{helpWanted}, 121 }, 122 // Ignoring label events for labels other than sig labels prevents the 123 // plugin from adding and then removing the needs-sig label when new 124 // issues are created and include multiple label commands including a 125 // `/sig` command. In this case a label event caused by adding a non-sig 126 // label may occur before the `/sig` command is processed and the sig 127 // label is added. 128 { 129 name: "ignore non-sig label added events", 130 action: github.IssueActionLabeled, 131 body: "I am using a sig command.\n/kind bug\n/sig testing", 132 initialLabels: []string{helpWanted}, 133 unrelatedLabel: true, 134 }, 135 { 136 name: "ignore non-sig label removed events", 137 action: github.IssueActionUnlabeled, 138 body: "I am using a sig command.\n/kind bug\n/sig testing", 139 initialLabels: []string{helpWanted}, 140 unrelatedLabel: true, 141 }, 142 } 143 144 mentionRe := regexp.MustCompile(`(?m)@kubernetes/sig-testing-misc`) 145 for _, test := range tests { 146 fghc := &fakegithub.FakeClient{ 147 IssueComments: make(map[int][]github.IssueComment), 148 } 149 150 var initLabels []github.Label 151 for _, label := range test.initialLabels { 152 initLabels = append(initLabels, github.Label{Name: label}) 153 } 154 var pr *struct{} 155 if test.isPR { 156 pr = &struct{}{} 157 } 158 ie := &github.IssueEvent{ 159 Action: test.action, 160 Issue: github.Issue{ 161 Labels: initLabels, 162 Number: 5, 163 PullRequest: pr, 164 Body: test.body, 165 }, 166 } 167 if test.action == github.IssueActionUnlabeled || test.action == github.IssueActionLabeled { 168 if test.unrelatedLabel { 169 ie.Label.Name = labels.Bug 170 } else { 171 ie.Label.Name = "sig/awesome" 172 } 173 } 174 if err := handle(logrus.WithField("plugin", "require-sig"), fghc, &fakePruner{}, ie, mentionRe); err != nil { 175 t.Fatalf("[%s] Unexpected error from handle: %v.", test.name, err) 176 } 177 178 if got := len(fghc.IssueComments[5]); test.expectComment && got != 1 { 179 t.Errorf("[%s] Expected 1 comment to be created but got %d.", test.name, got) 180 } else if !test.expectComment && got != 0 { 181 t.Errorf("[%s] Expected no comments to be created but got %d.", test.name, got) 182 } 183 184 if count := len(fghc.IssueLabelsAdded); test.expectedAdd == "" && count != 0 { 185 t.Errorf("[%s] Unexpected labels added: %q.", test.name, fghc.IssueLabelsAdded) 186 } else if test.expectedAdd != "" && count == 1 { 187 if expected, got := "/#5:"+test.expectedAdd, fghc.IssueLabelsAdded[0]; got != expected { 188 t.Errorf("[%s] Expected label %q to be added but got %q.", test.name, expected, got) 189 } 190 } else if test.expectedAdd != "" && count > 1 { 191 t.Errorf("[%s] Expected label \"/#5:%s\" to be added but got %q.", test.name, test.expectedAdd, fghc.IssueLabelsAdded) 192 } 193 194 if count := len(fghc.IssueLabelsRemoved); test.expectedRemove == "" && count != 0 { 195 t.Errorf("[%s] Unexpected labels removed: %q.", test.name, fghc.IssueLabelsRemoved) 196 } else if test.expectedRemove != "" && count == 1 { 197 if expected, got := "/#5:"+test.expectedRemove, fghc.IssueLabelsRemoved[0]; got != expected { 198 t.Errorf("[%s] Expected label %q to be removed but got %q.", test.name, expected, got) 199 } 200 } else if test.expectedRemove != "" && count > 1 { 201 t.Errorf("[%s] Expected label \"/#5:%s\" to be removed but got %q.", test.name, test.expectedRemove, fghc.IssueLabelsRemoved) 202 } 203 } 204 }