-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use the builder pattern to improve readability of configuration table testing #1924
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
pkg/apis/serving/v1alpha1/testing/configuration_builder.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| 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 testing | ||
|
|
||
| import ( | ||
| buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" | ||
| "github.com/knative/serving/pkg/apis/serving/v1alpha1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| clientgotesting "k8s.io/client-go/testing" | ||
| ) | ||
|
|
||
| type ConfigToBuildFunc func(*v1alpha1.Configuration) *buildv1alpha1.Build | ||
| type ConfigToRevisionFunc func(*v1alpha1.Configuration) *v1alpha1.Revision | ||
|
|
||
| type ConfigBuilder struct { | ||
| obj *v1alpha1.Configuration | ||
| } | ||
|
|
||
| func Config(name string) ConfigBuilder { | ||
| return ConfigBuilder{&v1alpha1.Configuration{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| Namespace: "default", | ||
| }, | ||
| Spec: v1alpha1.ConfigurationSpec{ | ||
| RevisionTemplate: v1alpha1.RevisionTemplateSpec{ | ||
| Spec: v1alpha1.RevisionSpec{ | ||
| Container: corev1.Container{ | ||
| Image: "busybox", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Status: v1alpha1.ConfigurationStatus{ | ||
| Conditions: []v1alpha1.ConfigurationCondition{}, | ||
| }, | ||
| }} | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) Build() *v1alpha1.Configuration { | ||
| return bldr.obj.DeepCopy() | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) Object() runtime.Object { | ||
| return bldr.Build() | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) ToChildBuild(convert ConfigToBuildFunc) *buildv1alpha1.Build { | ||
| return convert(bldr.Build()) | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) ToChildRevision(convert ConfigToRevisionFunc) RevisionBuilder { | ||
| return RevisionBuilder{convert(bldr.Build())} | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) ToUpdateAction() clientgotesting.UpdateActionImpl { | ||
|
Member
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. Feels like this should be part of an interface this implements |
||
| action := clientgotesting.UpdateActionImpl{} | ||
| action.Verb = "update" | ||
| action.Object = bldr.Build() | ||
| return action | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithNamespace(namespace string) ConfigBuilder { | ||
| bldr.obj.ObjectMeta.Namespace = namespace | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithGeneration(generation int64) ConfigBuilder { | ||
| bldr.obj.Spec.Generation = generation | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithStatus(s v1alpha1.ConfigurationStatus) ConfigBuilder { | ||
| bldr.obj.Status = s | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithBuild(b buildv1alpha1.BuildSpec) ConfigBuilder { | ||
| bldr.obj.Spec.Build = &b | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithObservedGeneration(gen int64) ConfigBuilder { | ||
| bldr.obj.Status.ObservedGeneration = gen | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithLatestCreatedRevisionName(name string) ConfigBuilder { | ||
| bldr.obj.Status.LatestCreatedRevisionName = name | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithLatestReadyRevisionName(name string) ConfigBuilder { | ||
| bldr.obj.Status.LatestReadyRevisionName = name | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithConcurrencyModel(cm v1alpha1.RevisionRequestConcurrencyModelType) ConfigBuilder { | ||
| bldr.obj.Spec.RevisionTemplate.Spec.ConcurrencyModel = cm | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithReadyConditionUnknown() ConfigBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{ | ||
| { | ||
| Type: v1alpha1.ConfigurationConditionReady, | ||
| Status: corev1.ConditionUnknown, | ||
| }, | ||
| } | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithReadyConditionTrue() ConfigBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{ | ||
| { | ||
| Type: v1alpha1.ConfigurationConditionReady, | ||
| Status: corev1.ConditionTrue, | ||
| }, | ||
| } | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr ConfigBuilder) WithReadyConditionFalse(reason, message string) ConfigBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{ | ||
| { | ||
| Type: v1alpha1.ConfigurationConditionReady, | ||
| Status: corev1.ConditionFalse, | ||
| Reason: reason, | ||
| Message: message, | ||
| }, | ||
| } | ||
| return bldr | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| 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 testing | ||
|
|
||
| import ( | ||
| "github.com/knative/serving/pkg/apis/serving/v1alpha1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ) | ||
|
|
||
| type RevisionBuilder struct { | ||
| obj *v1alpha1.Revision | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) Build() *v1alpha1.Revision { | ||
| return bldr.obj.DeepCopy() | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) Object() runtime.Object { | ||
| return bldr.Build() | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) WithConcurrencyModel(cm v1alpha1.RevisionRequestConcurrencyModelType) RevisionBuilder { | ||
| bldr.obj.Spec.ConcurrencyModel = cm | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) WithReadyCondition(status corev1.ConditionStatus) RevisionBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{ | ||
| { | ||
| Type: v1alpha1.RevisionConditionReady, | ||
| Status: status, | ||
| }, | ||
| } | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) WithReadyConditionTrue() RevisionBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{ | ||
| { | ||
| Type: v1alpha1.RevisionConditionReady, | ||
| Status: corev1.ConditionTrue, | ||
| }, | ||
| } | ||
| return bldr | ||
| } | ||
|
|
||
| func (bldr RevisionBuilder) WithReadyConditionFalse(reason, message string) RevisionBuilder { | ||
| bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{ | ||
| { | ||
| Type: v1alpha1.RevisionConditionReady, | ||
| Status: corev1.ConditionFalse, | ||
| Reason: reason, | ||
| Message: message, | ||
| }, | ||
| } | ||
| return bldr | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Feels like this should maybe return a
BuildBuilder, WDYT?