c8d: Extract memoryLabelStore

Move to an internal testutils package

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-05-13 18:50:37 +02:00
parent 536b35299b
commit 153b16ad27
4 changed files with 56 additions and 94 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
@@ -244,48 +243,3 @@ func (s *delayedStore) Update(ctx context.Context, info content.Info, fieldpaths
s.delay()
return s.store.Update(ctx, info, fieldpaths...)
}
type memoryLabelStore struct {
mu sync.Mutex
labels map[digest.Digest]map[string]string
}
// Get returns all the labels for the given digest
func (s *memoryLabelStore) Get(dgst digest.Digest) (map[string]string, error) {
s.mu.Lock()
labels := s.labels[dgst]
s.mu.Unlock()
return labels, nil
}
// Set sets all the labels for a given digest
func (s *memoryLabelStore) Set(dgst digest.Digest, labels map[string]string) error {
s.mu.Lock()
if s.labels == nil {
s.labels = make(map[digest.Digest]map[string]string)
}
s.labels[dgst] = labels
s.mu.Unlock()
return nil
}
// Update replaces the given labels for a digest,
// a key with an empty value removes a label.
func (s *memoryLabelStore) Update(dgst digest.Digest, update map[string]string) (map[string]string, error) {
s.mu.Lock()
defer s.mu.Unlock()
labels, ok := s.labels[dgst]
if !ok {
labels = map[string]string{}
}
for k, v := range update {
labels[k] = v
}
if s.labels == nil {
s.labels = map[digest.Digest]map[string]string{}
}
s.labels[dgst] = labels
return labels, nil
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/containerd/containerd/v2/plugins/content/local"
"github.com/containerd/platforms"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/testutils/labelstore"
"github.com/docker/docker/internal/testutils/specialimage"
"github.com/moby/go-archive"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -27,7 +28,7 @@ func TestImageLoadMissing(t *testing.T) {
ctx := namespaces.WithNamespace(context.TODO(), "testing-"+t.Name())
store, err := local.NewLabeledStore(t.TempDir(), &memoryLabelStore{})
store, err := local.NewLabeledStore(t.TempDir(), &labelstore.InMemory{})
assert.NilError(t, err)
imgSvc := fakeImageService(t, ctx, store)

View File

@@ -3,7 +3,6 @@ package distribution
import (
"context"
"encoding/json"
"sync"
"testing"
"github.com/containerd/containerd/v2/core/content"
@@ -15,6 +14,7 @@ import (
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/manifest/ocischema"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/docker/internal/testutils/labelstore"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -42,51 +42,6 @@ func (m *mockManifestGetter) Exists(ctx context.Context, dgst digest.Digest) (bo
return ok, nil
}
type memoryLabelStore struct {
mu sync.Mutex
labels map[digest.Digest]map[string]string
}
// Get returns all the labels for the given digest
func (s *memoryLabelStore) Get(dgst digest.Digest) (map[string]string, error) {
s.mu.Lock()
labels := s.labels[dgst]
s.mu.Unlock()
return labels, nil
}
// Set sets all the labels for a given digest
func (s *memoryLabelStore) Set(dgst digest.Digest, labels map[string]string) error {
s.mu.Lock()
if s.labels == nil {
s.labels = make(map[digest.Digest]map[string]string)
}
s.labels[dgst] = labels
s.mu.Unlock()
return nil
}
// Update replaces the given labels for a digest,
// a key with an empty value removes a label.
func (s *memoryLabelStore) Update(dgst digest.Digest, update map[string]string) (map[string]string, error) {
s.mu.Lock()
defer s.mu.Unlock()
labels, ok := s.labels[dgst]
if !ok {
labels = map[string]string{}
}
for k, v := range update {
labels[k] = v
}
if s.labels == nil {
s.labels = map[digest.Digest]map[string]string{}
}
s.labels[dgst] = labels
return labels, nil
}
type testingContentStoreWrapper struct {
ContentStore
errorOnWriter error
@@ -134,7 +89,7 @@ func TestManifestStore(t *testing.T) {
t.Helper()
root := t.TempDir()
cs, err := local.NewLabeledStore(root, &memoryLabelStore{})
cs, err := local.NewLabeledStore(root, &labelstore.InMemory{})
assert.NilError(t, err)
mg := &mockManifestGetter{manifests: make(map[digest.Digest]distribution.Manifest)}

View File

@@ -0,0 +1,52 @@
package labelstore
import (
"sync"
"github.com/opencontainers/go-digest"
)
type InMemory struct {
mu sync.Mutex
labels map[digest.Digest]map[string]string
}
// Get returns all the labels for the given digest
func (s *InMemory) Get(dgst digest.Digest) (map[string]string, error) {
s.mu.Lock()
labels := s.labels[dgst]
s.mu.Unlock()
return labels, nil
}
// Set sets all the labels for a given digest
func (s *InMemory) Set(dgst digest.Digest, labels map[string]string) error {
s.mu.Lock()
if s.labels == nil {
s.labels = make(map[digest.Digest]map[string]string)
}
s.labels[dgst] = labels
s.mu.Unlock()
return nil
}
// Update replaces the given labels for a digest,
// a key with an empty value removes a label.
func (s *InMemory) Update(dgst digest.Digest, update map[string]string) (map[string]string, error) {
s.mu.Lock()
defer s.mu.Unlock()
labels, ok := s.labels[dgst]
if !ok {
labels = map[string]string{}
}
for k, v := range update {
labels[k] = v
}
if s.labels == nil {
s.labels = map[digest.Digest]map[string]string{}
}
s.labels[dgst] = labels
return labels, nil
}