github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/hold/hold_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 hold
    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  	"k8s.io/test-infra/prow/labels"
    28  )
    29  
    30  func TestHandle(t *testing.T) {
    31  	var tests = []struct {
    32  		name          string
    33  		body          string
    34  		hasLabel      bool
    35  		shouldLabel   bool
    36  		shouldUnlabel bool
    37  	}{
    38  		{
    39  			name:          "nothing to do",
    40  			body:          "noise",
    41  			hasLabel:      false,
    42  			shouldLabel:   false,
    43  			shouldUnlabel: false,
    44  		},
    45  		{
    46  			name:          "requested hold",
    47  			body:          "/hold",
    48  			hasLabel:      false,
    49  			shouldLabel:   true,
    50  			shouldUnlabel: false,
    51  		},
    52  		{
    53  			name:          "requested hold, Label already exists",
    54  			body:          "/hold",
    55  			hasLabel:      true,
    56  			shouldLabel:   false,
    57  			shouldUnlabel: false,
    58  		},
    59  		{
    60  			name:          "requested hold cancel",
    61  			body:          "/hold cancel",
    62  			hasLabel:      true,
    63  			shouldLabel:   false,
    64  			shouldUnlabel: true,
    65  		},
    66  		{
    67  			name:          "requested hold cancel, Label already gone",
    68  			body:          "/hold cancel",
    69  			hasLabel:      false,
    70  			shouldLabel:   false,
    71  			shouldUnlabel: false,
    72  		},
    73  	}
    74  
    75  	for _, tc := range tests {
    76  		fc := &fakegithub.FakeClient{
    77  			IssueComments: make(map[int][]github.IssueComment),
    78  		}
    79  
    80  		e := &github.GenericCommentEvent{
    81  			Action: github.GenericCommentActionCreated,
    82  			Body:   tc.body,
    83  			Number: 1,
    84  			Repo:   github.Repo{Owner: github.User{Login: "org"}, Name: "repo"},
    85  		}
    86  		hasLabel := func(label string, issueLabels []github.Label) bool {
    87  			return tc.hasLabel
    88  		}
    89  
    90  		if err := handle(fc, logrus.WithField("plugin", PluginName), e, hasLabel); err != nil {
    91  			t.Errorf("For case %s, didn't expect error from hold: %v", tc.name, err)
    92  			continue
    93  		}
    94  
    95  		fakeLabel := fmt.Sprintf("org/repo#1:%s", labels.Hold)
    96  		if tc.shouldLabel {
    97  			if len(fc.IssueLabelsAdded) != 1 || fc.IssueLabelsAdded[0] != fakeLabel {
    98  				t.Errorf("For case %s: expected to add %q Label but instead added: %v", tc.name, labels.Hold, fc.IssueLabelsAdded)
    99  			}
   100  		} else if len(fc.IssueLabelsAdded) > 0 {
   101  			t.Errorf("For case %s, expected to not add %q Label but added: %v", tc.name, labels.Hold, fc.IssueLabelsAdded)
   102  		}
   103  		if tc.shouldUnlabel {
   104  			if len(fc.IssueLabelsRemoved) != 1 || fc.IssueLabelsRemoved[0] != fakeLabel {
   105  				t.Errorf("For case %s: expected to remove %q Label but instead removed: %v", tc.name, labels.Hold, fc.IssueLabelsRemoved)
   106  			}
   107  		} else if len(fc.IssueLabelsRemoved) > 0 {
   108  			t.Errorf("For case %s, expected to not remove %q Label but removed: %v", tc.name, labels.Hold, fc.IssueLabelsRemoved)
   109  		}
   110  	}
   111  }