fix the OR processor failure issue

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-07-29 17:43:20 +08:00
parent 640fcdaee3
commit fa678a12cc
4 changed files with 71 additions and 46 deletions

View File

@ -125,13 +125,13 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re
op := func(art *res.Candidate) string { op := func(art *res.Candidate) string {
if e, exists := hash[art.Hash()]; exists { if e, exists := hash[art.Hash()]; exists {
if e != nil { if e != nil {
return "Err" return "ERR"
} }
return "X" return "DEL"
} }
return "" return "RETAIN"
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -158,7 +158,7 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re
table.AppendBulk(data) table.AppendBulk(data)
table.Render() table.Render()
logger.Infof("%s", buf.String()) logger.Infof("\n%s", buf.String())
// log all the concrete errors if have // log all the concrete errors if have
for _, r := range results { for _, r := range results {

View File

@ -154,12 +154,10 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) {
return return
} }
if len(processed) > 0 { // Pass to the outside
// Pass to the outside resChan <- &chanItem{
resChan <- &chanItem{ action: evaluator.Action(),
action: evaluator.Action(), processed: processed,
processed: processed,
}
} }
}(evaluator, selectors) }(evaluator, selectors)
} }

View File

@ -19,18 +19,21 @@ import (
"testing" "testing"
"time" "time"
"github.com/goharbor/harbor/src/pkg/retention/dep" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/always"
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
"github.com/goharbor/harbor/src/pkg/retention/policy/action" "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/alg"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule" "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/lastx"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps" "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/doublestar"
"github.com/goharbor/harbor/src/pkg/retention/res/selectors/label" "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/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -40,7 +43,6 @@ import (
type ProcessorTestSuite struct { type ProcessorTestSuite struct {
suite.Suite suite.Suite
p alg.Processor
all []*res.Candidate all []*res.Candidate
oldClient dep.Client 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) perf := action.NewRetainAction(suite.all, false)
params := make([]*alg.Parameter, 0)
lastxParams := make(map[string]rule.Parameter) lastxParams := make(map[string]rule.Parameter)
lastxParams[lastx.ParameterX] = 10 lastxParams[lastx.ParameterX] = 10
params = append(params, &alg.Parameter{ params = append(params, &alg.Parameter{
@ -97,20 +110,9 @@ func (suite *ProcessorTestSuite) SetupSuite() {
Performer: perf, Performer: perf,
}) })
suite.p = New(params) p := New(params)
suite.oldClient = dep.DefaultClient results, err := p.Process(suite.all)
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)
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(results)) assert.Equal(suite.T(), 1, len(results))
assert.Condition(suite.T(), func() bool { assert.Condition(suite.T(), func() bool {
@ -124,6 +126,43 @@ func (suite *ProcessorTestSuite) TestProcess() {
}, "no errors in the returned result list") }, "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{} type fakeRetentionClient struct{}
// GetCandidates ... // GetCandidates ...

View File

@ -133,9 +133,9 @@ func (suite *TestBuilderSuite) TestBuild() {
ScopeSelectors: scopeSelectors, ScopeSelectors: scopeSelectors,
TagSelectors: []*rule.Selector{ TagSelectors: []*rule.Selector{
{ {
Kind: label.Kind, Kind: doublestar.Kind,
Decoration: label.With, Decoration: doublestar.Matches,
Pattern: "L3", Pattern: "latest",
}, },
}, },
}}, }},
@ -145,19 +145,7 @@ func (suite *TestBuilderSuite) TestBuild() {
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
require.NotNil(suite.T(), p) require.NotNil(suite.T(), p)
artifacts := []*res.Candidate{ results, err := p.Process(suite.all)
{
NamespaceID: 1,
Namespace: "library",
Repository: "harbor",
Kind: "image",
Tag: "dev",
PushedTime: time.Now().Unix(),
Labels: []string{"L3"},
},
}
results, err := p.Process(artifacts)
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(results)) assert.Equal(suite.T(), 1, len(results))
assert.Condition(suite.T(), func() (success bool) { assert.Condition(suite.T(), func() (success bool) {
@ -165,7 +153,7 @@ func (suite *TestBuilderSuite) TestBuild() {
success = art.Error == nil && success = art.Error == nil &&
art.Target != nil && art.Target != nil &&
art.Target.Repository == "harbor" && art.Target.Repository == "harbor" &&
art.Target.Tag == "latest" art.Target.Tag == "dev"
return return
}) })