Skip to content

[PoC] Use builders to make table tests more readable#1762

Closed
dprotaso wants to merge 2 commits into
knative:masterfrom
dprotaso:poc-test-builders
Closed

[PoC] Use builders to make table tests more readable#1762
dprotaso wants to merge 2 commits into
knative:masterfrom
dprotaso:poc-test-builders

Conversation

@dprotaso
Copy link
Copy Markdown
Member

This is just a proof-of-concept to source feedback on whether folks like this pattern, and whether they feel this improves readability. This secondly helps reduce the combinatorial method names.

@google-prow-robot google-prow-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 30, 2018
@google-prow-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dprotaso
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: grantr

If they are not already assigned, you can assign the PR to them by writing /assign @grantr in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dprotaso
Copy link
Copy Markdown
Member Author

@dprotaso
Copy link
Copy Markdown
Member Author

dprotaso commented Jul 30, 2018

Looking at it while walking we could have higher level builder functions to remove more boilerplate

ie. AddStatusCondition

Edit: Pushed those changes

@google-prow-robot google-prow-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 31, 2018
The table test having WantCreate be a type of metav1.Object actually
results in more boilerplate - hence some of the calls to `Build`
@dprotaso dprotaso force-pushed the poc-test-builders branch from fe5d3d6 to 3f95d25 Compare July 31, 2018 02:08
@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on pkg/.
Say /test pull-knative-serving-go-coverage to run the coverage report again

File Old Coverage New Coverage Delta
pkg/controller/configuration/configuration.go 94.0% 93.8% -0.1

@dprotaso dprotaso changed the title [WIP] Use builders to make table tests more readable [PoC] Use builders to make table tests more readable Aug 1, 2018
@google-prow-robot google-prow-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 1, 2018
Copy link
Copy Markdown
Member

@mattmoor mattmoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally 👍 I wonder about applicability beyond the table tests, or even outside of the controllers.

We have a lot of little bits and pieces that have been thrown together to produce test data (I'm super guilty of this), it'd be good to consolidate it all around this pattern, if we can. WDYT?

case builder.ConfigBuilder:
ls.Configuration.Items = append(ls.Configuration.Items, o.Configuration)
case builder.RevisionBuilder:
ls.Revision.Items = append(ls.Revision.Items, o.Revision)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we take these instead of just calling Build()?

}

func (bldr RevisionBuilder) WithCondition(c v1alpha1.RevisionCondition) RevisionBuilder {
bldr.Revision.Status.Conditions = append(bldr.Revision.Status.Conditions, c)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could end up with duplicates

}

rev := resources.MakeRevision(config, buildName)
rev := resources.MakeRevision(config)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part of the change is a separable distraction from the test change. Want to send a separate PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,94 @@
package builder
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing license headers


type ConfigBuilder struct {
*v1alpha1.Configuration
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might better below under pkg/apis/serving/v1alpha1/testing?

func makeRevStatus(rev *v1alpha1.Revision, status v1alpha1.RevisionStatus) *v1alpha1.Revision {
rev.Status = status
return rev
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

WithCondition(v1alpha1.ConfigurationCondition{
Type: v1alpha1.ConfigurationConditionReady,
Status: corev1.ConditionTrue,
}).AsUpdateAction(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like shedding the UpdateActionImpl wrapper :)

Copy link
Copy Markdown
Contributor

@grantr grantr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this direction! Thanks @dprotaso for making it work.

Comment thread pkg/controller/testing/table.go Outdated
ls := NewListers(r.Objects)

for i, action := range r.WantUpdates {
if builder, ok := action.Object.(builder.ConfigBuilder); ok {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could avoid the reference to ConfigBuilder by defining an interface:

type Builder interface{
  Build() runtime.Object
}

but I'm not sure about that return value. Would it work with cmp.Diff or semantic equality or whatever is being used to compare objects?

},
WantCreates: []metav1.Object{
setRevConcurrencyModel(resources.MakeRevision(cfg("validation-failure", "foo", 1234), noBuildName), "Bogus"),
setRevConcurrencyModel(resources.MakeRevision(cfg("validation-failure", "foo", 1234)), "Bogus"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely like the builders better than this style (setRevConcurrencyModel).

@@ -0,0 +1,62 @@
package builder
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't thought about it too carefully, but would it make sense for these methods to be defined in the api package (or a subpackage of that) rather than in the controller testing package?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was @mattmoor's suggestion here:
#1762 (comment)

@evankanderson
Copy link
Copy Markdown
Member

/assign grantr mattmoor

@jonjohnsonjr
Copy link
Copy Markdown
Contributor

LGTM as well. I wanted to use this but then I realized it wasn't merged yet 😆

@dprotaso
Copy link
Copy Markdown
Member Author

Closing - moving work to #1924

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants