Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add validation unit tests #1045

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pkg/validation/config/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"testing"

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

func TestValidateCluster(t *testing.T) {
tests := []struct {
name string
path *field.Path
obj v1alpha1.Cluster
want field.ErrorList
}{{
name: "empty",
path: field.NewPath("foo"),
want: field.ErrorList{
&field.Error{
Type: field.ErrorTypeRequired,
BadValue: "",
Field: "foo.kubeconfig",
Detail: "a kubeconfig is required",
},
},
}, {
name: "valid",
path: field.NewPath("foo"),
obj: v1alpha1.Cluster{
Kubeconfig: "foo",
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateCluster(tt.path, tt.obj)
assert.Equal(t, tt.want, got)
})
}
}
6 changes: 4 additions & 2 deletions pkg/validation/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

func ValidateConfiguration(obj *v1alpha1.Configuration) field.ErrorList {
var errs field.ErrorList
var path *field.Path
errs = append(errs, ValidateConfigurationSpec(path.Child("spec"), obj.Spec)...)
if obj != nil {
var path *field.Path
errs = append(errs, ValidateConfigurationSpec(path.Child("spec"), obj.Spec)...)
}
return errs
}
48 changes: 48 additions & 0 deletions pkg/validation/config/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package config

import (
"testing"

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

func TestValidateConfiguration(t *testing.T) {
tests := []struct {
name string
obj *v1alpha1.Configuration
want field.ErrorList
}{{
name: "null",
}, {
name: "empty",
obj: &v1alpha1.Configuration{},
}, {
name: "with cluster",
obj: &v1alpha1.Configuration{
Spec: v1alpha1.ConfigurationSpec{
Clusters: map[string]v1alpha1.Cluster{
"foo": {
Kubeconfig: "foo",
},
},
},
},
}, {
name: "with catch",
obj: &v1alpha1.Configuration{
Spec: v1alpha1.ConfigurationSpec{
Catch: []v1alpha1.Catch{{
Events: &v1alpha1.Events{},
}},
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateConfiguration(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