Sort artifacts before processing and port tests to github.com/stretchr/testify/suite

Signed-off-by: Nathan Lowe <public@nlowe.me>
This commit is contained in:
Nathan Lowe 2019-07-14 22:45:36 -04:00
parent 4ecbe749e4
commit d7e6b1b621
No known key found for this signature in database
GPG Key ID: 1091439964459621
2 changed files with 26 additions and 5 deletions

View File

@ -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"
@ -38,6 +40,11 @@ type evaluator struct {
// Process the candidates based on the rule definition
func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) {
// 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)

View File

@ -1,15 +1,22 @@
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"
)
func TestEvaluator_New(t *testing.T) {
type EvaluatorTestSuite struct {
suite.Suite
}
func (e *EvaluatorTestSuite) TestNew() {
tests := []struct {
Name string
args rule.Parameters
@ -22,7 +29,7 @@ func TestEvaluator_New(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
e.T().Run(tt.Name, func(t *testing.T) {
e := New(tt.args).(*evaluator)
require.Equal(t, tt.expectedK, e.k)
@ -30,8 +37,11 @@ func TestEvaluator_New(t *testing.T) {
}
}
func TestEvaluator_Process(t *testing.T) {
data := []*res.Candidate{{}, {}, {}, {}, {}}
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
@ -45,7 +55,7 @@ func TestEvaluator_Process(t *testing.T) {
}
for _, tt := range tests {
t.Run(strconv.Itoa(tt.k), func(t *testing.T) {
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)
@ -55,3 +65,7 @@ func TestEvaluator_Process(t *testing.T) {
})
}
}
func TestEvaluator(t *testing.T) {
suite.Run(t, &EvaluatorTestSuite{})
}