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