github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/shrug/shurg_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 shrug
    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  	"k8s.io/test-infra/prow/labels"
    27  )
    28  
    29  func TestShrugComment(t *testing.T) {
    30  	var testcases = []struct {
    31  		name          string
    32  		body          string
    33  		hasShrug      bool
    34  		shouldShrug   bool
    35  		shouldUnshrug bool
    36  	}{
    37  		{
    38  			name:          "non-shrug comment",
    39  			body:          "uh oh",
    40  			hasShrug:      false,
    41  			shouldShrug:   false,
    42  			shouldUnshrug: false,
    43  		},
    44  		{
    45  			name:          "shrug",
    46  			body:          "/shrug",
    47  			hasShrug:      false,
    48  			shouldShrug:   true,
    49  			shouldUnshrug: false,
    50  		},
    51  		{
    52  			name:          "shrug over shrug",
    53  			body:          "/shrug",
    54  			hasShrug:      true,
    55  			shouldShrug:   false,
    56  			shouldUnshrug: false,
    57  		},
    58  		{
    59  			name:          "unshrug nothing",
    60  			body:          "/unshrug",
    61  			hasShrug:      false,
    62  			shouldShrug:   false,
    63  			shouldUnshrug: false,
    64  		},
    65  		{
    66  			name:          "unshrug the shrug",
    67  			body:          "/unshrug",
    68  			hasShrug:      true,
    69  			shouldShrug:   false,
    70  			shouldUnshrug: true,
    71  		},
    72  	}
    73  	for _, tc := range testcases {
    74  		fc := &fakegithub.FakeClient{
    75  			IssueComments: make(map[int][]github.IssueComment),
    76  		}
    77  		e := &github.GenericCommentEvent{
    78  			Action: github.GenericCommentActionCreated,
    79  			Body:   tc.body,
    80  			Number: 5,
    81  			Repo:   github.Repo{Owner: github.User{Login: "org"}, Name: "repo"},
    82  		}
    83  		if tc.hasShrug {
    84  			fc.IssueLabelsAdded = []string{"org/repo#5:" + labels.Shrug}
    85  		}
    86  		if err := handle(fc, logrus.WithField("plugin", pluginName), e); err != nil {
    87  			t.Errorf("For case %s, didn't expect error: %v", tc.name, err)
    88  			continue
    89  		}
    90  
    91  		hadShrug := 0
    92  		if tc.hasShrug {
    93  			hadShrug = 1
    94  		}
    95  		if tc.shouldShrug {
    96  			if len(fc.IssueLabelsAdded)-hadShrug != 1 {
    97  				t.Errorf("For case %s, should add shrug.", tc.name)
    98  			}
    99  			if len(fc.IssueLabelsRemoved) != 0 {
   100  				t.Errorf("For case %s, should not remove label.", tc.name)
   101  			}
   102  		} else if tc.shouldUnshrug {
   103  			if len(fc.IssueLabelsAdded)-hadShrug != 0 {
   104  				t.Errorf("For case %s, should not add shrug.", tc.name)
   105  			}
   106  			if len(fc.IssueLabelsRemoved) != 1 {
   107  				t.Errorf("For case %s, should remove shrug.", tc.name)
   108  			}
   109  		} else if len(fc.IssueLabelsAdded)-hadShrug > 0 || len(fc.IssueLabelsRemoved) > 0 {
   110  			t.Errorf("For case %s, should not have added/removed shrug.", tc.name)
   111  		}
   112  	}
   113  }