mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
8b4c445f54
Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
51 lines
959 B
Go
51 lines
959 B
Go
package local
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
"github.com/pkg/errors"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestDecode(t *testing.T) {
|
|
marshal := makeMarshaller()
|
|
|
|
buf, err := marshal(&logger.Message{Line: []byte("hello")})
|
|
assert.NilError(t, err)
|
|
|
|
for i := 0; i < len(buf); i++ {
|
|
testDecode(t, buf, i)
|
|
}
|
|
}
|
|
|
|
func testDecode(t *testing.T, buf []byte, split int) {
|
|
fw, err := os.CreateTemp("", t.Name())
|
|
assert.NilError(t, err)
|
|
defer os.Remove(fw.Name())
|
|
|
|
fr, err := os.Open(fw.Name())
|
|
assert.NilError(t, err)
|
|
|
|
d := &decoder{rdr: fr}
|
|
|
|
if split > 0 {
|
|
_, err = fw.Write(buf[0:split])
|
|
assert.NilError(t, err)
|
|
|
|
_, err = d.Decode()
|
|
assert.Assert(t, errors.Is(err, io.EOF))
|
|
|
|
_, err = fw.Write(buf[split:])
|
|
assert.NilError(t, err)
|
|
} else {
|
|
_, err = fw.Write(buf)
|
|
assert.NilError(t, err)
|
|
}
|
|
|
|
message, err := d.Decode()
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, "hello\n", string(message.Line))
|
|
}
|