Skip to content
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

Refactor crtb and prtb manager to make it more testable #45433

Merged

Conversation

JonCrowther
Copy link
Contributor

@JonCrowther JonCrowther commented May 9, 2024

Summary

As it currently stands, there are no unit tests for crtb_handler.go or prtb_handler.go. Part of the reason for that is that the code is set up in a way that is very difficult to test. This PR is a potential refactor that makes the code much more testable. It is important to note that this is just a code refactor, with no changes to functionality.

Commits

I tried as best I could to split the commits into logical blocks to help reviewers

  • Commit 1: Move resource clients from manager.go to crtb_handler.go. That way, the manager struct is only referenced to access it's functions, not it's fields.
  • Commit 2: Same as 1, but for prtb_handler.go
  • Commit 3: Now that several of the clients have been moved out of manager.go, remove any unused clients from the manager struct
  • Commit 4: Create a managerInterface that contains all the functions used by crtb_handler.go
  • Commit 5: Same as 4, but for prtb_handler.go
  • Commit 6: Add an example of a test for crtb_handler.go where we mock the managerInterface
  • Commit 7: Use a mock generator for manager and added almost full test coverage of reconcileBindings
  • Commit 8: All of the above, but for managementuser

Future work:

  • Some of the manager functions really don't need to be member functions, which would simplify the mock. Often these functions reference a single field from manager, which we could pass in when the function is called instead.

DO NOT MERGE

This is a for example purposes only. After discussion it may get merged.

@JonCrowther JonCrowther self-assigned this May 9, 2024
Copy link

@andreas-kupries andreas-kupries left a comment

Choose a reason for hiding this comment

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

Thank you for making the effort here to make the system more testable.
I like the approach very much. Especially as it means that only the high-level
functions need mocking instead of deeper details. This makes tests much simpler.

The only thing I am missing is the equivalent work for the
pkg/controllers/managementuser/rbac/[cp]rtb_handler.go.

That however is something I could do as part of my work on the CRTB status field.

I wholeheartedly approve this PR.
Despite effectively undoing the test work I did for the status field.

Copy link

@andreas-kupries andreas-kupries left a comment

Choose a reason for hiding this comment

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

Approving again.

@JonCrowther JonCrowther requested a review from crobby May 14, 2024 15:54
Copy link
Contributor

@crobby crobby left a comment

Choose a reason for hiding this comment

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

a big improvement, lgtm

@JonCrowther JonCrowther merged commit 6303df4 into rancher:release/v2.9 May 16, 2024
2 checks passed
@JonCrowther JonCrowther deleted the refactor-crtb-prtb-manager branch May 16, 2024 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants