github.com/abayer/test-infra@v0.0.5/prow/plugins/owners-label/owners-label_test.go (about) 1 /* 2 Copyright 2018 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 ownerslabel 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "k8s.io/apimachinery/pkg/util/sets" 26 "k8s.io/test-infra/prow/github" 27 ) 28 29 type fakeGithubClient struct { 30 changes []github.PullRequestChange 31 initialLabels []github.Label 32 labelsAdded sets.String 33 } 34 35 func newFakeGithubClient(filesChanged []string, initialLabels []string) *fakeGithubClient { 36 changes := make([]github.PullRequestChange, 0, len(filesChanged)) 37 for _, name := range filesChanged { 38 changes = append(changes, github.PullRequestChange{Filename: name}) 39 } 40 labels := make([]github.Label, 0, len(initialLabels)) 41 for _, label := range initialLabels { 42 labels = append(labels, github.Label{Name: label}) 43 } 44 return &fakeGithubClient{ 45 changes: changes, 46 initialLabels: labels, 47 labelsAdded: sets.NewString(), 48 } 49 } 50 51 func (c *fakeGithubClient) AddLabel(org, repo string, number int, label string) error { 52 if org != "org" { 53 return errors.New("org should be 'org'") 54 } 55 if repo != "repo" { 56 return errors.New("repo should be 'repo'") 57 } 58 if number != 5 { 59 return errors.New("number should be 5") 60 } 61 c.labelsAdded.Insert(label) 62 return nil 63 } 64 65 func (c *fakeGithubClient) GetPullRequestChanges(org, repo string, num int) ([]github.PullRequestChange, error) { 66 if org != "org" { 67 return nil, errors.New("org should be 'org'") 68 } 69 if repo != "repo" { 70 return nil, errors.New("repo should be 'repo'") 71 } 72 if num != 5 { 73 return nil, errors.New("number should be 5") 74 } 75 return c.changes, nil 76 } 77 78 func (c *fakeGithubClient) GetIssueLabels(org, repo string, num int) ([]github.Label, error) { 79 if org != "org" { 80 return nil, errors.New("org should be 'org'") 81 } 82 if repo != "repo" { 83 return nil, errors.New("repo should be 'repo'") 84 } 85 if num != 5 { 86 return nil, errors.New("number should be 5") 87 } 88 return c.initialLabels, nil 89 } 90 91 type fakeOwnersClient struct { 92 labels map[string]sets.String 93 } 94 95 func (foc *fakeOwnersClient) FindLabelsForFile(path string) sets.String { 96 return foc.labels[path] 97 } 98 99 // TestHandle tests that the handle function requests reviews from the correct number of unique users. 100 func TestHandle(t *testing.T) { 101 foc := &fakeOwnersClient{ 102 labels: map[string]sets.String{ 103 "a.go": sets.NewString("lgtm", "approved", "kind/docs"), 104 "b.go": sets.NewString("lgtm"), 105 "c.go": sets.NewString("lgtm", "dnm/frozen-docs"), 106 "d.sh": sets.NewString("dnm/bash"), 107 "e.sh": sets.NewString("dnm/bash"), 108 }, 109 } 110 111 var testcases = []struct { 112 name string 113 filesChanged []string 114 initialLabels []string 115 expectedAdded sets.String 116 }{ 117 { 118 name: "no labels", 119 filesChanged: []string{"other.go", "something.go"}, 120 expectedAdded: sets.NewString(), 121 }, 122 { 123 name: "1 file 1 label", 124 filesChanged: []string{"b.go"}, 125 expectedAdded: sets.NewString("lgtm"), 126 }, 127 { 128 name: "1 file 3 labels", 129 filesChanged: []string{"a.go"}, 130 expectedAdded: sets.NewString("lgtm", "approved", "kind/docs"), 131 }, 132 { 133 name: "2 files no overlap", 134 filesChanged: []string{"c.go", "d.sh"}, 135 expectedAdded: sets.NewString("lgtm", "dnm/frozen-docs", "dnm/bash"), 136 }, 137 { 138 name: "2 files partial overlap", 139 filesChanged: []string{"a.go", "b.go"}, 140 expectedAdded: sets.NewString("lgtm", "approved", "kind/docs"), 141 }, 142 { 143 name: "2 files complete overlap", 144 filesChanged: []string{"d.sh", "e.sh"}, 145 expectedAdded: sets.NewString("dnm/bash"), 146 }, 147 { 148 name: "3 files partial overlap", 149 filesChanged: []string{"a.go", "b.go", "c.go"}, 150 expectedAdded: sets.NewString("lgtm", "approved", "kind/docs", "dnm/frozen-docs"), 151 }, 152 { 153 name: "no labels to add, initial unrelated label", 154 filesChanged: []string{"other.go", "something.go"}, 155 initialLabels: []string{"lgtm"}, 156 expectedAdded: sets.NewString(), 157 }, 158 { 159 name: "1 file 1 label, already present", 160 filesChanged: []string{"b.go"}, 161 initialLabels: []string{"lgtm"}, 162 expectedAdded: sets.NewString(), 163 }, 164 { 165 name: "2 files no overlap, 1 label already present", 166 filesChanged: []string{"c.go", "d.sh"}, 167 initialLabels: []string{"dnm/bash", "approved"}, 168 expectedAdded: sets.NewString("lgtm", "dnm/frozen-docs"), 169 }, 170 { 171 name: "2 files complete overlap, label already present", 172 filesChanged: []string{"d.sh", "e.sh"}, 173 initialLabels: []string{"dnm/bash"}, 174 expectedAdded: sets.NewString(), 175 }, 176 } 177 for _, tc := range testcases { 178 fghc := newFakeGithubClient(tc.filesChanged, tc.initialLabels) 179 pre := &github.PullRequestEvent{ 180 Number: 5, 181 PullRequest: github.PullRequest{User: github.User{Login: "author"}}, 182 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 183 } 184 185 if err := handle(fghc, foc, logrus.WithField("plugin", pluginName), pre); err != nil { 186 t.Errorf("[%s] unexpected error from handle: %v", tc.name, err) 187 continue 188 } 189 if !fghc.labelsAdded.Equal(tc.expectedAdded) { 190 t.Errorf("[%s] expected the labels %q to be added, but got %q.", tc.name, tc.expectedAdded.List(), fghc.labelsAdded.List()) 191 } 192 } 193 }