Skip to content

Commit

Permalink
use only 1 repo and tag selector to match
Browse files Browse the repository at this point in the history
Signed-off-by: wang yan <wangyan@vmware.com>
  • Loading branch information
wy65701436 committed Oct 15, 2019
1 parent a354647 commit ae4c698
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
49 changes: 27 additions & 22 deletions src/pkg/immutabletag/match/rule/match.go
Expand Up @@ -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
Expand Down
69 changes: 65 additions & 4 deletions src/pkg/immutabletag/match/rule/match_test.go
Expand Up @@ -20,6 +20,7 @@ type MatchTestSuite struct {
require *require.Assertions
ctr immutabletag.APIController
ruleID int64
ruleID2 int64
}

// SetupSuite ...
Expand Down Expand Up @@ -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",
},
},
},
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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")
}

0 comments on commit ae4c698

Please sign in to comment.