github.com/abayer/test-infra@v0.0.5/prow/plugins/lifecycle/reopen_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 lifecycle
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"k8s.io/test-infra/prow/github"
    25  )
    26  
    27  type fakeClientReopen struct {
    28  	commented bool
    29  	open      bool
    30  }
    31  
    32  func (c *fakeClientReopen) CreateComment(owner, repo string, number int, comment string) error {
    33  	c.commented = true
    34  	return nil
    35  }
    36  
    37  func (c *fakeClientReopen) ReopenIssue(owner, repo string, number int) error {
    38  	c.open = true
    39  	return nil
    40  }
    41  
    42  func (c *fakeClientReopen) ReopenPR(owner, repo string, number int) error {
    43  	c.open = true
    44  	return nil
    45  }
    46  
    47  func TestReopenComment(t *testing.T) {
    48  	var testcases = []struct {
    49  		name          string
    50  		action        github.GenericCommentEventAction
    51  		state         string
    52  		body          string
    53  		commenter     string
    54  		shouldReopen  bool
    55  		shouldComment bool
    56  	}{
    57  		{
    58  			name:          "non-open comment",
    59  			action:        github.GenericCommentActionCreated,
    60  			state:         "open",
    61  			body:          "does not matter",
    62  			commenter:     "o",
    63  			shouldReopen:  false,
    64  			shouldComment: false,
    65  		},
    66  		{
    67  			name:          "re-open by author",
    68  			action:        github.GenericCommentActionCreated,
    69  			state:         "closed",
    70  			body:          "/reopen",
    71  			commenter:     "a",
    72  			shouldReopen:  true,
    73  			shouldComment: false,
    74  		},
    75  		{
    76  			name:          "re-open by reviewer",
    77  			action:        github.GenericCommentActionCreated,
    78  			state:         "closed",
    79  			body:          "/reopen",
    80  			commenter:     "r1",
    81  			shouldReopen:  true,
    82  			shouldComment: false,
    83  		},
    84  		{
    85  			name:          "re-open by reviewer, trailing space.",
    86  			action:        github.GenericCommentActionCreated,
    87  			state:         "closed",
    88  			body:          "/reopen \r",
    89  			commenter:     "r1",
    90  			shouldReopen:  true,
    91  			shouldComment: false,
    92  		},
    93  		{
    94  			name:          "re-open edited by author",
    95  			action:        github.GenericCommentActionEdited,
    96  			state:         "closed",
    97  			body:          "/reopen",
    98  			commenter:     "a",
    99  			shouldReopen:  false,
   100  			shouldComment: false,
   101  		},
   102  		{
   103  			name:          "open by author on already open issue",
   104  			action:        github.GenericCommentActionCreated,
   105  			state:         "open",
   106  			body:          "/reopen",
   107  			commenter:     "a",
   108  			shouldReopen:  false,
   109  			shouldComment: false,
   110  		},
   111  		{
   112  			name:          "re-open by other person",
   113  			action:        github.GenericCommentActionCreated,
   114  			state:         "closed",
   115  			body:          "/reopen",
   116  			commenter:     "o",
   117  			shouldReopen:  false,
   118  			shouldComment: true,
   119  		},
   120  	}
   121  	for _, tc := range testcases {
   122  		fc := &fakeClientReopen{}
   123  		e := &github.GenericCommentEvent{
   124  			Action:      tc.action,
   125  			IssueState:  tc.state,
   126  			Body:        tc.body,
   127  			User:        github.User{Login: tc.commenter},
   128  			Number:      5,
   129  			Assignees:   []github.User{{Login: "a"}, {Login: "r1"}, {Login: "r2"}},
   130  			IssueAuthor: github.User{Login: "a"},
   131  		}
   132  		if err := handleReopen(fc, logrus.WithField("plugin", "fake-reopen"), e); err != nil {
   133  			t.Errorf("For case %s, didn't expect error from handle: %v", tc.name, err)
   134  			continue
   135  		}
   136  		if tc.shouldReopen && !fc.open {
   137  			t.Errorf("For case %s, should have reopened but didn't.", tc.name)
   138  		} else if !tc.shouldReopen && fc.open {
   139  			t.Errorf("For case %s, should not have reopened but did.", tc.name)
   140  		}
   141  		if tc.shouldComment && !fc.commented {
   142  			t.Errorf("For case %s, should have commented but didn't.", tc.name)
   143  		} else if !tc.shouldComment && fc.commented {
   144  			t.Errorf("For case %s, should not have commented but did.", tc.name)
   145  		}
   146  	}
   147  }