-
Notifications
You must be signed in to change notification settings - Fork 630
Validate flow as told in the flow spec comments. #398
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /* | ||
| 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" | ||
| "k8s.io/api/core/v1" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestFlowSpecValidation(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| f *FlowSpec | ||
| want *apis.FieldError | ||
| }{{ | ||
| name: "valid with URI", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http://example.com/action"), | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| }, { | ||
| name: "valid with Target", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| Target: &v1.ObjectReference{ | ||
| Name: "foo", | ||
| }, | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| }, { | ||
| name: "invalid with URI", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http//example.com/action"), | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: `invalid value "http//example.com/action"`, | ||
| Paths: []string{"action.targetURI"}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am still catching up with the changes for validation code while I was gone. I like what's being done with them, I'm just little confused on what the eventual returned to the user and how the paths get stitched together. But that's not really part of this PR, just need to go read some more code :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is that the field context is prefixed as you come back up the call stack, so you can traverse a object graph and call validate on sub-objects. If those objects throw errors, you add the context only you know about in the context the object is used. IE: AnotherObj's type was able to have a validate function that is only aware of the objects requirements. Only obj knew the context in which the user knows how they used AnotherObj. Check out |
||
| }, | ||
| }, { | ||
| name: "invalid with Target", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| Target: &v1.ObjectReference{ | ||
| Name: "f@o", | ||
| }, | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: `invalid value "f@o"`, | ||
| Paths: []string{"action.target.name"}, | ||
| }, | ||
| }, { | ||
| name: "invalid with both target and targetURI", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http://example.com/action"), | ||
| Target: &v1.ObjectReference{ | ||
| Name: "foo", | ||
| }, | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: `expected exactly one, got both`, | ||
| Paths: []string{"action.target", "action.targetURI"}, | ||
| }, | ||
| }, { | ||
| name: "invalid, no target or targetURI", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{}, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: `expected exactly one, got neither`, | ||
| Paths: []string{"action.target", "action.targetURI"}, | ||
| }, | ||
| }, { | ||
| name: "invalid, missing event type", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http://example.com/action"), | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: "missing field(s)", | ||
| Paths: []string{"trigger.eventType"}, | ||
| }, | ||
| }, { | ||
| name: "invalid event type", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http://example.com/action"), | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "f@o", | ||
| Resource: "bar", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: `invalid value "f@o"`, | ||
| Paths: []string{"trigger.eventType"}, | ||
| }, | ||
| }, { | ||
| name: "invalid, missing resource", | ||
| f: &FlowSpec{ | ||
| Action: FlowAction{ | ||
| TargetURI: stringPtr("http://example.com/action"), | ||
| }, | ||
| Trigger: EventTrigger{ | ||
| EventType: "foo", | ||
| }, | ||
| }, | ||
| want: &apis.FieldError{ | ||
| Message: "missing field(s)", | ||
| Paths: []string{"trigger.resource"}, | ||
| }, | ||
| }, { | ||
| name: "empty", | ||
| f: &FlowSpec{}, | ||
| want: &apis.FieldError{ | ||
| Message: "missing field(s)", | ||
| Paths: []string{"trigger", "action"}, | ||
| }, | ||
| }} | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| got := test.f.Validate() | ||
| ignoreArguments := cmpopts.IgnoreFields(apis.FieldError{}, "Details") | ||
| if diff := cmp.Diff(test.want, got, ignoreArguments); diff != "" { | ||
| t.Errorf("validateFlow (-want, +got) = %v", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // stringPtr returns a pointer to the passed string. | ||
| func stringPtr(s string) *string { | ||
| return &s | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gets recreated a few times. Maybe pull it into a variable?
validEventTrigger := ...