Skip to content

Commit

Permalink
Merge pull request #16777 from dmage/test-logger
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue.

Write log using t.Log in registry unit tests

cc @legionus @miminar
  • Loading branch information
openshift-merge-robot authored Oct 13, 2017
2 parents 3b758ea + 367e535 commit 8db89e0
Show file tree
Hide file tree
Showing 12 changed files with 1,068 additions and 955 deletions.
8 changes: 6 additions & 2 deletions pkg/dockerregistry/server/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/openshift/origin/pkg/dockerregistry/server/client"
"github.com/openshift/origin/pkg/dockerregistry/server/configuration"
"github.com/openshift/origin/pkg/dockerregistry/testutil"
)

var scheme = runtime.NewScheme()
Expand Down Expand Up @@ -74,6 +75,7 @@ func TestVerifyImageStreamAccess(t *testing.T) {
}
for _, test := range tests {
ctx := context.Background()
ctx = testutil.WithTestLogger(ctx, t)
server, _ := simulateOpenShiftMaster([]response{test.openshiftResponse})

cfg := clientcmd.NewConfig()
Expand Down Expand Up @@ -409,6 +411,9 @@ func TestAccessController(t *testing.T) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", test.bearerToken))
}

ctx := context.Background()
ctx = testutil.WithTestLogger(ctx, t)

server, actions := simulateOpenShiftMaster(test.openshiftResponses)
cfg := clientcmd.NewConfig()
cfg.SkipEnv = true
Expand All @@ -418,15 +423,14 @@ func TestAccessController(t *testing.T) {
TLSClientConfig: restclient.TLSClientConfig{Insecure: true},
}
app := &App{
ctx: context.Background(),
ctx: ctx,
registryClient: client.NewRegistryClient(cfg),
extraConfig: &configuration.Configuration{},
}
accessController, err := app.newAccessController(options)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
ctx = context.WithRequest(ctx, req)
authCtx, err := accessController.Authorized(ctx, test.access...)
server.Close()
Expand Down
6 changes: 4 additions & 2 deletions pkg/dockerregistry/server/blobdescriptorservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ func GetTestPassThroughToUpstream(ctx context.Context) bool {
// It relies on the fact that blobDescriptorService requires higher levels to set repository object on given
// context. If the object isn't given, its method will err out.
func TestBlobDescriptorServiceIsApplied(t *testing.T) {
ctx := context.Background()
ctx = registrytest.WithTestLogger(ctx, t)

// don't do any authorization check
installFakeAccessController(t)
m := fakeBlobDescriptorService(t)
// to make other unit tests working
defer m.changeUnsetRepository(false)

fos, imageClient := registrytest.NewFakeOpenShiftWithClient()
fos, imageClient := registrytest.NewFakeOpenShiftWithClient(ctx)
testImage := registrytest.AddRandomImage(t, fos, "user", "app", "latest")

ctx := context.Background()
os.Setenv("OPENSHIFT_DEFAULT_REGISTRY", "localhost:5000")
app := NewApp(ctx, registryclient.NewFakeRegistryClient(imageClient), &configuration.Configuration{
Loglevel: "debug",
Expand Down
45 changes: 26 additions & 19 deletions pkg/dockerregistry/server/manifestservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,50 @@ import (
)

func TestManifestServiceExists(t *testing.T) {
ctx := context.Background()
ctx = testutil.WithTestLogger(ctx, t)

namespace := "user"
repo := "app"
tag := "latest"

fos, imageClient := testutil.NewFakeOpenShiftWithClient()
fos, imageClient := testutil.NewFakeOpenShiftWithClient(ctx)
testImage := testutil.AddRandomImage(t, fos, namespace, repo, tag)

r := newTestRepository(t, namespace, repo, testRepositoryOptions{
r := newTestRepository(ctx, t, namespace, repo, testRepositoryOptions{
client: registryclient.NewFakeRegistryAPIClient(nil, imageClient),
})

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

ok, err := ms.Exists(context.Background(), digest.Digest(testImage.Name))
ok, err := ms.Exists(ctx, 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)
ok, err = ms.Exists(ctx, unknownBlobDigest)
if err == nil {
t.Errorf("ms.Exists(ctx, %q): got success, want error", unknownBlobDigest)
}
}

func TestManifestServiceGetDoesntChangeDockerImageReference(t *testing.T) {
ctx := context.Background()
ctx = testutil.WithTestLogger(ctx, t)

namespace := "user"
repo := "app"
tag := "latest"
const img1Manifest = `{"_":"some json to start migration"}`

fos, imageClient := testutil.NewFakeOpenShiftWithClient()
fos, imageClient := testutil.NewFakeOpenShiftWithClient(ctx)

testImage, err := testutil.CreateRandomImage(namespace, repo)
if err != nil {
Expand All @@ -78,20 +84,20 @@ func TestManifestServiceGetDoesntChangeDockerImageReference(t *testing.T) {
t.Fatalf("img.DockerImageReference: want %q, got %q", "1", img.DockerImageReference)
}

r := newTestRepository(t, namespace, repo, testRepositoryOptions{
r := newTestRepository(ctx, t, namespace, repo, testRepositoryOptions{
client: registryclient.NewFakeRegistryAPIClient(nil, imageClient),
})

ms := &manifestService{
ctx: context.Background(),
ctx: ctx,
repo: r,
manifests: newTestManifestService(repo, map[digest.Digest]distribution.Manifest{
digest.Digest(testImage.Name): &schema2.DeserializedManifest{},
}),
acceptschema2: r.config.acceptSchema2,
}

_, err = ms.Get(context.Background(), digest.Digest(testImage.Name))
_, err = ms.Get(ctx, digest.Digest(testImage.Name))
if err != nil {
t.Fatalf("ms.Get(ctx, %q): %s", testImage.Name, err)
}
Expand All @@ -114,25 +120,28 @@ func TestManifestServiceGetDoesntChangeDockerImageReference(t *testing.T) {
}

func TestManifestServicePut(t *testing.T) {
ctx := context.Background()
ctx = testutil.WithTestLogger(ctx, t)

namespace := "user"
repo := "app"
repoName := fmt.Sprintf("%s/%s", namespace, repo)

_, imageClient := testutil.NewFakeOpenShiftWithClient()
_, imageClient := testutil.NewFakeOpenShiftWithClient(ctx)

bs := newTestBlobStore(nil, blobContents{
"test:1": []byte("{}"),
})

tms := newTestManifestService(repoName, nil)

r := newTestRepository(t, namespace, repo, testRepositoryOptions{
r := newTestRepository(ctx, t, namespace, repo, testRepositoryOptions{
client: registryclient.NewFakeRegistryAPIClient(nil, imageClient),
blobs: bs,
})

ms := &manifestService{
ctx: context.Background(),
ctx: ctx,
repo: r,
manifests: tms,
acceptschema2: r.config.acceptSchema2,
Expand All @@ -151,28 +160,26 @@ func TestManifestServicePut(t *testing.T) {
t.Fatal(err)
}

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

// recreate repository to reset cached image stream
r = newTestRepository(t, namespace, repo, testRepositoryOptions{
r = newTestRepository(ctx, t, namespace, repo, testRepositoryOptions{
client: registryclient.NewFakeRegistryAPIClient(nil, imageClient),
blobs: bs,
})

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

ctx = context.Background()
_, err = ms.Get(ctx, dgst)
if err != nil {
t.Errorf("ms.Get(ctx, %q): %s", dgst, err)
Expand Down
Loading

0 comments on commit 8db89e0

Please sign in to comment.