OLD | NEW |
| (Empty) |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 package buildbucket | |
6 | |
7 import ( | |
8 "encoding/json" | |
9 "flag" | |
10 "fmt" | |
11 "io/ioutil" | |
12 "os" | |
13 "path/filepath" | |
14 "testing" | |
15 | |
16 "github.com/luci/gae/impl/memory" | |
17 "github.com/luci/luci-go/common/clock/testclock" | |
18 memcfg "github.com/luci/luci-go/common/config/impl/memory" | |
19 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" | |
20 "github.com/luci/luci-go/milo/appengine/common" | |
21 "golang.org/x/net/context" | |
22 | |
23 . "github.com/smartystreets/goconvey/convey" | |
24 ) | |
25 | |
26 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") | |
27 | |
28 func TestBuilder(t *testing.T) { | |
29 t.Parallel() | |
30 | |
31 testCases := []struct{ bucket, builder string }{ | |
32 {"master.tryserver.infra", "InfraPresubmit"}, | |
33 {"master.tryserver.infra", "InfraPresubmit.Swarming"}, | |
34 } | |
35 | |
36 Convey("Builder", t, func() { | |
37 c := memory.UseWithAppID(context.Background(), "luci-milo-dev") | |
38 c, _ = testclock.UseTime(c, testclock.TestRecentTimeUTC) | |
39 c = testconfig.WithCommonClient(c, memcfg.New(bktConfigFull)) | |
40 // Update the service config so that the settings are loaded. | |
41 err := common.UpdateServiceConfig(c) | |
42 So(err, ShouldBeNil) | |
43 | |
44 for _, tc := range testCases { | |
45 tc := tc | |
46 Convey(fmt.Sprintf("%s:%s", tc.bucket, tc.builder), func
() { | |
47 expectationFilePath := filepath.Join("expectatio
ns", tc.bucket, tc.builder+".json") | |
48 err := os.MkdirAll(filepath.Dir(expectationFileP
ath), 0777) | |
49 So(err, ShouldBeNil) | |
50 | |
51 actual, err := builderImpl(c, | |
52 builderQuery{ | |
53 Bucket: tc.bucket, | |
54 Builder: tc.builder, | |
55 Limit: 0, | |
56 }) | |
57 So(err, ShouldBeNil) | |
58 actualJSON, err := json.MarshalIndent(actual, ""
, " ") | |
59 So(err, ShouldBeNil) | |
60 | |
61 if *generate { | |
62 err := ioutil.WriteFile(expectationFileP
ath, actualJSON, 0777) | |
63 So(err, ShouldBeNil) | |
64 } else { | |
65 expectedJSON, err := ioutil.ReadFile(exp
ectationFilePath) | |
66 So(err, ShouldBeNil) | |
67 So(string(actualJSON), ShouldEqual, stri
ng(expectedJSON)) | |
68 } | |
69 }) | |
70 } | |
71 }) | |
72 } | |
73 | |
74 var bktConfig = ` | |
75 buildbucket: { | |
76 host: "debug" | |
77 } | |
78 ` | |
79 | |
80 var bktConfigFull = map[string]memcfg.ConfigSet{ | |
81 "services/luci-milo-dev": { | |
82 "settings.cfg": bktConfig, | |
83 }, | |
84 } | |
OLD | NEW |