github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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 "net/http" 23 "runtime" 24 "testing" 25 26 github_util "k8s.io/test-infra/mungegithub/github" 27 github_test "k8s.io/test-infra/mungegithub/github/testing" 28 29 "github.com/golang/glog" 30 "github.com/google/go-github/github" 31 ) 32 33 var ( 34 _ = fmt.Printf 35 _ = glog.Errorf 36 ) 37 38 func docsProposalIssue(testBotName string) *github.Issue { 39 return github_test.Issue(testBotName, 1, []string{cncfClaYesLabel, "kind/design"}, true) 40 } 41 42 // Commit returns a filled out github.Commit which happened at time.Unix(t, 0) 43 func commitFiles(path []string) []*github.CommitFile { 44 files := []*github.CommitFile{} 45 for _, p := range path { 46 f := &github.CommitFile{ 47 Filename: stringPtr(p), 48 } 49 files = append(files, f) 50 } 51 return files 52 } 53 54 func BotAddedDesign(testBotName string) []*github.IssueEvent { 55 return github_test.Events([]github_test.LabelTime{ 56 {User: testBotName, Label: "kind/design", Time: 9}, 57 {User: "bob", Label: "kind/design", Time: 8}, 58 }) 59 } 60 61 func OtherAddedDesign(testBotName string) []*github.IssueEvent { 62 return github_test.Events([]github_test.LabelTime{ 63 {User: testBotName, Label: "kind/design", Time: 8}, 64 {User: "bob", Label: "kind/design", Time: 9}, 65 }) 66 } 67 68 func TestPathLabelMunge(t *testing.T) { 69 const testBotName = "dummy" 70 runtime.GOMAXPROCS(runtime.NumCPU()) 71 72 tests := []struct { 73 files []*github.CommitFile 74 events []*github.IssueEvent 75 mustHave []string 76 mustNotHave []string 77 }{ 78 { 79 files: commitFiles([]string{"docs/proposals"}), 80 events: BotAddedDesign(testBotName), 81 mustHave: []string{"kind/design"}, 82 mustNotHave: []string{"kind/api-change", "kind/new-api"}, 83 }, 84 { 85 files: commitFiles([]string{"docs/my/proposals"}), 86 events: BotAddedDesign(testBotName), 87 mustHave: []string{}, 88 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 89 }, 90 { 91 files: commitFiles([]string{"pkg/api/types.go"}), 92 events: BotAddedDesign(testBotName), 93 mustHave: []string{"kind/api-change"}, 94 mustNotHave: []string{"kind/design", "kind/new-api"}, 95 }, 96 { 97 files: commitFiles([]string{"pkg/api/v1/types.go"}), 98 events: BotAddedDesign(testBotName), 99 mustHave: []string{"kind/api-change"}, 100 mustNotHave: []string{"kind/design", "kind/new-api"}, 101 }, 102 { 103 files: commitFiles([]string{"pkg/api/v1/duh/types.go"}), 104 events: BotAddedDesign(testBotName), 105 mustHave: []string{}, 106 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 107 }, 108 { 109 files: commitFiles([]string{"pkg/apis/experimental/register.go"}), 110 events: BotAddedDesign(testBotName), 111 mustHave: []string{"kind/new-api"}, 112 mustNotHave: []string{"kind/api-change", "kind/design"}, 113 }, 114 { 115 files: commitFiles([]string{"pkg/apis/experimental/v1beta1/register.go"}), 116 events: BotAddedDesign(testBotName), 117 mustHave: []string{"kind/new-api"}, 118 mustNotHave: []string{"kind/api-change", "kind/design"}, 119 }, 120 { 121 files: commitFiles([]string{"pkg/apis/experiments/v1beta1/duh/register.go"}), 122 events: BotAddedDesign(testBotName), 123 mustHave: []string{}, 124 mustNotHave: []string{"kind/design", "kind/api-change", "kind/new-api"}, 125 }, 126 { 127 files: commitFiles([]string{"README"}), 128 events: OtherAddedDesign(testBotName), 129 mustHave: []string{"kind/design"}, 130 mustNotHave: []string{"kind/api-change", "kind/new-api"}, 131 }, 132 } 133 for testNum, test := range tests { 134 client, server, mux := github_test.InitServer(t, docsProposalIssue(testBotName), ValidPR(), test.events, nil, nil, nil, test.files) 135 mux.HandleFunc("/repos/o/r/issues/1/labels/kind/design", func(w http.ResponseWriter, r *http.Request) { 136 w.WriteHeader(http.StatusOK) 137 w.Write([]byte{}) 138 }) 139 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { 140 w.WriteHeader(http.StatusOK) 141 out := []github.Label{{}} 142 data, err := json.Marshal(out) 143 if err != nil { 144 t.Errorf("Unexpected error: %v", err) 145 } 146 w.Write(data) 147 148 }) 149 150 config := &github_util.Config{ 151 Org: "o", 152 Project: "r", 153 } 154 config.SetClient(client) 155 config.BotName = testBotName 156 157 p := PathLabelMunger{pathLabelFile: "../path-label.txt"} 158 err := p.Initialize(config, nil) 159 if err != nil { 160 t.Fatalf("%v", err) 161 } 162 163 obj, err := config.GetObject(1) 164 if err != nil { 165 t.Fatalf("%v", err) 166 } 167 168 p.Munge(obj) 169 170 for _, l := range test.mustHave { 171 if !obj.HasLabel(l) { 172 t.Errorf("%d: Did not find label %q, labels: %v", testNum, l, obj.Issue.Labels) 173 } 174 } 175 for _, l := range test.mustNotHave { 176 if obj.HasLabel(l) { 177 t.Errorf("%d: Found label %q and should not have, labels: %v", testNum, l, obj.Issue.Labels) 178 } 179 } 180 server.Close() 181 } 182 }