github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/slackevents/slackevents_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 slackevents 18 19 import ( 20 "encoding/json" 21 "strings" 22 "testing" 23 24 "k8s.io/test-infra/prow/github" 25 "k8s.io/test-infra/prow/github/fakegithub" 26 "k8s.io/test-infra/prow/plugins" 27 "k8s.io/test-infra/prow/slack" 28 "k8s.io/test-infra/prow/slack/fakeslack" 29 ) 30 31 func TestPush(t *testing.T) { 32 var pushStr string = `{ 33 "ref": "refs/heads/master", 34 "before": "d73a75b4b1ddb63870954b9a60a63acaa4cb1ca5", 35 "after": "045a6dca07840efaf3311450b615e19b5c75f787", 36 "compare": "https://github.com/kubernetes/kubernetes/compare/d73a75b4b1dd...045a6dca0784", 37 "commits": [ 38 { 39 "id": "8427d5a27478c80167fd66affe1bd7cd01d3f9a8", 40 "message": "Decrease fluentd cpu request", 41 "url": "https://github.com/kubernetes/kubernetes/commit/8427d5a27478c80167fd66affe1bd7cd01d3f9a8" 42 }, 43 { 44 "id": "045a6dca07840efaf3311450b615e19b5c75f787", 45 "message": "Merge pull request #47906 from gmarek/fluentd\n\nDecrese fluentd cpu request\n\nFix #47905\r\n\r\ncc @piosz - this should fix your tests.\r\ncc @dchen1107", 46 "url": "https://github.com/kubernetes/kubernetes/commit/045a6dca07840efaf3311450b615e19b5c75f787" 47 } 48 ], 49 "repository": { 50 "id": 20580498, 51 "name": "kubernetes", 52 "owner": { 53 "name": "kubernetes", 54 "login": "kubernetes" 55 }, 56 "url": "https://github.com/kubernetes/kubernetes" 57 }, 58 "pusher": { 59 "name": "k8s-merge-robot", 60 "email": "k8s-merge-robot@users.noreply.github.com" 61 } 62 }` 63 64 var pushEv github.PushEvent 65 if err := json.Unmarshal([]byte(pushStr), &pushEv); err != nil { 66 t.Fatalf("Failed to parse Push Notification: %s", err) 67 } 68 69 // Non bot user merged the PR 70 pushEvManual := pushEv 71 pushEvManual.Pusher.Name = "Jester Tester" 72 pushEvManual.Pusher.Email = "tester@users.noreply.github.com" 73 pushEvManual.Sender.Login = "tester" 74 75 type testCase struct { 76 name string 77 pushReq github.PushEvent 78 expectedMessages map[string][]string 79 } 80 81 testcases := []testCase{ 82 { 83 name: "If PR merged manually by a user we send message to sig-contribex and kubernetes-dev.", 84 pushReq: pushEvManual, 85 expectedMessages: map[string][]string{ 86 "sig-contribex": {"Warning: <@tester> manually merged https://github.com/kubernetes/kubernetes/compare/d73a75b4b1dd...045a6dca0784"}, 87 "kubernetes-dev": {"Warning: <@tester> manually merged https://github.com/kubernetes/kubernetes/compare/d73a75b4b1dd...045a6dca0784"}}, 88 }, 89 { 90 name: "If PR merged by k8s merge bot we should NOT send message to sig-contribex and kubernetes-dev.", 91 pushReq: pushEv, 92 expectedMessages: map[string][]string{}, 93 }, 94 } 95 96 pc := client{ 97 SlackConfig: plugins.Slack{ 98 MergeWarnings: []plugins.MergeWarning{ 99 { 100 Repos: []string{"kubernetes/kubernetes"}, 101 Channels: []string{"kubernetes-dev", "sig-contribex"}, 102 WhiteList: []string{"k8s-merge-robot"}, 103 }, 104 }, 105 }, 106 SlackClient: slack.NewFakeClient(), 107 } 108 109 //should not fail if slackClient is nil 110 for _, tc := range testcases { 111 if err := notifyOnSlackIfManualMerge(pc, tc.pushReq); err != nil { 112 t.Fatalf("Didn't expect error if slack client is nil: %s", err) 113 } 114 } 115 116 //repeat the tests with a fake slack client 117 for _, tc := range testcases { 118 slackClient := &fakeslack.FakeClient{ 119 SentMessages: make(map[string][]string), 120 } 121 pc.SlackClient = slackClient 122 123 if err := notifyOnSlackIfManualMerge(pc, tc.pushReq); err != nil { 124 t.Fatalf("Didn't expect error: %s", err) 125 } 126 if len(tc.expectedMessages) != len(slackClient.SentMessages) { 127 t.Fatalf("Test: %s The number of messages sent do not tally. Expecting %d messages but received %d messages.", 128 tc.name, len(tc.expectedMessages), len(slackClient.SentMessages)) 129 } 130 for k, v := range tc.expectedMessages { 131 if _, ok := slackClient.SentMessages[k]; !ok { 132 t.Fatalf("Test: %s Messages is not sent to channel %s", tc.name, k) 133 } 134 if strings.Compare(v[0], slackClient.SentMessages[k][0]) != 0 { 135 t.Fatalf("Expecting message: %s\nReceived message: %s", v, slackClient.SentMessages[k]) 136 } 137 if len(v) != len(slackClient.SentMessages[k]) { 138 t.Fatalf("Test: %s All messages are not delivered to the channel ", tc.name) 139 } 140 } 141 } 142 143 } 144 145 //Make sure we are sending message to proper sig mentions 146 func TestComment(t *testing.T) { 147 orgMember := "cjwagner" 148 bot := "k8s-ci-robot" 149 type testCase struct { 150 name string 151 action github.GenericCommentEventAction 152 body string 153 expectedMessages map[string][]string 154 issueLabels []string 155 repoLabels []string 156 commenter string 157 } 158 testcases := []testCase{ 159 { 160 name: "If sig mentioned then we send a message to the sig with the body of the comment", 161 action: github.GenericCommentActionCreated, 162 body: "@kubernetes/sig-node-misc This issue needs update.", 163 expectedMessages: map[string][]string{"sig-node": {"This issue needs update."}}, 164 commenter: orgMember, 165 }, 166 { 167 name: "Don't sent message if comment isn't new.", 168 action: github.GenericCommentActionEdited, 169 body: "@kubernetes/sig-node-misc This issue needs update.", 170 expectedMessages: map[string][]string{}, 171 commenter: orgMember, 172 }, 173 { 174 name: "Don't sent message if commenter is the bot.", 175 action: github.GenericCommentActionEdited, 176 body: "@kubernetes/sig-node-misc This issue needs update.", 177 expectedMessages: map[string][]string{}, 178 commenter: bot, 179 }, 180 { 181 name: "If multiple sigs mentioned, we send a message to each sig with the body of the comment", 182 action: github.GenericCommentActionCreated, 183 body: "@kubernetes/sig-node-misc, @kubernetes/sig-api-machinery-misc Message sent to multiple sigs.", 184 expectedMessages: map[string][]string{"sig-api-machinery": {"Message sent to multiple sigs."}, "sig-node": {"Message sent to multiple sigs."}}, 185 commenter: orgMember, 186 }, 187 { 188 name: "If multiple sigs mentioned, but only one channel is whitelisted, only send to one channel.", 189 action: github.GenericCommentActionCreated, 190 body: "@kubernetes/sig-node-misc, @kubernetes/sig-testing-misc Message sent to multiple sigs.", 191 expectedMessages: map[string][]string{"sig-node": {"Message sent to multiple sigs."}}, 192 issueLabels: []string{}, 193 commenter: orgMember, 194 }, 195 { 196 name: "Message should not be sent if the pattern for the channel does not match", 197 action: github.GenericCommentActionCreated, 198 body: "@kubernetes/node-misc No message sent", 199 expectedMessages: map[string][]string{}, 200 commenter: orgMember, 201 }, 202 { 203 name: "Message sent only if the pattern for the channel match", 204 action: github.GenericCommentActionCreated, 205 body: "@kubernetes/node-misc @kubernetes/sig-api-machinery-bugs Message sent to matching sigs.", 206 expectedMessages: map[string][]string{"sig-api-machinery": {"Message sent to matching sigs."}}, 207 commenter: orgMember, 208 }, 209 } 210 211 for _, tc := range testcases { 212 fakeSlackClient := &fakeslack.FakeClient{ 213 SentMessages: make(map[string][]string), 214 } 215 client := client{ 216 GithubClient: &fakegithub.FakeClient{}, 217 SlackClient: fakeSlackClient, 218 SlackConfig: plugins.Slack{MentionChannels: []string{"sig-node", "sig-api-machinery"}}, 219 } 220 e := github.GenericCommentEvent{ 221 Action: tc.action, 222 Body: tc.body, 223 User: github.User{Login: tc.commenter}, 224 } 225 226 if err := echoToSlack(client, e); err != nil { 227 t.Fatalf("For case %s, didn't expect error from label test: %v", tc.name, err) 228 } 229 if len(tc.expectedMessages) != len(fakeSlackClient.SentMessages) { 230 t.Fatalf("The number of messages sent do not tally. Expecting %d messages but received %d messages.", 231 len(tc.expectedMessages), len(fakeSlackClient.SentMessages)) 232 } 233 for k, v := range tc.expectedMessages { 234 if _, ok := fakeSlackClient.SentMessages[k]; !ok { 235 t.Fatalf("Messages is not sent to channel %s", k) 236 } 237 if len(v) != len(fakeSlackClient.SentMessages[k]) { 238 t.Fatalf("All messages are not delivered to the channel %s", k) 239 } 240 } 241 } 242 }