go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamproto/properties_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 streamproto 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "go.chromium.org/luci/logdog/api/logpb" 22 "go.chromium.org/luci/logdog/common/types" 23 24 . "github.com/smartystreets/goconvey/convey" 25 ) 26 27 func TestFlags(t *testing.T) { 28 Convey(`A Flags instance`, t, func() { 29 f := Flags{ 30 Name: "my/stream", 31 ContentType: "foo/bar", 32 } 33 34 Convey(`Converts to LogStreamDescriptor.`, func() { 35 So(f.Descriptor(), ShouldResemble, &logpb.LogStreamDescriptor{ 36 Name: "my/stream", 37 ContentType: "foo/bar", 38 StreamType: logpb.StreamType_TEXT, 39 }) 40 }) 41 }) 42 } 43 44 func TestFlagsJSON(t *testing.T) { 45 Convey(`A Flags instance`, t, func() { 46 f := Flags{} 47 Convey(`Will decode a TEXT stream.`, func() { 48 t := `{"name": "my/stream", "contentType": "foo/bar", "type": "text"}` 49 So(json.Unmarshal([]byte(t), &f), ShouldBeNil) 50 51 So(f.Descriptor(), ShouldResemble, &logpb.LogStreamDescriptor{ 52 Name: "my/stream", 53 ContentType: "foo/bar", 54 StreamType: logpb.StreamType_TEXT, 55 }) 56 }) 57 58 Convey(`Will fail to decode an invalid stream type.`, func() { 59 t := `{"name": "my/stream", "type": "XXX_whatisthis?"}` 60 So(json.Unmarshal([]byte(t), &f), ShouldNotBeNil) 61 }) 62 63 Convey(`Will decode a BINARY stream.`, func() { 64 t := `{"name": "my/stream", "type": "binary"}` 65 So(json.Unmarshal([]byte(t), &f), ShouldBeNil) 66 67 So(f.Descriptor(), ShouldResemble, &logpb.LogStreamDescriptor{ 68 Name: "my/stream", 69 StreamType: logpb.StreamType_BINARY, 70 ContentType: string(types.ContentTypeBinary), 71 }) 72 }) 73 74 Convey(`Will decode a DATAGRAM stream.`, func() { 75 t := `{"name": "my/stream", "type": "datagram"}` 76 So(json.Unmarshal([]byte(t), &f), ShouldBeNil) 77 78 So(f.Descriptor(), ShouldResemble, &logpb.LogStreamDescriptor{ 79 Name: "my/stream", 80 StreamType: logpb.StreamType_DATAGRAM, 81 ContentType: string(types.ContentTypeLogdogDatagram), 82 }) 83 }) 84 }) 85 }