Skip to content

Commit

Permalink
Merge branch 'main' into unit-tests-3
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycharly authored Mar 5, 2024
2 parents 1f8b605 + 7da4d79 commit 601634c
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 19 deletions.
6 changes: 4 additions & 2 deletions .release-notes/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Release notes for `TODO`.
## ⛵ Tutorials ⛵
## 🔧 Fixes 🔧
## 📚 Docs 📚
## 🎸 Misc 🎸
-->

## 🔧 Fixes 🔧

- Added `config` in the list of forbidden binding names
2 changes: 1 addition & 1 deletion pkg/apis/v1alpha1/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var (
identifier = regexp.MustCompile(`^\w+$`)
forbiddenNames = []string{"namespace", "client", "error", "values", "stdout", "stderr"}
forbiddenNames = []string{"namespace", "client", "config", "error", "values", "stdout", "stderr"}
forbidden = sets.New(forbiddenNames...)
)

Expand Down
99 changes: 99 additions & 0 deletions pkg/apis/v1alpha1/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBinding_CheckName(t *testing.T) {
tests := []struct {
name string
bindingName string
bindingValue Any
wantErr bool
}{{
name: "empty",
wantErr: true,
}, {
name: "simple",
bindingName: "simple",
wantErr: false,
}, {
name: "with dollar",
bindingName: "$simple",
wantErr: true,
}, {
name: "with space",
bindingName: "simple one",
wantErr: true,
}, {
name: "with dot",
bindingName: "simple.one",
wantErr: true,
}, {
name: "forbidden",
bindingName: "config",
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := Binding{
Name: tt.bindingName,
Value: tt.bindingValue,
}
err := b.CheckName()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

func TestBinding_CheckEnvName(t *testing.T) {
tests := []struct {
name string
bindingName string
bindingValue Any
wantErr bool
}{{
name: "empty",
wantErr: true,
}, {
name: "simple",
bindingName: "simple",
wantErr: false,
}, {
name: "with dollar",
bindingName: "$simple",
wantErr: true,
}, {
name: "with space",
bindingName: "simple one",
wantErr: true,
}, {
name: "with dot",
bindingName: "simple.one",
wantErr: true,
}, {
name: "forbidden",
bindingName: "config",
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := Binding{
Name: tt.bindingName,
Value: tt.bindingValue,
}
err := b.CheckEnvName()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
91 changes: 91 additions & 0 deletions pkg/validation/test/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package test

import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestValidateBinding(t *testing.T) {
tests := []struct {
name string
path *field.Path
obj v1alpha1.Binding
want field.ErrorList
}{{
name: "empty",
path: field.NewPath("foo"),
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo.name",
BadValue: "",
Detail: "invalid name ",
},
},
}, {
name: "valid name",
path: field.NewPath("foo"),
obj: v1alpha1.Binding{
Name: "foo",
},
}, {
name: "invalid name",
path: field.NewPath("foo"),
obj: v1alpha1.Binding{
Name: "$foo",
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo.name",
BadValue: "$foo",
Detail: "invalid name $foo",
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateBinding(tt.path, tt.obj)
assert.Equal(t, tt.want, got)
})
}
}

func TestValidateBindings(t *testing.T) {
tests := []struct {
name string
path *field.Path
objs []v1alpha1.Binding
want field.ErrorList
}{{
name: "null",
}, {
name: "empty",
objs: []v1alpha1.Binding{},
}, {
name: "valid",
objs: []v1alpha1.Binding{{
Name: "foo",
}},
}, {
name: "invalid",
objs: []v1alpha1.Binding{{}},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "[0].name",
BadValue: "",
Detail: "invalid name ",
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateBindings(tt.path, tt.objs...)
assert.Equal(t, tt.want, got)
})
}
}
74 changes: 74 additions & 0 deletions pkg/validation/test/for_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package test

import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestValidateFor(t *testing.T) {
tests := []struct {
name string
path *field.Path
obj *v1alpha1.For
want field.ErrorList
}{{
name: "null",
}, {
name: "empty",
path: field.NewPath("for"),
obj: &v1alpha1.For{},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "for",
BadValue: &v1alpha1.For{},
Detail: "either a deletion or a condition must be specified",
},
},
}, {
name: "no condition name",
path: field.NewPath("for"),
obj: &v1alpha1.For{
Condition: &v1alpha1.Condition{},
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "for.condition.name",
BadValue: &v1alpha1.For{Condition: &v1alpha1.Condition{}},
Detail: "a condition name must be specified",
},
},
}, {
name: "both condition and deletion",
path: field.NewPath("for"),
obj: &v1alpha1.For{
Condition: &v1alpha1.Condition{
Name: "foo",
},
Deletion: &v1alpha1.Deletion{},
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "for",
BadValue: &v1alpha1.For{
Condition: &v1alpha1.Condition{
Name: "foo",
},
Deletion: &v1alpha1.Deletion{},
},
Detail: "a deletion or a condition must be specified (found both)",
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateFor(tt.path, tt.obj)
assert.Equal(t, tt.want, got)
})
}
}
87 changes: 87 additions & 0 deletions pkg/validation/test/resource_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package test

import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestValidateResourceReference(t *testing.T) {
tests := []struct {
name string
path *field.Path
obj v1alpha1.ResourceReference
want field.ErrorList
}{{
name: "empty",
path: field.NewPath("foo"),
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo",
BadValue: v1alpha1.ResourceReference{},
Detail: "kind or resource must be specified",
},
},
}, {
name: "both kind and resource",
path: field.NewPath("foo"),
obj: v1alpha1.ResourceReference{
Resource: "foo",
Kind: "bar",
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo",
BadValue: v1alpha1.ResourceReference{
Resource: "foo",
Kind: "bar",
},
Detail: "kind or resource must be specified (found both)",
},
},
}, {
name: "resource and apiVersion",
path: field.NewPath("foo"),
obj: v1alpha1.ResourceReference{
APIVersion: "v1",
Resource: "foo",
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo.apiVersion",
BadValue: v1alpha1.ResourceReference{
APIVersion: "v1",
Resource: "foo",
},
Detail: "apiVersion must not be specified when resource is set",
},
},
}, {
name: "kind and no apiVersion",
path: field.NewPath("foo"),
obj: v1alpha1.ResourceReference{
Kind: "foo",
},
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "foo.apiVersion",
BadValue: v1alpha1.ResourceReference{
Kind: "foo",
},
Detail: "apiVersion must be specified when kind is set",
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateResourceReference(tt.path, tt.obj)
assert.Equal(t, tt.want, got)
})
}
}
Loading

0 comments on commit 601634c

Please sign in to comment.