OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 swarming | |
6 | |
7 import ( | |
8 "encoding/json" | |
9 "flag" | |
10 "fmt" | |
11 "io/ioutil" | |
12 "path/filepath" | |
13 "testing" | |
14 "time" | |
15 | |
16 "github.com/luci/luci-go/common/clock/testclock" | |
17 memcfg "github.com/luci/luci-go/common/config/impl/memory" | |
18 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" | |
19 "github.com/luci/luci-go/server/auth" | |
20 "github.com/luci/luci-go/server/auth/authtest" | |
21 "github.com/luci/luci-go/server/auth/identity" | |
22 | |
23 "github.com/luci/gae/impl/memory" | |
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 := filepath.Join("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 t.Parallel() | |
49 | |
50 if *generate { | |
51 c := context.Background() | |
52 // This is one hour after the start timestamp in the sample test
data. | |
53 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11,
0, 0, 0, time.UTC)) | |
54 c = memory.UseWithAppID(c, "dev~luci-milo") | |
55 c = testconfig.WithCommonClient(c, memcfg.New(aclConfgs)) | |
56 c = auth.WithState(c, &authtest.FakeState{ | |
57 Identity: "user:alicebob@google.com", | |
58 IdentityGroups: []string{"all", "googlers"}, | |
59 }) | |
60 | |
61 for _, tc := range getTestCases() { | |
62 bl := buildLoader{ | |
63 logDogClientFunc: logDogClientFunc(tc), | |
64 } | |
65 svc := debugSwarmingService{tc} | |
66 | |
67 fmt.Printf("Generating expectations for %s\n", tc) | |
68 | |
69 build, err := bl.swarmingBuildImpl(c, svc, "foo", tc.nam
e) | |
70 if err != nil { | |
71 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc, err)) | |
72 } | |
73 buildJSON, err := json.MarshalIndent(build, "", " ") | |
74 if err != nil { | |
75 panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc.name, err)) | |
76 } | |
77 filename := filepath.Join("expectations", tc.name+".json
") | |
78 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) | |
79 if err != nil { | |
80 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) | |
81 } | |
82 } | |
83 return | |
84 } | |
85 | |
86 Convey(`A test Environment`, t, func() { | |
87 c := context.Background() | |
88 // This is one hour after the start timestamp in the sample test
data. | |
89 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11,
0, 0, 0, time.UTC)) | |
90 c = memory.UseWithAppID(c, "dev~luci-milo") | |
91 c = testconfig.WithCommonClient(c, memcfg.New(aclConfgs)) | |
92 c = auth.WithState(c, &authtest.FakeState{ | |
93 Identity: "user:alicebob@google.com", | |
94 IdentityGroups: []string{"all", "googlers"}, | |
95 }) | |
96 | |
97 for _, tc := range getTestCases() { | |
98 Convey(fmt.Sprintf("Test Case: %s", tc.name), func() { | |
99 bl := buildLoader{ | |
100 logDogClientFunc: logDogClientFunc(tc), | |
101 } | |
102 svc := debugSwarmingService{tc} | |
103 | |
104 // Special case: The build-internal test case to
check that ACLs should fail. | |
105 if tc.name == "build-internal" { | |
106 Convey("Should fail", func() { | |
107 c := auth.WithState(c, &authtest
.FakeState{ | |
108 Identity: identity
.AnonymousIdentity, | |
109 IdentityGroups: []string
{"all"}, | |
110 }) | |
111 _, err := bl.swarmingBuildImpl(c
, svc, "foo", tc.name) | |
112 So(err.Error(), ShouldResemble,
"Not a Milo Job or access denied") | |
113 }) | |
114 } | |
115 | |
116 build, err := bl.swarmingBuildImpl(c, svc, "foo"
, tc.name) | |
117 So(err, ShouldBeNil) | |
118 So(build, shouldMatchExpectationsFor, tc.name+".
json") | |
119 }) | |
120 } | |
121 }) | |
122 } | |
OLD | NEW |