Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: appengine/cmd/milo/buildbot/pubsub_test.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package buildbot 5 package buildbot
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/base64" 9 "encoding/base64"
10 "encoding/json" 10 "encoding/json"
11 "io" 11 "io"
12 "io/ioutil" 12 "io/ioutil"
13 "net/http" 13 "net/http"
14 "net/http/httptest" 14 "net/http/httptest"
15 "testing" 15 "testing"
16 "time" 16 "time"
17 17
18 "github.com/julienschmidt/httprouter" 18 "github.com/julienschmidt/httprouter"
19 "github.com/luci/gae/impl/memory" 19 "github.com/luci/gae/impl/memory"
20 "github.com/luci/gae/service/datastore" 20 "github.com/luci/gae/service/datastore"
21 "github.com/luci/luci-go/common/clock/testclock" 21 "github.com/luci/luci-go/common/clock/testclock"
22 "github.com/luci/luci-go/common/logging/gologger" 22 "github.com/luci/luci-go/common/logging/gologger"
23 . "github.com/luci/luci-go/common/testing/assertions" 23 . "github.com/luci/luci-go/common/testing/assertions"
24 "github.com/luci/luci-go/server/router"
24 . "github.com/smartystreets/goconvey/convey" 25 . "github.com/smartystreets/goconvey/convey"
25 "golang.org/x/net/context" 26 "golang.org/x/net/context"
26 ) 27 )
27 28
28 var ( 29 var (
29 fakeTime = time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC) 30 fakeTime = time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
30 ) 31 )
31 32
32 func buildbotTimesFinished(start, end float64) []*float64 { 33 func buildbotTimesFinished(start, end float64) []*float64 {
33 return []*float64{&start, &end} 34 return []*float64{&start, &end}
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 } 128 }
128 ms := buildbotMaster{ 129 ms := buildbotMaster{
129 Name: "fakename", 130 Name: "fakename",
130 Project: buildbotProject{Title: "some title"}, 131 Project: buildbotProject{Title: "some title"},
131 Slaves: slaves, 132 Slaves: slaves,
132 } 133 }
133 r := &http.Request{ 134 r := &http.Request{
134 Body: newCombinedPsBody([]buildbotBuild{b}, &ms) , 135 Body: newCombinedPsBody([]buildbotBuild{b}, &ms) ,
135 } 136 }
136 p := httprouter.Params{} 137 p := httprouter.Params{}
137 » » » PubSubHandler(c, h, r, p) 138 » » » PubSubHandler(&router.Context{Context: c, Writer: h, Req uest: r, Params: p})
138 So(h.Code, ShouldEqual, 200) 139 So(h.Code, ShouldEqual, 200)
139 Convey("And stores correctly", func() { 140 Convey("And stores correctly", func() {
140 loadB := &buildbotBuild{ 141 loadB := &buildbotBuild{
141 Master: "Fake Master", 142 Master: "Fake Master",
142 Buildername: "Fake buildername", 143 Buildername: "Fake buildername",
143 Number: 1234, 144 Number: 1234,
144 } 145 }
145 err := ds.Get(loadB) 146 err := ds.Get(loadB)
146 So(err, ShouldBeNil) 147 So(err, ShouldBeNil)
147 So(loadB.Master, ShouldEqual, "Fake Master") 148 So(loadB.Master, ShouldEqual, "Fake Master")
(...skipping 11 matching lines...) Expand all
159 ShouldEqual, 2222) 160 ShouldEqual, 2222)
160 }) 161 })
161 162
162 Convey("And a new master overwrites", func() { 163 Convey("And a new master overwrites", func() {
163 c, _ = testclock.UseTime(c, fakeTime.Add(time.Du ration(1*time.Second))) 164 c, _ = testclock.UseTime(c, fakeTime.Add(time.Du ration(1*time.Second)))
164 ms.Project.Title = "some other title" 165 ms.Project.Title = "some other title"
165 h = httptest.NewRecorder() 166 h = httptest.NewRecorder()
166 r := &http.Request{ 167 r := &http.Request{
167 Body: newCombinedPsBody([]buildbotBuild{ b}, &ms)} 168 Body: newCombinedPsBody([]buildbotBuild{ b}, &ms)}
168 p = httprouter.Params{} 169 p = httprouter.Params{}
169 » » » » PubSubHandler(c, h, r, p) 170 » » » » PubSubHandler(&router.Context{Context: c, Writer : h, Request: r, Params: p})
iannucci 2016/06/13 19:23:29 (I know you didn't write this :)), but it probably
nishanths 2016/06/15 18:25:08 Will do!
170 So(h.Code, ShouldEqual, 200) 171 So(h.Code, ShouldEqual, 200)
171 m, internal, t, err := getDSMasterJSON(c, "faken ame") 172 m, internal, t, err := getDSMasterJSON(c, "faken ame")
172 So(err, ShouldBeNil) 173 So(err, ShouldBeNil)
173 So(internal, ShouldEqual, false) 174 So(internal, ShouldEqual, false)
174 So(m.Project.Title, ShouldEqual, "some other tit le") 175 So(m.Project.Title, ShouldEqual, "some other tit le")
175 So(t.Unix(), ShouldEqual, 981173107) 176 So(t.Unix(), ShouldEqual, 981173107)
176 So(m.Name, ShouldEqual, "fakename") 177 So(m.Name, ShouldEqual, "fakename")
177 }) 178 })
178 Convey("And a new build overwrites", func() { 179 Convey("And a new build overwrites", func() {
179 b.Times = buildbotTimesFinished(123.0, 124.0) 180 b.Times = buildbotTimesFinished(123.0, 124.0)
180 h = httptest.NewRecorder() 181 h = httptest.NewRecorder()
181 r = &http.Request{ 182 r = &http.Request{
182 Body: newCombinedPsBody([]buildbotBuild{ b}, &ms), 183 Body: newCombinedPsBody([]buildbotBuild{ b}, &ms),
183 } 184 }
184 p = httprouter.Params{} 185 p = httprouter.Params{}
185 » » » » PubSubHandler(c, h, r, p) 186 » » » » PubSubHandler(&router.Context{Context: c, Writer : h, Request: r, Params: p})
186 So(h.Code, ShouldEqual, 200) 187 So(h.Code, ShouldEqual, 200)
187 loadB := &buildbotBuild{ 188 loadB := &buildbotBuild{
188 Master: "Fake Master", 189 Master: "Fake Master",
189 Buildername: "Fake buildername", 190 Buildername: "Fake buildername",
190 Number: 1234, 191 Number: 1234,
191 } 192 }
192 err := ds.Get(loadB) 193 err := ds.Get(loadB)
193 So(err, ShouldBeNil) 194 So(err, ShouldBeNil)
194 So(*loadB.Times[0], ShouldEqual, 123.0) 195 So(*loadB.Times[0], ShouldEqual, 123.0)
195 So(*loadB.Times[1], ShouldEqual, 124.0) 196 So(*loadB.Times[1], ShouldEqual, 124.0)
196 Convey("And another pending build is rejected", func() { 197 Convey("And another pending build is rejected", func() {
197 b.Times = buildbotTimesPending(123.0) 198 b.Times = buildbotTimesPending(123.0)
198 h = httptest.NewRecorder() 199 h = httptest.NewRecorder()
199 r = &http.Request{ 200 r = &http.Request{
200 Body: newCombinedPsBody([]buildb otBuild{b}, &ms), 201 Body: newCombinedPsBody([]buildb otBuild{b}, &ms),
201 } 202 }
202 p = httprouter.Params{} 203 p = httprouter.Params{}
203 » » » » » PubSubHandler(c, h, r, p) 204 » » » » » PubSubHandler(&router.Context{Context: c , Writer: h, Request: r, Params: p})
204 So(h.Code, ShouldEqual, 200) 205 So(h.Code, ShouldEqual, 200)
205 loadB := &buildbotBuild{ 206 loadB := &buildbotBuild{
206 Master: "Fake Master", 207 Master: "Fake Master",
207 Buildername: "Fake buildername", 208 Buildername: "Fake buildername",
208 Number: 1234, 209 Number: 1234,
209 } 210 }
210 err := ds.Get(loadB) 211 err := ds.Get(loadB)
211 So(err, ShouldBeNil) 212 So(err, ShouldBeNil)
212 So(*loadB.Times[0], ShouldEqual, 123.0) 213 So(*loadB.Times[0], ShouldEqual, 123.0)
213 So(*loadB.Times[1], ShouldEqual, 124.0) 214 So(*loadB.Times[1], ShouldEqual, 124.0)
214 }) 215 })
215 }) 216 })
216 }) 217 })
217 218
218 Convey("Empty pubsub message", func() { 219 Convey("Empty pubsub message", func() {
219 h := httptest.NewRecorder() 220 h := httptest.NewRecorder()
220 r := &http.Request{Body: ioutil.NopCloser(bytes.NewReade r([]byte{}))} 221 r := &http.Request{Body: ioutil.NopCloser(bytes.NewReade r([]byte{}))}
221 p := httprouter.Params{} 222 p := httprouter.Params{}
222 » » » PubSubHandler(c, h, r, p) 223 » » » PubSubHandler(&router.Context{Context: c, Writer: h, Req uest: r, Params: p})
223 So(h.Code, ShouldEqual, 200) 224 So(h.Code, ShouldEqual, 200)
224 }) 225 })
225 }) 226 })
226 } 227 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698