update doublestar pattern and the failed launcher UT cases

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-07-18 13:58:28 +08:00
parent 0994e5efc9
commit 24190291b6
4 changed files with 80 additions and 10 deletions

View File

@ -216,15 +216,15 @@ func (l *launchTestSuite) TestLaunch() {
ScopeSelectors: map[string][]*rule.Selector{ ScopeSelectors: map[string][]*rule.Selector{
"project": { "project": {
{ {
Kind: "regularExpression", Kind: "doublestar",
Decoration: "matches", Decoration: "nsMatches",
Pattern: "**", Pattern: "**",
}, },
}, },
"repository": { "repository": {
{ {
Kind: "regularExpression", Kind: "doublestar",
Decoration: "matches", Decoration: "repoMatches",
Pattern: "**", Pattern: "**",
}, },
}, },

View File

@ -102,7 +102,7 @@ func (suite *ProcessorTestSuite) TearDownSuite() {}
func (suite *ProcessorTestSuite) TestProcess() { func (suite *ProcessorTestSuite) TestProcess() {
results, err := suite.p.Process(suite.all) results, err := suite.p.Process(suite.all)
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
assert.Equal(suite.T(), 2, len(results)) assert.Equal(suite.T(), 1, len(results))
assert.Condition(suite.T(), func() bool { assert.Condition(suite.T(), func() bool {
for _, r := range results { for _, r := range results {
if r.Error != nil { if r.Error != nil {

View File

@ -31,6 +31,10 @@ const (
RepoMatches = "repoMatches" RepoMatches = "repoMatches"
// RepoExcludes represents repository excludes [pattern] // RepoExcludes represents repository excludes [pattern]
RepoExcludes = "repoExcludes" RepoExcludes = "repoExcludes"
// NSMatches represents namespace matches [pattern]
NSMatches = "nsMatches"
// NSExcludes represents namespace excludes [pattern]
NSExcludes = "nsExcludes"
) )
// selector for regular expression // selector for regular expression
@ -59,6 +63,11 @@ func (s *selector) Select(artifacts []*res.Candidate) (selected []*res.Candidate
case RepoExcludes: case RepoExcludes:
value = art.Repository value = art.Repository
excludes = true excludes = true
case NSMatches:
value = art.Namespace
case NSExcludes:
value = art.Namespace
excludes = true
} }
if len(value) > 0 { if len(value) > 0 {
@ -95,5 +104,12 @@ func match(pattern, str string) (bool, error) {
func init() { func init() {
// Register doublestar selector // Register doublestar selector
selectors.Register(Kind, []string{Matches, Excludes}, New) selectors.Register(Kind, []string{
Matches,
Excludes,
RepoMatches,
RepoExcludes,
NSMatches,
NSExcludes,
}, New)
} }

View File

@ -51,8 +51,8 @@ func (suite *RegExpSelectorTestSuite) SetupSuite() {
Labels: []string{"label1", "label2", "label3"}, Labels: []string{"label1", "label2", "label3"},
}, },
{ {
NamespaceID: 1, NamespaceID: 2,
Namespace: "library", Namespace: "retention",
Repository: "redis", Repository: "redis",
Tag: "4.0", Tag: "4.0",
Kind: res.Image, Kind: res.Image,
@ -62,8 +62,8 @@ func (suite *RegExpSelectorTestSuite) SetupSuite() {
Labels: []string{"label1", "label4", "label5"}, Labels: []string{"label1", "label4", "label5"},
}, },
{ {
NamespaceID: 1, NamespaceID: 2,
Namespace: "library", Namespace: "retention",
Repository: "redis", Repository: "redis",
Tag: "4.1", Tag: "4.1",
Kind: res.Image, Kind: res.Image,
@ -180,6 +180,60 @@ func (suite *RegExpSelectorTestSuite) TestRepoExcludes() {
}) })
} }
// TestNSMatches tests the namespace `matches` case
func (suite *RegExpSelectorTestSuite) TestNSMatches() {
repoMatches := &selector{
decoration: NSMatches,
pattern: "{library}",
}
selected, err := repoMatches.Select(suite.artifacts)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(selected))
assert.Condition(suite.T(), func() bool {
return expect([]string{"harbor:latest"}, selected)
})
repoMatches2 := &selector{
decoration: RepoMatches,
pattern: "re*",
}
selected, err = repoMatches2.Select(suite.artifacts)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), 2, len(selected))
assert.Condition(suite.T(), func() bool {
return expect([]string{"redis:4.0", "redis:4.1"}, selected)
})
}
// TestNSExcludes tests the namespace `excludes` case
func (suite *RegExpSelectorTestSuite) TestNSExcludes() {
repoExcludes := &selector{
decoration: NSExcludes,
pattern: "{library}",
}
selected, err := repoExcludes.Select(suite.artifacts)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), 2, len(selected))
assert.Condition(suite.T(), func() bool {
return expect([]string{"redis:4.0", "redis:4.1"}, selected)
})
repoExcludes2 := &selector{
decoration: NSExcludes,
pattern: "re*",
}
selected, err = repoExcludes2.Select(suite.artifacts)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(selected))
assert.Condition(suite.T(), func() bool {
return expect([]string{"harbor:latest"}, selected)
})
}
// Check whether the returned result matched the expected ones (only check repo:tag) // Check whether the returned result matched the expected ones (only check repo:tag)
func expect(expected []string, candidates []*res.Candidate) bool { func expect(expected []string, candidates []*res.Candidate) bool {
hash := make(map[string]bool) hash := make(map[string]bool)