diff --git a/pkg/apis/feeds/v1alpha1/cluster_event_type_validation.go b/pkg/apis/feeds/v1alpha1/cluster_event_type_validation.go index 0415a4637fd..b809348f459 100644 --- a/pkg/apis/feeds/v1alpha1/cluster_event_type_validation.go +++ b/pkg/apis/feeds/v1alpha1/cluster_event_type_validation.go @@ -18,14 +18,21 @@ package v1alpha1 import ( "github.com/knative/pkg/apis" + "k8s.io/apimachinery/pkg/util/validation" ) -func (cet *ClusterEventType) Validate() *apis.FieldError { - return cet.Spec.Validate().ViaField("spec") +func (et *ClusterEventType) Validate() *apis.FieldError { + return et.Spec.Validate().ViaField("spec") } -func (cess *ClusterEventTypeSpec) Validate() *apis.FieldError { - return cess.CommonEventTypeSpec.Validate() +func (ets *ClusterEventTypeSpec) Validate() *apis.FieldError { + if ets.ClusterEventSource == "" { + return apis.ErrMissingField("eventSource") + } + if errs := validation.IsQualifiedName(ets.ClusterEventSource); len(errs) > 0 { + return apis.ErrInvalidValue(ets.ClusterEventSource, "eventSource") + } + return ets.CommonEventTypeSpec.Validate() } func (current *ClusterEventType) CheckImmutableFields(og apis.Immutable) *apis.FieldError { @@ -37,7 +44,13 @@ func (current *ClusterEventType) CheckImmutableFields(og apis.Immutable) *apis.F return nil } - // TODO + // EventSource for an EventType should not change. + if original.Spec.ClusterEventSource != current.Spec.ClusterEventSource { + return &apis.FieldError{ + Message: "Immutable fields changed", + Paths: []string{"spec.eventSource"}, + } + } return nil } diff --git a/pkg/apis/feeds/v1alpha1/cluster_event_type_validation_test.go b/pkg/apis/feeds/v1alpha1/cluster_event_type_validation_test.go new file mode 100644 index 00000000000..4cc13cf2c64 --- /dev/null +++ b/pkg/apis/feeds/v1alpha1/cluster_event_type_validation_test.go @@ -0,0 +1,123 @@ +/* +Copyright 2018 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/knative/pkg/apis" + "testing" +) + +func TestClusterEventTypeSpecValidation(t *testing.T) { + tests := []struct { + name string + et *ClusterEventTypeSpec + want *apis.FieldError + }{{ + name: "valid", + et: &ClusterEventTypeSpec{ + ClusterEventSource: "foo", + }, + }, { + name: "invalid source", + et: &ClusterEventTypeSpec{ + ClusterEventSource: "f@o", + }, + want: apis.ErrInvalidValue("f@o", "eventSource"), + }, { + name: "empty", + et: &ClusterEventTypeSpec{}, + want: apis.ErrMissingField("eventSource"), + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := test.et.Validate() + ignoreArguments := cmpopts.IgnoreFields(apis.FieldError{}, "Details") + if diff := cmp.Diff(test.want, got, ignoreArguments); diff != "" { + t.Errorf("validateClusterEventType (-want, +got) = %v", diff) + } + }) + } +} + +func TestClusterEventTypeImmutableFields(t *testing.T) { + tests := []struct { + name string + new apis.Immutable + old apis.Immutable + want *apis.FieldError + }{{ + name: "good (no change)", + new: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "foo", + }, + }, + old: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "foo", + }, + }, + want: nil, + }, { + name: "good (description change)", + new: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "foo", + CommonEventTypeSpec: CommonEventTypeSpec{ + Description: "Foo foo foo.", + }, + }, + }, + old: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "foo", + CommonEventTypeSpec: CommonEventTypeSpec{ + Description: "Bar bar bar.", + }, + }, + }, + want: nil, + }, { + name: "bad (source changes)", + new: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "foo", + }, + }, + old: &ClusterEventType{ + Spec: ClusterEventTypeSpec{ + ClusterEventSource: "bar", + }, + }, + want: &apis.FieldError{ + Message: "Immutable fields changed", + Paths: []string{"spec.eventSource"}, + }, + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := test.new.CheckImmutableFields(test.old) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("Validate (-want, +got) = %v", diff) + } + }) + } +} diff --git a/pkg/apis/feeds/v1alpha1/common_event_type_validation.go b/pkg/apis/feeds/v1alpha1/common_event_type_validation.go index a60ae064c4e..9457e14a422 100644 --- a/pkg/apis/feeds/v1alpha1/common_event_type_validation.go +++ b/pkg/apis/feeds/v1alpha1/common_event_type_validation.go @@ -21,5 +21,6 @@ import ( ) func (cess *CommonEventTypeSpec) Validate() *apis.FieldError { + // nothing to validate. return nil } diff --git a/pkg/apis/feeds/v1alpha1/event_type_validation.go b/pkg/apis/feeds/v1alpha1/event_type_validation.go index 3478bc83e6a..dd53e1b5dec 100644 --- a/pkg/apis/feeds/v1alpha1/event_type_validation.go +++ b/pkg/apis/feeds/v1alpha1/event_type_validation.go @@ -18,6 +18,7 @@ package v1alpha1 import ( "github.com/knative/pkg/apis" + "k8s.io/apimachinery/pkg/util/validation" ) func (et *EventType) Validate() *apis.FieldError { @@ -25,6 +26,12 @@ func (et *EventType) Validate() *apis.FieldError { } func (ets *EventTypeSpec) Validate() *apis.FieldError { + if ets.EventSource == "" { + return apis.ErrMissingField("eventSource") + } + if errs := validation.IsQualifiedName(ets.EventSource); len(errs) > 0 { + return apis.ErrInvalidValue(ets.EventSource, "eventSource") + } return ets.CommonEventTypeSpec.Validate() } @@ -37,7 +44,13 @@ func (current *EventType) CheckImmutableFields(og apis.Immutable) *apis.FieldErr return nil } - // TODO + // EventSource for an EventType should not change. + if original.Spec.EventSource != current.Spec.EventSource { + return &apis.FieldError{ + Message: "Immutable fields changed", + Paths: []string{"spec.eventSource"}, + } + } return nil } diff --git a/pkg/apis/feeds/v1alpha1/event_type_validation_test.go b/pkg/apis/feeds/v1alpha1/event_type_validation_test.go new file mode 100644 index 00000000000..30a04f3e126 --- /dev/null +++ b/pkg/apis/feeds/v1alpha1/event_type_validation_test.go @@ -0,0 +1,123 @@ +/* +Copyright 2018 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/knative/pkg/apis" + "testing" +) + +func TestEventTypeSpecValidation(t *testing.T) { + tests := []struct { + name string + et *EventTypeSpec + want *apis.FieldError + }{{ + name: "valid", + et: &EventTypeSpec{ + EventSource: "foo", + }, + }, { + name: "invalid source", + et: &EventTypeSpec{ + EventSource: "f@o", + }, + want: apis.ErrInvalidValue("f@o", "eventSource"), + }, { + name: "empty", + et: &EventTypeSpec{}, + want: apis.ErrMissingField("eventSource"), + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := test.et.Validate() + ignoreArguments := cmpopts.IgnoreFields(apis.FieldError{}, "Details") + if diff := cmp.Diff(test.want, got, ignoreArguments); diff != "" { + t.Errorf("validateClusterEventType (-want, +got) = %v", diff) + } + }) + } +} + +func TestEventTypeImmutableFields(t *testing.T) { + tests := []struct { + name string + new apis.Immutable + old apis.Immutable + want *apis.FieldError + }{{ + name: "good (no change)", + new: &EventType{ + Spec: EventTypeSpec{ + EventSource: "foo", + }, + }, + old: &EventType{ + Spec: EventTypeSpec{ + EventSource: "foo", + }, + }, + want: nil, + }, { + name: "good (description change)", + new: &EventType{ + Spec: EventTypeSpec{ + EventSource: "foo", + CommonEventTypeSpec: CommonEventTypeSpec{ + Description: "Foo foo foo.", + }, + }, + }, + old: &EventType{ + Spec: EventTypeSpec{ + EventSource: "foo", + CommonEventTypeSpec: CommonEventTypeSpec{ + Description: "Bar bar bar.", + }, + }, + }, + want: nil, + }, { + name: "bad (source changes)", + new: &EventType{ + Spec: EventTypeSpec{ + EventSource: "foo", + }, + }, + old: &EventType{ + Spec: EventTypeSpec{ + EventSource: "bar", + }, + }, + want: &apis.FieldError{ + Message: "Immutable fields changed", + Paths: []string{"spec.eventSource"}, + }, + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := test.new.CheckImmutableFields(test.old) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("Validate (-want, +got) = %v", diff) + } + }) + } +}