github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/heart/heart_test.go (about)

     1  /*
     2  Copyright 2016 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 heart
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"k8s.io/test-infra/prow/github"
    25  	"k8s.io/test-infra/prow/github/fakegithub"
    26  )
    27  
    28  func TestHandlePR(t *testing.T) {
    29  	basicPR := github.PullRequest{
    30  		Number: 1,
    31  		Base: github.PullRequestBranch{
    32  			Repo: github.Repo{
    33  				Owner: github.User{
    34  					Login: "kubernetes",
    35  				},
    36  				Name: "kubernetes",
    37  			},
    38  		},
    39  	}
    40  
    41  	testcases := []struct {
    42  		prAction              github.PullRequestEventAction
    43  		changes               []github.PullRequestChange
    44  		expectedReactionAdded bool
    45  	}{
    46  		// PR opened against kubernetes/kubernetes that adds 1 line to
    47  		// an OWNERS file
    48  		{
    49  			prAction: github.PullRequestActionOpened,
    50  			changes: []github.PullRequestChange{
    51  				{
    52  					Filename:  "foo/bar/OWNERS",
    53  					Additions: 1,
    54  				},
    55  			},
    56  			expectedReactionAdded: true,
    57  		},
    58  		// PR opened against kubernetes/kubernetes that deletes 1 line
    59  		// from an OWNERS file
    60  		{
    61  			prAction: github.PullRequestActionOpened,
    62  			changes: []github.PullRequestChange{
    63  				{
    64  					Filename:  "foo/bar/OWNERS",
    65  					Deletions: 1,
    66  				},
    67  			},
    68  			expectedReactionAdded: false,
    69  		},
    70  		// PR opened against kubernetes/kubernetes with no changes to
    71  		// OWNERS
    72  		{
    73  			prAction: github.PullRequestActionOpened,
    74  			changes: []github.PullRequestChange{
    75  				{
    76  					Filename:  "foo/bar/foo.go",
    77  					Additions: 1,
    78  				},
    79  			},
    80  			expectedReactionAdded: false,
    81  		},
    82  		// PR reopened against kubernetes/kubernetes
    83  		{
    84  			prAction: github.PullRequestActionReopened,
    85  			changes: []github.PullRequestChange{
    86  				{
    87  					Filename:  "foo/bar/OWNERS",
    88  					Additions: 1,
    89  				},
    90  			},
    91  			expectedReactionAdded: false,
    92  		},
    93  		// PR opened against kubernetes/kubernetes that adds 1 line to
    94  		// an OWNERS_ALIASES file
    95  		{
    96  			prAction: github.PullRequestActionOpened,
    97  			changes: []github.PullRequestChange{
    98  				{
    99  					Filename:  "foo/bar/OWNERS_ALIASES",
   100  					Additions: 1,
   101  				},
   102  			},
   103  			expectedReactionAdded: true,
   104  		},
   105  	}
   106  
   107  	for _, tc := range testcases {
   108  		event := github.PullRequestEvent{
   109  			Action:      tc.prAction,
   110  			Number:      basicPR.Number,
   111  			PullRequest: basicPR,
   112  		}
   113  		fakeGitHubClient := &fakegithub.FakeClient{
   114  			PullRequests: map[int]*github.PullRequest{
   115  				basicPR.Number: &basicPR,
   116  			},
   117  			PullRequestChanges: map[int][]github.PullRequestChange{
   118  				basicPR.Number: tc.changes,
   119  			},
   120  		}
   121  		fakeClient := client{
   122  			GitHubClient: fakeGitHubClient,
   123  			Logger:       logrus.WithField("plugin", pluginName),
   124  		}
   125  
   126  		err := handlePR(fakeClient, event)
   127  		if err != nil {
   128  			t.Fatal(err)
   129  		}
   130  
   131  		if len(fakeGitHubClient.IssueReactionsAdded) > 0 && !tc.expectedReactionAdded {
   132  			t.Fatalf("Expected no reactions to be added for %+v", tc)
   133  
   134  		} else if len(fakeGitHubClient.IssueReactionsAdded) == 0 && tc.expectedReactionAdded {
   135  			t.Fatalf("Expected reaction to be added for %+v", tc)
   136  		}
   137  	}
   138  }