| OLD | NEW |
| 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 "compress/zlib" | 9 "compress/zlib" |
| 10 "encoding/base64" | 10 "encoding/base64" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 ) | 33 ) |
| 34 | 34 |
| 35 func buildbotTimesFinished(start, end float64) []*float64 { | 35 func buildbotTimesFinished(start, end float64) []*float64 { |
| 36 return []*float64{&start, &end} | 36 return []*float64{&start, &end} |
| 37 } | 37 } |
| 38 | 38 |
| 39 func buildbotTimesPending(start float64) []*float64 { | 39 func buildbotTimesPending(start float64) []*float64 { |
| 40 return []*float64{&start, nil} | 40 return []*float64{&start, nil} |
| 41 } | 41 } |
| 42 | 42 |
| 43 func newCombinedPsBody(bs []buildbotBuild, m *buildbotMaster) io.ReadCloser { | 43 func newCombinedPsBody(bs []buildbotBuild, m *buildbotMaster, internal bool) io.
ReadCloser { |
| 44 bmsg := buildMasterMsg{ | 44 bmsg := buildMasterMsg{ |
| 45 Master: m, | 45 Master: m, |
| 46 Builds: bs, | 46 Builds: bs, |
| 47 } | 47 } |
| 48 bm, _ := json.Marshal(bmsg) | 48 bm, _ := json.Marshal(bmsg) |
| 49 var b bytes.Buffer | 49 var b bytes.Buffer |
| 50 zw := zlib.NewWriter(&b) | 50 zw := zlib.NewWriter(&b) |
| 51 zw.Write(bm) | 51 zw.Write(bm) |
| 52 zw.Close() | 52 zw.Close() |
| 53 sub := "projects/luci-milo/subscriptions/buildbot-public" |
| 54 if internal { |
| 55 sub = "projects/luci-milo/subscriptions/buildbot-private" |
| 56 } |
| 53 msg := pubSubSubscription{ | 57 msg := pubSubSubscription{ |
| 54 » » Subscription: "projects/luci-milo/subscriptions/buildbot-public"
, | 58 » » Subscription: sub, |
| 55 Message: pubSubMessage{ | 59 Message: pubSubMessage{ |
| 56 Data: base64.StdEncoding.EncodeToString(b.Bytes()), | 60 Data: base64.StdEncoding.EncodeToString(b.Bytes()), |
| 57 }, | 61 }, |
| 58 } | 62 } |
| 59 jmsg, _ := json.Marshal(msg) | 63 jmsg, _ := json.Marshal(msg) |
| 60 return ioutil.NopCloser(bytes.NewReader(jmsg)) | 64 return ioutil.NopCloser(bytes.NewReader(jmsg)) |
| 61 } | 65 } |
| 62 | 66 |
| 63 func TestPubSub(t *testing.T) { | 67 func TestPubSub(t *testing.T) { |
| 64 Convey(`A test Environment`, t, func() { | 68 Convey(`A test Environment`, t, func() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 81 So(err, ShouldBeNil) | 85 So(err, ShouldBeNil) |
| 82 Convey("Load build entry", func() { | 86 Convey("Load build entry", func() { |
| 83 loadB := &buildbotBuild{ | 87 loadB := &buildbotBuild{ |
| 84 Master: "Fake Master", | 88 Master: "Fake Master", |
| 85 Buildername: "Fake buildername", | 89 Buildername: "Fake buildername", |
| 86 Number: 1234, | 90 Number: 1234, |
| 87 } | 91 } |
| 88 err = ds.Get(loadB) | 92 err = ds.Get(loadB) |
| 89 So(err, ShouldBeNil) | 93 So(err, ShouldBeNil) |
| 90 So(loadB.Master, ShouldEqual, "Fake Master") | 94 So(loadB.Master, ShouldEqual, "Fake Master") |
| 95 So(loadB.Internal, ShouldEqual, false) |
| 91 So(loadB.Currentstep.(string), ShouldEqual, "thi
s is a string") | 96 So(loadB.Currentstep.(string), ShouldEqual, "thi
s is a string") |
| 92 So(loadB.Finished, ShouldEqual, true) | 97 So(loadB.Finished, ShouldEqual, true) |
| 93 }) | 98 }) |
| 94 | 99 |
| 95 Convey("Query build entry", func() { | 100 Convey("Query build entry", func() { |
| 96 q := datastore.NewQuery("buildbotBuild") | 101 q := datastore.NewQuery("buildbotBuild") |
| 97 buildbots := []*buildbotBuild{} | 102 buildbots := []*buildbotBuild{} |
| 98 err = ds.GetAll(q, &buildbots) | 103 err = ds.GetAll(q, &buildbots) |
| 99 So(err, ShouldBeNil) | 104 So(err, ShouldBeNil) |
| 100 | 105 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 Times: []*float64{&ft, nil
}, | 177 Times: []*float64{&ft, nil
}, |
| 173 }, | 178 }, |
| 174 }, | 179 }, |
| 175 } | 180 } |
| 176 ms := buildbotMaster{ | 181 ms := buildbotMaster{ |
| 177 Name: "fakename", | 182 Name: "fakename", |
| 178 Project: buildbotProject{Title: "some title"}, | 183 Project: buildbotProject{Title: "some title"}, |
| 179 Slaves: slaves, | 184 Slaves: slaves, |
| 180 } | 185 } |
| 181 r := &http.Request{ | 186 r := &http.Request{ |
| 182 » » » » Body: newCombinedPsBody([]buildbotBuild{b}, &ms)
, | 187 » » » » Body: newCombinedPsBody([]buildbotBuild{b}, &ms,
false), |
| 183 } | 188 } |
| 184 p := httprouter.Params{} | 189 p := httprouter.Params{} |
| 185 PubSubHandler(&router.Context{ | 190 PubSubHandler(&router.Context{ |
| 186 Context: c, | 191 Context: c, |
| 187 Writer: h, | 192 Writer: h, |
| 188 Request: r, | 193 Request: r, |
| 189 Params: p, | 194 Params: p, |
| 190 }) | 195 }) |
| 191 So(h.Code, ShouldEqual, 200) | 196 So(h.Code, ShouldEqual, 200) |
| 192 Convey("And stores correctly", func() { | 197 Convey("And stores correctly", func() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 210 So(len(m.Slaves["testslave"].RunningbuildsMap),
ShouldEqual, 1) | 215 So(len(m.Slaves["testslave"].RunningbuildsMap),
ShouldEqual, 1) |
| 211 So(m.Slaves["testslave"].RunningbuildsMap["Fake
buildername"][0], | 216 So(m.Slaves["testslave"].RunningbuildsMap["Fake
buildername"][0], |
| 212 ShouldEqual, 2222) | 217 ShouldEqual, 2222) |
| 213 }) | 218 }) |
| 214 | 219 |
| 215 Convey("And a new master overwrites", func() { | 220 Convey("And a new master overwrites", func() { |
| 216 c, _ = testclock.UseTime(c, fakeTime.Add(time.Du
ration(1*time.Second))) | 221 c, _ = testclock.UseTime(c, fakeTime.Add(time.Du
ration(1*time.Second))) |
| 217 ms.Project.Title = "some other title" | 222 ms.Project.Title = "some other title" |
| 218 h = httptest.NewRecorder() | 223 h = httptest.NewRecorder() |
| 219 r := &http.Request{ | 224 r := &http.Request{ |
| 220 » » » » » Body: newCombinedPsBody([]buildbotBuild{
b}, &ms)} | 225 » » » » » Body: newCombinedPsBody([]buildbotBuild{
b}, &ms, false)} |
| 221 p = httprouter.Params{} | 226 p = httprouter.Params{} |
| 222 PubSubHandler(&router.Context{ | 227 PubSubHandler(&router.Context{ |
| 223 Context: c, | 228 Context: c, |
| 224 Writer: h, | 229 Writer: h, |
| 225 Request: r, | 230 Request: r, |
| 226 Params: p, | 231 Params: p, |
| 227 }) | 232 }) |
| 228 So(h.Code, ShouldEqual, 200) | 233 So(h.Code, ShouldEqual, 200) |
| 229 m, internal, t, err := getMasterJSON(c, "fakenam
e") | 234 m, internal, t, err := getMasterJSON(c, "fakenam
e") |
| 230 So(err, ShouldBeNil) | 235 So(err, ShouldBeNil) |
| 231 So(internal, ShouldEqual, false) | 236 So(internal, ShouldEqual, false) |
| 232 So(m.Project.Title, ShouldEqual, "some other tit
le") | 237 So(m.Project.Title, ShouldEqual, "some other tit
le") |
| 233 So(t.Unix(), ShouldEqual, 981173107) | 238 So(t.Unix(), ShouldEqual, 981173107) |
| 234 So(m.Name, ShouldEqual, "fakename") | 239 So(m.Name, ShouldEqual, "fakename") |
| 235 }) | 240 }) |
| 236 Convey("And a new build overwrites", func() { | 241 Convey("And a new build overwrites", func() { |
| 237 b.Times = buildbotTimesFinished(123.0, 124.0) | 242 b.Times = buildbotTimesFinished(123.0, 124.0) |
| 238 h = httptest.NewRecorder() | 243 h = httptest.NewRecorder() |
| 239 r = &http.Request{ | 244 r = &http.Request{ |
| 240 » » » » » Body: newCombinedPsBody([]buildbotBuild{
b}, &ms), | 245 » » » » » Body: newCombinedPsBody([]buildbotBuild{
b}, &ms, false), |
| 241 } | 246 } |
| 242 p = httprouter.Params{} | 247 p = httprouter.Params{} |
| 243 PubSubHandler(&router.Context{ | 248 PubSubHandler(&router.Context{ |
| 244 Context: c, | 249 Context: c, |
| 245 Writer: h, | 250 Writer: h, |
| 246 Request: r, | 251 Request: r, |
| 247 Params: p, | 252 Params: p, |
| 248 }) | 253 }) |
| 249 So(h.Code, ShouldEqual, 200) | 254 So(h.Code, ShouldEqual, 200) |
| 250 loadB := &buildbotBuild{ | 255 loadB := &buildbotBuild{ |
| 251 Master: "Fake Master", | 256 Master: "Fake Master", |
| 252 Buildername: "Fake buildername", | 257 Buildername: "Fake buildername", |
| 253 Number: 1234, | 258 Number: 1234, |
| 254 } | 259 } |
| 255 err := ds.Get(loadB) | 260 err := ds.Get(loadB) |
| 256 So(err, ShouldBeNil) | 261 So(err, ShouldBeNil) |
| 257 So(*loadB.Times[0], ShouldEqual, 123.0) | 262 So(*loadB.Times[0], ShouldEqual, 123.0) |
| 258 So(*loadB.Times[1], ShouldEqual, 124.0) | 263 So(*loadB.Times[1], ShouldEqual, 124.0) |
| 259 Convey("And another pending build is rejected",
func() { | 264 Convey("And another pending build is rejected",
func() { |
| 260 b.Times = buildbotTimesPending(123.0) | 265 b.Times = buildbotTimesPending(123.0) |
| 261 h = httptest.NewRecorder() | 266 h = httptest.NewRecorder() |
| 262 r = &http.Request{ | 267 r = &http.Request{ |
| 263 » » » » » » Body: newCombinedPsBody([]buildb
otBuild{b}, &ms), | 268 » » » » » » Body: newCombinedPsBody([]buildb
otBuild{b}, &ms, false), |
| 264 } | 269 } |
| 265 p = httprouter.Params{} | 270 p = httprouter.Params{} |
| 266 PubSubHandler(&router.Context{ | 271 PubSubHandler(&router.Context{ |
| 267 Context: c, | 272 Context: c, |
| 268 Writer: h, | 273 Writer: h, |
| 269 Request: r, | 274 Request: r, |
| 270 Params: p, | 275 Params: p, |
| 271 }) | 276 }) |
| 272 So(h.Code, ShouldEqual, 200) | 277 So(h.Code, ShouldEqual, 200) |
| 273 loadB := &buildbotBuild{ | 278 loadB := &buildbotBuild{ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 288 r := &http.Request{Body: ioutil.NopCloser(bytes.NewReade
r([]byte{}))} | 293 r := &http.Request{Body: ioutil.NopCloser(bytes.NewReade
r([]byte{}))} |
| 289 p := httprouter.Params{} | 294 p := httprouter.Params{} |
| 290 PubSubHandler(&router.Context{ | 295 PubSubHandler(&router.Context{ |
| 291 Context: c, | 296 Context: c, |
| 292 Writer: h, | 297 Writer: h, |
| 293 Request: r, | 298 Request: r, |
| 294 Params: p, | 299 Params: p, |
| 295 }) | 300 }) |
| 296 So(h.Code, ShouldEqual, 200) | 301 So(h.Code, ShouldEqual, 200) |
| 297 }) | 302 }) |
| 303 |
| 304 Convey("Internal master + build pusbsub subscription", func() { |
| 305 h := httptest.NewRecorder() |
| 306 slaves := map[string]*buildbotSlave{} |
| 307 ft := 1234.0 |
| 308 slaves["testslave"] = &buildbotSlave{ |
| 309 Name: "testslave", |
| 310 Connected: true, |
| 311 Runningbuilds: []buildbotBuild{ |
| 312 { |
| 313 Master: "Fake Master", |
| 314 Buildername: "Fake buildername", |
| 315 Number: 2222, |
| 316 Times: []*float64{&ft, nil
}, |
| 317 }, |
| 318 }, |
| 319 } |
| 320 ms := buildbotMaster{ |
| 321 Name: "fakename", |
| 322 Project: buildbotProject{Title: "some title"}, |
| 323 Slaves: slaves, |
| 324 } |
| 325 r := &http.Request{ |
| 326 Body: newCombinedPsBody([]buildbotBuild{b}, &ms,
true), |
| 327 } |
| 328 p := httprouter.Params{} |
| 329 PubSubHandler(&router.Context{ |
| 330 Context: c, |
| 331 Writer: h, |
| 332 Request: r, |
| 333 Params: p, |
| 334 }) |
| 335 So(h.Code, ShouldEqual, 200) |
| 336 Convey("And stores correctly", func() { |
| 337 loadB := &buildbotBuild{ |
| 338 Master: "Fake Master", |
| 339 Buildername: "Fake buildername", |
| 340 Number: 1234, |
| 341 } |
| 342 err := ds.Get(loadB) |
| 343 So(err, ShouldBeNil) |
| 344 So(loadB.Master, ShouldEqual, "Fake Master") |
| 345 So(loadB.Internal, ShouldEqual, true) |
| 346 So(loadB.Currentstep.(string), ShouldEqual, "thi
s is a string") |
| 347 m, internal, t, err := getMasterJSON(c, "fakenam
e") |
| 348 So(err, ShouldBeNil) |
| 349 So(internal, ShouldEqual, true) |
| 350 So(t.Unix(), ShouldEqual, 981173106) |
| 351 So(m.Name, ShouldEqual, "fakename") |
| 352 So(m.Project.Title, ShouldEqual, "some title") |
| 353 So(m.Slaves["testslave"].Name, ShouldEqual, "tes
tslave") |
| 354 So(len(m.Slaves["testslave"].Runningbuilds), Sho
uldEqual, 0) |
| 355 So(len(m.Slaves["testslave"].RunningbuildsMap),
ShouldEqual, 1) |
| 356 So(m.Slaves["testslave"].RunningbuildsMap["Fake
buildername"][0], |
| 357 ShouldEqual, 2222) |
| 358 }) |
| 359 }) |
| 298 }) | 360 }) |
| 299 } | 361 } |
| OLD | NEW |