github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/cla/cla_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 cla
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"k8s.io/test-infra/prow/github"
    27  	"k8s.io/test-infra/prow/github/fakegithub"
    28  )
    29  
    30  func TestCLALabels(t *testing.T) {
    31  	var testcases = []struct {
    32  		name          string
    33  		context       string
    34  		state         string
    35  		statusSHA     string
    36  		issues        []github.Issue
    37  		pullRequests  []github.PullRequest
    38  		labels        []string
    39  		addedLabels   []string
    40  		removedLabels []string
    41  	}{
    42  		{
    43  			name:          "unrecognized status context has no effect",
    44  			context:       "unknown",
    45  			state:         "success",
    46  			addedLabels:   nil,
    47  			removedLabels: nil,
    48  		},
    49  		{
    50  			name:          "cla/linuxfoundation status pending has no effect",
    51  			context:       "cla/linuxfoundation",
    52  			state:         "pending",
    53  			addedLabels:   nil,
    54  			removedLabels: nil,
    55  		},
    56  		{
    57  			name: "cla/linuxfoundation status success does not add/remove labels " +
    58  				"when not the head commit in a PR",
    59  			context:   "cla/linuxfoundation",
    60  			state:     "success",
    61  			statusSHA: "a",
    62  			issues: []github.Issue{
    63  				{Number: 3, State: "open", Labels: []github.Label{}},
    64  			},
    65  			pullRequests: []github.PullRequest{
    66  				{Number: 3, Head: github.PullRequestBranch{SHA: "b"}},
    67  			},
    68  			addedLabels:   nil,
    69  			removedLabels: nil,
    70  		},
    71  		{
    72  			name: "cla/linuxfoundation status failure does not add/remove labels " +
    73  				"when not the head commit in a PR",
    74  			context:   "cla/linuxfoundation",
    75  			state:     "failure",
    76  			statusSHA: "a",
    77  			issues: []github.Issue{
    78  				{Number: 3, State: "open", Labels: []github.Label{{Name: claYesLabel}}},
    79  			},
    80  			pullRequests: []github.PullRequest{
    81  				{Number: 3, Head: github.PullRequestBranch{SHA: "b"}},
    82  			},
    83  			addedLabels:   nil,
    84  			removedLabels: nil,
    85  		},
    86  		{
    87  			name:      "cla/linuxfoundation status on head commit of PR adds the cla-yes label when its state is \"success\"",
    88  			context:   "cla/linuxfoundation",
    89  			state:     "success",
    90  			statusSHA: "a",
    91  			issues: []github.Issue{
    92  				{Number: 3, State: "open", Labels: []github.Label{}},
    93  			},
    94  			pullRequests: []github.PullRequest{
    95  				{Number: 3, Head: github.PullRequestBranch{SHA: "a"}},
    96  			},
    97  			addedLabels:   []string{fmt.Sprintf("/#3:%s", claYesLabel)},
    98  			removedLabels: nil,
    99  		},
   100  		{
   101  			name:      "cla/linuxfoundation status on head commit of PR does nothing when pending",
   102  			context:   "cla/linuxfoundation",
   103  			state:     "pending",
   104  			statusSHA: "a",
   105  			issues: []github.Issue{
   106  				{Number: 3, State: "open", Labels: []github.Label{}},
   107  			},
   108  			pullRequests: []github.PullRequest{
   109  				{Number: 3, Head: github.PullRequestBranch{SHA: "a"}},
   110  			},
   111  			addedLabels:   nil,
   112  			removedLabels: nil,
   113  		},
   114  		{
   115  			name:      "cla/linuxfoundation status success removes \"cncf-cla: no\" label",
   116  			context:   "cla/linuxfoundation",
   117  			state:     "success",
   118  			statusSHA: "a",
   119  			issues: []github.Issue{
   120  				{Number: 3, State: "open", Labels: []github.Label{{Name: claNoLabel}}},
   121  			},
   122  			pullRequests: []github.PullRequest{
   123  				{Number: 3, Head: github.PullRequestBranch{SHA: "a"}},
   124  			},
   125  			addedLabels:   []string{fmt.Sprintf("/#3:%s", claYesLabel)},
   126  			removedLabels: []string{fmt.Sprintf("/#3:%s", claNoLabel)},
   127  		},
   128  		{
   129  			name:      "cla/linuxfoundation status failure removes \"cncf-cla: yes\" label",
   130  			context:   "cla/linuxfoundation",
   131  			state:     "failure",
   132  			statusSHA: "a",
   133  			issues: []github.Issue{
   134  				{Number: 3, State: "open", Labels: []github.Label{{Name: claYesLabel}}},
   135  			},
   136  			pullRequests: []github.PullRequest{
   137  				{Number: 3, Head: github.PullRequestBranch{SHA: "a"}},
   138  			},
   139  			addedLabels:   []string{fmt.Sprintf("/#3:%s", claNoLabel)},
   140  			removedLabels: []string{fmt.Sprintf("/#3:%s", claYesLabel)},
   141  		},
   142  	}
   143  	for _, tc := range testcases {
   144  		pullRequests := make(map[int]*github.PullRequest)
   145  		for _, pr := range tc.pullRequests {
   146  			pullRequests[pr.Number] = &pr
   147  		}
   148  
   149  		fc := &fakegithub.FakeClient{
   150  			PullRequests:  pullRequests,
   151  			Issues:        tc.issues,
   152  			IssueComments: make(map[int][]github.IssueComment),
   153  		}
   154  		se := github.StatusEvent{
   155  			Context: tc.context,
   156  			SHA:     tc.statusSHA,
   157  			State:   tc.state,
   158  		}
   159  		if err := handle(fc, logrus.WithField("plugin", pluginName), se); err != nil {
   160  			t.Errorf("For case %s, didn't expect error from cla plugin: %v", tc.name, err)
   161  			continue
   162  		}
   163  
   164  		if !reflect.DeepEqual(fc.LabelsAdded, tc.addedLabels) {
   165  			t.Errorf("Expected: %#v, Got %#v in case %s.", tc.addedLabels, fc.LabelsAdded, tc.name)
   166  		}
   167  
   168  		if !reflect.DeepEqual(fc.LabelsRemoved, tc.removedLabels) {
   169  			t.Errorf("Expected: %#v, Got %#v in case %s.", tc.removedLabels, fc.LabelsRemoved, tc.name)
   170  		}
   171  	}
   172  }