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 buildbot | |
6 | |
7 import ( | |
8 "encoding/json" | |
9 "flag" | |
10 "fmt" | |
11 "io/ioutil" | |
12 "path" | |
13 "strings" | |
14 "testing" | |
15 | |
16 "github.com/luci/gae/impl/memory" | |
17 ds "github.com/luci/gae/service/datastore" | |
18 "github.com/luci/luci-go/common/clock/testclock" | |
19 memcfg "github.com/luci/luci-go/common/config/impl/memory" | |
20 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" | |
21 "github.com/luci/luci-go/server/auth" | |
22 "github.com/luci/luci-go/server/auth/authtest" | |
23 "github.com/luci/luci-go/server/auth/identity" | |
24 | |
25 "golang.org/x/net/context" | |
26 | |
27 . "github.com/smartystreets/goconvey/convey" | |
28 ) | |
29 | |
30 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") | |
31 | |
32 func load(name string) ([]byte, error) { | |
33 filename := strings.Join([]string{"expectations", name}, "/") | |
34 return ioutil.ReadFile(filename) | |
35 } | |
36 | |
37 func shouldMatchExpectationsFor(actualContents interface{}, expectedFilename ...
interface{}) string { | |
38 refBuild, err := load(expectedFilename[0].(string)) | |
39 if err != nil { | |
40 return fmt.Sprintf("Could not load %s: %s", expectedFilename[0],
err.Error()) | |
41 } | |
42 actualBuild, err := json.MarshalIndent(actualContents, "", " ") | |
43 return ShouldEqual(string(actualBuild), string(refBuild)) | |
44 | |
45 } | |
46 | |
47 func TestBuild(t *testing.T) { | |
48 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") | |
49 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | |
50 | |
51 if *generate { | |
52 for _, tc := range TestCases { | |
53 fmt.Printf("Generating expectations for %s/%s\n", tc.Bui
lder, tc.Build) | |
54 build, err := Build(c, "debug", tc.Builder, tc.Build) | |
55 if err != nil { | |
56 panic(fmt.Errorf("Could not run build() for %s/%
s: %s", tc.Builder, tc.Build, err)) | |
57 } | |
58 buildJSON, err := json.MarshalIndent(build, "", " ") | |
59 if err != nil { | |
60 panic(fmt.Errorf("Could not JSON marshal %s/%s:
%s", tc.Builder, tc.Build, err)) | |
61 } | |
62 fname := fmt.Sprintf("%s.%d.build.json", tc.Builder, tc.
Build) | |
63 fpath := path.Join("expectations", fname) | |
64 err = ioutil.WriteFile(fpath, []byte(buildJSON), 0644) | |
65 if err != nil { | |
66 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", fpath, err)) | |
67 } | |
68 } | |
69 return | |
70 } | |
71 | |
72 Convey(`A test Environment`, t, func() { | |
73 c = testconfig.WithCommonClient(c, memcfg.New(bbAclConfigs)) | |
74 c = auth.WithState(c, &authtest.FakeState{ | |
75 Identity: identity.AnonymousIdentity, | |
76 IdentityGroups: []string{"all"}, | |
77 }) | |
78 | |
79 for _, tc := range TestCases { | |
80 Convey(fmt.Sprintf("Test Case: %s/%s", tc.Builder, tc.Bu
ild), func() { | |
81 build, err := Build(c, "debug", tc.Builder, tc.B
uild) | |
82 So(err, ShouldBeNil) | |
83 fname := fmt.Sprintf("%s.%d.build.json", tc.Buil
der, tc.Build) | |
84 So(build, shouldMatchExpectationsFor, fname) | |
85 }) | |
86 } | |
87 | |
88 Convey(`Disallow anonomyous users from accessing internal builds
`, func() { | |
89 ds.Put(c, &buildbotBuild{ | |
90 Master: "fake", | |
91 Buildername: "fake", | |
92 Number: 1, | |
93 Internal: true, | |
94 }) | |
95 _, err := getBuild(c, "fake", "fake", 1) | |
96 So(err, ShouldResemble, errNotAuth) | |
97 }) | |
98 }) | |
99 } | |
100 | |
101 var internalConfig = ` | |
102 buildbot: { | |
103 internal_reader: "googlers" | |
104 public_subscription: "projects/luci-milo/subscriptions/buildbot-public" | |
105 internal_subscription: "projects/luci-milo/subscriptions/buildbot-privat
e" | |
106 } | |
107 ` | |
108 | |
109 var bbAclConfigs = map[string]memcfg.ConfigSet{ | |
110 "services/luci-milo": { | |
111 "settings.cfg": internalConfig, | |
112 }, | |
113 } | |
OLD | NEW |