1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/logger/local/read_test.go
Kazuyoshi Kato 8b4c445f54 test: use os.CreateTemp instead of ioutil.TempFile
Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
2021-12-23 09:09:47 -08:00

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))
}