github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/milestonestatus/milestonestatus_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 milestonestatus 18 19 import ( 20 "fmt" 21 "reflect" 22 "sort" 23 "testing" 24 25 "github.com/sirupsen/logrus" 26 27 "sigs.k8s.io/prow/pkg/github" 28 "sigs.k8s.io/prow/pkg/github/fakegithub" 29 "sigs.k8s.io/prow/pkg/plugins" 30 ) 31 32 func formatLabels(labels ...string) []string { 33 r := []string{} 34 for _, l := range labels { 35 r = append(r, fmt.Sprintf("%s/%s#%d:%s", "org", "repo", 1, l)) 36 } 37 if len(r) == 0 { 38 return nil 39 } 40 return r 41 } 42 43 func TestMilestoneStatus(t *testing.T) { 44 type testCase struct { 45 name string 46 body string 47 commenter string 48 expectedNewLabels []string 49 shouldComment bool 50 noRepoMaintainer bool 51 } 52 testcases := []testCase{ 53 { 54 name: "Don't label when non sig-lead user approves", 55 body: "/status approved-for-milestone", 56 expectedNewLabels: []string{}, 57 commenter: "sig-follow", 58 shouldComment: true, 59 }, 60 { 61 name: "Don't label when non sig-lead user marks in progress", 62 body: "/status in-progress", 63 expectedNewLabels: []string{}, 64 commenter: "sig-follow", 65 shouldComment: true, 66 }, 67 { 68 name: "Don't label when non sig-lead user marks in review", 69 body: "/status in-review", 70 expectedNewLabels: []string{}, 71 commenter: "sig-follow", 72 shouldComment: true, 73 }, 74 { 75 name: "Label when sig-lead user approves", 76 body: "/status approved-for-milestone", 77 expectedNewLabels: []string{"status/approved-for-milestone"}, 78 commenter: "sig-lead", 79 shouldComment: false, 80 }, 81 { 82 name: "Label when sig-lead user marks in progress", 83 body: "/status in-progress", 84 expectedNewLabels: []string{"status/in-progress"}, 85 commenter: "sig-lead", 86 shouldComment: false, 87 }, 88 { 89 name: "Label when sig-lead user marks in review", 90 body: "/status in-review", 91 expectedNewLabels: []string{"status/in-review"}, 92 commenter: "sig-lead", 93 shouldComment: false, 94 }, 95 { 96 name: "Don't label when sig-lead user marks invalid status", 97 body: "/status in-valid", 98 expectedNewLabels: []string{}, 99 commenter: "sig-lead", 100 shouldComment: false, 101 }, 102 { 103 name: "Don't label when sig-lead user marks empty status", 104 body: "/status ", 105 expectedNewLabels: []string{}, 106 commenter: "sig-lead", 107 shouldComment: false, 108 }, 109 { 110 name: "Use default maintainer team when none is specified", 111 body: "/status in-progress", 112 expectedNewLabels: []string{"status/in-progress"}, 113 commenter: "default-sig-lead", 114 shouldComment: false, 115 noRepoMaintainer: true, 116 }, 117 { 118 name: "Don't use default maintainer team when one is specified", 119 body: "/status in-progress", 120 expectedNewLabels: []string{}, 121 commenter: "default-sig-lead", 122 shouldComment: true, 123 noRepoMaintainer: false, 124 }, 125 } 126 127 for _, tc := range testcases { 128 fakeClient := fakegithub.NewFakeClient() 129 e := &github.GenericCommentEvent{ 130 Action: github.GenericCommentActionCreated, 131 Body: tc.body, 132 Number: 1, 133 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 134 User: github.User{Login: tc.commenter}, 135 } 136 137 repoMilestone := map[string]plugins.Milestone{"": {MaintainersTeam: "admins"}} 138 139 if !tc.noRepoMaintainer { 140 repoMilestone["org/repo"] = plugins.Milestone{MaintainersTeam: "leads"} 141 } 142 143 if err := handle(fakeClient, logrus.WithField("plugin", pluginName), e, repoMilestone); err != nil { 144 t.Errorf("(%s): Unexpected error from handle: %v.", tc.name, err) 145 continue 146 } 147 148 // Check that the correct labels were added. 149 expectLabels := formatLabels(tc.expectedNewLabels...) 150 sort.Strings(expectLabels) 151 sort.Strings(fakeClient.IssueLabelsAdded) 152 if !reflect.DeepEqual(expectLabels, fakeClient.IssueLabelsAdded) { 153 t.Errorf("(%s): Expected issue to end with labels %q, but ended with %q.", tc.name, expectLabels, fakeClient.IssueLabelsAdded) 154 } 155 156 // Check that a comment was left iff one should have been left. 157 comments := len(fakeClient.IssueComments[1]) 158 if tc.shouldComment && comments != 1 { 159 t.Errorf("(%s): 1 comment should have been made, but %d comments were made.", tc.name, comments) 160 } else if !tc.shouldComment && comments != 0 { 161 t.Errorf("(%s): No comment should have been made, but %d comments were made.", tc.name, comments) 162 } 163 } 164 }