go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/host/spy_test.go (about) 1 // Copyright 2019 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package host 16 17 import ( 18 "bytes" 19 "compress/zlib" 20 "context" 21 "runtime" 22 "testing" 23 24 "google.golang.org/protobuf/proto" 25 26 bbpb "go.chromium.org/luci/buildbucket/proto" 27 "go.chromium.org/luci/logdog/client/butlerlib/bootstrap" 28 "go.chromium.org/luci/logdog/client/butlerlib/streamclient" 29 "go.chromium.org/luci/lucictx" 30 "go.chromium.org/luci/luciexe" 31 32 . "github.com/smartystreets/goconvey/convey" 33 34 . "go.chromium.org/luci/common/testing/assertions" 35 ) 36 37 func TestSpy(t *testing.T) { 38 if runtime.GOOS == "windows" { 39 t.Skip("disabled on windows due to crbug.com/998936") 40 } 41 42 Convey(`test build spy environment`, t, func() { 43 ctx, closer := testCtx() 44 defer closer() 45 46 sawBuild := false 47 sawBuildC := make(chan struct{}) 48 defer func() { 49 if !sawBuild { 50 close(sawBuildC) 51 } 52 }() 53 54 Convey(`butler active within Run`, func(c C) { 55 ch, err := Run(ctx, nil, func(ctx context.Context, _ Options, _ <-chan lucictx.DeadlineEvent, _ func()) { 56 bs, _ := bootstrap.Get() 57 58 stream, err := bs.Client.NewDatagramStream( 59 ctx, luciexe.BuildProtoStreamSuffix, 60 streamclient.WithContentType(luciexe.BuildProtoZlibContentType)) 61 c.So(err, ShouldBeNil) 62 defer stream.Close() 63 64 data, err := proto.Marshal(&bbpb.Build{ 65 SummaryMarkdown: "we did it", 66 Status: bbpb.Status_SUCCESS, 67 Output: &bbpb.Build_Output{ 68 Status: bbpb.Status_SUCCESS, 69 }, 70 }) 71 c.So(err, ShouldBeNil) 72 73 buf := bytes.Buffer{} 74 z := zlib.NewWriter(&buf) 75 _, err = z.Write(data) 76 c.So(err, ShouldBeNil) 77 c.So(z.Close(), ShouldBeNil) 78 data = buf.Bytes() 79 80 err = stream.WriteDatagram(data) 81 c.So(err, ShouldBeNil) 82 c.So(stream.Close(), ShouldBeNil) 83 84 // NOTE: This is very much cheating. Currently (Sept 2019) there's a bug 85 // in Logdog Butler (crbug.com/1007022) where the butler protocol is TOO 86 // asynchronous, and it's possible for this entire callback to execute 87 // and return before the butler has registered the stream above. 88 // 89 // Without sawBuildC, it's possible that Run() will close the "u/" 90 // namespace before the butler sees the stream that we just opened. 91 <-sawBuildC 92 }) 93 94 So(err, ShouldBeNil) 95 for build := range ch { 96 if build.EndTime != nil && !sawBuild { 97 build := proto.Clone(build).(*bbpb.Build) 98 So(build.UpdateTime, ShouldNotBeNil) 99 So(build.EndTime, ShouldNotBeNil) 100 build.UpdateTime = nil 101 build.EndTime = nil 102 103 So(build, ShouldResembleProto, &bbpb.Build{ 104 SummaryMarkdown: "we did it", 105 Status: bbpb.Status_SUCCESS, 106 Output: &bbpb.Build_Output{ 107 Status: bbpb.Status_SUCCESS, 108 }, 109 }) 110 close(sawBuildC) 111 sawBuild = true 112 } 113 } 114 So(sawBuild, ShouldBeTrue) 115 }) 116 117 }) 118 }