github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/local/read_test.go (about)

     1  package local
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/daemon/logger"
     9  	"github.com/pkg/errors"
    10  	"gotest.tools/v3/assert"
    11  )
    12  
    13  func TestDecode(t *testing.T) {
    14  	buf := make([]byte, 0)
    15  
    16  	err := marshal(&logger.Message{Line: []byte("hello")}, &buf)
    17  	assert.NilError(t, err)
    18  
    19  	for i := 0; i < len(buf); i++ {
    20  		testDecode(t, buf, i)
    21  	}
    22  }
    23  
    24  func testDecode(t *testing.T, buf []byte, split int) {
    25  	fw, err := os.CreateTemp("", t.Name())
    26  	assert.NilError(t, err)
    27  	defer os.Remove(fw.Name())
    28  
    29  	fr, err := os.Open(fw.Name())
    30  	assert.NilError(t, err)
    31  
    32  	d := &decoder{rdr: fr}
    33  
    34  	if split > 0 {
    35  		_, err = fw.Write(buf[0:split])
    36  		assert.NilError(t, err)
    37  
    38  		_, err = d.Decode()
    39  		assert.Assert(t, errors.Is(err, io.EOF))
    40  
    41  		_, err = fw.Write(buf[split:])
    42  		assert.NilError(t, err)
    43  	} else {
    44  		_, err = fw.Write(buf)
    45  		assert.NilError(t, err)
    46  	}
    47  
    48  	message, err := d.Decode()
    49  	assert.NilError(t, err)
    50  	assert.Equal(t, "hello\n", string(message.Line))
    51  }