| 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 "fmt" | |
| 10 "io/ioutil" | |
| 11 "path/filepath" | |
| 12 "sort" | |
| 13 "strings" | |
| 14 "time" | |
| 15 | |
| 16 "github.com/golang/protobuf/proto" | |
| 17 "github.com/luci/gae/impl/memory" | |
| 18 "golang.org/x/net/context" | |
| 19 "google.golang.org/grpc" | |
| 20 | |
| 21 swarming "github.com/luci/luci-go/common/api/swarming/swarming/v1" | |
| 22 "github.com/luci/luci-go/common/clock/testclock" | |
| 23 memcfg "github.com/luci/luci-go/common/config/impl/memory" | |
| 24 "github.com/luci/luci-go/common/errors" | |
| 25 miloProto "github.com/luci/luci-go/common/proto/milo" | |
| 26 "github.com/luci/luci-go/logdog/api/endpoints/coordinator/logs/v1" | |
| 27 "github.com/luci/luci-go/logdog/api/logpb" | |
| 28 "github.com/luci/luci-go/logdog/client/coordinator" | |
| 29 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" | |
| 30 "github.com/luci/luci-go/milo/api/resp" | |
| 31 "github.com/luci/luci-go/milo/appengine/common" | |
| 32 "github.com/luci/luci-go/milo/appengine/common/model" | |
| 33 "github.com/luci/luci-go/server/auth" | |
| 34 "github.com/luci/luci-go/server/auth/authtest" | |
| 35 "github.com/luci/luci-go/server/templates" | |
| 36 ) | |
| 37 | |
| 38 type testCase struct { | |
| 39 name string | |
| 40 | |
| 41 swarmResult string | |
| 42 swarmOutput string | |
| 43 annotations string | |
| 44 } | |
| 45 | |
| 46 func getTestCases() []*testCase { | |
| 47 testCases := make(map[string]*testCase) | |
| 48 | |
| 49 // References "milo/appengine/swarming/testdata". | |
| 50 testdata := filepath.Join("..", "swarming", "testdata") | |
| 51 f, err := ioutil.ReadDir(testdata) | |
| 52 if err != nil { | |
| 53 panic(err) | |
| 54 } | |
| 55 for _, fi := range f { | |
| 56 fileName := fi.Name() | |
| 57 parts := strings.SplitN(fileName, ".", 2) | |
| 58 | |
| 59 name := parts[0] | |
| 60 tc := testCases[name] | |
| 61 if tc == nil { | |
| 62 tc = &testCase{name: name} | |
| 63 testCases[name] = tc | |
| 64 } | |
| 65 | |
| 66 switch { | |
| 67 case len(parts) == 1: | |
| 68 tc.swarmOutput = fileName | |
| 69 case parts[1] == "swarm": | |
| 70 tc.swarmResult = fileName | |
| 71 case parts[1] == "pb.txt": | |
| 72 tc.annotations = fileName | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 // Order test cases by name. | |
| 77 names := make([]string, 0, len(testCases)) | |
| 78 for name := range testCases { | |
| 79 names = append(names, name) | |
| 80 } | |
| 81 sort.Strings(names) | |
| 82 | |
| 83 results := make([]*testCase, len(names)) | |
| 84 for i, name := range names { | |
| 85 results[i] = testCases[name] | |
| 86 } | |
| 87 | |
| 88 return results | |
| 89 } | |
| 90 | |
| 91 func (tc *testCase) getContent(name string) []byte { | |
| 92 if name == "" { | |
| 93 return nil | |
| 94 } | |
| 95 | |
| 96 // ../swarming below assumes that | |
| 97 // - this code is not executed by tests outside of this dir | |
| 98 // - this dir is a sibling of frontend dir | |
| 99 path := filepath.Join("..", "swarming", "testdata", name) | |
| 100 data, err := ioutil.ReadFile(path) | |
| 101 if err != nil { | |
| 102 panic(fmt.Errorf("failed to read [%s]: %s", path, err)) | |
| 103 } | |
| 104 return data | |
| 105 } | |
| 106 | |
| 107 func (tc *testCase) getSwarmingResult() *swarming.SwarmingRpcsTaskResult { | |
| 108 var sr swarming.SwarmingRpcsTaskResult | |
| 109 data := tc.getContent(tc.swarmResult) | |
| 110 if err := json.Unmarshal(data, &sr); err != nil { | |
| 111 panic(fmt.Errorf("failed to unmarshal [%s]: %s", tc.swarmResult,
err)) | |
| 112 } | |
| 113 return &sr | |
| 114 } | |
| 115 | |
| 116 func (tc *testCase) getSwarmingOutput() string { | |
| 117 return string(tc.getContent(tc.swarmOutput)) | |
| 118 } | |
| 119 | |
| 120 func (tc *testCase) getAnnotation() *miloProto.Step { | |
| 121 var step miloProto.Step | |
| 122 data := tc.getContent(tc.annotations) | |
| 123 if len(data) == 0 { | |
| 124 return nil | |
| 125 } | |
| 126 | |
| 127 if err := proto.UnmarshalText(string(data), &step); err != nil { | |
| 128 panic(fmt.Errorf("failed to unmarshal text protobuf [%s]: %s", t
c.annotations, err)) | |
| 129 } | |
| 130 return &step | |
| 131 } | |
| 132 | |
| 133 type debugSwarmingService struct { | |
| 134 tc *testCase | |
| 135 } | |
| 136 | |
| 137 func (svc debugSwarmingService) getHost() string { return "example.com" } | |
| 138 | |
| 139 func (svc debugSwarmingService) getSwarmingResult(c context.Context, taskID stri
ng) ( | |
| 140 *swarming.SwarmingRpcsTaskResult, error) { | |
| 141 | |
| 142 return svc.tc.getSwarmingResult(), nil | |
| 143 } | |
| 144 | |
| 145 func (svc debugSwarmingService) getSwarmingRequest(c context.Context, taskID str
ing) ( | |
| 146 *swarming.SwarmingRpcsTaskRequest, error) { | |
| 147 | |
| 148 return nil, errors.New("not implemented") | |
| 149 } | |
| 150 | |
| 151 func (svc debugSwarmingService) getTaskOutput(c context.Context, taskID string)
(string, error) { | |
| 152 return svc.tc.getSwarmingOutput(), nil | |
| 153 } | |
| 154 | |
| 155 // LogTestData returns sample test data for log pages. | |
| 156 func LogTestData() []common.TestBundle { | |
| 157 return []common.TestBundle{ | |
| 158 { | |
| 159 Description: "Basic log", | |
| 160 Data: templates.Args{ | |
| 161 "Log": "This is the log", | |
| 162 "Closed": true, | |
| 163 }, | |
| 164 }, | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 // testLogDogClient is a minimal functional LogsClient implementation. | |
| 169 // | |
| 170 // It retains its latest input parameter and returns its configured err (if not | |
| 171 // nil) or resp. | |
| 172 type testLogDogClient struct { | |
| 173 logdog.LogsClient | |
| 174 | |
| 175 req interface{} | |
| 176 resp interface{} | |
| 177 err error | |
| 178 } | |
| 179 | |
| 180 func (tc *testLogDogClient) Tail(ctx context.Context, in *logdog.TailRequest, op
ts ...grpc.CallOption) ( | |
| 181 *logdog.GetResponse, error) { | |
| 182 | |
| 183 tc.req = in | |
| 184 if tc.err != nil { | |
| 185 return nil, tc.err | |
| 186 } | |
| 187 if tc.resp == nil { | |
| 188 return nil, coordinator.ErrNoSuchStream | |
| 189 } | |
| 190 return tc.resp.(*logdog.GetResponse), nil | |
| 191 } | |
| 192 | |
| 193 func logDogClientFunc(tc *testCase) func(context.Context, string) (*coordinator.
Client, error) { | |
| 194 anno := tc.getAnnotation() | |
| 195 var resp interface{} | |
| 196 if anno != nil { | |
| 197 resp = datagramGetResponse("testproject", "foo/bar", tc.getAnnot
ation()) | |
| 198 } | |
| 199 | |
| 200 return func(c context.Context, host string) (*coordinator.Client, error)
{ | |
| 201 return &coordinator.Client{ | |
| 202 Host: host, | |
| 203 C: &testLogDogClient{ | |
| 204 resp: resp, | |
| 205 }, | |
| 206 }, nil | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 func datagramGetResponse(project, prefix string, msg proto.Message) *logdog.GetR
esponse { | |
| 211 data, err := proto.Marshal(msg) | |
| 212 if err != nil { | |
| 213 panic(err) | |
| 214 } | |
| 215 return &logdog.GetResponse{ | |
| 216 Project: project, | |
| 217 Desc: &logpb.LogStreamDescriptor{ | |
| 218 Prefix: prefix, | |
| 219 ContentType: miloProto.ContentTypeAnnotations, | |
| 220 StreamType: logpb.StreamType_DATAGRAM, | |
| 221 }, | |
| 222 State: &logdog.LogStreamState{}, | |
| 223 Logs: []*logpb.LogEntry{ | |
| 224 { | |
| 225 Content: &logpb.LogEntry_Datagram{ | |
| 226 Datagram: &logpb.Datagram{ | |
| 227 Data: data, | |
| 228 }, | |
| 229 }, | |
| 230 }, | |
| 231 }, | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 // BuildTestData returns sample test data for swarming build pages. | |
| 236 func BuildTestData() []common.TestBundle { | |
| 237 basic := resp.MiloBuild{ | |
| 238 Summary: resp.BuildComponent{ | |
| 239 Label: "Test swarming build", | |
| 240 Status: model.Success, | |
| 241 Started: time.Date(2016, 1, 2, 15, 4, 5, 999999999, tim
e.UTC), | |
| 242 Finished: time.Date(2016, 1, 2, 15, 4, 6, 999999999, tim
e.UTC), | |
| 243 Duration: time.Second, | |
| 244 }, | |
| 245 } | |
| 246 results := []common.TestBundle{ | |
| 247 { | |
| 248 Description: "Basic successful build", | |
| 249 Data: templates.Args{"Build": basic}, | |
| 250 }, | |
| 251 } | |
| 252 c := context.Background() | |
| 253 c = memory.UseWithAppID(c, "dev~luci-milo") | |
| 254 c = testconfig.WithCommonClient(c, memcfg.New(aclConfgs)) | |
| 255 c = auth.WithState(c, &authtest.FakeState{ | |
| 256 Identity: "user:alicebob@google.com", | |
| 257 IdentityGroups: []string{"all", "googlers"}, | |
| 258 }) | |
| 259 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) | |
| 260 | |
| 261 for _, tc := range getTestCases() { | |
| 262 svc := debugSwarmingService{tc} | |
| 263 bl := buildLoader{ | |
| 264 logDogClientFunc: logDogClientFunc(tc), | |
| 265 } | |
| 266 | |
| 267 build, err := bl.swarmingBuildImpl(c, svc, "foo", tc.name) | |
| 268 if err != nil { | |
| 269 panic(fmt.Errorf("Error while processing %s: %s", tc.nam
e, err)) | |
| 270 } | |
| 271 results = append(results, common.TestBundle{ | |
| 272 Description: tc.name, | |
| 273 Data: templates.Args{"Build": build}, | |
| 274 }) | |
| 275 } | |
| 276 return results | |
| 277 } | |
| 278 | |
| 279 var secretProjectCfg = ` | |
| 280 name: "secret" | |
| 281 access: "group:googlers" | |
| 282 ` | |
| 283 | |
| 284 var publicProjectCfg = ` | |
| 285 name: "opensource" | |
| 286 access: "group:all" | |
| 287 ` | |
| 288 | |
| 289 var aclConfgs = map[string]memcfg.ConfigSet{ | |
| 290 "projects/secret": { | |
| 291 "project.cfg": secretProjectCfg, | |
| 292 }, | |
| 293 "projects/opensource": { | |
| 294 "project.cfg": publicProjectCfg, | |
| 295 }, | |
| 296 } | |
| OLD | NEW |