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