go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamclient/options_test.go (about) 1 // Copyright 2015 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 streamclient 16 17 import ( 18 "context" 19 "testing" 20 21 "go.chromium.org/luci/common/clock/testclock" 22 "go.chromium.org/luci/logdog/client/butlerlib/streamproto" 23 24 . "github.com/smartystreets/goconvey/convey" 25 . "go.chromium.org/luci/common/testing/assertions" 26 ) 27 28 func TestOptions(t *testing.T) { 29 t.Parallel() 30 31 Convey(`options`, t, func() { 32 scFake, client := NewUnregisteredFake("") 33 34 ctx, _ := testclock.UseTime(context.Background(), testclock.TestTimeUTC) 35 36 Convey(`defaults`, func() { 37 _, err := client.NewStream(ctx, "test") 38 So(err, ShouldBeNil) 39 defaultFlags := scFake.Data()["test"].GetFlags() 40 So(defaultFlags.ContentType, ShouldEqual, "text/plain; charset=utf-8") 41 So(defaultFlags.Timestamp.Time(), ShouldEqual, testclock.TestTimeUTC) 42 So(defaultFlags.Tags, ShouldBeEmpty) 43 }) 44 45 Convey(`can change content type`, func() { 46 _, err := client.NewStream(ctx, "test", WithContentType("narple")) 47 So(err, ShouldBeNil) 48 testFlags := scFake.Data()["test"].GetFlags() 49 So(testFlags.ContentType, ShouldEqual, "narple") 50 }) 51 52 Convey(`can set initial timestamp`, func() { 53 _, err := client.NewStream(ctx, "test", WithTimestamp(testclock.TestRecentTimeUTC)) 54 So(err, ShouldBeNil) 55 testFlags := scFake.Data()["test"].GetFlags() 56 So(testFlags.Timestamp.Time(), ShouldEqual, testclock.TestRecentTimeUTC) 57 }) 58 59 Convey(`can set tags nicely`, func() { 60 _, err := client.NewStream(ctx, "test", WithTags( 61 "key1", "value", 62 "key2", "value", 63 )) 64 So(err, ShouldBeNil) 65 testFlags := scFake.Data()["test"].GetFlags() 66 So(testFlags.Tags, ShouldResemble, streamproto.TagMap{ 67 "key1": "value", 68 "key2": "value", 69 }) 70 }) 71 72 Convey(`WithTags expects an even number of args`, func() { 73 So(func() { 74 WithTags("hi") 75 }, ShouldPanicLike, "even number of arguments") 76 }) 77 78 Convey(`can set tags practically`, func() { 79 _, err := client.NewStream(ctx, "test", WithTagMap(map[string]string{ 80 "key1": "value", 81 "key2": "value", 82 })) 83 So(err, ShouldBeNil) 84 testFlags := scFake.Data()["test"].GetFlags() 85 So(testFlags.Tags, ShouldResemble, streamproto.TagMap{ 86 "key1": "value", 87 "key2": "value", 88 }) 89 }) 90 91 }) 92 }