github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/path_label_test.go (about) 1 /* 2 Copyright 2015 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 mungers 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io/ioutil" 23 "net/http" 24 "os" 25 "runtime" 26 "testing" 27 28 github_util "k8s.io/test-infra/mungegithub/github" 29 github_test "k8s.io/test-infra/mungegithub/github/testing" 30 31 "github.com/golang/glog" 32 "github.com/google/go-github/github" 33 ) 34 35 var ( 36 _ = fmt.Printf 37 _ = glog.Errorf 38 ) 39 40 const ( 41 pathLabelTestContents = `# This file is used by the path-label munger and is of the form: 42 # PATH REGEXP LABEL 43 44 ^docs/proposals kind/design 45 ^docs/design kind/design 46 47 # examples: 48 # pkg/api/types.go 49 # pkg/api/*/types.go 50 ^pkg/api/([^/]+/)?types.go$ kind/api-change 51 ^pkg/api/([^/]+/)?register.go$ kind/new-api 52 53 # examples: 54 # pkg/apis/*/types.go 55 # pkg/apis/*/*/types.go 56 ^pkg/apis/[^/]+/([^/]+/)?types.go$ kind/api-change 57 ^pkg/apis/[^/]+/([^/]+/)?register.go$ kind/new-api 58 59 # docs which are going away with move to separate doc repo 60 ^docs/getting-started-guides kind/old-docs 61 ^docs/admin kind/old-docs 62 ^docs/user-guide kind/old-docs 63 ^docs/devel kind/old-docs 64 ^docs/design kind/old-docs 65 ^docs/proposals kind/old-docs 66 ` 67 ) 68 69 func docsProposalIssue(testBotName string) *github.Issue { 70 return github_test.Issue(testBotName, 1, []string{cncfClaYesLabel, "kind/design"}, true) 71 } 72 73 // Commit returns a filled out github.Commit which happened at time.Unix(t, 0) 74 func commitFiles(path []string) []*github.CommitFile { 75 files := []*github.CommitFile{} 76 for _, p := range path { 77 f := &github.CommitFile{ 78 Filename: stringPtr(p), 79 } 80 files = append(files, f) 81 } 82 return files 83 } 84 85 func BotAddedDesign(testBotName string) []*github.IssueEvent { 86 return github_test.Events([]github_test.LabelTime{ 87 {User: testBotName, Label: "kind/design", Time: 9}, 88 {User: "bob", Label: "kind/design", Time: 8}, 89 }) 90 } 91 92 func OtherAddedDesign(testBotName string) []*github.IssueEvent { 93 return github_test.Events([]github_test.LabelTime{ 94 {User: testBotName, Label: "kind/design", Time: 8}, 95 {User: "bob", Label: "kind/design", Time: 9}, 96 }) 97 } 98 99 func TestPathLabelMunge(t *testing.T) { 100 const testBotName = "dummy" 101 runtime.GOMAXPROCS(runtime.NumCPU()) 102 103 tests := []struct { 104 files []*github.CommitFile 105 events []*github.IssueEvent 106 mustHave []string 107 mustNotHave []string 108 }{ 109 { 110 files: commitFiles([]string{"docs/proposals"}), 111 events: BotAddedDesign(testBotName), 112 mustHave: []string{"kind/design"}, 113 mustNotHave: []string{"kind/api-change", "kind/new-api"}, 114 }, 115 { 116 files: commitFiles([]string{"docs/my/proposals"}), 117 events: BotAddedDesign(testBotName), 118 mustHave: []string{}, 119 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 120 }, 121 { 122 files: commitFiles([]string{"pkg/api/types.go"}), 123 events: BotAddedDesign(testBotName), 124 mustHave: []string{"kind/api-change"}, 125 mustNotHave: []string{"kind/design", "kind/new-api"}, 126 }, 127 { 128 files: commitFiles([]string{"pkg/api/v1/types.go"}), 129 events: BotAddedDesign(testBotName), 130 mustHave: []string{"kind/api-change"}, 131 mustNotHave: []string{"kind/design", "kind/new-api"}, 132 }, 133 { 134 files: commitFiles([]string{"pkg/api/v1/duh/types.go"}), 135 events: BotAddedDesign(testBotName), 136 mustHave: []string{}, 137 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 138 }, 139 { 140 files: commitFiles([]string{"pkg/apis/experimental/register.go"}), 141 events: BotAddedDesign(testBotName), 142 mustHave: []string{"kind/new-api"}, 143 mustNotHave: []string{"kind/api-change", "kind/design"}, 144 }, 145 { 146 files: commitFiles([]string{"pkg/apis/experimental/v1beta1/register.go"}), 147 events: BotAddedDesign(testBotName), 148 mustHave: []string{"kind/new-api"}, 149 mustNotHave: []string{"kind/api-change", "kind/design"}, 150 }, 151 { 152 files: commitFiles([]string{"pkg/apis/experiments/v1beta1/duh/register.go"}), 153 events: BotAddedDesign(testBotName), 154 mustHave: []string{}, 155 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 156 }, 157 { 158 files: commitFiles([]string{"README"}), 159 events: OtherAddedDesign(testBotName), 160 mustHave: []string{"kind/design"}, 161 mustNotHave: []string{"kind/api-change", "kind/new-api"}, 162 }, 163 } 164 for testNum, test := range tests { 165 client, server, mux := github_test.InitServer(t, docsProposalIssue(testBotName), ValidPR(), test.events, nil, nil, nil, test.files) 166 mux.HandleFunc("/repos/o/r/issues/1/labels/kind/design", func(w http.ResponseWriter, r *http.Request) { 167 w.WriteHeader(http.StatusOK) 168 w.Write([]byte{}) 169 }) 170 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { 171 w.WriteHeader(http.StatusOK) 172 out := []github.Label{{}} 173 data, err := json.Marshal(out) 174 if err != nil { 175 t.Errorf("Unexpected error: %v", err) 176 } 177 w.Write(data) 178 179 }) 180 181 config := &github_util.Config{ 182 Org: "o", 183 Project: "r", 184 } 185 config.SetClient(client) 186 config.BotName = testBotName 187 188 pathLabelTestFile, err := ioutil.TempFile("", "path-label.txt") 189 if err != nil { 190 t.Fatalf("open tempfile for writing: %v", err) 191 } 192 defer os.Remove(pathLabelTestFile.Name()) // clean up temp file 193 if _, err := pathLabelTestFile.Write([]byte(pathLabelTestContents)); err != nil { 194 t.Fatalf("write to %q: %v", pathLabelTestFile.Name(), err) 195 } 196 if err := pathLabelTestFile.Close(); err != nil { 197 t.Fatalf("closing tempfile %q: %v", pathLabelTestFile.Name(), err) 198 } 199 200 p := PathLabelMunger{pathLabelFile: pathLabelTestFile.Name()} 201 err = p.Initialize(config, nil) 202 if err != nil { 203 t.Fatalf("%v", err) 204 } 205 206 obj, err := config.GetObject(1) 207 if err != nil { 208 t.Fatalf("%v", err) 209 } 210 211 p.Munge(obj) 212 213 for _, l := range test.mustHave { 214 if !obj.HasLabel(l) { 215 t.Errorf("%d: Did not find label %q, labels: %v", testNum, l, obj.Issue.Labels) 216 } 217 } 218 for _, l := range test.mustNotHave { 219 if obj.HasLabel(l) { 220 t.Errorf("%d: Found label %q and should not have, labels: %v", testNum, l, obj.Issue.Labels) 221 } 222 } 223 server.Close() 224 } 225 }