github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/submit-queue-batch_test.go (about) 1 /* 2 Copyright 2016 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 "errors" 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "reflect" 25 "strconv" 26 "strings" 27 "testing" 28 29 "k8s.io/test-infra/mungegithub/mungeopts" 30 "k8s.io/test-infra/mungegithub/options" 31 32 githubapi "github.com/google/go-github/github" 33 ) 34 35 func expectEqual(t *testing.T, msg string, have interface{}, want interface{}) { 36 if !reflect.DeepEqual(have, want) { 37 t.Errorf("bad %s: got %v, wanted %v", 38 msg, have, want) 39 } 40 } 41 42 type stringHandler string 43 44 func (h stringHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 45 fmt.Fprintf(w, "%s", h) 46 } 47 48 func TestGetSuccessfulBatchJobs(t *testing.T) { 49 body := `[ 50 {"type":"batch", "repo":"a", "refs":"1", "state":"success", "context":"$"}, 51 {"type":"batch", "repo":"a", "refs":"2", "state":"success", "context":"!"}, 52 53 {"type":"pr", "repo":"a", "refs":"1", "state":"success", "context":"$"}, 54 {"type":"batch", "repo":"b", "refs":"1", "state":"success", "context":"$"}, 55 {"type":"batch", "repo":"a", "refs":"1", "state":"fail", "context":"$"} 56 ]` 57 serv := httptest.NewServer(stringHandler(body)) 58 defer serv.Close() 59 jobs, err := getJobs(serv.URL) 60 if err != nil { 61 t.Fatal(err) 62 } 63 jobs = jobs.repo("a").batch().successful() 64 expectEqual(t, "batchJobs", jobs, prowJobs{ 65 {Type: "batch", Repo: "a", State: "success", Refs: "1", Context: "$"}, 66 {Type: "batch", Repo: "a", State: "success", Refs: "2", Context: "!"}, 67 }) 68 } 69 70 func TestBatchRefToBatch(t *testing.T) { 71 _, strconvErr := strconv.ParseInt("a", 10, 32) 72 for _, test := range []struct { 73 ref string 74 expected Batch 75 err error 76 }{ 77 {"m:a", Batch{"m", "a", nil}, nil}, 78 {"m:a,1:b", Batch{"m", "a", []batchPull{{1, "b"}}}, nil}, 79 {"m:a,1:b,2:c", Batch{"m", "a", []batchPull{{1, "b"}, {2, "c"}}}, nil}, 80 {"asdf", Batch{}, errors.New("bad batchref: asdf")}, 81 {"m:a,a:3", Batch{}, fmt.Errorf("bad batchref: m:a,a:3 (%v)", strconvErr)}, 82 } { 83 batch, err := batchRefToBatch(test.ref) 84 expectEqual(t, "error", err, test.err) 85 expectEqual(t, "batch", batch, test.expected) 86 if err == nil { 87 expectEqual(t, "batch.String()", batch.String(), test.ref) 88 } 89 } 90 } 91 92 func TestGetCompletedBatches(t *testing.T) { 93 mungeopts.RequiredContexts.Retest = []string{"rt"} 94 mungeopts.RequiredContexts.Merge = []string{"st"} 95 sq := SubmitQueue{opts: options.New()} 96 for _, test := range []struct { 97 jobs prowJobs 98 batches []Batch 99 }{ 100 {prowJobs{}, []Batch{}}, 101 {prowJobs{{Refs: "m:a", Context: "rt"}}, []Batch{}}, 102 {prowJobs{{Refs: "m:a", Context: "st"}}, []Batch{}}, 103 {prowJobs{{Refs: "m:a", Context: "rt"}, {Refs: "m:a", Context: "st"}}, []Batch{{"m", "a", nil}}}, 104 } { 105 expectEqual(t, "getCompletedBatches", sq.getCompleteBatches(test.jobs), test.batches) 106 } 107 } 108 109 func TestBatchMatchesCommits(t *testing.T) { 110 makeCommits := func(spec []string) []*githubapi.RepositoryCommit { 111 out := []*githubapi.RepositoryCommit{} 112 for _, s := range spec { 113 i := strings.Index(s, " ") 114 refs := s[:i] 115 msg := s[i+1:] 116 split := strings.Split(refs, ":") 117 commit := githubapi.RepositoryCommit{ 118 SHA: &split[0], 119 Commit: &githubapi.Commit{Message: &msg}, 120 } 121 for _, parent := range strings.Split(split[1], ",") { 122 p := string(parent) // thanks, Go! 123 commit.Parents = append(commit.Parents, githubapi.Commit{SHA: &p}) 124 } 125 out = append(out, &commit) 126 } 127 return out 128 } 129 130 for _, test := range []struct { 131 pulls []batchPull 132 commits []string 133 expected int 134 err string 135 }{ 136 // no commits 137 {nil, []string{}, 0, "no commits"}, 138 // base matches 139 {nil, []string{"a:0 blah"}, 0, ""}, 140 // base doesn't match 141 {nil, []string{"b:0 blaga"}, 0, "Unknown non-merge commit b"}, 142 // PR could apply 143 {[]batchPull{{1, "c"}}, []string{"a:0 blah"}, 0, ""}, 144 // PR already applied 145 {[]batchPull{{1, "c"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 1, ""}, 146 // unknown merge 147 {[]batchPull{{2, "d"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 0, "Merge of something not in batch"}, 148 // unknown commit 149 {[]batchPull{{2, "d"}}, []string{"c:a fix stuff", "a:0 blah"}, 0, "Unknown non-merge commit c"}, 150 // PRs could apply 151 {[]batchPull{{1, "c"}, {2, "e"}}, []string{"a:0 blah"}, 0, ""}, 152 // 1 PR applied 153 {[]batchPull{{1, "c"}, {2, "e"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 1, ""}, 154 // other PR merged 155 {[]batchPull{{1, "c"}, {2, "e"}}, []string{"d:a,g Merge #3", "g:a add feature", "a:0 blah"}, 0, "Merge of something not in batch"}, 156 // both PRs already merged 157 {[]batchPull{{1, "c"}, {2, "e"}}, 158 []string{"f:d,e Merge #2", "e:a fix bug", "d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 2, ""}, 159 // PRs merged in wrong order 160 {[]batchPull{{1, "c"}, {2, "e"}}, 161 []string{"f:d,c Merge #1", "d:a,e Merge #2", "e:a fix bug", "c:a fix stuff", "a:0 blah"}, 0, "Batch PRs merged out-of-order"}, 162 } { 163 batch := Batch{"m", "a", test.pulls} 164 commits := makeCommits(test.commits) 165 actual, err := batch.matchesCommits(commits) 166 if err == nil { 167 err = errors.New("") 168 } 169 expectEqual(t, "batch.matchesCommits", actual, test.expected) 170 expectEqual(t, "batch.matchesCommits err", err.Error(), test.err) 171 } 172 }