From 498a81329915a01e71aa0e8fb042ef71934ba7b4 Mon Sep 17 00:00:00 2001 From: Ziming Zhang Date: Mon, 5 Aug 2019 18:08:04 +0800 Subject: [PATCH] retain nothing rule Change-Id: I4e7a4ecb40fe39b80e41a6d9bf8b5fb3968a41af Signed-off-by: Ziming Zhang --- src/core/api/retention.go | 7 +++ src/pkg/retention/policy/rule/index/index.go | 8 +++ .../retention/policy/rule/index/index_test.go | 2 +- .../policy/rule/nothing/evaluator.go | 42 ++++++++++++++++ .../policy/rule/nothing/evaluator_test.go | 49 +++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/pkg/retention/policy/rule/nothing/evaluator.go create mode 100644 src/pkg/retention/policy/rule/nothing/evaluator_test.go diff --git a/src/core/api/retention.go b/src/core/api/retention.go index 1caee7c1d..ec5c39b5d 100644 --- a/src/core/api/retention.go +++ b/src/core/api/retention.go @@ -88,6 +88,13 @@ func (r *RetentionAPI) GetMetadatas() { "required": true } ] + }, + { + "rule_template": "nothing", + "display_text": "none", + "action": "retain", + "params": [ + ] } ], "scope_selectors": [ diff --git a/src/pkg/retention/policy/rule/index/index.go b/src/pkg/retention/policy/rule/index/index.go index dd2c9d9ac..f7971dc39 100644 --- a/src/pkg/retention/policy/rule/index/index.go +++ b/src/pkg/retention/policy/rule/index/index.go @@ -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, diff --git a/src/pkg/retention/policy/rule/index/index_test.go b/src/pkg/retention/policy/rule/index/index_test.go index 44eb7db53..230c9c9fa 100644 --- a/src/pkg/retention/policy/rule/index/index_test.go +++ b/src/pkg/retention/policy/rule/index/index_test.go @@ -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" && diff --git a/src/pkg/retention/policy/rule/nothing/evaluator.go b/src/pkg/retention/policy/rule/nothing/evaluator.go new file mode 100644 index 000000000..8bc4b9063 --- /dev/null +++ b/src/pkg/retention/policy/rule/nothing/evaluator.go @@ -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{} +} diff --git a/src/pkg/retention/policy/rule/nothing/evaluator_test.go b/src/pkg/retention/policy/rule/nothing/evaluator_test.go new file mode 100644 index 000000000..1432db651 --- /dev/null +++ b/src/pkg/retention/policy/rule/nothing/evaluator_test.go @@ -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{}) +}