use only 1 repo and tag selector to match

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-10-15 16:44:14 +08:00
parent a3546478eb
commit ae4c698ab4
2 changed files with 92 additions and 26 deletions

View File

@ -28,39 +28,44 @@ func (rm *Matcher) Match(c art.Candidate) (bool, error) {
// match repositories according to the repository selectors
var repositoryCandidates []*art.Candidate
for _, repositorySelector := range r.ScopeSelectors["repository"] {
selector, err := index.Get(repositorySelector.Kind, repositorySelector.Decoration,
repositorySelector.Pattern)
if err != nil {
return false, err
}
repositoryCandidates, err = selector.Select(cands)
if err != nil {
return false, err
}
repositorySelectors := r.ScopeSelectors["repository"]
if len(repositorySelectors) < 1 {
continue
}
repositorySelector := repositorySelectors[0]
selector, err := index.Get(repositorySelector.Kind, repositorySelector.Decoration,
repositorySelector.Pattern)
if err != nil {
return false, err
}
repositoryCandidates, err = selector.Select(cands)
if err != nil {
return false, err
}
if len(repositoryCandidates) == 0 {
continue
}
// match tag according to the tag selectors
var tagCandidates []*art.Candidate
for _, tagSelector := range r.TagSelectors {
selector, err := index.Get(tagSelector.Kind, tagSelector.Decoration,
tagSelector.Pattern)
if err != nil {
return false, err
}
tagCandidates, err = selector.Select(cands)
if err != nil {
return false, err
}
tagSelectors := r.TagSelectors
if len(tagSelectors) < 0 {
continue
}
tagSelector := r.TagSelectors[0]
selector, err = index.Get(tagSelector.Kind, tagSelector.Decoration,
tagSelector.Pattern)
if err != nil {
return false, err
}
tagCandidates, err = selector.Select(cands)
if err != nil {
return false, err
}
if len(tagCandidates) == 0 {
continue
}
return true, nil
}
return false, nil

View File

@ -20,6 +20,7 @@ type MatchTestSuite struct {
require *require.Assertions
ctr immutabletag.APIController
ruleID int64
ruleID2 int64
}
// SetupSuite ...
@ -50,7 +51,31 @@ func (s *MatchTestSuite) TestImmuMatch() {
{
Kind: "doublestar",
Decoration: "matches",
Pattern: "**",
Pattern: "redis",
},
},
},
}
rule2 := &model.Metadata{
ID: 1,
ProjectID: 2,
Priority: 1,
Template: "latestPushedK",
Action: "immuablity",
TagSelectors: []*model.Selector{
{
Kind: "doublestar",
Decoration: "matches",
Pattern: "**",
},
},
ScopeSelectors: map[string][]*model.Selector{
"repository": {
{
Kind: "doublestar",
Decoration: "matches",
Pattern: "mysql",
},
},
},
@ -60,6 +85,12 @@ func (s *MatchTestSuite) TestImmuMatch() {
s.ruleID = id
s.require.NotNil(err)
id, err = s.ctr.CreateImmutableRule(rule2)
s.ruleID2 = id
s.require.NotNil(err)
match := NewRuleMatcher(2)
c1 := art.Candidate{
NamespaceID: 2,
Namespace: "immutable",
@ -71,8 +102,6 @@ func (s *MatchTestSuite) TestImmuMatch() {
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1", "label4", "label5"},
}
match := NewRuleMatcher(2)
isMatch, err := match.Match(c1)
s.require.Equal(isMatch, true)
s.require.Nil(err)
@ -88,14 +117,46 @@ func (s *MatchTestSuite) TestImmuMatch() {
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1", "label4", "label5"},
}
isMatch, err = match.Match(c2)
s.require.Equal(isMatch, false)
s.require.Nil(err)
c3 := art.Candidate{
NamespaceID: 2,
Namespace: "immutable",
Repository: "mysql",
Tag: "9.4.8",
Kind: art.Image,
PushedTime: time.Now().Unix() - 3600,
PulledTime: time.Now().Unix(),
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1"},
}
isMatch, err = match.Match(c3)
s.require.Equal(isMatch, true)
s.require.Nil(err)
c4 := art.Candidate{
NamespaceID: 2,
Namespace: "immutable",
Repository: "hello",
Tag: "world",
Kind: art.Image,
PushedTime: time.Now().Unix() - 3600,
PulledTime: time.Now().Unix(),
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1"},
}
isMatch, err = match.Match(c4)
s.require.Equal(isMatch, false)
s.require.Nil(err)
}
// TearDownSuite clears env for test suite
func (s *MatchTestSuite) TearDownSuite() {
err := s.ctr.DeleteImmutableRule(s.ruleID)
require.NoError(s.T(), err, "delete immutable")
err = s.ctr.DeleteImmutableRule(s.ruleID2)
require.NoError(s.T(), err, "delete immutable")
}