1d33874951
Go 1.13 and up enforce import paths to be versioned if a project contains a go.mod and has released v2 or up. The current v2.x branches (and releases) do not yet have a go.mod, and therefore are still allowed to be imported with a non-versioned import path (go modules add a `+incompatible` annotation in that case). However, now that this project has a `go.mod` file, incompatible import paths will not be accepted by go modules, and attempting to use code from this repository will fail. This patch uses `v3` for the import-paths (not `v2`), because changing import paths itself is a breaking change, which means that the next release should increment the "major" version to comply with SemVer (as go modules dictate). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/distribution/distribution/v3"
|
|
digest "github.com/opencontainers/go-digest"
|
|
)
|
|
|
|
func TestCacheSet(t *testing.T) {
|
|
cache := newTestStatter()
|
|
backend := newTestStatter()
|
|
st := NewCachedBlobStatter(cache, backend)
|
|
ctx := context.Background()
|
|
|
|
dgst := digest.Digest("dontvalidate")
|
|
_, err := st.Stat(ctx, dgst)
|
|
if err != distribution.ErrBlobUnknown {
|
|
t.Fatalf("Unexpected error %v, expected %v", err, distribution.ErrBlobUnknown)
|
|
}
|
|
|
|
desc := distribution.Descriptor{
|
|
Digest: dgst,
|
|
}
|
|
if err := backend.SetDescriptor(ctx, dgst, desc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
actual, err := st.Stat(ctx, dgst)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual.Digest != desc.Digest {
|
|
t.Fatalf("Unexpected descriptor %v, expected %v", actual, desc)
|
|
}
|
|
|
|
if len(cache.sets) != 1 || len(cache.sets[dgst]) == 0 {
|
|
t.Fatalf("Expected cache set")
|
|
}
|
|
if cache.sets[dgst][0].Digest != desc.Digest {
|
|
t.Fatalf("Unexpected descriptor %v, expected %v", cache.sets[dgst][0], desc)
|
|
}
|
|
|
|
desc2 := distribution.Descriptor{
|
|
Digest: digest.Digest("dontvalidate 2"),
|
|
}
|
|
cache.sets[dgst] = append(cache.sets[dgst], desc2)
|
|
|
|
actual, err = st.Stat(ctx, dgst)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual.Digest != desc2.Digest {
|
|
t.Fatalf("Unexpected descriptor %v, expected %v", actual, desc)
|
|
}
|
|
}
|
|
|
|
func TestCacheError(t *testing.T) {
|
|
cache := newErrTestStatter(errors.New("cache error"))
|
|
backend := newTestStatter()
|
|
st := NewCachedBlobStatter(cache, backend)
|
|
ctx := context.Background()
|
|
|
|
dgst := digest.Digest("dontvalidate")
|
|
_, err := st.Stat(ctx, dgst)
|
|
if err != distribution.ErrBlobUnknown {
|
|
t.Fatalf("Unexpected error %v, expected %v", err, distribution.ErrBlobUnknown)
|
|
}
|
|
|
|
desc := distribution.Descriptor{
|
|
Digest: dgst,
|
|
}
|
|
if err := backend.SetDescriptor(ctx, dgst, desc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
actual, err := st.Stat(ctx, dgst)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual.Digest != desc.Digest {
|
|
t.Fatalf("Unexpected descriptor %v, expected %v", actual, desc)
|
|
}
|
|
|
|
if len(cache.sets) > 0 {
|
|
t.Fatalf("Set should not be called after stat error")
|
|
}
|
|
}
|
|
|
|
func newTestStatter() *testStatter {
|
|
return &testStatter{
|
|
stats: []digest.Digest{},
|
|
sets: map[digest.Digest][]distribution.Descriptor{},
|
|
}
|
|
}
|
|
|
|
func newErrTestStatter(err error) *testStatter {
|
|
return &testStatter{
|
|
sets: map[digest.Digest][]distribution.Descriptor{},
|
|
err: err,
|
|
}
|
|
}
|
|
|
|
type testStatter struct {
|
|
stats []digest.Digest
|
|
sets map[digest.Digest][]distribution.Descriptor
|
|
err error
|
|
}
|
|
|
|
func (s *testStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
if s.err != nil {
|
|
return distribution.Descriptor{}, s.err
|
|
}
|
|
|
|
if set := s.sets[dgst]; len(set) > 0 {
|
|
return set[len(set)-1], nil
|
|
}
|
|
|
|
return distribution.Descriptor{}, distribution.ErrBlobUnknown
|
|
}
|
|
|
|
func (s *testStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc distribution.Descriptor) error {
|
|
s.sets[dgst] = append(s.sets[dgst], desc)
|
|
return s.err
|
|
}
|
|
|
|
func (s *testStatter) Clear(ctx context.Context, dgst digest.Digest) error {
|
|
return s.err
|
|
}
|