github.com/abayer/test-infra@v0.0.5/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  	}
    94  
    95  	for _, tc := range testcases {
    96  		event := github.PullRequestEvent{
    97  			Action:      tc.prAction,
    98  			Number:      basicPR.Number,
    99  			PullRequest: basicPR,
   100  		}
   101  		fakeGitHubClient := &fakegithub.FakeClient{
   102  			PullRequests: map[int]*github.PullRequest{
   103  				basicPR.Number: &basicPR,
   104  			},
   105  			PullRequestChanges: map[int][]github.PullRequestChange{
   106  				basicPR.Number: tc.changes,
   107  			},
   108  		}
   109  		fakeClient := client{
   110  			GitHubClient: fakeGitHubClient,
   111  			Logger:       logrus.WithField("plugin", pluginName),
   112  		}
   113  
   114  		err := handlePR(fakeClient, event)
   115  		if err != nil {
   116  			t.Fatal(err)
   117  		}
   118  
   119  		if len(fakeGitHubClient.IssueReactionsAdded) > 0 && !tc.expectedReactionAdded {
   120  			t.Fatalf("Expected no reactions to be added for %+v", tc)
   121  
   122  		} else if len(fakeGitHubClient.IssueReactionsAdded) == 0 && tc.expectedReactionAdded {
   123  			t.Fatalf("Expected reaction to be added for %+v", tc)
   124  		}
   125  	}
   126  }