Skip to content

Commit

Permalink
Merge d7e6b1b into 2c20251
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Jul 15, 2019
2 parents 2c20251 + d7e6b1b commit e91908c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pkg/retention/policy/rule/latestk/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package latestk

import (
"sort"

"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/pkg/retention/policy/action"
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
Expand All @@ -38,8 +40,17 @@ type evaluator struct {

// Process the candidates based on the rule definition
func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) {
// TODO: REPLACE SAMPLE CODE WITH REAL IMPLEMENTATION
return artifacts, nil
// The updated proposal does not guarantee the order artifacts are provided, so we have to sort them first
sort.Slice(artifacts, func(i, j int) bool {
return artifacts[i].PushedTime < artifacts[j].PushedTime
})

i := e.k
if i > len(artifacts) {
i = len(artifacts)
}

return artifacts[:i], nil
}

// Specify what action is performed to the candidates processed by this evaluator
Expand All @@ -51,7 +62,7 @@ func (e *evaluator) Action() string {
func New(params rule.Parameters) rule.Evaluator {
if params != nil {
if param, ok := params[ParameterK]; ok {
if v, ok := param.(int); ok {
if v, ok := param.(int); ok && v >= 0 {
return &evaluator{
k: v,
}
Expand Down
71 changes: 71 additions & 0 deletions src/pkg/retention/policy/rule/latestk/evaluator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package latestk

import (
"math/rand"
"strconv"
"testing"

"github.com/stretchr/testify/suite"

"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
"github.com/goharbor/harbor/src/pkg/retention/res"
"github.com/stretchr/testify/require"
)

type EvaluatorTestSuite struct {
suite.Suite
}

func (e *EvaluatorTestSuite) TestNew() {
tests := []struct {
Name string
args rule.Parameters
expectedK int
}{
{Name: "Valid", args: map[string]rule.Parameter{ParameterK: 5}, expectedK: 5},
{Name: "Default If Negative", args: map[string]rule.Parameter{ParameterK: -1}, expectedK: DefaultK},
{Name: "Default If Not Set", args: map[string]rule.Parameter{}, expectedK: DefaultK},
{Name: "Default If Wrong Type", args: map[string]rule.Parameter{ParameterK: "foo"}, expectedK: DefaultK},
}

for _, tt := range tests {
e.T().Run(tt.Name, func(t *testing.T) {
e := New(tt.args).(*evaluator)

require.Equal(t, tt.expectedK, e.k)
})
}
}

func (e *EvaluatorTestSuite) TestProcess() {
data := []*res.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}, {PushedTime: 4}}
rand.Shuffle(len(data), func(i, j int) {
data[i], data[j] = data[j], data[i]
})

tests := []struct {
k int
expected int
}{
{k: 0, expected: 0},
{k: 1, expected: 1},
{k: 3, expected: 3},
{k: 5, expected: 5},
{k: 6, expected: 5},
}

for _, tt := range tests {
e.T().Run(strconv.Itoa(tt.k), func(t *testing.T) {
e := New(map[string]rule.Parameter{ParameterK: tt.k})

result, err := e.Process(data)

require.NoError(t, err)
require.Len(t, result, tt.expected)
})
}
}

func TestEvaluator(t *testing.T) {
suite.Run(t, &EvaluatorTestSuite{})
}

0 comments on commit e91908c

Please sign in to comment.