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

     1  /*
     2  Copyright 2018 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 branchcleaner
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"k8s.io/test-infra/prow/github"
    26  	"k8s.io/test-infra/prow/github/fakegithub"
    27  )
    28  
    29  func TestBranchCleaner(t *testing.T) {
    30  	baseRepoOrg := "my-org"
    31  	baseRepoRepo := "repo"
    32  	baseRepoFullName := fmt.Sprintf("%s/%s", baseRepoOrg, baseRepoRepo)
    33  
    34  	testcases := []struct {
    35  		name                 string
    36  		prAction             github.PullRequestEventAction
    37  		merged               bool
    38  		headRepoFullName     string
    39  		branchDeleteExpected bool
    40  	}{
    41  		{
    42  			name:                 "Opened PR nothing to do",
    43  			prAction:             github.PullRequestActionOpened,
    44  			merged:               false,
    45  			branchDeleteExpected: false,
    46  		},
    47  		{
    48  			name:                 "Closed PR unmerged nothing to do",
    49  			prAction:             github.PullRequestActionClosed,
    50  			merged:               false,
    51  			branchDeleteExpected: false,
    52  		},
    53  		{
    54  			name:                 "PR from different repo nothing to do",
    55  			prAction:             github.PullRequestActionClosed,
    56  			merged:               true,
    57  			headRepoFullName:     "different-org/repo",
    58  			branchDeleteExpected: false,
    59  		},
    60  		{
    61  			name:                 "PR from same repo delete head ref",
    62  			prAction:             github.PullRequestActionClosed,
    63  			merged:               true,
    64  			headRepoFullName:     "my-org/repo",
    65  			branchDeleteExpected: true,
    66  		},
    67  	}
    68  
    69  	mergeSHA := "abc"
    70  	prNumber := 1
    71  
    72  	for _, tc := range testcases {
    73  
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			log := logrus.WithField("plugin", pluginName)
    76  			event := github.PullRequestEvent{
    77  				Action: tc.prAction,
    78  				Number: prNumber,
    79  				PullRequest: github.PullRequest{
    80  					Base: github.PullRequestBranch{
    81  						Ref: "master",
    82  						Repo: github.Repo{
    83  							DefaultBranch: "master",
    84  							FullName:      baseRepoFullName,
    85  							Name:          baseRepoRepo,
    86  							Owner:         github.User{Login: baseRepoOrg},
    87  						},
    88  					},
    89  					Head: github.PullRequestBranch{
    90  						Ref: "my-feature",
    91  						Repo: github.Repo{
    92  							FullName: tc.headRepoFullName,
    93  						},
    94  					},
    95  					Merged: tc.merged},
    96  			}
    97  			if tc.merged {
    98  				event.PullRequest.MergeSHA = &mergeSHA
    99  			}
   100  
   101  			fgc := &fakegithub.FakeClient{
   102  				PullRequests: map[int]*github.PullRequest{
   103  					prNumber: {
   104  						Number: prNumber,
   105  					},
   106  				},
   107  			}
   108  			if err := handle(fgc, log, event); err != nil {
   109  				t.Fatalf("error in handle: %v", err)
   110  			}
   111  			if tc.branchDeleteExpected != (len(fgc.RefsDeleted) == 1) {
   112  				t.Fatalf("branchDeleteExpected: %v, refsDeleted: %d", tc.branchDeleteExpected, len(fgc.RefsDeleted))
   113  			}
   114  
   115  			if tc.branchDeleteExpected {
   116  				if fgc.RefsDeleted[0].Org != event.PullRequest.Base.Repo.Owner.Login {
   117  					t.Errorf("Expected org of deleted ref to be %s but was %s", event.PullRequest.Base.Repo.Owner.Login, fgc.RefsDeleted[0].Org)
   118  				}
   119  				if fgc.RefsDeleted[0].Repo != event.PullRequest.Base.Repo.Name {
   120  					t.Errorf("Expected repo of deleted ref to be %s but was %s", baseRepoRepo, fgc.RefsDeleted[0].Repo)
   121  				}
   122  				expectedRefName := fmt.Sprintf("heads/%s", event.PullRequest.Head.Ref)
   123  				if fgc.RefsDeleted[0].Ref != expectedRefName {
   124  					t.Errorf("Expected name of deleted ref to be %s but was %s", expectedRefName, fgc.RefsDeleted[0].Ref)
   125  				}
   126  			}
   127  
   128  		})
   129  
   130  	}
   131  }