retain nothing rule

Change-Id: I4e7a4ecb40fe39b80e41a6d9bf8b5fb3968a41af
Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
Ziming Zhang 2019-08-05 18:08:04 +08:00
parent 0fd375c7a8
commit 498a813299
5 changed files with 107 additions and 1 deletions

View File

@ -88,6 +88,13 @@ func (r *RetentionAPI) GetMetadatas() {
"required": true
}
]
},
{
"rule_template": "nothing",
"display_text": "none",
"action": "retain",
"params": [
]
}
],
"scope_selectors": [

View File

@ -15,6 +15,7 @@
package index
import (
"github.com/goharbor/harbor/src/pkg/retention/policy/rule/nothing"
"sync"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
@ -125,6 +126,13 @@ func init() {
},
}, lastx.New)
// Register nothing
Register(&Metadata{
TemplateID: nothing.TemplateID,
Action: action.Retain,
Parameters: []*IndexedParam{},
}, nothing.New)
// Register always
Register(&Metadata{
TemplateID: always.TemplateID,

View File

@ -84,7 +84,7 @@ func (suite *IndexTestSuite) TestGet() {
// TestIndex tests Index
func (suite *IndexTestSuite) TestIndex() {
metas := Index()
require.Equal(suite.T(), 6, len(metas))
require.Equal(suite.T(), 7, len(metas))
assert.Condition(suite.T(), func() bool {
for _, m := range metas {
if m.TemplateID == "fakeEvaluator" &&

View File

@ -0,0 +1,42 @@
// 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 nothing
import (
"github.com/goharbor/harbor/src/pkg/retention/policy/action"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
"github.com/goharbor/harbor/src/pkg/retention/res"
)
const (
// TemplateID of the always retain rule
TemplateID = "nothing"
)
type evaluator struct{}
// Process for the "nothing" Evaluator simply returns the input with no error
func (e *evaluator) Process(artifacts []*res.Candidate) (processed []*res.Candidate, err error) {
return processed, err
}
func (e *evaluator) Action() string {
return action.Retain
}
// New returns an "always" Evaluator. It requires no parameters.
func New(_ rule.Parameters) rule.Evaluator {
return &evaluator{}
}

View File

@ -0,0 +1,49 @@
// 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 nothing
import (
"testing"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
"github.com/goharbor/harbor/src/pkg/retention/res"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type EvaluatorTestSuite struct {
suite.Suite
}
func (e *EvaluatorTestSuite) TestNew() {
sut := New(rule.Parameters{})
require.NotNil(e.T(), sut)
require.IsType(e.T(), &evaluator{}, sut)
}
func (e *EvaluatorTestSuite) TestProcess() {
sut := New(rule.Parameters{})
input := []*res.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}}
result, err := sut.Process(input)
require.NoError(e.T(), err)
require.Len(e.T(), result, 0)
}
func TestEvaluatorSuite(t *testing.T) {
suite.Run(t, &EvaluatorTestSuite{})
}