mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-23 09:08:26 +01:00
add tag retention model test
Signed-off-by: Ziming Zhang <zziming@vmware.com> Change-Id: I3f8b06b994024fe6052b0dee87600ed932edaaff
This commit is contained in:
parent
311028be37
commit
39db65e90f
@ -37,17 +37,13 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -55,7 +51,7 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -104,17 +100,13 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -122,7 +114,7 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -154,17 +146,13 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -172,7 +160,7 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -183,17 +171,13 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ID: 2,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -201,7 +185,7 @@ func TestCreatePolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -237,17 +221,13 @@ func TestPolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -255,7 +235,7 @@ func TestPolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -307,17 +287,13 @@ func TestPolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -325,7 +301,7 @@ func TestPolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "b.+",
|
||||
},
|
||||
@ -359,17 +335,13 @@ func TestPolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -377,7 +349,7 @@ func TestPolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "b.+",
|
||||
},
|
||||
@ -388,17 +360,13 @@ func TestPolicy(t *testing.T) {
|
||||
ID: 2,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -406,7 +374,7 @@ func TestPolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "b.+",
|
||||
},
|
||||
|
@ -29,17 +29,13 @@ func TestPolicy(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -47,7 +43,7 @@ func TestPolicy(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
@ -107,17 +103,13 @@ func TestExecution(t *testing.T) {
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Template: "recentXdays",
|
||||
Action: "retain",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "label",
|
||||
Decoration: "with",
|
||||
Pattern: "latest",
|
||||
},
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
@ -125,7 +117,7 @@ func TestExecution(t *testing.T) {
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "regularExpression",
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
|
@ -59,6 +59,14 @@ type Metadata struct {
|
||||
|
||||
// Valid Valid
|
||||
func (m *Metadata) Valid(v *validation.Validation) {
|
||||
if m.Trigger == nil {
|
||||
_ = v.SetError("Trigger", "Trigger is required")
|
||||
return
|
||||
}
|
||||
if m.Scope == nil {
|
||||
_ = v.SetError("Scope", "Scope is required")
|
||||
return
|
||||
}
|
||||
if m.Trigger != nil && m.Trigger.Kind == TriggerKindSchedule {
|
||||
if m.Trigger.Settings == nil {
|
||||
_ = v.SetError("Trigger.Settings", "Trigger.Settings is required")
|
||||
@ -68,6 +76,13 @@ func (m *Metadata) Valid(v *validation.Validation) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if !v.HasErrors() {
|
||||
for _, r := range m.Rules {
|
||||
if ok, _ := v.Valid(&r); !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger of the policy
|
||||
|
85
src/pkg/retention/policy/models_test.go
Normal file
85
src/pkg/retention/policy/models_test.go
Normal file
@ -0,0 +1,85 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAlgorithm(t *testing.T) {
|
||||
p := Metadata{
|
||||
Algorithm: "bad",
|
||||
}
|
||||
v := &validation.Validation{}
|
||||
ok, err := v.Valid(p)
|
||||
require.Nil(t, err)
|
||||
require.False(t, ok)
|
||||
}
|
||||
|
||||
// beego 1.9.0 has bug to deal with nil interface
|
||||
// func TestTrigger(t *testing.T) {
|
||||
// p := Metadata{
|
||||
// Algorithm: "or",
|
||||
// }
|
||||
// v := &validation.Validation{}
|
||||
// ok, err := v.Valid(p)
|
||||
//
|
||||
// require.Nil(t, err)
|
||||
// require.False(t, ok)
|
||||
// for _, e := range v.Errors {
|
||||
// fmt.Print(e.Field, e.Message)
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestRule(t *testing.T) {
|
||||
p := &Metadata{
|
||||
Algorithm: "or",
|
||||
Rules: []rule.Metadata{
|
||||
{
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Action: "retain",
|
||||
Template: "recentXdays",
|
||||
Parameters: rule.Parameters{
|
||||
"num": 10,
|
||||
},
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: "badkind", // validate doublestar
|
||||
Decoration: "matches",
|
||||
Pattern: "release-[\\d\\.]+",
|
||||
},
|
||||
},
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: "doublestar",
|
||||
Decoration: "matches",
|
||||
Pattern: ".+",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Trigger: &Trigger{
|
||||
Kind: "Schedule",
|
||||
Settings: map[string]interface{}{
|
||||
"cron": "* 22 11 * * *",
|
||||
},
|
||||
},
|
||||
Scope: &Scope{
|
||||
Level: "project",
|
||||
Reference: 1,
|
||||
},
|
||||
}
|
||||
v := &validation.Validation{}
|
||||
ok, err := v.Valid(p)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.False(t, ok)
|
||||
for _, e := range v.Errors {
|
||||
fmt.Printf("%s %s\n", e.Field, e.Message)
|
||||
}
|
||||
}
|
@ -14,6 +14,10 @@
|
||||
|
||||
package rule
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/validation"
|
||||
)
|
||||
|
||||
// Metadata of the retention rule
|
||||
type Metadata struct {
|
||||
// UUID of rule
|
||||
@ -42,6 +46,22 @@ type Metadata struct {
|
||||
ScopeSelectors map[string][]*Selector `json:"scope_selectors" valid:"Required"`
|
||||
}
|
||||
|
||||
// Valid Valid
|
||||
func (m *Metadata) Valid(v *validation.Validation) {
|
||||
for _, ts := range m.TagSelectors {
|
||||
if pass, _ := v.Valid(ts); !pass {
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, ss := range m.ScopeSelectors {
|
||||
for _, s := range ss {
|
||||
if pass, _ := v.Valid(s); !pass {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Selector to narrow down the list
|
||||
type Selector struct {
|
||||
// Kind of the selector
|
||||
|
Loading…
Reference in New Issue
Block a user