|
| 1 | +// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package client_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/sapcc/go-bits/assert" |
| 11 | + "github.com/sapcc/go-bits/easypg" |
| 12 | + "github.com/sapcc/go-bits/must" |
| 13 | + |
| 14 | + "github.com/sapcc/keppel/internal/client" |
| 15 | + "github.com/sapcc/keppel/internal/models" |
| 16 | + "github.com/sapcc/keppel/internal/test" |
| 17 | +) |
| 18 | + |
| 19 | +func TestMain(m *testing.M) { |
| 20 | + easypg.WithTestDB(m, func() int { return m.Run() }) |
| 21 | +} |
| 22 | + |
| 23 | +func TestRepoClientBasic(t *testing.T) { |
| 24 | + ctx := t.Context() |
| 25 | + test.WithRoundTripper(func(tt *test.RoundTripper) { |
| 26 | + s := test.NewSetup(t, |
| 27 | + test.WithAccount(models.Account{Name: "test1", AuthTenantID: "test1authtenant"}), |
| 28 | + test.WithQuotas, |
| 29 | + ) |
| 30 | + const hostName = "registry.example.org" |
| 31 | + tt.Handlers[hostName] = s.Handler |
| 32 | + s.AD.ExpectedUserName = "alice" |
| 33 | + s.AD.ExpectedPassword = "swordfish" |
| 34 | + s.AD.GrantedPermissions = "pull:test1authtenant,push:test1authtenant" |
| 35 | + |
| 36 | + rc := &client.RepoClient{ |
| 37 | + Scheme: "http", |
| 38 | + Host: "registry.example.org", |
| 39 | + RepoName: "test1/foo", |
| 40 | + UserName: s.AD.ExpectedUserName, |
| 41 | + Password: s.AD.ExpectedPassword, |
| 42 | + } |
| 43 | + |
| 44 | + // test uploading an image using RepoClient |
| 45 | + img := test.GenerateImage( |
| 46 | + test.GenerateExampleLayer(1), |
| 47 | + ) |
| 48 | + |
| 49 | + digest := must.ReturnT(rc.UploadMonolithicBlob(ctx, img.Layers[0].Contents))(t) |
| 50 | + assert.Equal(t, digest, img.Layers[0].Digest) |
| 51 | + |
| 52 | + digest = must.ReturnT(rc.UploadMonolithicBlob(ctx, img.Config.Contents))(t) |
| 53 | + assert.Equal(t, digest, img.Config.Digest) |
| 54 | + |
| 55 | + digest = must.ReturnT(rc.UploadManifest(ctx, img.Manifest.Contents, img.Manifest.MediaType, "latest"))(t) |
| 56 | + assert.Equal(t, digest, img.Manifest.Digest) |
| 57 | + |
| 58 | + // test downloading the same image using RepoClient |
| 59 | + buf, mediaType, err := rc.DownloadManifest(ctx, models.ManifestReference{Tag: "latest"}, nil) |
| 60 | + must.SucceedT(t, err) |
| 61 | + assert.Equal(t, string(buf), string(img.Manifest.Contents)) |
| 62 | + assert.Equal(t, mediaType, img.Manifest.MediaType) |
| 63 | + |
| 64 | + readCloser, sizeBytes, err := rc.DownloadBlob(ctx, img.Config.Digest) |
| 65 | + buf = must.ReturnT(io.ReadAll(readCloser))(t) |
| 66 | + must.SucceedT(t, readCloser.Close()) |
| 67 | + assert.Equal(t, sizeBytes, uint64(len(img.Config.Contents))) |
| 68 | + assert.Equal(t, string(buf), string(img.Config.Contents)) |
| 69 | + |
| 70 | + readCloser, sizeBytes, err = rc.DownloadBlob(ctx, img.Layers[0].Digest) |
| 71 | + buf = must.ReturnT(io.ReadAll(readCloser))(t) |
| 72 | + must.SucceedT(t, readCloser.Close()) |
| 73 | + assert.Equal(t, sizeBytes, uint64(len(img.Layers[0].Contents))) |
| 74 | + assert.Equal(t, string(buf), string(img.Layers[0].Contents)) |
| 75 | + }) |
| 76 | +} |
0 commit comments