github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/milestone/milestone_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 milestone
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"sigs.k8s.io/prow/pkg/github"
    25  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    26  	"sigs.k8s.io/prow/pkg/plugins"
    27  )
    28  
    29  func TestMilestoneStatus(t *testing.T) {
    30  	type testCase struct {
    31  		name              string
    32  		body              string
    33  		commenter         string
    34  		previousMilestone int
    35  		expectedMilestone int
    36  		noRepoMaintainer  bool
    37  	}
    38  	var milestonesMap = map[string]int{"v1.0": 1}
    39  	testcases := []testCase{
    40  		{
    41  			name:              "Update the milestone when a sig-lead uses the command",
    42  			body:              "/milestone v1.0",
    43  			commenter:         "sig-lead",
    44  			previousMilestone: 0,
    45  			expectedMilestone: 1,
    46  		},
    47  		{
    48  			name:              "Don't update the milestone if a sig-lead enters an invalid milestone",
    49  			body:              "/milestone v2.0",
    50  			commenter:         "sig-lead",
    51  			previousMilestone: 0,
    52  			expectedMilestone: 0,
    53  		},
    54  		{
    55  			name:              "Don't update the milestone when a sig-lead uses the command with an invalid milestone",
    56  			body:              "/milestone abc",
    57  			commenter:         "sig-lead",
    58  			previousMilestone: 0,
    59  			expectedMilestone: 0,
    60  		},
    61  		{
    62  			name:              "Don't update the milestone if a sig-follow enters a valid milestone",
    63  			body:              "/milestone v1.0",
    64  			commenter:         "sig-follow",
    65  			previousMilestone: 0,
    66  			expectedMilestone: 0,
    67  		},
    68  		{
    69  			name:              "Clear the milestone if a sig lead tries to clear",
    70  			body:              "/milestone clear",
    71  			commenter:         "sig-lead",
    72  			previousMilestone: 1,
    73  			expectedMilestone: 0,
    74  		},
    75  		{
    76  			name:              "Don't clear the milestone if a sig follow tries to clear",
    77  			body:              "/milestone clear",
    78  			commenter:         "sig-follow",
    79  			previousMilestone: 10,
    80  			expectedMilestone: 10,
    81  		},
    82  		{
    83  			name:              "Multiline comment",
    84  			body:              "Foo\n/milestone v1.0\r\n/priority critical-urgent",
    85  			commenter:         "sig-lead",
    86  			previousMilestone: 10,
    87  			expectedMilestone: 1,
    88  		},
    89  		{
    90  			name:              "Use default maintainer team when none is specified",
    91  			body:              "Foo\n/milestone v1.0\r\n/priority critical-urgent",
    92  			commenter:         "default-sig-lead",
    93  			previousMilestone: 10,
    94  			expectedMilestone: 1,
    95  			noRepoMaintainer:  true,
    96  		},
    97  		{
    98  			name:              "Don't use default maintainer team when one is specified",
    99  			body:              "Foo\n/milestone v1.0\r\n/priority critical-urgent",
   100  			commenter:         "default-sig-lead",
   101  			previousMilestone: 10,
   102  			expectedMilestone: 10,
   103  			noRepoMaintainer:  false,
   104  		},
   105  	}
   106  
   107  	for _, tc := range testcases {
   108  		fakeClient := fakegithub.NewFakeClient()
   109  		fakeClient.MilestoneMap = milestonesMap
   110  		fakeClient.Milestone = tc.previousMilestone
   111  
   112  		maintainersTeamName := "leads"
   113  		e := &github.GenericCommentEvent{
   114  			Action: github.GenericCommentActionCreated,
   115  			Body:   tc.body,
   116  			Number: 1,
   117  			Repo:   github.Repo{Owner: github.User{Login: "org"}, Name: "repo"},
   118  			User:   github.User{Login: tc.commenter},
   119  		}
   120  
   121  		repoMilestone := map[string]plugins.Milestone{"": {MaintainersTeam: "admins"}}
   122  
   123  		if !tc.noRepoMaintainer {
   124  			repoMilestone["org/repo"] = plugins.Milestone{MaintainersTeam: maintainersTeamName}
   125  		}
   126  
   127  		if err := handle(fakeClient, logrus.WithField("plugin", pluginName), e, repoMilestone); err != nil {
   128  			t.Errorf("(%s): Unexpected error from handle: %v.", tc.name, err)
   129  			continue
   130  		}
   131  
   132  		// Check that the milestone was set if it was supposed to be set
   133  		if fakeClient.Milestone != tc.expectedMilestone {
   134  			t.Errorf("Expected the milestone to be updated for the issue for %s.  Expected Milestone %v, Actual Milestone %v.", tc.name, tc.expectedMilestone, fakeClient.Milestone)
   135  		}
   136  	}
   137  }