2018-03-22 01:10:08 +00:00
|
|
|
package contentutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-03-22 01:10:08 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadWrite(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
b := NewBuffer()
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err := content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foo0")), ocispecs.Descriptor{Size: -1})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err = content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foo1")), ocispecs.Descriptor{Size: 4})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err = content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foo2")), ocispecs.Descriptor{Size: 3})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err = content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foo3")), ocispecs.Descriptor{Size: -1, Digest: digest.FromBytes([]byte("foo4"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err = content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foo4")), ocispecs.Descriptor{Size: -1, Digest: digest.FromBytes([]byte("foo4"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
dt, err := content.ReadBlob(ctx, b, ocispecs.Descriptor{Digest: digest.FromBytes([]byte("foo1"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(dt), "foo1")
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
_, err = content.ReadBlob(ctx, b, ocispecs.Descriptor{Digest: digest.FromBytes([]byte("foo3"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.Error(t, err)
|
2020-04-19 05:17:47 +00:00
|
|
|
require.Equal(t, true, errors.Is(err, errdefs.ErrNotFound))
|
2018-03-22 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderAt(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
b := NewBuffer()
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
err := content.WriteBlob(ctx, b, "foo", bytes.NewBuffer([]byte("foobar")), ocispecs.Descriptor{Size: -1})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
rdr, err := b.ReaderAt(ctx, ocispecs.Descriptor{Digest: digest.FromBytes([]byte("foobar"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(6), rdr.Size())
|
|
|
|
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
|
|
|
|
n, err := rdr.ReadAt(buf, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "oob", string(buf[:n]))
|
|
|
|
|
|
|
|
buf = make([]byte, 7)
|
|
|
|
|
|
|
|
n, err = rdr.ReadAt(buf, 3)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, err, io.EOF)
|
|
|
|
require.Equal(t, "bar", string(buf[:n]))
|
|
|
|
}
|