2019-07-25 07:40:26 +02:00
|
|
|
// Copyright 2018 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 api
|
|
|
|
|
|
|
|
import (
|
2020-04-12 18:14:12 +02:00
|
|
|
"context"
|
2019-07-25 07:40:26 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
o "github.com/astaxie/beego/orm"
|
|
|
|
"github.com/goharbor/harbor/src/controller/quota"
|
|
|
|
"github.com/goharbor/harbor/src/lib/orm"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/quota/driver"
|
2019-07-25 07:40:26 +02:00
|
|
|
"github.com/goharbor/harbor/src/pkg/types"
|
|
|
|
"github.com/goharbor/harbor/src/testing/apitests/apilib"
|
2020-04-12 18:14:12 +02:00
|
|
|
"github.com/goharbor/harbor/src/testing/mock"
|
|
|
|
drivertesting "github.com/goharbor/harbor/src/testing/pkg/quota/driver"
|
|
|
|
"github.com/google/uuid"
|
2019-07-25 07:40:26 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-12 18:14:12 +02:00
|
|
|
reference = uuid.New().String()
|
2020-04-05 20:40:25 +02:00
|
|
|
hardLimits = types.ResourceList{types.ResourceStorage: -1}
|
2019-07-25 07:40:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-04-12 18:14:12 +02:00
|
|
|
mockDriver := &drivertesting.Driver{}
|
2019-07-25 07:40:26 +02:00
|
|
|
|
|
|
|
mockHardLimitsFn := func() types.ResourceList {
|
|
|
|
return hardLimits
|
|
|
|
}
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
mockLoadFn := func(ctx context.Context, key string) driver.RefObject {
|
2019-07-25 07:40:26 +02:00
|
|
|
return driver.RefObject{"id": key}
|
|
|
|
}
|
|
|
|
|
|
|
|
mockValidateFn := func(hardLimits types.ResourceList) error {
|
|
|
|
if len(hardLimits) == 0 {
|
|
|
|
return fmt.Errorf("no resources found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockDriver.On("HardLimits").Return(mockHardLimitsFn)
|
2020-04-12 18:14:12 +02:00
|
|
|
mock.OnAnything(mockDriver, "Load").Return(mockLoadFn, nil)
|
|
|
|
mock.OnAnything(mockDriver, "Validate").Return(mockValidateFn)
|
2019-07-25 07:40:26 +02:00
|
|
|
|
|
|
|
driver.Register(reference, mockDriver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaAPIList(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
apiTest := newHarborAPI()
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
ctx := orm.NewContext(context.TODO(), o.NewOrm())
|
|
|
|
var quotaIDs []int64
|
|
|
|
defer func() {
|
|
|
|
for _, quotaID := range quotaIDs {
|
|
|
|
quota.Ctl.Delete(ctx, quotaID)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-25 07:40:26 +02:00
|
|
|
count := 10
|
|
|
|
for i := 0; i < count; i++ {
|
2020-04-12 18:14:12 +02:00
|
|
|
quotaID, err := quota.Ctl.Create(ctx, reference, uuid.New().String(), hardLimits)
|
2019-07-25 07:40:26 +02:00
|
|
|
assert.Nil(err)
|
2020-04-12 18:14:12 +02:00
|
|
|
quotaIDs = append(quotaIDs, quotaID)
|
2019-07-25 07:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
code, quotas, err := apiTest.QuotasGet(&apilib.QuotaQuery{Reference: reference}, *admin)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
|
|
|
assert.Len(quotas, count, fmt.Sprintf("quotas len should be %d", count))
|
|
|
|
|
|
|
|
code, quotas, err = apiTest.QuotasGet(&apilib.QuotaQuery{Reference: reference, PageSize: 1}, *admin)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
|
|
|
assert.Len(quotas, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaAPIGet(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
apiTest := newHarborAPI()
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
ctx := orm.NewContext(context.TODO(), o.NewOrm())
|
|
|
|
quotaID, err := quota.Ctl.Create(ctx, reference, uuid.New().String(), hardLimits)
|
2019-07-25 07:40:26 +02:00
|
|
|
assert.Nil(err)
|
2020-04-12 18:14:12 +02:00
|
|
|
defer quota.Ctl.Delete(ctx, quotaID)
|
2019-07-25 07:40:26 +02:00
|
|
|
|
|
|
|
code, quota, err := apiTest.QuotasGetByID(*admin, fmt.Sprintf("%d", quotaID))
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
2020-04-05 20:40:25 +02:00
|
|
|
assert.Equal(map[string]int64{"storage": -1}, quota.Hard)
|
2019-07-25 07:40:26 +02:00
|
|
|
|
|
|
|
code, _, err = apiTest.QuotasGetByID(*admin, "100")
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(404), code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaPut(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
apiTest := newHarborAPI()
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
ctx := orm.NewContext(context.TODO(), o.NewOrm())
|
|
|
|
quotaID, err := quota.Ctl.Create(ctx, reference, uuid.New().String(), hardLimits)
|
2019-07-25 07:40:26 +02:00
|
|
|
assert.Nil(err)
|
2020-04-12 18:14:12 +02:00
|
|
|
defer quota.Ctl.Delete(ctx, quotaID)
|
2019-07-25 07:40:26 +02:00
|
|
|
|
|
|
|
code, quota, err := apiTest.QuotasGetByID(*admin, fmt.Sprintf("%d", quotaID))
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
2020-04-05 20:40:25 +02:00
|
|
|
assert.Equal(map[string]int64{"storage": -1}, quota.Hard)
|
2019-07-25 07:40:26 +02:00
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
code, err = apiTest.QuotasPut(*admin, fmt.Sprintf("%d", quotaID), QuotaUpdateRequest{})
|
2019-07-25 07:40:26 +02:00
|
|
|
assert.Nil(err, err)
|
|
|
|
assert.Equal(int(400), code)
|
|
|
|
|
2020-04-12 18:14:12 +02:00
|
|
|
code, err = apiTest.QuotasPut(*admin, fmt.Sprintf("%d", quotaID), QuotaUpdateRequest{Hard: types.ResourceList{types.ResourceStorage: 100}})
|
2019-07-25 07:40:26 +02:00
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
|
|
|
|
|
|
|
code, quota, err = apiTest.QuotasGetByID(*admin, fmt.Sprintf("%d", quotaID))
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(int(200), code)
|
2020-04-05 20:40:25 +02:00
|
|
|
assert.Equal(map[string]int64{"storage": 100}, quota.Hard)
|
2019-07-25 07:40:26 +02:00
|
|
|
}
|