harbor/src/controller/proxy/controller_test.go

229 lines
7.7 KiB
Go
Raw Normal View History

// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxy
import (
"context"
"io"
"testing"
Upgrade to distribution (registry) v3 alpha (#19784) * registryctl/api/registry/blob: fix dropped test error (#19721) Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com> * Remove robot account update quota permission (#19819) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> * Cache image list with digest key (#19801) fixes #19429 Signed-off-by: stonezdj <daojunz@vmware.com> Co-authored-by: stonezdj <daojunz@vmware.com> * Add quota permissions testcase (#19822) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> * deprecate gosec in makefile (#19828) remove the unused the part from makefile Signed-off-by: wang yan <wangyan@vmware.com> * Add verification that robot account duration is not 0 (#19829) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> * fix artifact page bug (#19807) * fix artifact page bug * update testcase * Upgrade to distribution (registry) v3 alpha This includes all the benefits of the v3 distribution, but also all breaking changes. Most notably, Image Manifest v2 Schema v1 support has been dropped, as well as the `oss` and `swift` storage drivers. Currently, this still relies on v2's github.com/docker/distribution/registry/client/auth/challenge, because that code has been removed from the public API in v3. Signed-off-by: Aaron Dewes <aaron.dewes@protonmail.com> --------- Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com> Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Signed-off-by: stonezdj <daojunz@vmware.com> Signed-off-by: wang yan <wangyan@vmware.com> Signed-off-by: Aaron Dewes <aaron.dewes@protonmail.com> Co-authored-by: Lars Lehtonen <lars.lehtonen@gmail.com> Co-authored-by: Yang Jiao <72076317+YangJiao0817@users.noreply.github.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: stonezdj(Daojun Zhang) <stonezdj@gmail.com> Co-authored-by: stonezdj <daojunz@vmware.com> Co-authored-by: Wang Yan <wangyan@vmware.com> Co-authored-by: ShengqiWang <124650040+ShengqiWang@users.noreply.github.com>
2024-01-26 22:48:06 +01:00
"github.com/distribution/distribution/v3"
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/blob"
"github.com/goharbor/harbor/src/lib"
_ "github.com/goharbor/harbor/src/lib/cache"
"github.com/goharbor/harbor/src/lib/errors"
proModels "github.com/goharbor/harbor/src/pkg/project/models"
testproxy "github.com/goharbor/harbor/src/testing/controller/proxy"
)
type localInterfaceMock struct {
mock.Mock
}
func (l *localInterfaceMock) SendPullEvent(ctx context.Context, repo, tag string) {
panic("implement me")
}
func (l *localInterfaceMock) GetManifest(ctx context.Context, art lib.ArtifactInfo) (*artifact.Artifact, error) {
args := l.Called(ctx, art)
var a *artifact.Artifact
if args.Get(0) != nil {
a = args.Get(0).(*artifact.Artifact)
}
return a, args.Error(1)
}
func (l *localInterfaceMock) SameArtifact(ctx context.Context, repo, tag, dig string) (bool, error) {
panic("implement me")
}
func (l *localInterfaceMock) BlobExist(ctx context.Context, art lib.ArtifactInfo) (bool, error) {
args := l.Called(ctx, art)
return args.Bool(0), args.Error(1)
}
func (l *localInterfaceMock) PushBlob(localRepo string, desc distribution.Descriptor, bReader io.ReadCloser) error {
panic("implement me")
}
func (l *localInterfaceMock) PushManifest(repo string, tag string, manifest distribution.Manifest) error {
args := l.Called(repo, tag, manifest)
return args.Error(0)
}
func (l *localInterfaceMock) PushManifestList(ctx context.Context, repo string, tag string, man distribution.Manifest) error {
panic("implement me")
}
func (l *localInterfaceMock) CheckDependencies(ctx context.Context, repo string, man distribution.Manifest) []distribution.Descriptor {
panic("implement me")
}
func (l *localInterfaceMock) DeleteManifest(repo, ref string) {
}
type proxyControllerTestSuite struct {
suite.Suite
local *localInterfaceMock
remote *testproxy.RemoteInterface
ctr Controller
proj *proModels.Project
}
func (p *proxyControllerTestSuite) SetupTest() {
p.local = &localInterfaceMock{}
p.remote = &testproxy.RemoteInterface{}
p.proj = &proModels.Project{RegistryID: 1}
p.ctr = &controller{
blobCtl: blob.Ctl,
artifactCtl: artifact.Ctl,
local: p.local,
}
}
func (p *proxyControllerTestSuite) TestUseLocalManifest_True() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.local.On("GetManifest", mock.Anything, mock.Anything).Return(&artifact.Artifact{}, nil)
result, _, err := p.ctr.UseLocalManifest(ctx, art, p.remote)
p.Assert().Nil(err)
p.Assert().True(result)
}
func (p *proxyControllerTestSuite) TestUseLocalManifest_False() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
desc := &distribution.Descriptor{Digest: digest.Digest(dig)}
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.remote.On("ManifestExist", mock.Anything, mock.Anything).Return(true, desc, nil)
p.local.On("GetManifest", mock.Anything, mock.Anything).Return(nil, nil)
result, _, err := p.ctr.UseLocalManifest(ctx, art, p.remote)
p.Assert().Nil(err)
p.Assert().False(result)
}
func (p *proxyControllerTestSuite) TestUseLocalManifest_429() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
desc := &distribution.Descriptor{Digest: digest.Digest(dig)}
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.remote.On("ManifestExist", mock.Anything, mock.Anything).Return(false, desc, errors.New("too many requests").WithCode(errors.RateLimitCode))
p.local.On("GetManifest", mock.Anything, mock.Anything).Return(nil, nil)
_, _, err := p.ctr.UseLocalManifest(ctx, art, p.remote)
p.Assert().NotNil(err)
errors.IsRateLimitError(err)
}
func (p *proxyControllerTestSuite) TestUseLocalManifest_429ToLocal() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
desc := &distribution.Descriptor{Digest: digest.Digest(dig)}
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.remote.On("ManifestExist", mock.Anything, mock.Anything).Return(false, desc, errors.New("too many requests").WithCode(errors.RateLimitCode))
p.local.On("GetManifest", mock.Anything, mock.Anything).Return(&artifact.Artifact{}, nil)
result, _, err := p.ctr.UseLocalManifest(ctx, art, p.remote)
p.Assert().Nil(err)
p.Assert().True(result)
}
func (p *proxyControllerTestSuite) TestUseLocalManifestWithTag_False() {
ctx := context.Background()
art := lib.ArtifactInfo{Repository: "library/hello-world", Tag: "latest"}
desc := &distribution.Descriptor{}
p.local.On("GetManifest", mock.Anything, mock.Anything).Return(&artifact.Artifact{}, nil)
p.remote.On("ManifestExist", mock.Anything, mock.Anything).Return(false, desc, nil)
result, _, err := p.ctr.UseLocalManifest(ctx, art, p.remote)
p.Assert().True(errors.IsNotFoundErr(err))
p.Assert().False(result)
}
func (p *proxyControllerTestSuite) TestUseLocalBlob_True() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.local.On("BlobExist", mock.Anything, mock.Anything).Return(true, nil)
result := p.ctr.UseLocalBlob(ctx, art)
p.Assert().True(result)
}
func (p *proxyControllerTestSuite) TestUseLocalBlob_False() {
ctx := context.Background()
dig := "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
art := lib.ArtifactInfo{Repository: "library/hello-world", Digest: dig}
p.local.On("BlobExist", mock.Anything, mock.Anything).Return(false, nil)
result := p.ctr.UseLocalBlob(ctx, art)
p.Assert().False(result)
}
func TestProxyControllerTestSuite(t *testing.T) {
suite.Run(t, &proxyControllerTestSuite{})
}
func TestProxyCacheRemoteRepo(t *testing.T) {
cases := []struct {
name string
in lib.ArtifactInfo
want string
}{
{
name: `normal test`,
in: lib.ArtifactInfo{ProjectName: "dockerhub_proxy", Repository: "dockerhub_proxy/firstfloor/hello-world"},
want: "firstfloor/hello-world",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := getRemoteRepo(tt.in)
if got != tt.want {
t.Errorf(`(%v) = %v; want "%v"`, tt.in, got, tt.want)
}
})
}
}
func TestGetRef(t *testing.T) {
cases := []struct {
name string
in lib.ArtifactInfo
want string
}{
{
name: `normal`,
in: lib.ArtifactInfo{Repository: "hello-world", Tag: "latest", Digest: "sha256:aabbcc"},
Upgrade to distribution (registry) v3 alpha (#19784) * registryctl/api/registry/blob: fix dropped test error (#19721) Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com> * Remove robot account update quota permission (#19819) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> * Cache image list with digest key (#19801) fixes #19429 Signed-off-by: stonezdj <daojunz@vmware.com> Co-authored-by: stonezdj <daojunz@vmware.com> * Add quota permissions testcase (#19822) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> * deprecate gosec in makefile (#19828) remove the unused the part from makefile Signed-off-by: wang yan <wangyan@vmware.com> * Add verification that robot account duration is not 0 (#19829) Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> * fix artifact page bug (#19807) * fix artifact page bug * update testcase * Upgrade to distribution (registry) v3 alpha This includes all the benefits of the v3 distribution, but also all breaking changes. Most notably, Image Manifest v2 Schema v1 support has been dropped, as well as the `oss` and `swift` storage drivers. Currently, this still relies on v2's github.com/docker/distribution/registry/client/auth/challenge, because that code has been removed from the public API in v3. Signed-off-by: Aaron Dewes <aaron.dewes@protonmail.com> --------- Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com> Signed-off-by: Yang Jiao <yang.jiao@broadcom.com> Signed-off-by: stonezdj <daojunz@vmware.com> Signed-off-by: wang yan <wangyan@vmware.com> Signed-off-by: Aaron Dewes <aaron.dewes@protonmail.com> Co-authored-by: Lars Lehtonen <lars.lehtonen@gmail.com> Co-authored-by: Yang Jiao <72076317+YangJiao0817@users.noreply.github.com> Co-authored-by: Yang Jiao <yang.jiao@broadcom.com> Co-authored-by: stonezdj(Daojun Zhang) <stonezdj@gmail.com> Co-authored-by: stonezdj <daojunz@vmware.com> Co-authored-by: Wang Yan <wangyan@vmware.com> Co-authored-by: ShengqiWang <124650040+ShengqiWang@users.noreply.github.com>
2024-01-26 22:48:06 +01:00
want: "sha256:aabbcc",
},
{
name: `digest_only`,
in: lib.ArtifactInfo{Repository: "hello-world", Tag: "", Digest: "sha256:aabbcc"},
want: "sha256:aabbcc",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := getReference(tt.in)
if got != tt.want {
t.Errorf(`(%v) = %v; want "%v"`, tt.in, got, tt.want)
}
})
}
}