diff --git a/src/pkg/retention/job.go b/src/pkg/retention/job.go index 710f5a19d..76820af12 100644 --- a/src/pkg/retention/job.go +++ b/src/pkg/retention/job.go @@ -125,13 +125,13 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re op := func(art *res.Candidate) string { if e, exists := hash[art.Hash()]; exists { if e != nil { - return "Err" + return "ERR" } - return "X" + return "DEL" } - return "√" + return "RETAIN" } var buf bytes.Buffer @@ -158,7 +158,7 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re table.AppendBulk(data) table.Render() - logger.Infof("%s", buf.String()) + logger.Infof("\n%s", buf.String()) // log all the concrete errors if have for _, r := range results { diff --git a/src/pkg/retention/policy/alg/or/processor.go b/src/pkg/retention/policy/alg/or/processor.go index 302933fb9..623e4f050 100644 --- a/src/pkg/retention/policy/alg/or/processor.go +++ b/src/pkg/retention/policy/alg/or/processor.go @@ -154,12 +154,10 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { return } - if len(processed) > 0 { - // Pass to the outside - resChan <- &chanItem{ - action: evaluator.Action(), - processed: processed, - } + // Pass to the outside + resChan <- &chanItem{ + action: evaluator.Action(), + processed: processed, } }(evaluator, selectors) } diff --git a/src/pkg/retention/policy/alg/or/processor_test.go b/src/pkg/retention/policy/alg/or/processor_test.go index 685b8de90..e49516f37 100644 --- a/src/pkg/retention/policy/alg/or/processor_test.go +++ b/src/pkg/retention/policy/alg/or/processor_test.go @@ -19,18 +19,21 @@ import ( "testing" "time" - "github.com/goharbor/harbor/src/pkg/retention/dep" - - "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" + "github.com/goharbor/harbor/src/pkg/retention/policy/rule/always" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/alg" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/lastx" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" "github.com/goharbor/harbor/src/pkg/retention/res/selectors/label" + + "github.com/goharbor/harbor/src/pkg/retention/dep" + + "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" + + "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -40,7 +43,6 @@ import ( type ProcessorTestSuite struct { suite.Suite - p alg.Processor all []*res.Candidate oldClient dep.Client @@ -72,10 +74,21 @@ func (suite *ProcessorTestSuite) SetupSuite() { }, } - params := make([]*alg.Parameter, 0) + suite.oldClient = dep.DefaultClient + dep.DefaultClient = &fakeRetentionClient{} +} + +// TearDownSuite ... +func (suite *ProcessorTestSuite) TearDownSuite() { + dep.DefaultClient = suite.oldClient +} + +// TestProcess tests process method +func (suite *ProcessorTestSuite) TestProcess() { perf := action.NewRetainAction(suite.all, false) + params := make([]*alg.Parameter, 0) lastxParams := make(map[string]rule.Parameter) lastxParams[lastx.ParameterX] = 10 params = append(params, &alg.Parameter{ @@ -97,20 +110,9 @@ func (suite *ProcessorTestSuite) SetupSuite() { Performer: perf, }) - suite.p = New(params) + p := New(params) - suite.oldClient = dep.DefaultClient - dep.DefaultClient = &fakeRetentionClient{} -} - -// TearDownSuite ... -func (suite *ProcessorTestSuite) TearDownSuite() { - dep.DefaultClient = suite.oldClient -} - -// TestProcess tests process method -func (suite *ProcessorTestSuite) TestProcess() { - results, err := suite.p.Process(suite.all) + results, err := p.Process(suite.all) require.NoError(suite.T(), err) assert.Equal(suite.T(), 1, len(results)) assert.Condition(suite.T(), func() bool { @@ -124,6 +126,43 @@ func (suite *ProcessorTestSuite) TestProcess() { }, "no errors in the returned result list") } +// TestProcess2 ... +func (suite *ProcessorTestSuite) TestProcess2() { + perf := action.NewRetainAction(suite.all, false) + + params := make([]*alg.Parameter, 0) + alwaysParams := make(map[string]rule.Parameter) + params = append(params, &alg.Parameter{ + Evaluator: always.New(alwaysParams), + Selectors: []res.Selector{ + doublestar.New(doublestar.Matches, "latest"), + label.New(label.With, ""), + }, + Performer: perf, + }) + + p := New(params) + + results, err := p.Process(suite.all) + require.NoError(suite.T(), err) + assert.Equal(suite.T(), 1, len(results)) + assert.Condition(suite.T(), func() bool { + found := false + for _, r := range results { + if r.Error != nil { + return false + } + + if r.Target.Tag == "dev" { + found = true + } + } + + return found + }, "no errors in the returned result list") + +} + type fakeRetentionClient struct{} // GetCandidates ... diff --git a/src/pkg/retention/policy/builder_test.go b/src/pkg/retention/policy/builder_test.go index 54117c043..515f57dfe 100644 --- a/src/pkg/retention/policy/builder_test.go +++ b/src/pkg/retention/policy/builder_test.go @@ -133,9 +133,9 @@ func (suite *TestBuilderSuite) TestBuild() { ScopeSelectors: scopeSelectors, TagSelectors: []*rule.Selector{ { - Kind: label.Kind, - Decoration: label.With, - Pattern: "L3", + Kind: doublestar.Kind, + Decoration: doublestar.Matches, + Pattern: "latest", }, }, }}, @@ -145,19 +145,7 @@ func (suite *TestBuilderSuite) TestBuild() { require.NoError(suite.T(), err) require.NotNil(suite.T(), p) - artifacts := []*res.Candidate{ - { - NamespaceID: 1, - Namespace: "library", - Repository: "harbor", - Kind: "image", - Tag: "dev", - PushedTime: time.Now().Unix(), - Labels: []string{"L3"}, - }, - } - - results, err := p.Process(artifacts) + results, err := p.Process(suite.all) require.NoError(suite.T(), err) assert.Equal(suite.T(), 1, len(results)) assert.Condition(suite.T(), func() (success bool) { @@ -165,7 +153,7 @@ func (suite *TestBuilderSuite) TestBuild() { success = art.Error == nil && art.Target != nil && art.Target.Repository == "harbor" && - art.Target.Tag == "latest" + art.Target.Tag == "dev" return })