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