logs_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "testing"
  17. "google.golang.org/grpc/metadata"
  18. "gotest.tools/v3/assert"
  19. "gotest.tools/v3/assert/cmp"
  20. v1 "github.com/docker/api/protos/containers/v1"
  21. )
  22. type logServer struct {
  23. logs interface{}
  24. }
  25. func (ls *logServer) Send(response *v1.LogsResponse) error {
  26. return nil
  27. }
  28. func (ls *logServer) SetHeader(metadata.MD) error {
  29. return nil
  30. }
  31. func (ls *logServer) SendHeader(metadata.MD) error {
  32. return nil
  33. }
  34. func (ls *logServer) SetTrailer(metadata.MD) {
  35. }
  36. func (ls *logServer) Context() context.Context {
  37. return nil
  38. }
  39. func (ls *logServer) SendMsg(m interface{}) error {
  40. ls.logs = m
  41. return nil
  42. }
  43. func (ls *logServer) RecvMsg(m interface{}) error {
  44. return nil
  45. }
  46. func TestLogStreamWriter(t *testing.T) {
  47. ls := &logServer{}
  48. sw := newStreamWriter(ls)
  49. in := []byte{104, 101, 108, 108, 111}
  50. expected := &v1.LogsResponse{
  51. Value: in,
  52. }
  53. l, err := sw.Write(in)
  54. assert.NilError(t, err)
  55. assert.Assert(t, cmp.Len(in, l))
  56. logs, ok := (ls.logs).(*v1.LogsResponse)
  57. assert.Assert(t, ok)
  58. assert.DeepEqual(t, logs.Value, expected.Value)
  59. }