Skip to content
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
23 changes: 18 additions & 5 deletions pkg/apis/feeds/v1alpha1/cluster_event_type_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
123 changes: 123 additions & 0 deletions pkg/apis/feeds/v1alpha1/cluster_event_type_validation_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/apis/feeds/v1alpha1/common_event_type_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import (
)

func (cess *CommonEventTypeSpec) Validate() *apis.FieldError {
// nothing to validate.
return nil
}
15 changes: 14 additions & 1 deletion pkg/apis/feeds/v1alpha1/event_type_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ package v1alpha1

import (
"github.com/knative/pkg/apis"
"k8s.io/apimachinery/pkg/util/validation"
)

func (et *EventType) Validate() *apis.FieldError {
return et.Spec.Validate().ViaField("spec")
}

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()
}

Expand All @@ -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
}
123 changes: 123 additions & 0 deletions pkg/apis/feeds/v1alpha1/event_type_validation_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}