stream_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package streams
  14. import (
  15. "context"
  16. "errors"
  17. "testing"
  18. "google.golang.org/grpc/metadata"
  19. "google.golang.org/protobuf/types/known/anypb"
  20. "github.com/golang/protobuf/ptypes/any"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/assert/cmp"
  23. streamsv1 "github.com/docker/compose-cli/cli/server/protos/streams/v1"
  24. )
  25. type byteStream struct {
  26. recvResult *any.Any
  27. recvErr error
  28. sendResult interface{}
  29. }
  30. func (bs *byteStream) SetHeader(metadata.MD) error {
  31. return nil
  32. }
  33. func (bs *byteStream) SendHeader(metadata.MD) error {
  34. return nil
  35. }
  36. func (bs *byteStream) SetTrailer(metadata.MD) {
  37. }
  38. func (bs *byteStream) Context() context.Context {
  39. return nil
  40. }
  41. func (bs *byteStream) SendMsg(m interface{}) error {
  42. bs.sendResult = m
  43. return nil
  44. }
  45. func (bs *byteStream) Send(*any.Any) error {
  46. return nil
  47. }
  48. func (bs *byteStream) Recv() (*any.Any, error) {
  49. return bs.recvResult, bs.recvErr
  50. }
  51. func (bs *byteStream) RecvMsg(m interface{}) error {
  52. return nil
  53. }
  54. func getReader(t *testing.T, in []byte, errResult error) IO {
  55. message := streamsv1.BytesMessage{
  56. Type: streamsv1.IOStream_STDOUT,
  57. Value: in,
  58. }
  59. m, err := anypb.New(&message)
  60. assert.NilError(t, err)
  61. return IO{
  62. Stream: &Stream{
  63. Streaming_NewStreamServer: &byteStream{
  64. recvResult: m,
  65. recvErr: errResult,
  66. },
  67. },
  68. }
  69. }
  70. func getAny(t *testing.T, in []byte) *any.Any {
  71. value, err := anypb.New(&streamsv1.BytesMessage{
  72. Type: streamsv1.IOStream_STDOUT,
  73. Value: in,
  74. })
  75. assert.NilError(t, err)
  76. return value
  77. }
  78. func TestStreamReader(t *testing.T) {
  79. in := []byte{104, 101, 108, 108, 111}
  80. r := getReader(t, in, nil)
  81. buffer := make([]byte, 5)
  82. n, err := r.Read(buffer)
  83. assert.NilError(t, err)
  84. assert.Equal(t, n, 5)
  85. assert.DeepEqual(t, buffer, in)
  86. }
  87. func TestStreamReaderError(t *testing.T) {
  88. errResult := errors.New("err")
  89. r := getReader(t, nil, errResult)
  90. var buffer []byte
  91. n, err := r.Read(buffer)
  92. assert.Equal(t, n, 0)
  93. assert.Error(t, err, errResult.Error())
  94. }
  95. func TestStreamWriter(t *testing.T) {
  96. in := []byte{104, 101, 108, 108, 111}
  97. expected := getAny(t, in)
  98. bs := byteStream{}
  99. w := IO{
  100. Stream: &Stream{
  101. Streaming_NewStreamServer: &bs,
  102. },
  103. }
  104. n, err := w.Write(in)
  105. assert.NilError(t, err)
  106. assert.Assert(t, cmp.Len(in, n))
  107. sendResult, ok := (bs.sendResult).(*anypb.Any)
  108. assert.Assert(t, ok)
  109. assert.DeepEqual(t, sendResult.Value, expected.Value)
  110. }