diff --git a/pkg/validation/config/cluster_test.go b/pkg/validation/config/cluster_test.go new file mode 100644 index 000000000..063de9a92 --- /dev/null +++ b/pkg/validation/config/cluster_test.go @@ -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) + }) + } +} diff --git a/pkg/validation/config/configuration.go b/pkg/validation/config/configuration.go index a70c6ae89..dfdd82309 100644 --- a/pkg/validation/config/configuration.go +++ b/pkg/validation/config/configuration.go @@ -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 } diff --git a/pkg/validation/config/configuration_test.go b/pkg/validation/config/configuration_test.go new file mode 100644 index 000000000..886d38a96 --- /dev/null +++ b/pkg/validation/config/configuration_test.go @@ -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) + }) + } +} diff --git a/pkg/validation/test/operation_test.go b/pkg/validation/test/operation_test.go index bfdee6e73..b56a8bd93 100644 --- a/pkg/validation/test/operation_test.go +++ b/pkg/validation/test/operation_test.go @@ -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 @@ -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) {