-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into unit-tests-3
- Loading branch information
Showing
8 changed files
with
401 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.