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