Skip to content

Commit

Permalink
fix env filter
Browse files Browse the repository at this point in the history
  • Loading branch information
airycanon committed Sep 19, 2022
1 parent 11cf545 commit 0647e78
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 62 deletions.
22 changes: 16 additions & 6 deletions apis/meta/v1alpha1/artifact_filter_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
)

func (a ArtifactFilterRegexList) Validate(path *field.Path) (errs field.ErrorList) {
if len(a) == 0 {
errs = append(errs, field.Required(path, kvalidation.EmptyError()))
}

for i, regex := range a {
if regex == "" {
errs = append(errs, field.Required(path.Index(i), kvalidation.EmptyError()))
Expand All @@ -42,18 +46,14 @@ func (a *ArtifactTagFilter) Validate(path *field.Path) (errs field.ErrorList) {
}

func (a *ArtifactEnvFilter) Validate(path *field.Path) (errs field.ErrorList) {
if a.Name == "" {
errs = append(errs, field.Required(path.Child("name"), kvalidation.EmptyError()))
} else {
errs = append(errs, validation.ValidateItemName(a.Name, false, path.Child("name"))...)
}
errs = append(errs, validateName(a.Name, path.Child("name"))...)
errs = append(errs, a.Regex.Validate(path.Child("regex"))...)

return
}

func (a *ArtifactLabelFilter) Validate(path *field.Path) (errs field.ErrorList) {
errs = append(errs, validation.ValidateItemName(a.Name, false, path.Child("name"))...)
errs = append(errs, validateName(a.Name, path.Child("name"))...)
errs = append(errs, a.Regex.Validate(path.Child("regex"))...)

return
Expand Down Expand Up @@ -90,3 +90,13 @@ func (a *ArtifactFilterSet) Validate(path *field.Path) (errs field.ErrorList) {

return
}

func validateName(name string, path *field.Path) (errs field.ErrorList) {
if name == "" {
errs = append(errs, field.Required(path, kvalidation.EmptyError()))
} else {
errs = append(errs, validation.ValidateGenericResourceName(name, path)...)
}

return
}
308 changes: 252 additions & 56 deletions apis/meta/v1alpha1/artifact_filter_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -29,6 +30,8 @@ func TestArtifactFilterRegexList_Validate(t *testing.T) {
g = NewGomegaWithT(t)
)

g.Expect(list.Validate(field.NewPath("test"))).To(HaveLen(1))

list = ArtifactFilterRegexList{
"abc",
"abc def",
Expand All @@ -52,85 +55,278 @@ func TestArtifactFilterRegexList_Validate(t *testing.T) {

func TestArtifactEnvFilter_Validate(t *testing.T) {
var (
filter ArtifactEnvFilter
g = NewGomegaWithT(t)
g = NewGomegaWithT(t)
tests = []struct {
name string
regex ArtifactFilterRegexList
evaluate func(g *GomegaWithT, errs field.ErrorList)
}{
{
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(2))
},
},
{
name: "test",
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
regex: ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
name: "test",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(BeEmpty())
},
},
{
name: "TEST",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(BeEmpty())
},
},
{
name: "?!#",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
name: "test",
regex: ArtifactFilterRegexList{
"abc",
"[",
"(",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(2))
},
},
{
name: "?!#",
regex: ArtifactFilterRegexList{
"abc",
"[",
"(",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(3))
},
},
}
)

g.Expect(filter.Validate(field.NewPath("test"))).NotTo(BeEmpty())

filter.Name = "test"
g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())
for i, item := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
filter := ArtifactEnvFilter{
Name: item.name,
Regex: item.regex,
}

filter.Regex = ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
errs := filter.Validate(field.NewPath("test"))
item.evaluate(g, errs)
})
}

g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())
}

func TestArtifactTagFilter_Validate(t *testing.T) {
var (
filter ArtifactTagFilter
g = NewGomegaWithT(t)
g = NewGomegaWithT(t)
tests = []struct {
regex ArtifactFilterRegexList
evaluate func(g *GomegaWithT, errs field.ErrorList)
}{
{
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
regex: ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(BeEmpty())
},
},
{
regex: ArtifactFilterRegexList{
"abc",
"[",
"(",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(2))
},
},
}
)

g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())
for i, item := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
filter := ArtifactTagFilter{
Regex: item.regex,
}

filter.Regex = ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
errs := filter.Validate(field.NewPath("test"))
item.evaluate(g, errs)
})
}
}

g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())
func TestArtifactLabelFilter_Validate(t *testing.T) {
var (
g = NewGomegaWithT(t)
tests = []struct {
name string
regex ArtifactFilterRegexList
evaluate func(g *GomegaWithT, errs field.ErrorList)
}{
{
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(2))
},
},
{
name: "test",
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
regex: ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
name: "test",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(BeEmpty())
},
},
{
name: "TEST",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(BeEmpty())
},
},
{
name: "?!#",
regex: ArtifactFilterRegexList{
"abc",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(1))
},
},
{
name: "test",
regex: ArtifactFilterRegexList{
"abc",
"[",
"(",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(2))
},
},
{
name: "?!#",
regex: ArtifactFilterRegexList{
"abc",
"[",
"(",
},
evaluate: func(g *GomegaWithT, errs field.ErrorList) {
g.Expect(errs).To(HaveLen(3))
},
},
}
)

filter.Regex = ArtifactFilterRegexList{
"abc",
"[",
for i, item := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
filter := ArtifactLabelFilter{
Name: item.name,
Regex: item.regex,
}

errs := filter.Validate(field.NewPath("test"))
item.evaluate(g, errs)
})
}
g.Expect(filter.Validate(field.NewPath("test"))).To(HaveLen(1))
}

func TestArtifactLabelFilter_Validate(t *testing.T) {
func TestArtifactFilterSet_Validate(t *testing.T) {
var (
filter ArtifactLabelFilter
g = NewGomegaWithT(t)
path = field.NewPath("test")
filter ArtifactFilterSet
)

g.Expect(filter.Validate(field.NewPath("test"))).NotTo(BeEmpty())

filter.Name = "test"
g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())

filter.Regex = ArtifactFilterRegexList{
"abc",
"abc def",
"^abc$",
"abc+?",
"abc*",
"(abc)",
"[abc]",
`\d\w`,
filter.Any = nil
filter.All = []ArtifactFilter{
{},
}
g.Expect(filter.Validate(path)).To(BeEmpty())

g.Expect(filter.Validate(field.NewPath("test"))).To(BeEmpty())
filter.All = nil
filter.Any = []ArtifactFilter{
{},
}
g.Expect(filter.Validate(path)).To(BeEmpty())

filter.Name = ""
filter.Regex = ArtifactFilterRegexList{
"abc",
"[",
filter.All = []ArtifactFilter{
{},
}
filter.Any = []ArtifactFilter{
{},
}
g.Expect(filter.Validate(field.NewPath("test"))).To(HaveLen(2))
g.Expect(filter.Validate(path)).To(HaveLen(1))
}

0 comments on commit 0647e78

Please sign in to comment.