Retention: Implement Evaluator: Retain if created less than x days ago

Signed-off-by: Nathan Lowe <public@nlowe.me>
This commit is contained in:
Nathan Lowe 2019-07-09 23:46:26 -04:00
parent 185f4f3861
commit 8b174825c1
No known key found for this signature in database
GPG Key ID: 1091439964459621
2 changed files with 81 additions and 4 deletions

View File

@ -15,6 +15,8 @@
package lastx
import (
"time"
"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"
@ -37,9 +39,15 @@ 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
func (e *evaluator) Process(artifacts []*res.Candidate) (retain []*res.Candidate, err error) {
cutoff := time.Now().Add(time.Duration(e.x*-24) * time.Hour)
for _, a := range artifacts {
if time.Unix(a.PushedTime, 0).UTC().After(cutoff) {
retain = append(retain, a)
}
}
return
}
// Specify what action is performed to the candidates processed by this evaluator
@ -51,7 +59,7 @@ func (e *evaluator) Action() string {
func New(params rule.Parameters) rule.Evaluator {
if params != nil {
if param, ok := params[ParameterX]; ok {
if v, ok := param.(int); ok {
if v, ok := param.(int); ok && v >= 0 {
return &evaluator{
x: v,
}

View File

@ -0,0 +1,69 @@
package lastx
import (
"fmt"
"testing"
"time"
"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) {
tests := []struct {
Name string
args rule.Parameters
expectedX int
}{
{Name: "Valid", args: map[string]rule.Parameter{ParameterX: 3}, expectedX: 3},
{Name: "Default If Negative", args: map[string]rule.Parameter{ParameterX: -3}, expectedX: DefaultX},
{Name: "Default If Not Set", args: map[string]rule.Parameter{}, expectedX: DefaultX},
{Name: "Default If Wrong Type", args: map[string]rule.Parameter{}, expectedX: DefaultX},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
e := New(tt.args).(*evaluator)
require.Equal(t, tt.expectedX, e.x)
})
}
}
func TestEvaluator_Process(t *testing.T) {
now := time.Now().UTC()
data := []*res.Candidate{
{PushedTime: now.Add(time.Duration(1*-24) * time.Hour).Unix()},
{PushedTime: now.Add(time.Duration(2*-24) * time.Hour).Unix()},
{PushedTime: now.Add(time.Duration(3*-24) * time.Hour).Unix()},
{PushedTime: now.Add(time.Duration(4*-24) * time.Hour).Unix()},
{PushedTime: now.Add(time.Duration(5*-24) * time.Hour).Unix()},
{PushedTime: now.Add(time.Duration(99*-24) * time.Hour).Unix()},
}
tests := []struct {
days int
expected int
}{
{days: 0, expected: 0},
{days: 1, expected: 0},
{days: 2, expected: 1},
{days: 3, expected: 2},
{days: 4, expected: 3},
{days: 5, expected: 4},
{days: 6, expected: 5},
{days: 7, expected: 5},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d days - should keep %d", tt.days, tt.expected), func(t *testing.T) {
e := New(rule.Parameters{ParameterX: tt.days})
result, err := e.Process(data)
require.NoError(t, err)
require.Len(t, result, tt.expected)
})
}
}