| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "testing" |
| 9 |
| 10 swarming "github.com/luci/luci-go/common/api/swarming/swarming/v1" |
| 11 miloProto "github.com/luci/luci-go/common/proto/milo" |
| 12 "github.com/luci/luci-go/logdog/api/endpoints/coordinator/logs/v1" |
| 13 "github.com/luci/luci-go/logdog/api/logpb" |
| 14 "github.com/luci/luci-go/logdog/client/coordinator" |
| 15 milo "github.com/luci/luci-go/milo/api/proto" |
| 16 |
| 17 "github.com/luci/gae/impl/memory" |
| 18 |
| 19 "github.com/golang/protobuf/proto" |
| 20 "golang.org/x/net/context" |
| 21 "google.golang.org/grpc" |
| 22 |
| 23 . "github.com/luci/luci-go/common/testing/assertions" |
| 24 . "github.com/smartystreets/goconvey/convey" |
| 25 ) |
| 26 |
| 27 // testLogDogClient is a minimal functional LogsClient implementation. |
| 28 // |
| 29 // It retains its latest input parameter and returns its configured err (if not |
| 30 // nil) or resp. |
| 31 type testLogDogClient struct { |
| 32 logdog.LogsClient |
| 33 |
| 34 req interface{} |
| 35 resp interface{} |
| 36 err error |
| 37 } |
| 38 |
| 39 func (tc *testLogDogClient) Tail(ctx context.Context, in *logdog.TailRequest, op
ts ...grpc.CallOption) ( |
| 40 *logdog.GetResponse, error) { |
| 41 |
| 42 tc.req = in |
| 43 if tc.err != nil { |
| 44 return nil, tc.err |
| 45 } |
| 46 return tc.resp.(*logdog.GetResponse), nil |
| 47 } |
| 48 |
| 49 type testSwarmingService struct { |
| 50 swarmingService |
| 51 |
| 52 host string |
| 53 req swarming.SwarmingRpcsTaskRequest |
| 54 res swarming.SwarmingRpcsTaskResult |
| 55 } |
| 56 |
| 57 func (sf *testSwarmingService) getHost() string { return sf.host } |
| 58 |
| 59 func (sf *testSwarmingService) getSwarmingResult(c context.Context, taskID strin
g) ( |
| 60 *swarming.SwarmingRpcsTaskResult, error) { |
| 61 |
| 62 return &sf.res, nil |
| 63 } |
| 64 |
| 65 func (sf *testSwarmingService) getSwarmingRequest(c context.Context, taskID stri
ng) ( |
| 66 *swarming.SwarmingRpcsTaskRequest, error) { |
| 67 |
| 68 return &sf.req, nil |
| 69 } |
| 70 |
| 71 func datagramGetResponse(project, prefix string, msg proto.Message) *logdog.GetR
esponse { |
| 72 data, err := proto.Marshal(msg) |
| 73 if err != nil { |
| 74 panic(err) |
| 75 } |
| 76 return &logdog.GetResponse{ |
| 77 Project: project, |
| 78 Desc: &logpb.LogStreamDescriptor{ |
| 79 Prefix: prefix, |
| 80 ContentType: miloProto.ContentTypeAnnotations, |
| 81 StreamType: logpb.StreamType_DATAGRAM, |
| 82 }, |
| 83 State: &logdog.LogStreamState{}, |
| 84 Logs: []*logpb.LogEntry{ |
| 85 { |
| 86 Content: &logpb.LogEntry_Datagram{ |
| 87 Datagram: &logpb.Datagram{ |
| 88 Data: data, |
| 89 }, |
| 90 }, |
| 91 }, |
| 92 }, |
| 93 } |
| 94 } |
| 95 |
| 96 func TestBuildInfo(t *testing.T) { |
| 97 t.Parallel() |
| 98 |
| 99 Convey("A testing BuildInfoProvider", t, func() { |
| 100 c := context.Background() |
| 101 c = memory.Use(c) |
| 102 |
| 103 testClient := testLogDogClient{} |
| 104 testSvc := testSwarmingService{ |
| 105 host: "swarming.example.com", |
| 106 req: swarming.SwarmingRpcsTaskRequest{ |
| 107 Properties: &swarming.SwarmingRpcsTaskProperties
{ |
| 108 Command: []string{"kitchen", "foo", "bar
", "-logdog-project", "testproject", "baz"}, |
| 109 }, |
| 110 Tags: []string{ |
| 111 "allow_milo:1", |
| 112 }, |
| 113 }, |
| 114 res: swarming.SwarmingRpcsTaskResult{ |
| 115 TaskId: "12345", |
| 116 State: TaskRunning, |
| 117 Tags: []string{ |
| 118 "allow_milo:1", |
| 119 "foo:1", |
| 120 "bar:2", |
| 121 }, |
| 122 }, |
| 123 } |
| 124 bip := BuildInfoProvider{ |
| 125 LogdogClientFunc: func(context.Context) (*coordinator.Cl
ient, error) { |
| 126 return &coordinator.Client{ |
| 127 C: &testClient, |
| 128 Host: "example.com", |
| 129 }, nil |
| 130 }, |
| 131 swarmingServiceFunc: func(context.Context) (swarmingServ
ice, error) { |
| 132 return &testSvc, nil |
| 133 }, |
| 134 } |
| 135 |
| 136 logdogStep := miloProto.Step{ |
| 137 Command: &miloProto.Step_Command{ |
| 138 CommandLine: []string{"foo", "bar", "baz"}, |
| 139 }, |
| 140 Text: []string{"test step"}, |
| 141 Property: []*miloProto.Step_Property{ |
| 142 {Name: "bar", Value: "log-bar"}, |
| 143 }, |
| 144 } |
| 145 |
| 146 biReq := milo.BuildInfoRequest{ |
| 147 Build: &milo.BuildInfoRequest_Swarming_{ |
| 148 Swarming: &milo.BuildInfoRequest_Swarming{ |
| 149 Task: "12345", |
| 150 }, |
| 151 }, |
| 152 } |
| 153 |
| 154 Convey("Will fail to load a non-Kitchen build.", func() { |
| 155 testSvc.req.Properties.Command = []string{"not", "kitche
n"} |
| 156 |
| 157 _, err := bip.GetBuildInfo(c, biReq.GetSwarming(), "") |
| 158 So(err, ShouldBeRPCNotFound) |
| 159 }) |
| 160 |
| 161 Convey("Can load a build, inferring project from Kitchen CLI.",
func() { |
| 162 testClient.resp = datagramGetResponse("testproject", "sw
arm/swarming.example.com/12345", &logdogStep) |
| 163 |
| 164 resp, err := bip.GetBuildInfo(c, biReq.GetSwarming(), ""
) |
| 165 So(err, ShouldBeNil) |
| 166 So(testClient.req, ShouldResemble, &logdog.TailRequest{ |
| 167 Project: "testproject", |
| 168 Path: "swarm/swarming.example.com/12345/+/ann
otations", |
| 169 State: true, |
| 170 }) |
| 171 So(resp, ShouldResemble, &milo.BuildInfoResponse{ |
| 172 Project: "testproject", |
| 173 Step: &miloProto.Step{ |
| 174 Command: &miloProto.Step_Command{ |
| 175 CommandLine: []string{"foo", "ba
r", "baz"}, |
| 176 }, |
| 177 Text: []string{"test step"}, |
| 178 Link: &miloProto.Link{ |
| 179 Label: "Task 12345", |
| 180 Value: &miloProto.Link_Url{ |
| 181 Url: "https://swarming.e
xample.com/user/task/12345", |
| 182 }, |
| 183 }, |
| 184 Property: []*miloProto.Step_Property{ |
| 185 {Name: "bar", Value: "log-bar"}, |
| 186 }, |
| 187 }, |
| 188 AnnotationStream: &miloProto.LogdogStream{ |
| 189 Server: "example.com", |
| 190 Prefix: "swarm/swarming.example.com/1234
5", |
| 191 Name: "annotations", |
| 192 }, |
| 193 }) |
| 194 }) |
| 195 |
| 196 Convey("Will fail to load Kitchen without LogDog and no project
hint.", func() { |
| 197 testSvc.req.Properties.Command = []string{"kitchen"} |
| 198 |
| 199 _, err := bip.GetBuildInfo(c, biReq.GetSwarming(), "") |
| 200 So(err, ShouldBeRPCNotFound) |
| 201 }) |
| 202 |
| 203 Convey("Will load Kitchen without LogDog if there is a project h
int.", func() { |
| 204 biReq.ProjectHint = "testproject" |
| 205 testSvc.req.Properties.Command = []string{"kitchen"} |
| 206 testClient.resp = datagramGetResponse("testproject", "sw
arm/swarming.example.com/12345", &logdogStep) |
| 207 |
| 208 resp, err := bip.GetBuildInfo(c, biReq.GetSwarming(), "t
estproject") |
| 209 So(err, ShouldBeNil) |
| 210 So(testClient.req, ShouldResemble, &logdog.TailRequest{ |
| 211 Project: "testproject", |
| 212 Path: "swarm/swarming.example.com/12345/+/ann
otations", |
| 213 State: true, |
| 214 }) |
| 215 So(resp, ShouldResemble, &milo.BuildInfoResponse{ |
| 216 Project: "testproject", |
| 217 Step: &miloProto.Step{ |
| 218 Command: &miloProto.Step_Command{ |
| 219 CommandLine: []string{"foo", "ba
r", "baz"}, |
| 220 }, |
| 221 Text: []string{"test step"}, |
| 222 Link: &miloProto.Link{ |
| 223 Label: "Task 12345", |
| 224 Value: &miloProto.Link_Url{ |
| 225 Url: "https://swarming.e
xample.com/user/task/12345", |
| 226 }, |
| 227 }, |
| 228 Property: []*miloProto.Step_Property{ |
| 229 {Name: "bar", Value: "log-bar"}, |
| 230 }, |
| 231 }, |
| 232 AnnotationStream: &miloProto.LogdogStream{ |
| 233 Server: "example.com", |
| 234 Prefix: "swarm/swarming.example.com/1234
5", |
| 235 Name: "annotations", |
| 236 }, |
| 237 }) |
| 238 }) |
| 239 }) |
| 240 } |
| OLD | NEW |