2018-03-22 01:10:08 +00:00
|
|
|
package contentutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-06 08:02:01 +00:00
|
|
|
ocispec "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 TestMultiProvider(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
b0 := NewBuffer()
|
|
|
|
b1 := NewBuffer()
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
err := content.WriteBlob(ctx, b0, "foo", bytes.NewBuffer([]byte("foo0")), ocispec.Descriptor{Size: -1})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
err = content.WriteBlob(ctx, b1, "foo", bytes.NewBuffer([]byte("foo1")), ocispec.Descriptor{Size: -1})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mp := NewMultiProvider(nil)
|
|
|
|
mp.Add(digest.FromBytes([]byte("foo0")), b0)
|
|
|
|
mp.Add(digest.FromBytes([]byte("foo1")), b1)
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
dt, err := content.ReadBlob(ctx, mp, ocispec.Descriptor{Digest: digest.FromBytes([]byte("foo0"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(dt), "foo0")
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
dt, err = content.ReadBlob(ctx, mp, ocispec.Descriptor{Digest: digest.FromBytes([]byte("foo1"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(dt), "foo1")
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
_, err = content.ReadBlob(ctx, mp, ocispec.Descriptor{Digest: digest.FromBytes([]byte("foo2"))})
|
2018-03-22 01:10:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, errors.Cause(err), errdefs.ErrNotFound)
|
|
|
|
}
|