Skip to content

Commit

Permalink
Merge pull request #13366 from dmage/manifestservice
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored Mar 24, 2017
2 parents 6e70ad5 + 13b7440 commit 367a8d3
Show file tree
Hide file tree
Showing 12 changed files with 664 additions and 321 deletions.
11 changes: 7 additions & 4 deletions pkg/dockerregistry/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
registryauth "github.com/docker/distribution/registry/auth"

kerrors "k8s.io/kubernetes/pkg/api/errors"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/restclient"

authorizationapi "github.com/openshift/origin/pkg/authorization/api"
Expand Down Expand Up @@ -53,7 +53,7 @@ const (
// RegistryClient encapsulates getting access to the OpenShift API.
type RegistryClient interface {
// Clients return the authenticated clients to use with the server.
Clients() (client.Interface, kclientset.Interface, error)
Clients() (client.Interface, kcoreclient.CoreInterface, error)
// SafeClientConfig returns a client config without authentication info.
SafeClientConfig() restclient.Config
}
Expand All @@ -71,9 +71,12 @@ func NewRegistryClient(config *clientcmd.Config) RegistryClient {
}

// Client returns the authenticated client to use with the server.
func (r *registryClient) Clients() (client.Interface, kclientset.Interface, error) {
func (r *registryClient) Clients() (client.Interface, kcoreclient.CoreInterface, error) {
oc, kc, err := r.config.Clients()
return oc, kc, err
if err != nil {
return nil, nil, err
}
return oc, kc.Core(), err
}

// SafeClientConfig returns a client config without authentication info.
Expand Down
19 changes: 9 additions & 10 deletions pkg/dockerregistry/server/blobdescriptorservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"github.com/docker/distribution/registry/storage"

registrytest "github.com/openshift/origin/pkg/dockerregistry/testutil"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/restclient"

osclient "github.com/openshift/origin/pkg/client"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestBlobDescriptorServiceIsApplied(t *testing.T) {
client.AddReactor("get", "images", registrytest.GetFakeImageGetHandler(t, *testImage))

ctx := context.Background()
ctx = WithRegistryClient(ctx, makeFakeRegistryClient(client, fake.NewSimpleClientset()))
ctx = WithRegistryClient(ctx, makeFakeRegistryClient(client, nil))
app := handlers.NewApp(ctx, &configuration.Configuration{
Loglevel: "debug",
Auth: map[string]configuration.Parameters{
Expand Down Expand Up @@ -497,20 +496,20 @@ func (f *fakeAccessController) Authorized(ctx context.Context, access ...registr
return ctx, nil
}

func makeFakeRegistryClient(client osclient.Interface, kClient kclientset.Interface) RegistryClient {
func makeFakeRegistryClient(client osclient.Interface, kCoreClient kcoreclient.CoreInterface) RegistryClient {
return &fakeRegistryClient{
client: client,
kClient: kClient,
client: client,
kCoreClient: kCoreClient,
}
}

type fakeRegistryClient struct {
client osclient.Interface
kClient kclientset.Interface
client osclient.Interface
kCoreClient kcoreclient.CoreInterface
}

func (f *fakeRegistryClient) Clients() (osclient.Interface, kclientset.Interface, error) {
return f.client, f.kClient, nil
func (f *fakeRegistryClient) Clients() (osclient.Interface, kcoreclient.CoreInterface, error) {
return f.client, f.kCoreClient, nil
}
func (f *fakeRegistryClient) SafeClientConfig() restclient.Config {
return (&registryClient{}).SafeClientConfig()
Expand Down
98 changes: 98 additions & 0 deletions pkg/dockerregistry/server/manifestservice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package server

import (
"fmt"
"testing"

"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema2"

"github.com/openshift/origin/pkg/dockerregistry/testutil"
)

func TestManifestServiceExists(t *testing.T) {
namespace := "user"
repo := "app"
tag := "latest"

os, client := testutil.NewFakeOpenShiftWithClient()

testImage, err := testutil.RegisterRandomImage(os, namespace, repo, tag)
if err != nil {
t.Fatal(err)
}

r := newTestRepository(t, namespace, repo, testRepositoryOptions{
client: client,
})

ms := &manifestService{
ctx: context.Background(),
repo: r,
manifests: nil,
acceptschema2: r.acceptschema2,
}

ok, err := ms.Exists(context.Background(), digest.Digest(testImage.Name))
if err != nil {
t.Errorf("ms.Exists(ctx, %q): %s", testImage.Name, err)
} else if !ok {
t.Errorf("ms.Exists(ctx, %q): got false, want true", testImage.Name)
}

ok, err = ms.Exists(context.Background(), unknownBlobDigest)
if err == nil {
t.Errorf("ms.Exists(ctx, %q): got success, want error", unknownBlobDigest)
}
}

func TestManifestServicePut(t *testing.T) {
namespace := "user"
repo := "app"
repoName := fmt.Sprintf("%s/%s", namespace, repo)

_, client := testutil.NewFakeOpenShiftWithClient()

r := newTestRepository(t, namespace, repo, testRepositoryOptions{
client: client,
blobs: newTestBlobStore(map[digest.Digest][]byte{
"test:1": []byte("{}"),
}),
})

ms := &manifestService{
ctx: context.Background(),
repo: r,
manifests: newTestManifestService(repoName, nil),
acceptschema2: r.acceptschema2,
}

// TODO(dmage): eliminate global variables
quotaEnforcing = &quotaEnforcingConfig{
enforcementEnabled: false,
}

manifest := &schema2.DeserializedManifest{
Manifest: schema2.Manifest{
Config: distribution.Descriptor{
Digest: "test:1",
},
},
}

ctx := context.Background()
ctx = withAuthPerformed(ctx)
ctx = withUserClient(ctx, client)
dgst, err := ms.Put(ctx, manifest)
if err != nil {
t.Fatalf("ms.Put(ctx, manifest): %s", err)
}

ctx = context.Background()
_, err = ms.Get(ctx, dgst)
if err != nil {
t.Errorf("ms.Get(ctx, %q): %s", dgst, err)
}
}
60 changes: 12 additions & 48 deletions pkg/dockerregistry/server/pullthroughblobstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ func TestPullthroughServeBlob(t *testing.T) {
localBlobStore := newTestBlobStore(tc.localBlobs)

ctx := WithTestPassthroughToUpstream(context.Background(), false)
repo := newTestRepositoryForPullthrough(t, ctx, nil, namespace, name, client, true)
repo := newTestRepository(t, namespace, name, testRepositoryOptions{
client: client,
enablePullThrough: true,
})
ptbs := &pullthroughBlobStore{
BlobStore: localBlobStore,
repo: repo,
Expand Down Expand Up @@ -295,7 +298,10 @@ func TestPullthroughServeNotSeekableBlob(t *testing.T) {
localBlobStore := newTestBlobStore(nil)

ctx := WithTestPassthroughToUpstream(context.Background(), false)
repo := newTestRepositoryForPullthrough(t, ctx, nil, namespace, name, client, true)
repo := newTestRepository(t, namespace, name, testRepositoryOptions{
client: client,
enablePullThrough: true,
})
ptbs := &pullthroughBlobStore{
BlobStore: localBlobStore,
repo: repo,
Expand Down Expand Up @@ -671,7 +677,10 @@ func TestPullthroughServeBlobInsecure(t *testing.T) {

ctx := WithTestPassthroughToUpstream(context.Background(), false)

repo := newTestRepositoryForPullthrough(t, ctx, nil, namespace, repo1, client, true)
repo := newTestRepository(t, namespace, repo1, testRepositoryOptions{
client: client,
enablePullThrough: true,
})

ptbs := &pullthroughBlobStore{
BlobStore: localBlobStore,
Expand Down Expand Up @@ -734,51 +743,6 @@ func TestPullthroughServeBlobInsecure(t *testing.T) {
}
}

func newTestRepositoryForPullthrough(
t *testing.T,
ctx context.Context,
wrappedRepository distribution.Repository,
namespace, name string,
client *testclient.Fake,
enablePullThrough bool,
) *repository {
cachedLayers, err := newDigestToRepositoryCache(10)
if err != nil {
t.Fatal(err)
}

isGetter := &cachedImageStreamGetter{
ctx: ctx,
namespace: namespace,
name: name,
isNamespacer: client,
}

r := &repository{
Repository: wrappedRepository,
ctx: ctx,
namespace: namespace,
name: name,
pullthrough: enablePullThrough,
cachedLayers: cachedLayers,
registryOSClient: client,
imageStreamGetter: isGetter,
cachedImages: make(map[digest.Digest]*imageapi.Image),
}

if enablePullThrough {
r.remoteBlobGetter = NewBlobGetterService(
namespace,
name,
defaultBlobRepositoryCacheTTL,
isGetter.get,
client,
cachedLayers)
}

return r
}

const (
unknownBlobDigest = "sha256:bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721"
)
Expand Down
31 changes: 22 additions & 9 deletions pkg/dockerregistry/server/pullthroughmanifestservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func TestPullthroughManifests(t *testing.T) {
repoName := fmt.Sprintf("%s/%s", namespace, repo)
tag := "latest"

client := &testclient.Fake{}

installFakeAccessController(t)
setPassthroughBlobDescriptorServiceFactory()

Expand Down Expand Up @@ -92,9 +90,13 @@ func TestPullthroughManifests(t *testing.T) {
}
image.DockerImageReference = fmt.Sprintf("%s/%s/%s@%s", serverURL.Host, namespace, repo, image.Name)
image.DockerImageManifest = ""
client.AddReactor("get", "images", registrytest.GetFakeImageGetHandler(t, *image))

createTestImageStreamReactor(t, client, image, namespace, repo, tag)
os, client := registrytest.NewFakeOpenShiftWithClient()

err = registrytest.RegisterImage(os, image, namespace, repo, tag)
if err != nil {
t.Fatal(err)
}

for _, tc := range []struct {
name string
Expand Down Expand Up @@ -132,15 +134,17 @@ func TestPullthroughManifests(t *testing.T) {
} {
localManifestService := newTestManifestService(repoName, tc.localData)

ctx := WithTestPassthroughToUpstream(context.Background(), false)

repo := newTestRepositoryForPullthrough(t, ctx, nil, namespace, repo, client, true)
repo := newTestRepository(t, namespace, repo, testRepositoryOptions{
client: client,
enablePullThrough: true,
})

ptms := &pullthroughManifestService{
ManifestService: localManifestService,
repo: repo,
}

ctx := WithTestPassthroughToUpstream(context.Background(), false)
manifestResult, err := ptms.Get(ctx, tc.manifestDigest)
switch err.(type) {
case distribution.ErrManifestUnknownRevision:
Expand Down Expand Up @@ -377,7 +381,10 @@ func TestPullthroughManifestInsecure(t *testing.T) {
localManifestService := newTestManifestService(repoName, tc.localData)

ctx := WithTestPassthroughToUpstream(context.Background(), false)
repo := newTestRepositoryForPullthrough(t, ctx, nil, namespace, repo, client, true)
repo := newTestRepository(t, namespace, repo, testRepositoryOptions{
client: client,
enablePullThrough: true,
})
ctx = withRepository(ctx, repo)

ptms := &pullthroughManifestService{
Expand Down Expand Up @@ -463,7 +470,13 @@ func (t *testManifestService) Get(ctx context.Context, dgst digest.Digest, optio

func (t *testManifestService) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
t.calls["Put"]++
return "", fmt.Errorf("method not implemented")
_, payload, err := manifest.Payload()
if err != nil {
return "", err
}
dgst := digest.FromBytes(payload)
t.data[dgst] = manifest
return dgst, nil
}

func (t *testManifestService) Delete(ctx context.Context, dgst digest.Digest) error {
Expand Down
Loading

0 comments on commit 367a8d3

Please sign in to comment.