Clean up tech debt codes

Clean up tech debt codes

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2021-05-19 11:29:35 +08:00
parent 4492e47e89
commit 998e392bb4
37 changed files with 523 additions and 860 deletions

View File

@ -260,59 +260,6 @@ func TestGetOrmer(t *testing.T) {
}
}
func TestAddRepository(t *testing.T) {
repoRecord := models.RepoRecord{
Name: currentProject.Name + "/" + repositoryName,
ProjectID: currentProject.ProjectID,
Description: "testing repo",
PullCount: 0,
StarCount: 0,
}
err := AddRepository(repoRecord)
if err != nil {
t.Errorf("Error occurred in AddRepository: %v", err)
}
newRepoRecord, err := GetRepositoryByName(currentProject.Name + "/" + repositoryName)
if err != nil {
t.Errorf("Error occurred in GetRepositoryByName: %v", err)
}
if newRepoRecord == nil {
t.Errorf("No repository found queried by repository name: %v", currentProject.Name+"/"+repositoryName)
}
}
var currentRepository *models.RepoRecord
func TestGetRepositoryByName(t *testing.T) {
var err error
currentRepository, err = GetRepositoryByName(currentProject.Name + "/" + repositoryName)
if err != nil {
t.Errorf("Error occurred in GetRepositoryByName: %v", err)
}
if currentRepository == nil {
t.Errorf("No repository found queried by repository name: %v", currentProject.Name+"/"+repositoryName)
}
if currentRepository.Name != currentProject.Name+"/"+repositoryName {
t.Errorf("Repository name does not match, expected: %s, actual: %s", currentProject.Name+"/"+repositoryName, currentProject.Name)
}
}
func TestDeleteRepository(t *testing.T) {
err := DeleteRepository(currentRepository.Name)
if err != nil {
t.Errorf("Error occurred in DeleteRepository: %v", err)
}
repository, err := GetRepositoryByName(currentRepository.Name)
if err != nil {
t.Errorf("Error occurred in GetRepositoryByName: %v", err)
}
if repository != nil {
t.Errorf("repository is not nil after deletion, repository: %+v", repository)
}
}
func TestIsDupRecError(t *testing.T) {
assert.True(t, IsDupRecErr(fmt.Errorf("pq: duplicate key value violates unique constraint \"properties_k_key\"")))
assert.False(t, IsDupRecErr(fmt.Errorf("other error")))

View File

@ -1,91 +0,0 @@
// 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 dao
import (
"fmt"
"github.com/goharbor/harbor/src/lib/orm"
"time"
"github.com/goharbor/harbor/src/common/models"
)
// AddProjectMetadata adds metadata for a project
func AddProjectMetadata(meta *models.ProjectMetadata) error {
now := time.Now()
sql := `insert into project_metadata
(project_id, name, value, creation_time, update_time)
values (?, ?, ?, ?, ?)`
_, err := GetOrmer().Raw(sql, meta.ProjectID, meta.Name, meta.Value,
now, now).Exec()
return err
}
// DeleteProjectMetadata deleted metadata of a project. If name is absent
// all metadatas will be deleted, otherwise only the metadatas specified
// by name will be deleted
func DeleteProjectMetadata(projectID int64, name ...string) error {
params := make([]interface{}, 1)
sql := `delete from project_metadata
where project_id = ?`
params = append(params, projectID)
if len(name) > 0 {
sql += fmt.Sprintf(` and name in ( %s )`, orm.ParamPlaceholderForIn(len(name)))
params = append(params, name)
}
_, err := GetOrmer().Raw(sql, params).Exec()
return err
}
// UpdateProjectMetadata updates metadata of a project
func UpdateProjectMetadata(meta *models.ProjectMetadata) error {
sql := `update project_metadata
set value = ?, update_time = ?
where project_id = ? and name = ?`
_, err := GetOrmer().Raw(sql, meta.Value, time.Now(), meta.ProjectID,
meta.Name).Exec()
return err
}
// GetProjectMetadata returns the metadata of a project. If name is absent
// all metadatas will be returned, otherwise only the metadatas specified
// by name will be returned
func GetProjectMetadata(projectID int64, name ...string) ([]*models.ProjectMetadata, error) {
proMetas := []*models.ProjectMetadata{}
params := make([]interface{}, 1)
sql := `select * from project_metadata
where project_id = ? `
params = append(params, projectID)
if len(name) > 0 {
sql += fmt.Sprintf(` and name in ( %s )`, orm.ParamPlaceholderForIn(len(name)))
params = append(params, name)
}
_, err := GetOrmer().Raw(sql, params).QueryRows(&proMetas)
return proMetas, err
}
// ListProjectMetadata ...
func ListProjectMetadata(name, value string) ([]*models.ProjectMetadata, error) {
sql := `select * from project_metadata
where name = ? and value = ? `
metadatas := []*models.ProjectMetadata{}
_, err := GetOrmer().Raw(sql, name, value).QueryRows(&metadatas)
return metadatas, err
}

View File

@ -1,94 +0,0 @@
// 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 dao
import (
"testing"
"github.com/goharbor/harbor/src/common/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProMetaDaoMethods(t *testing.T) {
name1 := "key1"
value1 := "value1"
name2 := "key2"
value2 := "value2"
meta1 := &models.ProjectMetadata{
ProjectID: 1,
Name: name1,
Value: value1,
}
meta2 := &models.ProjectMetadata{
ProjectID: 1,
Name: name2,
Value: value2,
}
// test add
require.Nil(t, AddProjectMetadata(meta1))
defer func() {
// clean up
_, err := GetOrmer().Raw(`delete from project_metadata
where project_id = 1 and name = ?`, name1).Exec()
require.Nil(t, err)
}()
require.Nil(t, AddProjectMetadata(meta2))
defer func() {
// clean up
_, err := GetOrmer().Raw(`delete from project_metadata
where project_id = 1 and name = ?`, name2).Exec()
require.Nil(t, err)
}()
// test get
metas, err := GetProjectMetadata(1, name1, name2)
require.Nil(t, err)
assert.Equal(t, 2, len(metas))
m := map[string]*models.ProjectMetadata{}
for _, meta := range metas {
m[meta.Name] = meta
}
assert.Equal(t, value1, m[name1].Value)
assert.Equal(t, value2, m[name2].Value)
// test list
metas, err = ListProjectMetadata(name1, value1)
require.Nil(t, err)
assert.Equal(t, 1, len(metas))
assert.Equal(t, int64(1), metas[0].ProjectID)
// test update
newValue1 := "new_value1"
meta1.Value = newValue1
require.Nil(t, UpdateProjectMetadata(meta1))
metas, err = GetProjectMetadata(1, name1, name2)
require.Nil(t, err)
assert.Equal(t, 2, len(metas))
m = map[string]*models.ProjectMetadata{}
for _, meta := range metas {
m[meta.Name] = meta
}
assert.Equal(t, newValue1, m[name1].Value)
assert.Equal(t, value2, m[name2].Value)
// test delete
require.Nil(t, DeleteProjectMetadata(1, name1))
metas, err = GetProjectMetadata(1, name1, name2)
require.Nil(t, err)
assert.Equal(t, 1, len(metas))
assert.Equal(t, value2, metas[0].Value)
}

View File

@ -1,127 +0,0 @@
// 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 dao
import (
"fmt"
"github.com/goharbor/harbor/src/common/utils"
libOrm "github.com/goharbor/harbor/src/lib/orm"
"time"
"github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/models"
)
var orderMap = map[string]string{
"name": "name asc",
"+name": "name asc",
"-name": "name desc",
"creation_time": "creation_time asc",
"+creation_time": "creation_time asc",
"-creation_time": "creation_time desc",
"update_time": "update_time asc",
"+update_time": "update_time asc",
"-update_time": "update_time desc",
"pull_count": "pull_count asc",
"+pull_count": "pull_count asc",
"-pull_count": "pull_count desc",
}
// AddRepository adds a repo to the database.
func AddRepository(repo models.RepoRecord) error {
if repo.ProjectID == 0 {
return fmt.Errorf("invalid project ID: %d", repo.ProjectID)
}
o := GetOrmer()
now := time.Now()
repo.CreationTime = now
repo.UpdateTime = now
_, err := o.Insert(&repo)
return err
}
// GetRepositoryByName ...
func GetRepositoryByName(name string) (*models.RepoRecord, error) {
o := GetOrmer()
r := models.RepoRecord{Name: name}
err := o.Read(&r, "Name")
if err == orm.ErrNoRows {
return nil, nil
}
return &r, err
}
// DeleteRepository ...
func DeleteRepository(name string) error {
o := GetOrmer()
_, err := o.QueryTable("repository").Filter("name", name).Delete()
return err
}
// RepositoryExists returns whether the repository exists according to its name.
func RepositoryExists(name string) bool {
o := GetOrmer()
return o.QueryTable("repository").Filter("name", name).Exist()
}
// GetTotalOfRepositories ...
func GetTotalOfRepositories(query ...*models.RepositoryQuery) (int64, error) {
sql, params := repositoryQueryConditions(query...)
sql = `select count(*) ` + sql
var total int64
if err := GetOrmer().Raw(sql, params).QueryRow(&total); err != nil {
return 0, err
}
return total, nil
}
func repositoryQueryConditions(query ...*models.RepositoryQuery) (string, []interface{}) {
params := []interface{}{}
sql := `from repository r `
if len(query) == 0 || query[0] == nil {
return sql, params
}
q := query[0]
if q.LabelID > 0 {
sql += `join harbor_resource_label rl on r.repository_id = rl.resource_id
and rl.resource_type = 'r' `
}
sql += `where 1=1 `
if len(q.Name) > 0 {
sql += `and r.name like ? `
params = append(params, "%"+libOrm.Escape(q.Name)+"%")
}
if len(q.ProjectIDs) > 0 {
sql += fmt.Sprintf(`and r.project_id in ( %s ) `,
utils.ParamPlaceholderForIn(len(q.ProjectIDs)))
params = append(params, q.ProjectIDs)
}
if len(q.ProjectName) > 0 {
sql += `and r.name like ? `
params = append(params, q.ProjectName+"/%")
}
if q.LabelID > 0 {
sql += `and rl.label_id = ? `
params = append(params, q.LabelID)
}
return sql, params
}

View File

@ -1,57 +0,0 @@
// 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 dao
import (
"testing"
"github.com/goharbor/harbor/src/common/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
project = "library"
name = "library/repository-test"
repository = &models.RepoRecord{
Name: name,
ProjectID: 1,
}
)
func TestGetTotalOfRepositories(t *testing.T) {
total, err := GetTotalOfRepositories()
require.Nil(t, err)
err = addRepository(repository)
require.Nil(t, err)
defer deleteRepository(name)
n, err := GetTotalOfRepositories()
require.Nil(t, err)
assert.Equal(t, total+1, n)
}
func addRepository(repository *models.RepoRecord) error {
return AddRepository(*repository)
}
func deleteRepository(name string) error {
return DeleteRepository(name)
}
func clearRepositoryData() error {
return ClearTable(models.RepoTable)
}

View File

@ -1,34 +0,0 @@
// 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 dao
import (
"github.com/goharbor/harbor/src/common/models"
)
const (
// SchemaVersion is the version of database schema
SchemaVersion = "1.6.0"
)
// GetSchemaVersion return the version of database schema
func GetSchemaVersion() (*models.SchemaVersion, error) {
version := &models.SchemaVersion{}
if err := GetOrmer().Raw("select version_num from alembic_version").
QueryRow(version); err != nil {
return nil, err
}
return version, nil
}

View File

@ -1,28 +0,0 @@
// 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 dao
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetSchemaVersion(t *testing.T) {
version, err := GetSchemaVersion()
require.Nil(t, err)
assert.Equal(t, SchemaVersion, version.Version)
}

View File

@ -23,8 +23,6 @@ func init() {
new(User),
new(Project),
new(Role),
new(RepoRecord),
new(ProjectMetadata),
new(ResourceLabel),
new(JobLog),
new(OIDCUser),

View File

@ -14,10 +14,6 @@
package models
import (
"time"
)
// keys of project metadata and severity values
const (
ProMetaPublic = "public"
@ -27,13 +23,3 @@ const (
ProMetaAutoScan = "auto_scan"
ProMetaReuseSysCVEAllowlist = "reuse_sys_cve_allowlist"
)
// ProjectMetadata holds the metadata of a project.
type ProjectMetadata struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
Name string `orm:"column(name)" json:"name"`
Value string `orm:"column(value)" json:"value"`
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`
}

View File

@ -1,20 +0,0 @@
// 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 models
// SchemaVersion is the version of database schema
type SchemaVersion struct {
Version string `json:"version" orm:"column(version_num)"`
}

View File

@ -20,7 +20,6 @@ import (
"testing"
"time"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/artifact/processor/chart"
"github.com/goharbor/harbor/src/controller/artifact/processor/cnab"
"github.com/goharbor/harbor/src/controller/artifact/processor/image"
@ -32,6 +31,7 @@ import (
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/artifact"
"github.com/goharbor/harbor/src/pkg/label/model"
repomodel "github.com/goharbor/harbor/src/pkg/repository/model"
model_tag "github.com/goharbor/harbor/src/pkg/tag/model/tag"
tagtesting "github.com/goharbor/harbor/src/testing/controller/tag"
ormtesting "github.com/goharbor/harbor/src/testing/lib/orm"
@ -60,7 +60,7 @@ func (f *fakeAbstractor) AbstractMetadata(ctx context.Context, artifact *artifac
type controllerTestSuite struct {
suite.Suite
ctl *controller
repoMgr *repotesting.FakeManager
repoMgr *repotesting.Manager
artMgr *arttesting.FakeManager
artrashMgr *artrashtesting.FakeManager
blobMgr *blob.Manager
@ -72,7 +72,7 @@ type controllerTestSuite struct {
}
func (c *controllerTestSuite) SetupTest() {
c.repoMgr = &repotesting.FakeManager{}
c.repoMgr = &repotesting.Manager{}
c.artMgr = &arttesting.FakeManager{}
c.artrashMgr = &artrashtesting.FakeManager{}
c.blobMgr = &blob.Manager{}
@ -207,7 +207,7 @@ func (c *controllerTestSuite) TestEnsureArtifact() {
c.SetupTest()
// the artifact doesn't exist
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
ProjectID: 1,
}, nil)
c.artMgr.On("GetByDigest").Return(nil, errors.NotFoundError(nil))
@ -223,7 +223,7 @@ func (c *controllerTestSuite) TestEnsure() {
digest := "sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180"
// both the artifact and the tag don't exist
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
ProjectID: 1,
}, nil)
c.artMgr.On("GetByDigest").Return(nil, errors.NotFoundError(nil))
@ -267,10 +267,10 @@ func (c *controllerTestSuite) TestList() {
},
},
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
Name: "library/hello-world",
}, nil)
c.repoMgr.On("List").Return([]*models.RepoRecord{
c.repoMgr.On("List", mock.Anything, mock.Anything).Return([]*repomodel.RepoRecord{
{RepositoryID: 1, Name: "library/hello-world"},
}, nil)
artifacts, err := c.ctl.List(nil, query, option)
@ -286,7 +286,7 @@ func (c *controllerTestSuite) TestGet() {
ID: 1,
RepositoryID: 1,
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
art, err := c.ctl.Get(nil, 1, nil)
c.Require().Nil(err)
c.Require().NotNil(art)
@ -295,7 +295,7 @@ func (c *controllerTestSuite) TestGet() {
func (c *controllerTestSuite) TestGetByDigest() {
// not found
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.artMgr.On("GetByDigest").Return(nil, errors.NotFoundError(nil))
@ -308,14 +308,14 @@ func (c *controllerTestSuite) TestGetByDigest() {
c.SetupTest()
// success
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.artMgr.On("GetByDigest").Return(&artifact.Artifact{
ID: 1,
RepositoryID: 1,
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
art, err = c.ctl.getByDigest(nil, "library/hello-world",
"sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180", nil)
c.Require().Nil(err)
@ -325,7 +325,7 @@ func (c *controllerTestSuite) TestGetByDigest() {
func (c *controllerTestSuite) TestGetByTag() {
// not found
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.tagCtl.On("List").Return(nil, nil)
@ -337,7 +337,7 @@ func (c *controllerTestSuite) TestGetByTag() {
c.SetupTest()
// success
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.tagCtl.On("List").Return([]*tag.Tag{
@ -353,7 +353,7 @@ func (c *controllerTestSuite) TestGetByTag() {
c.artMgr.On("Get").Return(&artifact.Artifact{
ID: 1,
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
art, err = c.ctl.getByTag(nil, "library/hello-world", "latest", nil)
c.Require().Nil(err)
c.Require().NotNil(art)
@ -362,14 +362,14 @@ func (c *controllerTestSuite) TestGetByTag() {
func (c *controllerTestSuite) TestGetByReference() {
// reference is digest
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.artMgr.On("GetByDigest").Return(&artifact.Artifact{
ID: 1,
RepositoryID: 1,
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
art, err := c.ctl.GetByReference(nil, "library/hello-world",
"sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180", nil)
c.Require().Nil(err)
@ -380,7 +380,7 @@ func (c *controllerTestSuite) TestGetByReference() {
c.SetupTest()
// reference is tag
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
}, nil)
c.tagCtl.On("List").Return([]*tag.Tag{
@ -396,7 +396,7 @@ func (c *controllerTestSuite) TestGetByReference() {
c.artMgr.On("Get").Return(&artifact.Artifact{
ID: 1,
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
art, err = c.ctl.GetByReference(nil, "library/hello-world", "latest", nil)
c.Require().Nil(err)
c.Require().NotNil(art)
@ -431,7 +431,7 @@ func (c *controllerTestSuite) TestDeleteDeeply() {
},
},
}, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
c.artrashMgr.On("Create").Return(0, nil)
err = c.ctl.deleteDeeply(orm.NewContext(nil, &ormtesting.FakeOrmer{}), 1, false)
c.Require().Nil(err)
@ -442,7 +442,7 @@ func (c *controllerTestSuite) TestDeleteDeeply() {
// root artifact is referenced by other artifacts
c.artMgr.On("Get").Return(&artifact.Artifact{ID: 1}, nil)
c.tagCtl.On("List").Return(nil, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
c.artMgr.On("ListReferences").Return([]*artifact.Reference{
{
ID: 1,
@ -457,7 +457,7 @@ func (c *controllerTestSuite) TestDeleteDeeply() {
// child artifact contains no tag but referenced by other artifacts
c.artMgr.On("Get").Return(&artifact.Artifact{ID: 1}, nil)
c.tagCtl.On("List").Return(nil, nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
c.artMgr.On("ListReferences").Return([]*artifact.Reference{
{
ID: 1,
@ -472,7 +472,7 @@ func (c *controllerTestSuite) TestCopy() {
ID: 1,
Digest: "sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180",
}, nil)
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
Name: "library/hello-world",
}, nil)
@ -487,7 +487,7 @@ func (c *controllerTestSuite) TestCopy() {
},
}, nil)
c.tagCtl.On("Update").Return(nil)
c.repoMgr.On("Get").Return(&models.RepoRecord{
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{
RepositoryID: 1,
Name: "library/hello-world",
}, nil)

View File

@ -17,7 +17,6 @@ package repository
import (
"context"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/lib/errors"
@ -27,6 +26,7 @@ import (
art "github.com/goharbor/harbor/src/pkg/artifact"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository"
"github.com/goharbor/harbor/src/pkg/repository/model"
)
var (
@ -43,15 +43,15 @@ type Controller interface {
// Count returns the total count of repositories according to the query
Count(ctx context.Context, query *q.Query) (total int64, err error)
// List repositories according to the query
List(ctx context.Context, query *q.Query) (repositories []*models.RepoRecord, err error)
List(ctx context.Context, query *q.Query) (repositories []*model.RepoRecord, err error)
// Get the repository specified by ID
Get(ctx context.Context, id int64) (repository *models.RepoRecord, err error)
Get(ctx context.Context, id int64) (repository *model.RepoRecord, err error)
// GetByName gets the repository specified by name
GetByName(ctx context.Context, name string) (repository *models.RepoRecord, err error)
GetByName(ctx context.Context, name string) (repository *model.RepoRecord, err error)
// Delete the repository specified by ID
Delete(ctx context.Context, id int64) (err error)
// Update the repository. Specify the properties or all properties will be updated
Update(ctx context.Context, repository *models.RepoRecord, properties ...string) (err error)
Update(ctx context.Context, repository *model.RepoRecord, properties ...string) (err error)
// AddPullCount increase one pull count for the specified repository
AddPullCount(ctx context.Context, id int64) error
}
@ -99,7 +99,7 @@ func (c *controller) Ensure(ctx context.Context, name string) (bool, int64, erro
// use orm.WithTransaction here to avoid the issue:
// https://www.postgresql.org/message-id/002e01c04da9%24a8f95c20%2425efe6c1%40lasting.ro
if err = orm.WithTransaction(func(ctx context.Context) error {
id, err = c.repoMgr.Create(ctx, &models.RepoRecord{
id, err = c.repoMgr.Create(ctx, &model.RepoRecord{
ProjectID: project.ProjectID,
Name: name,
})
@ -129,15 +129,15 @@ func (c *controller) Count(ctx context.Context, query *q.Query) (int64, error) {
return c.repoMgr.Count(ctx, query)
}
func (c *controller) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
func (c *controller) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
return c.repoMgr.List(ctx, query)
}
func (c *controller) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
func (c *controller) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
return c.repoMgr.Get(ctx, id)
}
func (c *controller) GetByName(ctx context.Context, name string) (*models.RepoRecord, error) {
func (c *controller) GetByName(ctx context.Context, name string) (*model.RepoRecord, error) {
return c.repoMgr.GetByName(ctx, name)
}
@ -179,7 +179,7 @@ func (c *controller) Delete(ctx context.Context, id int64) error {
return c.repoMgr.Delete(ctx, id)
}
func (c *controller) Update(ctx context.Context, repository *models.RepoRecord, properties ...string) error {
func (c *controller) Update(ctx context.Context, repository *model.RepoRecord, properties ...string) error {
return c.repoMgr.Update(ctx, repository, properties...)
}

View File

@ -21,6 +21,7 @@ import (
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/repository/model"
artifacttesting "github.com/goharbor/harbor/src/testing/controller/artifact"
ormtesting "github.com/goharbor/harbor/src/testing/lib/orm"
"github.com/goharbor/harbor/src/testing/mock"
@ -34,14 +35,14 @@ type controllerTestSuite struct {
suite.Suite
ctl *controller
proMgr *project.Manager
repoMgr *repository.FakeManager
repoMgr *repository.Manager
argMgr *arttesting.FakeManager
artCtl *artifacttesting.Controller
}
func (c *controllerTestSuite) SetupTest() {
c.proMgr = &project.Manager{}
c.repoMgr = &repository.FakeManager{}
c.repoMgr = &repository.Manager{}
c.argMgr = &arttesting.FakeManager{}
c.artCtl = &artifacttesting.Controller{}
c.ctl = &controller{
@ -54,7 +55,7 @@ func (c *controllerTestSuite) SetupTest() {
func (c *controllerTestSuite) TestEnsure() {
// already exists
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&model.RepoRecord{
RepositoryID: 1,
ProjectID: 1,
Name: "library/hello-world",
@ -69,11 +70,11 @@ func (c *controllerTestSuite) TestEnsure() {
c.SetupTest()
// doesn't exist
c.repoMgr.On("GetByName").Return(nil, errors.NotFoundError(nil))
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(nil, errors.NotFoundError(nil))
c.proMgr.On("Get", mock.AnythingOfType("*context.valueCtx"), "library").Return(&models.Project{
ProjectID: 1,
}, nil)
c.repoMgr.On("Create").Return(1, nil)
c.repoMgr.On("Create", mock.Anything, mock.Anything).Return(int64(1), nil)
created, id, err = c.ctl.Ensure(orm.NewContext(nil, &ormtesting.FakeOrmer{}), "library/hello-world")
c.Require().Nil(err)
c.repoMgr.AssertExpectations(c.T())
@ -83,14 +84,14 @@ func (c *controllerTestSuite) TestEnsure() {
}
func (c *controllerTestSuite) TestCount() {
c.repoMgr.On("Count").Return(1, nil)
c.repoMgr.On("Count", mock.Anything, mock.Anything).Return(int64(1), nil)
total, err := c.ctl.Count(nil, nil)
c.Require().Nil(err)
c.Equal(int64(1), total)
}
func (c *controllerTestSuite) TestList() {
c.repoMgr.On("List").Return([]*models.RepoRecord{
c.repoMgr.On("List", mock.Anything, mock.Anything).Return([]*model.RepoRecord{
{
RepositoryID: 1,
},
@ -102,7 +103,7 @@ func (c *controllerTestSuite) TestList() {
}
func (c *controllerTestSuite) TestGet() {
c.repoMgr.On("Get").Return(&models.RepoRecord{
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&model.RepoRecord{
RepositoryID: 1,
}, nil)
repository, err := c.ctl.Get(nil, 1)
@ -112,7 +113,7 @@ func (c *controllerTestSuite) TestGet() {
}
func (c *controllerTestSuite) TestGetByName() {
c.repoMgr.On("GetByName").Return(&models.RepoRecord{
c.repoMgr.On("GetByName", mock.Anything, mock.Anything).Return(&model.RepoRecord{
RepositoryID: 1,
}, nil)
repository, err := c.ctl.GetByName(nil, "library/hello-world")
@ -127,14 +128,14 @@ func (c *controllerTestSuite) TestDelete() {
c.argMgr.On("ListReferences").Return(nil, nil)
mock.OnAnything(c.artCtl, "List").Return([]*artifact.Artifact{art}, nil)
mock.OnAnything(c.artCtl, "Delete").Return(nil)
c.repoMgr.On("Delete").Return(nil)
c.repoMgr.On("Delete", mock.Anything, mock.Anything).Return(nil)
err := c.ctl.Delete(nil, 1)
c.Require().Nil(err)
}
func (c *controllerTestSuite) TestUpdate() {
c.repoMgr.On("Update").Return(nil)
err := c.ctl.Update(nil, &models.RepoRecord{
c.repoMgr.On("Update", mock.Anything, mock.Anything, mock.Anything).Return(nil)
err := c.ctl.Update(nil, &model.RepoRecord{
RepositoryID: 1,
Description: "description",
}, "Description")
@ -142,7 +143,7 @@ func (c *controllerTestSuite) TestUpdate() {
}
func (c *controllerTestSuite) TestAddPullCount() {
c.repoMgr.On("AddPullCount").Return(nil)
c.repoMgr.On("AddPullCount", mock.Anything, mock.Anything).Return(nil)
err := c.ctl.AddPullCount(nil, 1)
c.Require().Nil(err)
}

View File

@ -60,7 +60,7 @@ func TestController(t *testing.T) {
func (s *ControllerTestSuite) TestPolicy() {
projectMgr := &project.Manager{}
repositoryMgr := &repository.FakeManager{}
repositoryMgr := &repository.Manager{}
retentionScheduler := &fakeRetentionScheduler{}
retentionLauncher := &fakeLauncher{}
execMgr := &testingTask.ExecutionManager{}
@ -196,7 +196,7 @@ func (s *ControllerTestSuite) TestPolicy() {
func (s *ControllerTestSuite) TestExecution() {
projectMgr := &project.Manager{}
repositoryMgr := &repository.FakeManager{}
repositoryMgr := &repository.Manager{}
retentionScheduler := &fakeRetentionScheduler{}
retentionLauncher := &fakeLauncher{}
execMgr := &testingTask.ExecutionManager{}

View File

@ -35,14 +35,14 @@ import (
type controllerTestSuite struct {
suite.Suite
ctl *controller
repoMgr *repository.FakeManager
repoMgr *repository.Manager
artMgr *artifact.FakeManager
tagMgr *tagtesting.FakeManager
immutableMtr *immutable.FakeMatcher
}
func (c *controllerTestSuite) SetupTest() {
c.repoMgr = &repository.FakeManager{}
c.repoMgr = &repository.Manager{}
c.artMgr = &artifact.FakeManager{}
c.tagMgr = &tagtesting.FakeManager{}
c.immutableMtr = &immutable.FakeMatcher{}

View File

@ -19,6 +19,7 @@ import (
memberModels "github.com/goharbor/harbor/src/pkg/member/models"
qtypes "github.com/goharbor/harbor/src/pkg/quota/types"
"github.com/goharbor/harbor/src/pkg/repository"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/pkg/user"
)
@ -30,8 +31,8 @@ var (
testPro2 = models.Project{OwnerID: 1, Name: "test2", Metadata: map[string]string{"public": "false"}}
rs1 = qtypes.ResourceList{qtypes.ResourceStorage: 100}
rs2 = qtypes.ResourceList{qtypes.ResourceStorage: 200}
repo1 = models.RepoRecord{Name: "repo1"}
repo2 = models.RepoRecord{Name: "repo2"}
repo1 = model.RepoRecord{Name: "repo1"}
repo2 = model.RepoRecord{Name: "repo2"}
pmIDs = []int{}
art1 = artifact.Artifact{RepositoryName: repo1.Name, Type: "IMAGE", Digest: "sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180"}
art2 = artifact.Artifact{RepositoryName: repo1.Name, Type: "IMAGE", Digest: "sha256:3198b18471892718923712837192831287312893712893712897312db1a3bc73"}

View File

@ -15,8 +15,20 @@
package models
import (
"github.com/goharbor/harbor/src/common/models"
"github.com/astaxie/beego/orm"
"time"
)
// ProjectMetadata ...
type ProjectMetadata = models.ProjectMetadata
func init() {
orm.RegisterModel(new(ProjectMetadata))
}
// ProjectMetadata holds the metadata of a project.
type ProjectMetadata struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
Name string `orm:"column(name)" json:"name"`
Value string `orm:"column(value)" json:"value"`
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`
}

View File

@ -17,9 +17,9 @@ package v1
import (
"fmt"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/reg/adapter/harbor/base"
"github.com/goharbor/harbor/src/pkg/reg/model"
repomodel "github.com/goharbor/harbor/src/pkg/repository/model"
)
type client struct {
@ -27,7 +27,7 @@ type client struct {
}
func (c *client) listRepositories(project *base.Project) ([]*model.Repository, error) {
repositories := []*models.RepoRecord{}
repositories := []*repomodel.RepoRecord{}
url := fmt.Sprintf("%s/repositories?project_id=%d", c.BasePath(), project.ID)
if err := c.C.GetAndIteratePagination(url, &repositories); err != nil {
return nil, err

View File

@ -16,12 +16,13 @@ package v2
import (
"fmt"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/lib/encode/repository"
"github.com/goharbor/harbor/src/pkg/reg/adapter/harbor/base"
"github.com/goharbor/harbor/src/pkg/reg/model"
repomodel "github.com/goharbor/harbor/src/pkg/repository/model"
)
type client struct {
@ -29,7 +30,7 @@ type client struct {
}
func (c *client) listRepositories(project *base.Project) ([]*model.Repository, error) {
repositories := []*models.RepoRecord{}
repositories := []*repomodel.RepoRecord{}
url := fmt.Sprintf("%s/projects/%s/repositories", c.BasePath(), project.Name)
if err := c.C.GetAndIteratePagination(url, &repositories); err != nil {
return nil, err
@ -79,7 +80,7 @@ func (c *client) deleteTag(repo, tag string) error {
}
func (c *client) getRepositoryByBlobDigest(digest string) (string, error) {
repositories := []*models.RepoRecord{}
repositories := []*repomodel.RepoRecord{}
url := fmt.Sprintf("%s/repositories?q=blob_digest=%s&page_size=1&page_number=1", c.BasePath(), digest)
if err := c.C.Get(url, &repositories); err != nil {
return "", err

View File

@ -20,10 +20,10 @@ import (
"time"
o "github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/repository/model"
)
// DAO is the data access object interface for repository
@ -31,19 +31,19 @@ type DAO interface {
// Count returns the total count of repositories according to the query
Count(ctx context.Context, query *q.Query) (count int64, err error)
// List repositories according to the query
List(ctx context.Context, query *q.Query) (repositories []*models.RepoRecord, err error)
List(ctx context.Context, query *q.Query) (repositories []*model.RepoRecord, err error)
// Get the repository specified by ID
Get(ctx context.Context, id int64) (repository *models.RepoRecord, err error)
Get(ctx context.Context, id int64) (repository *model.RepoRecord, err error)
// Create the repository
Create(ctx context.Context, repository *models.RepoRecord) (id int64, err error)
Create(ctx context.Context, repository *model.RepoRecord) (id int64, err error)
// Delete the repository specified by ID
Delete(ctx context.Context, id int64) (err error)
// Update updates the repository. Only the properties specified by "props" will be updated if it is set
Update(ctx context.Context, repository *models.RepoRecord, props ...string) (err error)
Update(ctx context.Context, repository *model.RepoRecord, props ...string) (err error)
// AddPullCount increase one pull count for the specified repository
AddPullCount(ctx context.Context, id int64) error
// NonEmptyRepos returns the repositories without any artifact or all the artifacts are untagged.
NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error)
NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error)
}
// New returns an instance of the default DAO
@ -54,15 +54,15 @@ func New() DAO {
type dao struct{}
func (d *dao) Count(ctx context.Context, query *q.Query) (int64, error) {
qs, err := orm.QuerySetterForCount(ctx, &models.RepoRecord{}, query)
qs, err := orm.QuerySetterForCount(ctx, &model.RepoRecord{}, query)
if err != nil {
return 0, err
}
return qs.Count()
}
func (d *dao) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
repositories := []*models.RepoRecord{}
qs, err := orm.QuerySetter(ctx, &models.RepoRecord{}, query)
func (d *dao) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
repositories := []*model.RepoRecord{}
qs, err := orm.QuerySetter(ctx, &model.RepoRecord{}, query)
if err != nil {
return nil, err
}
@ -72,8 +72,8 @@ func (d *dao) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, e
return repositories, nil
}
func (d *dao) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
repository := &models.RepoRecord{
func (d *dao) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
repository := &model.RepoRecord{
RepositoryID: id,
}
ormer, err := orm.FromContext(ctx)
@ -89,7 +89,7 @@ func (d *dao) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
return repository, nil
}
func (d *dao) Create(ctx context.Context, repository *models.RepoRecord) (int64, error) {
func (d *dao) Create(ctx context.Context, repository *model.RepoRecord) (int64, error) {
ormer, err := orm.FromContext(ctx)
if err != nil {
return 0, err
@ -106,7 +106,7 @@ func (d *dao) Delete(ctx context.Context, id int64) error {
if err != nil {
return err
}
n, err := ormer.Delete(&models.RepoRecord{
n, err := ormer.Delete(&model.RepoRecord{
RepositoryID: id,
})
if err != nil {
@ -118,7 +118,7 @@ func (d *dao) Delete(ctx context.Context, id int64) error {
return nil
}
func (d *dao) Update(ctx context.Context, repository *models.RepoRecord, props ...string) error {
func (d *dao) Update(ctx context.Context, repository *model.RepoRecord, props ...string) error {
ormer, err := orm.FromContext(ctx)
if err != nil {
return err
@ -138,7 +138,7 @@ func (d *dao) AddPullCount(ctx context.Context, id int64) error {
if err != nil {
return err
}
num, err := ormer.QueryTable(new(models.RepoRecord)).Filter("RepositoryID", id).Update(
num, err := ormer.QueryTable(new(model.RepoRecord)).Filter("RepositoryID", id).Update(
o.Params{
"pull_count": o.ColValue(o.ColAdd, 1),
"update_time": time.Now(),
@ -153,8 +153,8 @@ func (d *dao) AddPullCount(ctx context.Context, id int64) error {
return nil
}
func (d *dao) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error) {
var repos []*models.RepoRecord
func (d *dao) NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error) {
var repos []*model.RepoRecord
ormer, err := orm.FromContext(ctx)
if err != nil {
return nil, err

View File

@ -19,11 +19,11 @@ import (
"fmt"
beegoorm "github.com/astaxie/beego/orm"
common_dao "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q"
af_dao "github.com/goharbor/harbor/src/pkg/artifact/dao"
"github.com/goharbor/harbor/src/pkg/repository/model"
tag_dao "github.com/goharbor/harbor/src/pkg/tag/dao"
"github.com/goharbor/harbor/src/pkg/tag/model/tag"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
@ -54,7 +54,7 @@ func (d *daoTestSuite) SetupSuite() {
}
func (d *daoTestSuite) SetupTest() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
Name: repository,
ProjectID: 1,
Description: "",
@ -126,7 +126,7 @@ func (d *daoTestSuite) TestCreate() {
// the happy pass case is covered in Setup
// conflict
repository := &models.RepoRecord{
repository := &model.RepoRecord{
Name: repository,
ProjectID: 1,
}
@ -148,7 +148,7 @@ func (d *daoTestSuite) TestDelete() {
func (d *daoTestSuite) TestUpdate() {
// pass
err := d.dao.Update(d.ctx, &models.RepoRecord{
err := d.dao.Update(d.ctx, &model.RepoRecord{
RepositoryID: d.id,
PullCount: 1,
}, "PullCount")
@ -160,7 +160,7 @@ func (d *daoTestSuite) TestUpdate() {
d.Equal(int64(1), repository.PullCount)
// not exist
err = d.dao.Update(d.ctx, &models.RepoRecord{
err = d.dao.Update(d.ctx, &model.RepoRecord{
RepositoryID: 10000,
})
d.Require().NotNil(err)
@ -170,7 +170,7 @@ func (d *daoTestSuite) TestUpdate() {
}
func (d *daoTestSuite) TestAddPullCount() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
Name: "test/pullcount",
ProjectID: 10,
Description: "test pull count",
@ -191,7 +191,7 @@ func (d *daoTestSuite) TestAddPullCount() {
}
func (d *daoTestSuite) TestNonEmptyRepos() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
Name: "TestNonEmptyRepos",
ProjectID: 10,
Description: "test pull count",

View File

@ -16,10 +16,11 @@ package repository
import (
"context"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/repository/dao"
"github.com/goharbor/harbor/src/pkg/repository/model"
)
// Mgr is the global repository manager instance
@ -30,21 +31,21 @@ type Manager interface {
// Count returns the total count of repositories according to the query
Count(ctx context.Context, query *q.Query) (total int64, err error)
// List repositories according to the query
List(ctx context.Context, query *q.Query) (repositories []*models.RepoRecord, err error)
List(ctx context.Context, query *q.Query) (repositories []*model.RepoRecord, err error)
// Get the repository specified by ID
Get(ctx context.Context, id int64) (repository *models.RepoRecord, err error)
Get(ctx context.Context, id int64) (repository *model.RepoRecord, err error)
// GetByName gets the repository specified by name
GetByName(ctx context.Context, name string) (repository *models.RepoRecord, err error)
GetByName(ctx context.Context, name string) (repository *model.RepoRecord, err error)
// Create a repository
Create(ctx context.Context, repository *models.RepoRecord) (id int64, err error)
Create(ctx context.Context, repository *model.RepoRecord) (id int64, err error)
// Delete the repository specified by ID
Delete(ctx context.Context, id int64) (err error)
// Update updates the repository. Only the properties specified by "props" will be updated if it is set
Update(ctx context.Context, repository *models.RepoRecord, props ...string) (err error)
Update(ctx context.Context, repository *model.RepoRecord, props ...string) (err error)
// AddPullCount increase one pull count for the specified repository
AddPullCount(ctx context.Context, id int64) error
// NonEmptyRepos returns the repositories without any artifact or all the artifacts are untagged.
NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error)
NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error)
}
// New returns a default implementation of Manager
@ -62,7 +63,7 @@ func (m *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
return m.dao.Count(ctx, query)
}
func (m *manager) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
func (m *manager) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
repositories, err := m.dao.List(ctx, query)
if err != nil {
return nil, err
@ -70,11 +71,11 @@ func (m *manager) List(ctx context.Context, query *q.Query) ([]*models.RepoRecor
return repositories, nil
}
func (m *manager) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
func (m *manager) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
return m.dao.Get(ctx, id)
}
func (m *manager) GetByName(ctx context.Context, name string) (repository *models.RepoRecord, err error) {
func (m *manager) GetByName(ctx context.Context, name string) (repository *model.RepoRecord, err error) {
repositories, err := m.List(ctx, &q.Query{
Keywords: map[string]interface{}{
"Name": name,
@ -90,14 +91,14 @@ func (m *manager) GetByName(ctx context.Context, name string) (repository *model
return repositories[0], nil
}
func (m *manager) Create(ctx context.Context, repository *models.RepoRecord) (int64, error) {
func (m *manager) Create(ctx context.Context, repository *model.RepoRecord) (int64, error) {
return m.dao.Create(ctx, repository)
}
func (m *manager) Delete(ctx context.Context, id int64) error {
return m.dao.Delete(ctx, id)
}
func (m *manager) Update(ctx context.Context, repository *models.RepoRecord, props ...string) error {
func (m *manager) Update(ctx context.Context, repository *model.RepoRecord, props ...string) error {
return m.dao.Update(ctx, repository, props...)
}
@ -105,6 +106,6 @@ func (m *manager) AddPullCount(ctx context.Context, id int64) error {
return m.dao.AddPullCount(ctx, id)
}
func (m *manager) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error) {
func (m *manager) NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error) {
return m.dao.NonEmptyRepos(ctx)
}

View File

@ -18,7 +18,7 @@ import (
"context"
"testing"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/testing/pkg/repository/dao"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
@ -46,12 +46,12 @@ func (m *managerTestSuite) TestCount() {
}
func (m *managerTestSuite) TestList() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
RepositoryID: 1,
ProjectID: 1,
Name: "library/hello-world",
}
m.dao.On("List", mock.Anything, mock.Anything).Return([]*models.RepoRecord{repository}, nil)
m.dao.On("List", mock.Anything, mock.Anything).Return([]*model.RepoRecord{repository}, nil)
rpers, err := m.mgr.List(context.Background(), nil)
m.Nil(err)
m.Equal(1, len(rpers))
@ -59,7 +59,7 @@ func (m *managerTestSuite) TestList() {
}
func (m *managerTestSuite) TestGet() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
RepositoryID: 1,
ProjectID: 1,
Name: "library/hello-world",
@ -73,12 +73,12 @@ func (m *managerTestSuite) TestGet() {
}
func (m *managerTestSuite) TestGetByName() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
RepositoryID: 1,
ProjectID: 1,
Name: "library/hello-world",
}
m.dao.On("List", mock.Anything, mock.Anything).Return([]*models.RepoRecord{repository}, nil)
m.dao.On("List", mock.Anything, mock.Anything).Return([]*model.RepoRecord{repository}, nil)
repo, err := m.mgr.GetByName(context.Background(), "library/hello-world")
m.Require().Nil(err)
m.dao.AssertExpectations(m.T())
@ -88,7 +88,7 @@ func (m *managerTestSuite) TestGetByName() {
func (m *managerTestSuite) TestCreate() {
m.dao.On("Create", mock.Anything, mock.Anything).Return(int64(1), nil)
_, err := m.mgr.Create(context.Background(), &models.RepoRecord{})
_, err := m.mgr.Create(context.Background(), &model.RepoRecord{})
m.Nil(err)
m.dao.AssertExpectations(m.T())
}
@ -102,7 +102,7 @@ func (m *managerTestSuite) TestDelete() {
func (m *managerTestSuite) TestUpdate() {
m.dao.On("Update", mock.Anything, mock.Anything).Return(nil)
err := m.mgr.Update(context.Background(), &models.RepoRecord{})
err := m.mgr.Update(context.Background(), &model.RepoRecord{})
m.Nil(err)
m.dao.AssertExpectations(m.T())
}
@ -115,12 +115,12 @@ func (m *managerTestSuite) TestAddPullCount() {
}
func (m *managerTestSuite) TestNonEmptyRepos() {
repository := &models.RepoRecord{
repository := &model.RepoRecord{
RepositoryID: 1,
ProjectID: 1,
Name: "library/hello-world",
}
m.dao.On("NonEmptyRepos", mock.Anything).Return([]*models.RepoRecord{repository}, nil)
m.dao.On("NonEmptyRepos", mock.Anything).Return([]*model.RepoRecord{repository}, nil)
repo, err := m.mgr.NonEmptyRepos(nil)
m.Require().Nil(err)
m.dao.AssertExpectations(m.T())

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package models
package model
import (
"context"
@ -22,13 +22,13 @@ import (
"github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/lib/q"
"github.com/lib/pq"
"github.com/theupdateframework/notary/tuf/data"
)
// RepoTable is the table name for repository
const RepoTable = "repository"
// TODO move the model into pkg/repository
func init() {
orm.RegisterModel(
new(RepoRecord),
)
}
// RepoRecord holds the record of an repository in DB, all the infors are from the registry notification event.
type RepoRecord struct {
@ -59,7 +59,7 @@ func (r *RepoRecord) FilterByBlobDigest(ctx context.Context, qs orm.QuerySeter,
// TableName is required by by beego orm to map RepoRecord to table repository
func (r *RepoRecord) TableName() string {
return RepoTable
return "repository"
}
// GetDefaultSorts specifies the default sorts
@ -75,39 +75,3 @@ func (r *RepoRecord) GetDefaultSorts() []*q.Sort {
},
}
}
// RepositoryQuery : query parameters for repository
type RepositoryQuery struct {
Name string
ProjectIDs []int64
ProjectName string
LabelID int64
Pagination
Sorting
}
// TagDetail ...
type TagDetail struct {
Digest string `json:"digest"`
Name string `json:"name"`
Size int64 `json:"size"`
Architecture string `json:"architecture"`
OS string `json:"os"`
OSVersion string `json:"os.version"`
DockerVersion string `json:"docker_version"`
Author string `json:"author"`
Created time.Time `json:"created"`
Config *TagCfg `json:"config"`
Immutable bool `json:"immutable"`
}
// TagCfg ...
type TagCfg struct {
Labels map[string]string `json:"labels"`
}
// Signature ...
type Signature struct {
Tag string `json:"tag"`
Hashes data.Hashes `json:"hashes"`
}

View File

@ -23,6 +23,7 @@ import (
"github.com/goharbor/harbor/src/common/models"
_ "github.com/goharbor/harbor/src/lib/selector/selectors/doublestar"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/pkg/retention/policy"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
"github.com/goharbor/harbor/src/pkg/retention/q"
@ -109,7 +110,7 @@ type launchTestSuite struct {
projectMgr project.Manager
execMgr *tasktesting.ExecutionManager
taskMgr *tasktesting.Manager
repositoryMgr *repository.FakeManager
repositoryMgr *repository.Manager
retentionMgr Manager
jobserviceClient job.Client
}
@ -128,7 +129,7 @@ func (l *launchTestSuite) SetupTest() {
pro1, pro2,
}, nil)
l.projectMgr = projectMgr
l.repositoryMgr = &repository.FakeManager{}
l.repositoryMgr = &repository.Manager{}
l.retentionMgr = &fakeRetentionManager{}
l.execMgr = &tasktesting.ExecutionManager{}
l.taskMgr = &tasktesting.Manager{}
@ -147,7 +148,7 @@ func (l *launchTestSuite) TestGetProjects() {
}
func (l *launchTestSuite) TestGetRepositories() {
l.repositoryMgr.On("List").Return([]*models.RepoRecord{
l.repositoryMgr.On("List", mock.Anything, mock.Anything).Return([]*model.RepoRecord{
{
RepositoryID: 1,
ProjectID: 1,
@ -200,7 +201,7 @@ func (l *launchTestSuite) TestLaunch() {
require.NotNil(l.T(), err)
// system scope
l.repositoryMgr.On("List").Return([]*models.RepoRecord{
l.repositoryMgr.On("List", mock.Anything, mock.Anything).Return([]*model.RepoRecord{
{
RepositoryID: 1,
ProjectID: 1,

View File

@ -18,6 +18,7 @@ import (
"github.com/goharbor/harbor/src/pkg/artifact"
immu_model "github.com/goharbor/harbor/src/pkg/immutable/model"
"github.com/goharbor/harbor/src/pkg/repository"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/pkg/tag"
tag_model "github.com/goharbor/harbor/src/pkg/tag/model/tag"
"github.com/opencontainers/go-digest"
@ -96,7 +97,7 @@ func (suite *HandlerSuite) addArt(ctx context.Context, pid, repositoryID int64,
}
func (suite *HandlerSuite) addRepo(ctx context.Context, pid int64, repo string) int64 {
repoRec := &models.RepoRecord{
repoRec := &model.RepoRecord{
Name: repo,
ProjectID: pid,
}

View File

@ -16,8 +16,8 @@ package registry
import (
"encoding/json"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/repository"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/testing/mock"
repotesting "github.com/goharbor/harbor/src/testing/pkg/repository"
"github.com/stretchr/testify/suite"
@ -29,7 +29,7 @@ import (
type catalogTestSuite struct {
suite.Suite
originalRepoMgr repository.Manager
repoMgr *repotesting.FakeManager
repoMgr *repotesting.Manager
}
func (c *catalogTestSuite) SetupSuite() {
@ -37,7 +37,7 @@ func (c *catalogTestSuite) SetupSuite() {
}
func (c *catalogTestSuite) SetupTest() {
c.repoMgr = &repotesting.FakeManager{}
c.repoMgr = &repotesting.Manager{}
repository.Mgr = c.repoMgr
}
@ -52,7 +52,7 @@ func (c *catalogTestSuite) TestCatalog() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog", nil)
var w *httptest.ResponseRecorder
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*models.RepoRecord{
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*model.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
@ -78,7 +78,7 @@ func (c *catalogTestSuite) TestCatalog() {
func (c *catalogTestSuite) TestCatalogPaginationN1() {
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?n=1", nil)
var w *httptest.ResponseRecorder
c.repoMgr.On("NonEmptyRepos").Return([]*models.RepoRecord{
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*model.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
@ -104,7 +104,7 @@ func (c *catalogTestSuite) TestCatalogPaginationN1() {
func (c *catalogTestSuite) TestCatalogPaginationN2() {
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?n=3", nil)
var w *httptest.ResponseRecorder
c.repoMgr.On("NonEmptyRepos").Return([]*models.RepoRecord{
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*model.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
@ -130,7 +130,7 @@ func (c *catalogTestSuite) TestCatalogPaginationN2() {
func (c *catalogTestSuite) TestCatalogPaginationN3() {
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?last=busybox&n=1", nil)
var w *httptest.ResponseRecorder
c.repoMgr.On("NonEmptyRepos").Return([]*models.RepoRecord{
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*model.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
@ -156,7 +156,7 @@ func (c *catalogTestSuite) TestCatalogPaginationN3() {
func (c *catalogTestSuite) TestCatalogEmptyRepo() {
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog", nil)
var w *httptest.ResponseRecorder
c.repoMgr.On("NonEmptyRepos").Return([]*models.RepoRecord{}, nil)
mock.OnAnything(c.repoMgr, "NonEmptyRepos").Return([]*model.RepoRecord{}, nil)
w = httptest.NewRecorder()
newRepositoryHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)

View File

@ -36,7 +36,7 @@ type manifestTestSuite struct {
originalRepoCtl repository.Controller
originalArtCtl artifact.Controller
originalProxy http.Handler
repoCtl *repotesting.FakeController
repoCtl *repotesting.Controller
artCtl *arttesting.Controller
}
@ -47,7 +47,7 @@ func (m *manifestTestSuite) SetupSuite() {
}
func (m *manifestTestSuite) SetupTest() {
m.repoCtl = &repotesting.FakeController{}
m.repoCtl = &repotesting.Controller{}
m.artCtl = &arttesting.Controller{}
repository.Ctl = m.repoCtl
artifact.Ctl = m.artCtl
@ -141,7 +141,7 @@ func (m *manifestTestSuite) TestPutManifest() {
})
req := httptest.NewRequest(http.MethodPut, "/v2/library/hello-world/manifests/latest", nil)
w := &httptest.ResponseRecorder{}
m.repoCtl.On("Ensure").Return(false, 1, nil)
mock.OnAnything(m.repoCtl, "Ensure").Return(false, int64(1), nil)
putManifest(w, req)
m.Equal(http.StatusInternalServerError, w.Code)
@ -159,7 +159,7 @@ func (m *manifestTestSuite) TestPutManifest() {
})
req = httptest.NewRequest(http.MethodPut, "/v2/library/hello-world/manifests/latest", nil)
w = &httptest.ResponseRecorder{}
m.repoCtl.On("Ensure").Return(false, 1, nil)
mock.OnAnything(m.repoCtl, "Ensure").Return(false, int64(1), nil)
mock.OnAnything(m.artCtl, "Ensure").Return(true, int64(1), nil)
putManifest(w, req)
m.Equal(http.StatusCreated, w.Code)

View File

@ -16,12 +16,13 @@ package registry
import (
"encoding/json"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/repository"
"github.com/goharbor/harbor/src/controller/tag"
"github.com/goharbor/harbor/src/pkg/repository/model"
model_tag "github.com/goharbor/harbor/src/pkg/tag/model/tag"
repotesting "github.com/goharbor/harbor/src/testing/controller/repository"
tagtesting "github.com/goharbor/harbor/src/testing/controller/tag"
"github.com/goharbor/harbor/src/testing/mock"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
@ -31,7 +32,7 @@ import (
type tagTestSuite struct {
suite.Suite
originalRepoCtl repository.Controller
repoCtl *repotesting.FakeController
repoCtl *repotesting.Controller
originalTagCtl tag.Controller
tagCtl *tagtesting.FakeController
}
@ -42,7 +43,7 @@ func (c *tagTestSuite) SetupSuite() {
}
func (c *tagTestSuite) SetupTest() {
c.repoCtl = &repotesting.FakeController{}
c.repoCtl = &repotesting.Controller{}
repository.Ctl = c.repoCtl
c.tagCtl = &tagtesting.FakeController{}
tag.Ctl = c.tagCtl
@ -60,7 +61,7 @@ func (c *tagTestSuite) TestListTag() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/library/hello-world/tags/list", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
mock.OnAnything(c.repoCtl, "GetByName").Return(&model.RepoRecord{
RepositoryID: 1,
Name: "library/hello-world",
}, nil)
@ -95,7 +96,7 @@ func (c *tagTestSuite) TestListTagPagination1() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
mock.OnAnything(c.repoCtl, "GetByName").Return(&model.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)
@ -131,7 +132,7 @@ func (c *tagTestSuite) TestListTagPagination2() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?n=3", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
mock.OnAnything(c.repoCtl, "GetByName").Return(&model.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)
@ -167,7 +168,7 @@ func (c *tagTestSuite) TestListTagPagination3() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?last=v1&n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
mock.OnAnything(c.repoCtl, "GetByName").Return(&model.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)

View File

@ -2,13 +2,13 @@ package model
import (
"github.com/go-openapi/strfmt"
common_models "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/server/v2.0/models"
)
// RepoRecord model
type RepoRecord struct {
*common_models.RepoRecord
*model.RepoRecord
}
// ToSwagger converts the repository into the swagger model
@ -25,6 +25,6 @@ func (r *RepoRecord) ToSwagger() *models.Repository {
}
// NewRepoRecord ...
func NewRepoRecord(r *common_models.RepoRecord) *RepoRecord {
func NewRepoRecord(r *model.RepoRecord) *RepoRecord {
return &RepoRecord{RepoRecord: r}
}

View File

@ -19,7 +19,6 @@ import (
"fmt"
"github.com/go-openapi/runtime/middleware"
cmodels "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/security/local"
@ -31,6 +30,7 @@ import (
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/notification"
repomodel "github.com/goharbor/harbor/src/pkg/repository/model"
"github.com/goharbor/harbor/src/server/v2.0/handler/model"
"github.com/goharbor/harbor/src/server/v2.0/models"
operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/repository"
@ -209,7 +209,7 @@ func (r *repositoryAPI) UpdateRepository(ctx context.Context, params operation.U
if err != nil {
return r.SendError(ctx, err)
}
if err := r.repoCtl.Update(ctx, &cmodels.RepoRecord{
if err := r.repoCtl.Update(ctx, &repomodel.RepoRecord{
RepositoryID: repository.RepositoryID,
Description: params.Repository.Description,
}, "Description"); err != nil {

View File

@ -27,3 +27,4 @@ package controller
//go:generate mockery --case snake --dir ../../controller/retention --name Controller --output ./retention --outpkg retention
//go:generate mockery --case snake --dir ../../controller/config --name Controller --output ./config --outpkg config
//go:generate mockery --case snake --dir ../../controller/user --name Controller --output ./user --outpkg user
//go:generate mockery --case snake --dir ../../controller/repository --name Controller --output ./repository --outpkg repository

View File

@ -1,89 +1,184 @@
// 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.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package repository
import (
"context"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/q"
"github.com/stretchr/testify/mock"
context "context"
model "github.com/goharbor/harbor/src/pkg/repository/model"
mock "github.com/stretchr/testify/mock"
q "github.com/goharbor/harbor/src/lib/q"
)
// FakeController is a fake repository controller that implement src/api/repository.Controller interface
type FakeController struct {
// Controller is an autogenerated mock type for the Controller type
type Controller struct {
mock.Mock
}
// Ensure ...
func (f *FakeController) Ensure(ctx context.Context, name string) (bool, int64, error) {
args := f.Called()
return args.Bool(0), int64(args.Int(1)), args.Error(2)
}
// AddPullCount provides a mock function with given fields: ctx, id
func (_m *Controller) AddPullCount(ctx context.Context, id int64) error {
ret := _m.Called(ctx, id)
// Count ...
func (f *FakeController) Count(ctx context.Context, query *q.Query) (int64, error) {
args := f.Called()
return int64(args.Int(0)), args.Error(1)
}
// List ...
func (f *FakeController) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
args := f.Called()
var repositories []*models.RepoRecord
if args.Get(0) != nil {
repositories = args.Get(0).([]*models.RepoRecord)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok {
r0 = rf(ctx, id)
} else {
r0 = ret.Error(0)
}
return repositories, args.Error(1)
return r0
}
// Get ...
func (f *FakeController) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
args := f.Called()
var repository *models.RepoRecord
if args.Get(0) != nil {
repository = args.Get(0).(*models.RepoRecord)
// Count provides a mock function with given fields: ctx, query
func (_m *Controller) Count(ctx context.Context, query *q.Query) (int64, error) {
ret := _m.Called(ctx, query)
var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) int64); ok {
r0 = rf(ctx, query)
} else {
r0 = ret.Get(0).(int64)
}
return repository, args.Error(1)
}
// GetByName ...
func (f *FakeController) GetByName(ctx context.Context, name string) (*models.RepoRecord, error) {
args := f.Called()
var repository *models.RepoRecord
if args.Get(0) != nil {
repository = args.Get(0).(*models.RepoRecord)
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *q.Query) error); ok {
r1 = rf(ctx, query)
} else {
r1 = ret.Error(1)
}
return repository, args.Error(1)
return r0, r1
}
// Delete ...
func (f *FakeController) Delete(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
// Delete provides a mock function with given fields: ctx, id
func (_m *Controller) Delete(ctx context.Context, id int64) error {
ret := _m.Called(ctx, id)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok {
r0 = rf(ctx, id)
} else {
r0 = ret.Error(0)
}
return r0
}
// Update ...
func (f *FakeController) Update(ctx context.Context, repository *models.RepoRecord, properties ...string) error {
args := f.Called()
return args.Error(0)
// Ensure provides a mock function with given fields: ctx, name
func (_m *Controller) Ensure(ctx context.Context, name string) (bool, int64, error) {
ret := _m.Called(ctx, name)
var r0 bool
if rf, ok := ret.Get(0).(func(context.Context, string) bool); ok {
r0 = rf(ctx, name)
} else {
r0 = ret.Get(0).(bool)
}
var r1 int64
if rf, ok := ret.Get(1).(func(context.Context, string) int64); ok {
r1 = rf(ctx, name)
} else {
r1 = ret.Get(1).(int64)
}
var r2 error
if rf, ok := ret.Get(2).(func(context.Context, string) error); ok {
r2 = rf(ctx, name)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// AddPullCount ...
func (f *FakeController) AddPullCount(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
// Get provides a mock function with given fields: ctx, id
func (_m *Controller) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
ret := _m.Called(ctx, id)
var r0 *model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, int64) *model.RepoRecord); ok {
r0 = rf(ctx, id)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, int64) error); ok {
r1 = rf(ctx, id)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetByName provides a mock function with given fields: ctx, name
func (_m *Controller) GetByName(ctx context.Context, name string) (*model.RepoRecord, error) {
ret := _m.Called(ctx, name)
var r0 *model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, string) *model.RepoRecord); ok {
r0 = rf(ctx, name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// List provides a mock function with given fields: ctx, query
func (_m *Controller) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
ret := _m.Called(ctx, query)
var r0 []*model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) []*model.RepoRecord); ok {
r0 = rf(ctx, query)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *q.Query) error); ok {
r1 = rf(ctx, query)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Update provides a mock function with given fields: ctx, _a1, properties
func (_m *Controller) Update(ctx context.Context, _a1 *model.RepoRecord, properties ...string) error {
_va := make([]interface{}, len(properties))
for _i := range properties {
_va[_i] = properties[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, _a1)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *model.RepoRecord, ...string) error); ok {
r0 = rf(ctx, _a1, properties...)
} else {
r0 = ret.Error(0)
}
return r0
}

View File

@ -34,6 +34,7 @@ package pkg
//go:generate mockery --case snake --dir ../../pkg/rbac/dao --name DAO --output ./rbac/dao --outpkg dao
//go:generate mockery --case snake --dir ../../pkg/robot --name Manager --output ./robot --outpkg robot
//go:generate mockery --case snake --dir ../../pkg/robot/dao --name DAO --output ./robot/dao --outpkg dao
//go:generate mockery --case snake --dir ../../pkg/repository --name Manager --output ./repository --outpkg repository
//go:generate mockery --case snake --dir ../../pkg/repository/dao --name DAO --output ./repository/dao --outpkg dao
//go:generate mockery --case snake --dir ../../pkg/notification/job/dao --name DAO --output ./notification/job/dao --outpkg dao
//go:generate mockery --case snake --dir ../../pkg/notification/policy/dao --name DAO --output ./notification/policy/dao --outpkg dao

View File

@ -7,7 +7,7 @@ import (
mock "github.com/stretchr/testify/mock"
models "github.com/goharbor/harbor/src/common/models"
models "github.com/goharbor/harbor/src/pkg/project/metadata/models"
)
// Manager is an autogenerated mock type for the Manager type

View File

@ -7,7 +7,7 @@ import (
mock "github.com/stretchr/testify/mock"
models "github.com/goharbor/harbor/src/common/models"
model "github.com/goharbor/harbor/src/pkg/repository/model"
q "github.com/goharbor/harbor/src/lib/q"
)
@ -53,18 +53,18 @@ func (_m *DAO) Count(ctx context.Context, query *q.Query) (int64, error) {
}
// Create provides a mock function with given fields: ctx, repository
func (_m *DAO) Create(ctx context.Context, repository *models.RepoRecord) (int64, error) {
func (_m *DAO) Create(ctx context.Context, repository *model.RepoRecord) (int64, error) {
ret := _m.Called(ctx, repository)
var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, *models.RepoRecord) int64); ok {
if rf, ok := ret.Get(0).(func(context.Context, *model.RepoRecord) int64); ok {
r0 = rf(ctx, repository)
} else {
r0 = ret.Get(0).(int64)
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *models.RepoRecord) error); ok {
if rf, ok := ret.Get(1).(func(context.Context, *model.RepoRecord) error); ok {
r1 = rf(ctx, repository)
} else {
r1 = ret.Error(1)
@ -88,15 +88,15 @@ func (_m *DAO) Delete(ctx context.Context, id int64) error {
}
// Get provides a mock function with given fields: ctx, id
func (_m *DAO) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
func (_m *DAO) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
ret := _m.Called(ctx, id)
var r0 *models.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, int64) *models.RepoRecord); ok {
var r0 *model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, int64) *model.RepoRecord); ok {
r0 = rf(ctx, id)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*models.RepoRecord)
r0 = ret.Get(0).(*model.RepoRecord)
}
}
@ -111,15 +111,15 @@ func (_m *DAO) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
}
// List provides a mock function with given fields: ctx, query
func (_m *DAO) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
func (_m *DAO) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
ret := _m.Called(ctx, query)
var r0 []*models.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) []*models.RepoRecord); ok {
var r0 []*model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) []*model.RepoRecord); ok {
r0 = rf(ctx, query)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*models.RepoRecord)
r0 = ret.Get(0).([]*model.RepoRecord)
}
}
@ -134,15 +134,15 @@ func (_m *DAO) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord,
}
// NonEmptyRepos provides a mock function with given fields: ctx
func (_m *DAO) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error) {
func (_m *DAO) NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error) {
ret := _m.Called(ctx)
var r0 []*models.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context) []*models.RepoRecord); ok {
var r0 []*model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context) []*model.RepoRecord); ok {
r0 = rf(ctx)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*models.RepoRecord)
r0 = ret.Get(0).([]*model.RepoRecord)
}
}
@ -157,7 +157,7 @@ func (_m *DAO) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error)
}
// Update provides a mock function with given fields: ctx, repository, props
func (_m *DAO) Update(ctx context.Context, repository *models.RepoRecord, props ...string) error {
func (_m *DAO) Update(ctx context.Context, repository *model.RepoRecord, props ...string) error {
_va := make([]interface{}, len(props))
for _i := range props {
_va[_i] = props[_i]
@ -168,7 +168,7 @@ func (_m *DAO) Update(ctx context.Context, repository *models.RepoRecord, props
ret := _m.Called(_ca...)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *models.RepoRecord, ...string) error); ok {
if rf, ok := ret.Get(0).(func(context.Context, *model.RepoRecord, ...string) error); ok {
r0 = rf(ctx, repository, props...)
} else {
r0 = ret.Error(0)

View File

@ -1,97 +1,200 @@
// 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.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package repository
import (
"context"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/q"
"github.com/stretchr/testify/mock"
context "context"
model "github.com/goharbor/harbor/src/pkg/repository/model"
mock "github.com/stretchr/testify/mock"
q "github.com/goharbor/harbor/src/lib/q"
)
// FakeManager is a fake repository manager that implement src/pkg/repository.Manager interface
type FakeManager struct {
// Manager is an autogenerated mock type for the Manager type
type Manager struct {
mock.Mock
}
// Count ...
func (f *FakeManager) Count(ctx context.Context, query *q.Query) (int64, error) {
args := f.Called()
return int64(args.Int(0)), args.Error(1)
}
// AddPullCount provides a mock function with given fields: ctx, id
func (_m *Manager) AddPullCount(ctx context.Context, id int64) error {
ret := _m.Called(ctx, id)
// List ...
func (f *FakeManager) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
args := f.Called()
var repositories []*models.RepoRecord
if args.Get(0) != nil {
repositories = args.Get(0).([]*models.RepoRecord)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok {
r0 = rf(ctx, id)
} else {
r0 = ret.Error(0)
}
return repositories, args.Error(1)
return r0
}
// Get ...
func (f *FakeManager) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
args := f.Called()
var repository *models.RepoRecord
if args.Get(0) != nil {
repository = args.Get(0).(*models.RepoRecord)
// Count provides a mock function with given fields: ctx, query
func (_m *Manager) Count(ctx context.Context, query *q.Query) (int64, error) {
ret := _m.Called(ctx, query)
var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) int64); ok {
r0 = rf(ctx, query)
} else {
r0 = ret.Get(0).(int64)
}
return repository, args.Error(1)
}
// GetByName ...
func (f *FakeManager) GetByName(ctx context.Context, name string) (*models.RepoRecord, error) {
args := f.Called()
var repository *models.RepoRecord
if args.Get(0) != nil {
repository = args.Get(0).(*models.RepoRecord)
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *q.Query) error); ok {
r1 = rf(ctx, query)
} else {
r1 = ret.Error(1)
}
return repository, args.Error(1)
return r0, r1
}
// Delete ...
func (f *FakeManager) Delete(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
}
// Create provides a mock function with given fields: ctx, _a1
func (_m *Manager) Create(ctx context.Context, _a1 *model.RepoRecord) (int64, error) {
ret := _m.Called(ctx, _a1)
// Create ...
func (f *FakeManager) Create(ctx context.Context, repository *models.RepoRecord) (int64, error) {
args := f.Called()
return int64(args.Int(0)), args.Error(1)
}
// Update ...
func (f *FakeManager) Update(ctx context.Context, repository *models.RepoRecord, props ...string) error {
args := f.Called()
return args.Error(0)
}
// AddPullCount ...
func (f *FakeManager) AddPullCount(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
}
// NonEmptyRepos ...
func (f *FakeManager) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error) {
args := f.Called()
var repository []*models.RepoRecord
if args.Get(0) != nil {
repository = args.Get(0).([]*models.RepoRecord)
var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, *model.RepoRecord) int64); ok {
r0 = rf(ctx, _a1)
} else {
r0 = ret.Get(0).(int64)
}
return repository, args.Error(1)
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *model.RepoRecord) error); ok {
r1 = rf(ctx, _a1)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Delete provides a mock function with given fields: ctx, id
func (_m *Manager) Delete(ctx context.Context, id int64) error {
ret := _m.Called(ctx, id)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok {
r0 = rf(ctx, id)
} else {
r0 = ret.Error(0)
}
return r0
}
// Get provides a mock function with given fields: ctx, id
func (_m *Manager) Get(ctx context.Context, id int64) (*model.RepoRecord, error) {
ret := _m.Called(ctx, id)
var r0 *model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, int64) *model.RepoRecord); ok {
r0 = rf(ctx, id)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, int64) error); ok {
r1 = rf(ctx, id)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetByName provides a mock function with given fields: ctx, name
func (_m *Manager) GetByName(ctx context.Context, name string) (*model.RepoRecord, error) {
ret := _m.Called(ctx, name)
var r0 *model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, string) *model.RepoRecord); ok {
r0 = rf(ctx, name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// List provides a mock function with given fields: ctx, query
func (_m *Manager) List(ctx context.Context, query *q.Query) ([]*model.RepoRecord, error) {
ret := _m.Called(ctx, query)
var r0 []*model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context, *q.Query) []*model.RepoRecord); ok {
r0 = rf(ctx, query)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *q.Query) error); ok {
r1 = rf(ctx, query)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// NonEmptyRepos provides a mock function with given fields: ctx
func (_m *Manager) NonEmptyRepos(ctx context.Context) ([]*model.RepoRecord, error) {
ret := _m.Called(ctx)
var r0 []*model.RepoRecord
if rf, ok := ret.Get(0).(func(context.Context) []*model.RepoRecord); ok {
r0 = rf(ctx)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*model.RepoRecord)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context) error); ok {
r1 = rf(ctx)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Update provides a mock function with given fields: ctx, _a1, props
func (_m *Manager) Update(ctx context.Context, _a1 *model.RepoRecord, props ...string) error {
_va := make([]interface{}, len(props))
for _i := range props {
_va[_i] = props[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, _a1)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *model.RepoRecord, ...string) error); ok {
r0 = rf(ctx, _a1, props...)
} else {
r0 = ret.Error(0)
}
return r0
}