Skip to content

Commit

Permalink
Merge branch 'main' into unit-tests-3
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Mar 5, 2024
2 parents 1f8b605 + 7da4d79 commit 4e799ad
Show file tree
Hide file tree
Showing 9 changed files with 415 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)
})
}
}
14 changes: 14 additions & 0 deletions pkg/validation/test/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func TestValidateOperation(t *testing.T) {
exampleSleep := &v1alpha1.Sleep{
Duration: metav1.Duration{Duration: 5 * time.Second},
}
exampleWait := &v1alpha1.Wait{
ResourceReference: v1alpha1.ResourceReference{
Resource: "foos",
},
For: v1alpha1.For{
Deletion: &v1alpha1.Deletion{},
},
}
tests := []struct {
name string
input v1alpha1.Operation
Expand Down Expand Up @@ -145,6 +153,12 @@ func TestValidateOperation(t *testing.T) {
Sleep: exampleSleep,
},
expectErr: false,
}, {
name: "Only Wait operation statement provided",
input: v1alpha1.Operation{
Wait: exampleWait,
},
expectErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 4e799ad

Please sign in to comment.