github.com/abayer/test-infra@v0.0.5/prow/plugins/sigmention/sigmention_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 sigmention 18 19 import ( 20 "fmt" 21 "reflect" 22 "regexp" 23 "sort" 24 "strings" 25 "testing" 26 27 "github.com/sirupsen/logrus" 28 29 "k8s.io/test-infra/prow/github" 30 "k8s.io/test-infra/prow/github/fakegithub" 31 ) 32 33 func formatLabels(labels []string) []string { 34 r := []string{} 35 for _, l := range labels { 36 r = append(r, fmt.Sprintf("%s/%s#%d:%s", "org", "repo", 1, l)) 37 } 38 if len(r) == 0 { 39 return nil 40 } 41 return r 42 } 43 44 func TestSigMention(t *testing.T) { 45 orgMember := "cjwagner" 46 nonOrgMember := "john" 47 bot := "k8s-ci-robot" 48 type testCase struct { 49 name string 50 body string 51 commenter string 52 expectedRepeats []string 53 expectedNewLabels []string 54 issueLabels []string 55 repoLabels []string 56 regexp string 57 } 58 testcases := []testCase{ 59 { 60 name: "Dont repeat when org member mentions sig", 61 body: "@kubernetes/sig-node-misc", 62 expectedRepeats: []string{}, 63 expectedNewLabels: []string{"sig/node"}, 64 repoLabels: []string{"area/infra", "priority/urgent", "sig/node"}, 65 issueLabels: []string{}, 66 commenter: orgMember, 67 }, 68 { 69 name: "Dont repeat or label when bot adds mentions sig", 70 body: "@kubernetes/sig-node-misc", 71 expectedRepeats: []string{}, 72 expectedNewLabels: []string{}, 73 repoLabels: []string{"area/infra", "priority/urgent", "sig/node"}, 74 issueLabels: []string{}, 75 commenter: bot, 76 }, 77 { 78 name: "Repeat when non org adds one sig label (sig label already present)", 79 body: "@kubernetes/sig-node-bugs", 80 expectedRepeats: []string{"@kubernetes/sig-node-bugs"}, 81 expectedNewLabels: []string{"kind/bug"}, 82 repoLabels: []string{"area/infra", "priority/urgent", "sig/node", "kind/bug"}, 83 issueLabels: []string{"sig/node"}, 84 commenter: nonOrgMember, 85 }, 86 { 87 name: "Don't repeat non existent labels", 88 body: "@kubernetes/sig-node-misc @kubernetes/sig-api-machinery-bugs", 89 expectedRepeats: []string{}, 90 expectedNewLabels: []string{}, 91 repoLabels: []string{}, 92 issueLabels: []string{}, 93 commenter: nonOrgMember, 94 }, 95 { 96 name: "Dont repeat multiple if org member (all labels present).", 97 body: "@kubernetes/sig-node-misc @kubernetes/sig-api-machinery-bugs", 98 expectedRepeats: []string{}, 99 expectedNewLabels: []string{}, 100 repoLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 101 issueLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 102 commenter: orgMember, 103 }, 104 { 105 name: "Repeat multiple valid labels from non org member", 106 body: "@kubernetes/sig-node-misc @kubernetes/sig-api-machinery-bugs", 107 expectedRepeats: []string{"@kubernetes/sig-node-misc", "@kubernetes/sig-api-machinery-bugs"}, 108 expectedNewLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 109 repoLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 110 issueLabels: []string{}, 111 commenter: nonOrgMember, 112 }, 113 { 114 name: "Repeat multiple valid labels with a line break from non org member.", 115 body: "@kubernetes/sig-node-misc\n@kubernetes/sig-api-machinery-bugs", 116 expectedRepeats: []string{"@kubernetes/sig-node-misc", "@kubernetes/sig-api-machinery-bugs"}, 117 expectedNewLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 118 repoLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 119 issueLabels: []string{}, 120 commenter: nonOrgMember, 121 }, 122 { 123 name: "Repeat Multiple Sig Labels Different Lines With Other Text", 124 body: "Code Comment. Design Review\n@kubernetes/sig-node-proposals\ncc @kubernetes/sig-api-machinery-bugs", 125 expectedRepeats: []string{"@kubernetes/sig-node-proposals", "@kubernetes/sig-api-machinery-bugs"}, 126 expectedNewLabels: []string{"sig/node", "sig/api-machinery", "kind/bug"}, 127 repoLabels: []string{"area/infra", "priority/urgent", "sig/node", "sig/api-machinery", "kind/bug"}, 128 issueLabels: []string{}, 129 commenter: nonOrgMember, 130 }, 131 { 132 name: "Repeat when multiple label adding commands (sig labels present)", 133 body: "/area infra\n/priority urgent Design Review\n@kubernetes/sig-node-misc\ncc @kubernetes/sig-api-machinery-bugs", 134 expectedRepeats: []string{"@kubernetes/sig-node-misc", "@kubernetes/sig-api-machinery-bugs"}, 135 expectedNewLabels: []string{"sig/node", "kind/bug"}, 136 repoLabels: []string{"area/infra", "priority/urgent", "sig/node", "sig/api-machinery", "sig/testing", "kind/bug"}, 137 issueLabels: []string{"sig/api-machinery", "sig/testing"}, 138 commenter: nonOrgMember, 139 }, 140 { 141 name: "Works for non-specialized teams", 142 body: "@openshift/sig-node", 143 expectedRepeats: []string{}, 144 expectedNewLabels: []string{"sig/node"}, 145 repoLabels: []string{"area/infra", "priority/urgent", "sig/node"}, 146 issueLabels: []string{}, 147 commenter: orgMember, 148 regexp: `(?m)@openshift/sig-([\w-]*)`, 149 }, 150 } 151 152 for _, tc := range testcases { 153 fakeClient := &fakegithub.FakeClient{ 154 OrgMembers: map[string][]string{"org": {orgMember, bot}}, 155 ExistingLabels: tc.repoLabels, 156 IssueComments: make(map[int][]github.IssueComment), 157 } 158 // Add initial labels to issue. 159 for _, label := range tc.issueLabels { 160 fakeClient.AddLabel("org", "repo", 1, label) 161 } 162 e := &github.GenericCommentEvent{ 163 Action: github.GenericCommentActionCreated, 164 Body: tc.body, 165 Number: 1, 166 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 167 User: github.User{Login: tc.commenter}, 168 } 169 170 testRe := tc.regexp 171 if testRe == "" { 172 testRe = `(?m)@kubernetes/sig-([\w-]*)-(misc|test-failures|bugs|feature-requests|proposals|pr-reviews|api-reviews)` 173 } 174 re, err := regexp.Compile(testRe) 175 if err != nil { 176 t.Errorf("unexpected error: %v", err) 177 continue 178 } 179 180 if err := handle(fakeClient, logrus.WithField("plugin", pluginName), e, re); err != nil { 181 t.Errorf("(%s): Unexpected error from handle: %v.", tc.name, err) 182 continue 183 } 184 185 // Check that all the correct labels (and only the correct labels) were added. 186 expectLabels := append(formatLabels(tc.expectedNewLabels), formatLabels(tc.issueLabels)...) 187 sort.Strings(expectLabels) 188 sort.Strings(fakeClient.LabelsAdded) 189 if !reflect.DeepEqual(expectLabels, fakeClient.LabelsAdded) { 190 t.Errorf("(%s): Expected issue to end with labels %q, but ended with %q.", tc.name, expectLabels, fakeClient.LabelsAdded) 191 } 192 193 // Check that the comment contains the correct sig mentions repeats if it exists. 194 comments := fakeClient.IssueComments[1] 195 if len(tc.expectedRepeats) == 0 { 196 if len(comments) > 0 { 197 t.Errorf("(%s): No sig mentions should have been repeated, but a comment was still made.", tc.name) 198 } 199 continue 200 } 201 if len(comments) != 1 { 202 t.Errorf( 203 "(%s): Expected sig mentions to be repeated in 1 comment, but %d comments were created!", 204 tc.name, 205 len(comments), 206 ) 207 continue 208 } 209 for _, repeat := range tc.expectedRepeats { 210 if !strings.Contains(comments[0].Body, repeat) { 211 t.Errorf("(%s): Comment body does not contain sig mention %q.", tc.name, repeat) 212 } 213 } 214 } 215 }