Skip to content

MCD: Possible test pattern - #33

Closed
ashcrow wants to merge 3 commits into
openshift:masterfrom
ashcrow:mcd-test-example
Closed

ashcrow wants to merge 3 commits into
openshift:masterfrom
ashcrow:mcd-test-example

Conversation

@ashcrow

@ashcrow ashcrow commented Aug 29, 2018 •

Copy link
Copy Markdown
Member

Looking through the MCD code I found trying to write unittests a bit harder than normal. One pattern I've seen in the past is to pass functions (or structs, etc..) that have network requests/side effects as part of the design of the system. This unbinds said side effects at the unit level and, in theory, can provide a foundation for different execution paths chosen at runtime.

Note: New probably wasn't example to use to test with since it's pretty simple, but I think the idea still is shown.

This is a quick example of:

  • making a type of NodeAnnotationLoader
  • renaming loadNodeAnnotations to LoadNodeAnnotations
  • passing LoadNodeAnnotations to New
  • optional passing of login1.Conn to New. If passed nil one will be created inside of New.
  • creating a mocked function for testing that also implements NodeAnnotationLoader for unittesting
$ cd pkg/daemon/
$ go test -v -run ^TestNew$
=== RUN   TestNew
--- PASS: TestNew (0.00s)
PASS
ok      github.com/openshift/machine-config-operator/pkg/daemon 0.009s

New now requires an annotation loading function which implements
NodeAnnotationLoader.

Signed-off-by: Steve Milner <[email protected]>
@openshift-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ashcrow

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

@openshift-ci-robot openshift-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 29, 2018
@ashcrow

ashcrow commented Aug 29, 2018

Copy link
Copy Markdown
Member Author

/cc @sdemos

@openshift-ci-robot

Copy link
Copy Markdown
Contributor

@ashcrow: GitHub didn't allow me to request PR reviews from the following users: sdemos.

Note that only openshift members and repo collaborators can review this PR, and authors cannot review their own PRs.

Details

In response to this:

/cc @sdemos

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ashcrow

ashcrow commented Aug 29, 2018

Copy link
Copy Markdown
Member Author

Huh. Tests pass locally, fail in prow. Looking at this now.

@ashcrow

ashcrow commented Aug 29, 2018

Copy link
Copy Markdown
Member Author

Ah this makes sense. Because the unittest is happening within a container and is not mounting in /var/run/dbus/system_bus_socket the test fails. Being that I'm on a desktop it was able to see the file. Adding another function input.

Comment thread pkg/daemon/daemon.go Outdated

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.

coreclientv1 or `clientcorev1``

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.

Will update

@ashcrow

ashcrow commented Aug 30, 2018

Copy link
Copy Markdown
Member Author

/cc @sdemos

@sdemos

sdemos commented Sep 6, 2018

Copy link
Copy Markdown
Contributor

I'm a little on the fence with this approach. It gets the job done, and it does nicely move side-effect-related code to particular functions that will naturally be more controlled with this model.

The problem for me is that it seems unwieldy to have all of those types of functions have their own defined function prototype and be function pointer arguments into every function that needs them.

I think I'm partial to moving all related types of side-effect code that needs to be mocked to various interfaces, that then can have a real client and a mock client. then we just have a different client for each different kind of part (eg what we vaguely have now, but it's not well adhered to).

thoughts?

@ashcrow

ashcrow commented Sep 6, 2018

Copy link
Copy Markdown
Member Author

The problem for me is that it seems unwieldy to have all of those types of functions have their own defined function prototype and be function pointer arguments into every function that needs them.

Absolutely agree :-/

I think I'm partial to moving all related types of side-effect code that needs to be mocked to various interfaces, that then can have a real client and a mock client. then we just have a different client for each different kind of part (eg what we vaguely have now, but it's not well adhered to).

thoughts?

I think that's a good approach as well. The downside is that it's not just clients, but also functions that have side effects themselves. If we can broaden it to be more than just what is considered a client I think it would be doable. Or if we define client as anything interfacing with an external entity (IE: file system client, kubernetes client, login1 client, etc..).

@sdemos

sdemos commented Sep 6, 2018

Copy link
Copy Markdown
Contributor

Or if we define client as anything interfacing with an external entity (IE: file system client, kubernetes client, login1 client, etc..).

this is more concretely what I meant. I vaguely tried writing the original agent with this approach in mind, but didn't stick to it very well, and it's only gone further away from this approach afaict. it seems like the downside is that it would be a more significant refactor.

@ashcrow

ashcrow commented Sep 10, 2018

Copy link
Copy Markdown
Member Author

@sdemos post #14 merger I'll take a look at making clients so we can later implement mocks for tests.

@ashcrow

ashcrow commented Sep 10, 2018

Copy link
Copy Markdown
Member Author

@sdemos during a meeting I threw this together. In your opinion do you think each test suite should set up it's own mock to hang off clients, or should mocks be "smart" pass/fail based on some kind of input ... either to the client or via variable setting?

@ashcrow

ashcrow commented Sep 26, 2018

Copy link
Copy Markdown
Member Author

/hold

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 26, 2018
@ashcrow ashcrow closed this Sep 28, 2018
@ashcrow

ashcrow commented Sep 28, 2018

Copy link
Copy Markdown
Member Author

See #101

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

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants