mirror of
https://github.com/goharbor/harbor
synced 2025-04-12 18:23:43 +00:00

Add unique constraint to column name, scope and project_id of table harbor_label to make creating same name labels under different projects valid
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
|
//
|
|
// 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/vmware/harbor/src/common"
|
|
"github.com/vmware/harbor/src/common/models"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMethodsOfLabel(t *testing.T) {
|
|
labelName := "test"
|
|
label := &models.Label{
|
|
Name: labelName,
|
|
Level: common.LabelLevelUser,
|
|
Scope: common.LabelScopeProject,
|
|
ProjectID: 1,
|
|
}
|
|
|
|
// add
|
|
id, err := AddLabel(label)
|
|
require.Nil(t, err)
|
|
label.ID = id
|
|
|
|
// add a label which has the same name to another project
|
|
projectID, err := AddProject(models.Project{
|
|
OwnerID: 1,
|
|
Name: "project_for_label_test",
|
|
})
|
|
require.Nil(t, err)
|
|
defer GetOrmer().QueryTable(&models.Project{}).
|
|
Filter("project_id", projectID).Delete()
|
|
|
|
id2, err := AddLabel(&models.Label{
|
|
Name: labelName,
|
|
Level: common.LabelLevelUser,
|
|
Scope: common.LabelScopeProject,
|
|
ProjectID: projectID,
|
|
})
|
|
require.Nil(t, err)
|
|
defer DeleteLabel(id2)
|
|
|
|
// get
|
|
l, err := GetLabel(id)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, label.ID, l.ID)
|
|
assert.Equal(t, label.Name, l.Name)
|
|
assert.Equal(t, label.Scope, l.Scope)
|
|
assert.Equal(t, label.ProjectID, l.ProjectID)
|
|
|
|
// get total count
|
|
total, err := GetTotalOfLabels(&models.LabelQuery{
|
|
Scope: common.LabelScopeProject,
|
|
ProjectID: 1,
|
|
})
|
|
require.Nil(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
|
|
// list
|
|
labels, err := ListLabels(&models.LabelQuery{
|
|
Scope: common.LabelScopeProject,
|
|
ProjectID: 1,
|
|
Name: label.Name,
|
|
})
|
|
require.Nil(t, err)
|
|
assert.Equal(t, 1, len(labels))
|
|
|
|
// list
|
|
labels, err = ListLabels(&models.LabelQuery{
|
|
Scope: common.LabelScopeProject,
|
|
ProjectID: 1,
|
|
Name: "not_exist_label",
|
|
})
|
|
require.Nil(t, err)
|
|
assert.Equal(t, 0, len(labels))
|
|
|
|
// update
|
|
newName := "dev"
|
|
label.Name = newName
|
|
err = UpdateLabel(label)
|
|
require.Nil(t, err)
|
|
|
|
l, err = GetLabel(id)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, newName, l.Name)
|
|
|
|
// delete
|
|
err = DeleteLabel(id)
|
|
require.Nil(t, err)
|
|
|
|
l, err = GetLabel(id)
|
|
require.Nil(t, err)
|
|
assert.Nil(t, l)
|
|
}
|